Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / sis / sis_context.c
1 /**************************************************************************
2
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29  * Authors:
30  *   Sung-Ching Lin <sclin@sis.com.tw>
31  *   Eric Anholt <anholt@FreeBSD.org>
32  */
33
34 #include "sis_dri.h"
35
36 #include "sis_context.h"
37 #include "sis_state.h"
38 #include "sis_dd.h"
39 #include "sis_span.h"
40 #include "sis_stencil.h"
41 #include "sis_tex.h"
42 #include "sis_tris.h"
43 #include "sis_alloc.h"
44
45 #include "main/imports.h"
46 #include "utils.h"
47 #include "main/framebuffer.h"
48
49 #include "drivers/common/driverfuncs.h"
50
51 #include "swrast/swrast.h"
52 #include "swrast_setup/swrast_setup.h"
53 #include "vbo/vbo.h"
54
55 #include "tnl/tnl.h"
56
57 #define need_GL_EXT_fog_coord
58 #define need_GL_EXT_secondary_color
59 #include "main/remap_helper.h"
60
61 #ifndef SIS_DEBUG
62 int SIS_DEBUG = 0;
63 #endif
64
65 int GlobalCurrentHwcx = -1;
66 int GlobalHwcxCountBase = 1;
67 int GlobalCmdQueueLen = 0;
68
69 static struct dri_extension card_extensions[] =
70 {
71     { "GL_ARB_multitexture",               NULL },
72     { "GL_ARB_texture_border_clamp",       NULL },
73     { "GL_ARB_texture_mirrored_repeat",    NULL },
74     /*{ "GL_EXT_fog_coord",                  GL_EXT_fog_coord_functions },*/
75     { "GL_EXT_texture_lod_bias",           NULL },
76     { "GL_EXT_secondary_color",            GL_EXT_secondary_color_functions },
77     { "GL_EXT_stencil_wrap",               NULL },
78     { "GL_MESA_ycbcr_texture",             NULL },
79     { "GL_NV_blend_square",                NULL },
80     { NULL,                                NULL }
81 };
82
83 #if 0
84 static struct dri_extension card_extensions_6326[] =
85 {
86     /*{ "GL_ARB_texture_border_clamp",       NULL },*/
87     /*{ "GL_ARB_texture_mirrored_repeat",    NULL },*/
88     /*{ "GL_MESA_ycbcr_texture",             NULL },*/
89     { NULL,                                NULL }
90 };
91 #endif
92
93 static const struct dri_debug_control debug_control[] =
94 {
95     { "fall",  DEBUG_FALLBACKS },
96     { NULL,    0 }
97 };
98
99 void
100 WaitEngIdle (sisContextPtr smesa)
101 {
102    GLuint engineState;
103
104    if (smesa->is6326) {
105       do {
106          engineState = MMIO_READ(REG_3D_EngineFire); /* XXX right reg? */
107       } while ((engineState & ENG_3DIDLEQE) != 0);
108    } else {
109       do {
110          engineState = MMIO_READ(REG_CommandQueue);
111       } while ((engineState & SiS_EngIdle) != SiS_EngIdle);
112    }
113 }
114
115 void
116 Wait2DEngIdle (sisContextPtr smesa)
117 {
118    GLuint engineState;
119
120    if (smesa->is6326) {
121       do {
122          engineState = MMIO_READ(REG_6326_BitBlt_Cmd);
123       } while ((engineState & BLT_BUSY) != 0);
124    } else {
125       do {
126          engineState = MMIO_READ(REG_CommandQueue);
127       } while ((engineState & SiS_EngIdle2d) != SiS_EngIdle2d);
128    }
129 }
130
131 /* To be called from mWait3DCmdQueue.  Separate function for profiling
132  * purposes, and speed doesn't matter because we're spinning anyway.
133  */
134 void
135 WaitingFor3dIdle(sisContextPtr smesa, int wLen)
136 {
137    if (smesa->is6326) {
138       while (*(smesa->CurrentQueueLenPtr) < wLen) {
139          *(smesa->CurrentQueueLenPtr) =
140             ((GLuint)MMIO_READ(REG_3D_EngineFire) >> 16) * 2;
141       }
142    } else {
143       while (*(smesa->CurrentQueueLenPtr) < wLen) {
144          *(smesa->CurrentQueueLenPtr) =
145             (MMIO_READ(REG_CommandQueue) & MASK_QueueLen) - 20;
146       }
147    }
148 }
149
150 void sisReAllocateBuffers(struct gl_context *ctx, struct gl_framebuffer *drawbuffer,
151                           GLuint width, GLuint height)
152 {
153    sisContextPtr smesa = SIS_CONTEXT(ctx);
154
155    sisUpdateBufferSize(smesa);
156
157    _mesa_resize_framebuffer(ctx, drawbuffer, width, height);
158 }
159
160 GLboolean
161 sisCreateContext( gl_api api,
162                   const struct gl_config *glVisual,
163                   __DRIcontext *driContextPriv,
164                   void *sharedContextPrivate )
165 {
166    struct gl_context *ctx, *shareCtx;
167    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
168    sisContextPtr smesa;
169    sisScreenPtr sisScreen;
170    int i;
171    struct dd_function_table functions;
172
173    smesa = (sisContextPtr)CALLOC( sizeof(*smesa) );
174    if (smesa == NULL)
175       return GL_FALSE;
176
177    /* Init default driver functions then plug in our SIS-specific functions
178     * (the texture functions are especially important)
179     */
180    _mesa_init_driver_functions(&functions);
181    sisInitDriverFuncs(&functions);
182    sisInitTextureFuncs(&functions);
183
184    /* Allocate the Mesa context */
185    if (sharedContextPrivate)
186       shareCtx = ((sisContextPtr)sharedContextPrivate)->glCtx;
187    else 
188       shareCtx = NULL;
189    smesa->glCtx = _mesa_create_context( API_OPENGL, glVisual, shareCtx,
190                                         &functions, (void *) smesa);
191    if (!smesa->glCtx) {
192       FREE(smesa);
193       return GL_FALSE;
194    }
195    driContextPriv->driverPrivate = smesa;
196    ctx = smesa->glCtx;
197
198    sisScreen = smesa->sisScreen = (sisScreenPtr)(sPriv->private);
199
200    smesa->is6326 = GL_FALSE; /* XXX */
201    smesa->driContext = driContextPriv;
202    smesa->driScreen = sPriv;
203    smesa->driDrawable = NULL;
204    smesa->hHWContext = driContextPriv->hHWContext;
205    smesa->driHwLock = &sPriv->pSAREA->lock;
206    smesa->driFd = sPriv->fd;
207   
208    smesa->virtualX = sisScreen->screenX;
209    smesa->virtualY = sisScreen->screenY;
210    smesa->bytesPerPixel = sisScreen->cpp;
211    smesa->IOBase = sisScreen->mmio.map;
212    smesa->Chipset = sisScreen->deviceID;
213
214    smesa->FbBase = sPriv->pFB;
215    smesa->displayWidth = sPriv->fbWidth;
216    smesa->front.pitch = sPriv->fbStride;
217
218    smesa->sarea = (SISSAREAPriv *)((char *)sPriv->pSAREA +
219                                    sisScreen->sarea_priv_offset);
220
221    /* support ARGB8888 and RGB565 */
222    switch (smesa->bytesPerPixel)
223    {
224    case 4:
225       smesa->redMask = 0x00ff0000;
226       smesa->greenMask = 0x0000ff00;
227       smesa->blueMask = 0x000000ff;
228       smesa->alphaMask = 0xff000000;
229       smesa->colorFormat = DST_FORMAT_ARGB_8888;
230       break;
231    case 2:
232       smesa->redMask = 0xf800;
233       smesa->greenMask = 0x07e0;
234       smesa->blueMask = 0x001f;
235       smesa->alphaMask = 0;
236       smesa->colorFormat = DST_FORMAT_RGB_565;
237       break;
238    default:
239       sis_fatal_error("Bad bytesPerPixel %d.\n", smesa->bytesPerPixel);
240    }
241
242    if (smesa->is6326) {
243       ctx->Const.MaxTextureUnits = 1;
244       ctx->Const.MaxTextureLevels = 9;
245    } else {
246       ctx->Const.MaxTextureUnits = 2;
247       ctx->Const.MaxTextureLevels = 11;
248    }
249    ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
250    ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
251
252    /* Parse configuration files */
253    driParseConfigFiles (&smesa->optionCache, &sisScreen->optionCache,
254                         sisScreen->driScreen->myNum, "sis");
255
256 #if DO_DEBUG
257    SIS_DEBUG = driParseDebugString(getenv("SIS_DEBUG"), debug_control);
258 #endif
259
260    /* TODO: index mode */
261
262    smesa->CurrentQueueLenPtr = &(smesa->sarea->QueueLength);
263    smesa->FrameCountPtr = &(smesa->sarea->FrameCount);
264
265    /* set AGP */
266    smesa->AGPSize = sisScreen->agp.size;
267    smesa->AGPBase = sisScreen->agp.map;
268    smesa->AGPAddr = sisScreen->agpBaseOffset;
269
270    /* Create AGP command buffer */
271    if (smesa->AGPSize != 0 && 
272       !driQueryOptionb(&smesa->optionCache, "agp_disable"))
273    {
274       smesa->vb = sisAllocAGP(smesa, 64 * 1024, &smesa->vb_agp_handle);
275       if (smesa->vb != NULL) {
276          smesa->using_agp = GL_TRUE;
277          smesa->vb_cur = smesa->vb;
278          smesa->vb_last = smesa->vb;
279          smesa->vb_end = smesa->vb + 64 * 1024;
280          smesa->vb_agp_offset = ((long)smesa->vb - (long)smesa->AGPBase +
281             (long)smesa->AGPAddr);
282       }
283    }
284    if (!smesa->using_agp) {
285       smesa->vb = malloc(64 * 1024);
286       if (smesa->vb == NULL) {
287          FREE(smesa);
288          return GL_FALSE;
289       }
290       smesa->vb_cur = smesa->vb;
291       smesa->vb_last = smesa->vb;
292       smesa->vb_end = smesa->vb + 64 * 1024;
293    }
294
295    smesa->GlobalFlag = 0L;
296
297    smesa->Fallback = 0;
298
299    /* Initialize the software rasterizer and helper modules.
300     */
301    _swrast_CreateContext( ctx );
302    _vbo_CreateContext( ctx );
303    _tnl_CreateContext( ctx );
304    _swsetup_CreateContext( ctx );
305
306    _swrast_allow_pixel_fog( ctx, GL_TRUE );
307    _swrast_allow_vertex_fog( ctx, GL_FALSE );
308    _tnl_allow_pixel_fog( ctx, GL_TRUE );
309    _tnl_allow_vertex_fog( ctx, GL_FALSE );
310
311    /* XXX these should really go right after _mesa_init_driver_functions() */
312    if (smesa->is6326) {
313       sis6326DDInitStateFuncs( ctx );
314       sis6326DDInitState( smesa ); /* Initializes smesa->zFormat, important */
315    } else {
316       sisDDInitStateFuncs( ctx );
317       sisDDInitState( smesa );  /* Initializes smesa->zFormat, important */
318       sisDDInitStencilFuncs( ctx );
319    }
320    sisInitTriFuncs( ctx );
321    sisDDInitSpanFuncs( ctx );
322
323    driInitExtensions( ctx, card_extensions, GL_FALSE );
324
325    for (i = 0; i < SIS_MAX_TEXTURES; i++) {
326       smesa->TexStates[i] = 0;
327       smesa->PrevTexFormat[i] = 0;
328    }
329
330    if (driQueryOptionb(&smesa->optionCache, "no_rast")) {
331       fprintf(stderr, "disabling 3D acceleration\n");
332       FALLBACK(smesa, SIS_FALLBACK_DISABLE, 1);
333    }
334    smesa->texture_depth = driQueryOptioni(&smesa->optionCache, "texture_depth");
335
336    return GL_TRUE;
337 }
338
339 void
340 sisDestroyContext ( __DRIcontext *driContextPriv )
341 {
342    sisContextPtr smesa = (sisContextPtr)driContextPriv->driverPrivate;
343
344    assert( smesa != NULL );
345
346    if ( smesa != NULL ) {
347       _swsetup_DestroyContext( smesa->glCtx );
348       _tnl_DestroyContext( smesa->glCtx );
349       _vbo_DestroyContext( smesa->glCtx );
350       _swrast_DestroyContext( smesa->glCtx );
351
352       if (smesa->using_agp)
353          sisFreeAGP(smesa, smesa->vb_agp_handle);
354
355       /* free the Mesa context */
356       /* XXX: Is the next line needed?  The DriverCtx (smesa) reference is
357        * needed for sisDDDeleteTexture, since it needs to call the FB/AGP free
358        * function.
359        */
360       /* smesa->glCtx->DriverCtx = NULL; */
361       _mesa_destroy_context(smesa->glCtx);
362    }
363
364    FREE( smesa );
365 }
366
367 GLboolean
368 sisMakeCurrent( __DRIcontext *driContextPriv,
369                 __DRIdrawable *driDrawPriv,
370                 __DRIdrawable *driReadPriv )
371 {
372    if ( driContextPriv ) {
373       GET_CURRENT_CONTEXT(ctx);
374       sisContextPtr oldSisCtx = ctx ? SIS_CONTEXT(ctx) : NULL;
375       sisContextPtr newSisCtx = (sisContextPtr) driContextPriv->driverPrivate;
376       struct gl_framebuffer *drawBuffer, *readBuffer;
377
378       if ( newSisCtx != oldSisCtx) {
379          newSisCtx->GlobalFlag = GFLAG_ALL;
380       }
381
382       newSisCtx->driDrawable = driDrawPriv;
383
384       drawBuffer = (struct gl_framebuffer *)driDrawPriv->driverPrivate;
385       readBuffer = (struct gl_framebuffer *)driReadPriv->driverPrivate;
386
387       _mesa_make_current( newSisCtx->glCtx, drawBuffer, readBuffer );
388
389       sisUpdateBufferSize( newSisCtx );
390       sisUpdateClipping( newSisCtx->glCtx );
391    } else {
392       _mesa_make_current( NULL, NULL, NULL );
393    }
394
395    return GL_TRUE;
396 }
397
398 GLboolean
399 sisUnbindContext( __DRIcontext *driContextPriv )
400 {
401    return GL_TRUE;
402 }
403
404 void
405 sis_update_render_state( sisContextPtr smesa )
406 {
407    __GLSiSHardware *prev = &smesa->prev;
408
409    mWait3DCmdQueue (45);
410
411    if (smesa->GlobalFlag & GFLAG_ENABLESETTING) {
412       if (!smesa->clearTexCache) {
413          MMIO(REG_3D_TEnable, prev->hwCapEnable);
414       } else {
415          MMIO(REG_3D_TEnable, prev->hwCapEnable | MASK_TextureCacheClear);
416          MMIO(REG_3D_TEnable, prev->hwCapEnable);
417          smesa->clearTexCache = GL_FALSE;
418       }
419    }
420
421    if (smesa->GlobalFlag & GFLAG_ENABLESETTING2)
422       MMIO(REG_3D_TEnable2, prev->hwCapEnable2);
423
424    /* Z Setting */
425    if (smesa->GlobalFlag & GFLAG_ZSETTING)
426    {
427       MMIO(REG_3D_ZSet, prev->hwZ);
428       MMIO(REG_3D_ZStWriteMask, prev->hwZMask);
429       MMIO(REG_3D_ZAddress, prev->hwOffsetZ);
430    }
431
432    /* Alpha Setting */
433    if (smesa->GlobalFlag & GFLAG_ALPHASETTING)
434       MMIO(REG_3D_AlphaSet, prev->hwAlpha);
435
436    if (smesa->GlobalFlag & GFLAG_DESTSETTING) {
437       MMIO(REG_3D_DstSet, prev->hwDstSet);
438       MMIO(REG_3D_DstAlphaWriteMask, prev->hwDstMask);
439       MMIO(REG_3D_DstAddress, prev->hwOffsetDest);
440    }
441
442    /* Line Setting */
443 #if 0
444    if (smesa->GlobalFlag & GFLAG_LINESETTING) 
445       MMIO(REG_3D_LinePattern, prev->hwLinePattern);
446 #endif
447
448    /* Fog Setting */
449    if (smesa->GlobalFlag & GFLAG_FOGSETTING)
450    {
451       MMIO(REG_3D_FogSet, prev->hwFog);
452       MMIO(REG_3D_FogInverseDistance, prev->hwFogInverse);
453       MMIO(REG_3D_FogFarDistance, prev->hwFogFar);
454       MMIO(REG_3D_FogFactorDensity, prev->hwFogDensity);
455    }
456
457    /* Stencil Setting */
458    if (smesa->GlobalFlag & GFLAG_STENCILSETTING) {
459       MMIO(REG_3D_StencilSet, prev->hwStSetting);
460       MMIO(REG_3D_StencilSet2, prev->hwStSetting2);
461    }
462
463    /* Miscellaneous Setting */
464    if (smesa->GlobalFlag & GFLAG_DSTBLEND)
465       MMIO(REG_3D_DstBlendMode, prev->hwDstSrcBlend);
466    if (smesa->GlobalFlag & GFLAG_CLIPPING) {
467       MMIO(REG_3D_ClipTopBottom, prev->clipTopBottom);
468       MMIO(REG_3D_ClipLeftRight, prev->clipLeftRight);
469    }
470
471   smesa->GlobalFlag &= ~GFLAG_RENDER_STATES;
472 }
473
474 void
475 sis_update_texture_state (sisContextPtr smesa)
476 {
477    __GLSiSHardware *prev = &smesa->prev;
478
479    mWait3DCmdQueue (55);
480    if (smesa->clearTexCache || (smesa->GlobalFlag & GFLAG_TEXTUREADDRESS)) {
481       MMIO(REG_3D_TEnable, prev->hwCapEnable | MASK_TextureCacheClear);
482       MMIO(REG_3D_TEnable, prev->hwCapEnable);
483       smesa->clearTexCache = GL_FALSE;
484    }
485
486    /* Texture Setting */
487    if (smesa->GlobalFlag & CFLAG_TEXTURERESET)
488       MMIO(REG_3D_TextureSet, prev->texture[0].hwTextureSet);
489
490    if (smesa->GlobalFlag & GFLAG_TEXTUREMIPMAP)
491       MMIO(REG_3D_TextureMip, prev->texture[0].hwTextureMip);
492
493   /*
494   MMIO(REG_3D_TextureTransparencyColorHigh, prev->texture[0].hwTextureClrHigh);
495   MMIO(REG_3D_TextureTransparencyColorLow, prev->texture[0].hwTextureClrLow);
496   */
497
498    if (smesa->GlobalFlag & GFLAG_TEXBORDERCOLOR)
499       MMIO(REG_3D_TextureBorderColor, prev->texture[0].hwTextureBorderColor);
500
501    if (smesa->GlobalFlag & GFLAG_TEXTUREADDRESS) {
502       switch ((prev->texture[0].hwTextureSet & MASK_TextureLevel) >> 8)
503       {
504       case 11:
505          MMIO(REG_3D_TextureAddress11, prev->texture[0].texOffset11);
506       case 10:
507          MMIO(REG_3D_TextureAddress10, prev->texture[0].texOffset10);
508          MMIO(REG_3D_TexturePitch10, prev->texture[0].texPitch10);
509       case 9:
510          MMIO(REG_3D_TextureAddress9, prev->texture[0].texOffset9);
511       case 8:
512          MMIO(REG_3D_TextureAddress8, prev->texture[0].texOffset8);
513          MMIO(REG_3D_TexturePitch8, prev->texture[0].texPitch89);
514       case 7:
515          MMIO(REG_3D_TextureAddress7, prev->texture[0].texOffset7);
516       case 6:
517          MMIO(REG_3D_TextureAddress6, prev->texture[0].texOffset6);
518          MMIO(REG_3D_TexturePitch6, prev->texture[0].texPitch67);
519       case 5:
520          MMIO(REG_3D_TextureAddress5, prev->texture[0].texOffset5);
521       case 4:
522          MMIO(REG_3D_TextureAddress4, prev->texture[0].texOffset4);
523          MMIO(REG_3D_TexturePitch4, prev->texture[0].texPitch45);
524       case 3:
525          MMIO(REG_3D_TextureAddress3, prev->texture[0].texOffset3);
526       case 2:
527          MMIO(REG_3D_TextureAddress2, prev->texture[0].texOffset2);
528          MMIO(REG_3D_TexturePitch2, prev->texture[0].texPitch23);
529       case 1:
530          MMIO(REG_3D_TextureAddress1, prev->texture[0].texOffset1);
531       case 0:
532           MMIO(REG_3D_TextureAddress0, prev->texture[0].texOffset0);
533           MMIO(REG_3D_TexturePitch0, prev->texture[0].texPitch01);
534       }
535    }
536    if (smesa->GlobalFlag & CFLAG_TEXTURERESET_1)
537       MMIO(REG_3D_Texture1Set, prev->texture[1].hwTextureSet);
538    if (smesa->GlobalFlag & GFLAG_TEXTUREMIPMAP_1)
539       MMIO(REG_3D_Texture1Mip, prev->texture[1].hwTextureMip);
540
541    if (smesa->GlobalFlag & GFLAG_TEXBORDERCOLOR_1) {
542       MMIO(REG_3D_Texture1BorderColor,
543             prev->texture[1].hwTextureBorderColor);
544    }
545    if (smesa->GlobalFlag & GFLAG_TEXTUREADDRESS_1) {
546       switch ((prev->texture[1].hwTextureSet & MASK_TextureLevel) >> 8)
547       {
548       case 11:
549          MMIO(REG_3D_Texture1Address11, prev->texture[1].texOffset11);
550       case 10:
551          MMIO(REG_3D_Texture1Address10, prev->texture[1].texOffset10);
552          MMIO(REG_3D_Texture1Pitch10, prev->texture[1].texPitch10);
553       case 9:
554          MMIO(REG_3D_Texture1Address9, prev->texture[1].texOffset9);
555       case 8:
556          MMIO(REG_3D_Texture1Address8, prev->texture[1].texOffset8);
557          MMIO(REG_3D_Texture1Pitch8, prev->texture[1].texPitch89);
558       case 7:
559          MMIO(REG_3D_Texture1Address7, prev->texture[1].texOffset7);
560       case 6:
561          MMIO(REG_3D_Texture1Address6, prev->texture[1].texOffset6);
562          MMIO(REG_3D_Texture1Pitch6, prev->texture[1].texPitch67);
563       case 5:
564          MMIO(REG_3D_Texture1Address5, prev->texture[1].texOffset5);
565       case 4:
566          MMIO(REG_3D_Texture1Address4, prev->texture[1].texOffset4);
567          MMIO(REG_3D_Texture1Pitch4, prev->texture[1].texPitch45);
568       case 3:
569          MMIO(REG_3D_Texture1Address3, prev->texture[1].texOffset3);
570       case 2:
571          MMIO(REG_3D_Texture1Address2, prev->texture[1].texOffset2);
572          MMIO(REG_3D_Texture1Pitch2, prev->texture[1].texPitch23);
573       case 1:
574          MMIO(REG_3D_Texture1Address1, prev->texture[1].texOffset1);
575       case 0:
576          MMIO(REG_3D_Texture1Address0, prev->texture[1].texOffset0);
577          MMIO(REG_3D_Texture1Pitch0, prev->texture[1].texPitch01);
578       }
579    }
580
581    /* texture environment */
582    if (smesa->GlobalFlag & GFLAG_TEXTUREENV) {
583       MMIO(REG_3D_TextureBlendFactor, prev->hwTexEnvColor);
584       MMIO(REG_3D_TextureColorBlendSet0, prev->hwTexBlendColor0);
585       MMIO(REG_3D_TextureAlphaBlendSet0, prev->hwTexBlendAlpha0);
586    }
587    if (smesa->GlobalFlag & GFLAG_TEXTUREENV_1) {
588       MMIO(REG_3D_TextureBlendFactor, prev->hwTexEnvColor);
589       MMIO(REG_3D_TextureColorBlendSet1, prev->hwTexBlendColor1);
590       MMIO(REG_3D_TextureAlphaBlendSet1, prev->hwTexBlendAlpha1);
591    }
592
593    smesa->GlobalFlag &= ~GFLAG_TEXTURE_STATES;
594 }
595
596 void
597 sis6326_update_render_state( sisContextPtr smesa )
598 {
599    __GLSiSHardware *prev = &smesa->prev;
600
601    mWait3DCmdQueue (45);
602
603    if (smesa->GlobalFlag & GFLAG_ENABLESETTING) {
604       if (!smesa->clearTexCache) {
605          MMIO(REG_6326_3D_TEnable, prev->hwCapEnable);
606       } else {
607          MMIO(REG_6326_3D_TEnable, prev->hwCapEnable & ~S_ENABLE_TextureCache);
608          MMIO(REG_6326_3D_TEnable, prev->hwCapEnable);
609          smesa->clearTexCache = GL_FALSE;
610       }
611    }
612
613    /* Z Setting */
614    if (smesa->GlobalFlag & GFLAG_ZSETTING) {
615       MMIO(REG_6326_3D_ZSet, prev->hwZ);
616       MMIO(REG_6326_3D_ZAddress, prev->hwOffsetZ);
617    }
618
619    /* Alpha Setting */
620    if (smesa->GlobalFlag & GFLAG_ALPHASETTING)
621       MMIO(REG_6326_3D_AlphaSet, prev->hwAlpha);
622
623    if (smesa->GlobalFlag & GFLAG_DESTSETTING) {
624       MMIO(REG_6326_3D_DstSet, prev->hwDstSet);
625       MMIO(REG_6326_3D_DstAddress, prev->hwOffsetDest);
626    }
627
628    /* Fog Setting */
629    if (smesa->GlobalFlag & GFLAG_FOGSETTING) {
630       MMIO(REG_6326_3D_FogSet, prev->hwFog);
631    }
632
633    /* Miscellaneous Setting */
634    if (smesa->GlobalFlag & GFLAG_DSTBLEND)
635       MMIO(REG_6326_3D_DstSrcBlendMode, prev->hwDstSrcBlend);
636
637    if (smesa->GlobalFlag & GFLAG_CLIPPING) {
638       MMIO(REG_6326_3D_ClipTopBottom, prev->clipTopBottom);
639       MMIO(REG_6326_3D_ClipLeftRight, prev->clipLeftRight);
640    }
641
642   smesa->GlobalFlag &= ~GFLAG_RENDER_STATES;
643 }
644
645 void
646 sis6326_update_texture_state (sisContextPtr smesa)
647 {
648    __GLSiSHardware *prev = &smesa->prev;
649
650    mWait3DCmdQueue (55);
651    if (smesa->clearTexCache || (smesa->GlobalFlag & GFLAG_TEXTUREADDRESS)) {
652       MMIO(REG_6326_3D_TEnable, prev->hwCapEnable & ~S_ENABLE_TextureCache);
653       MMIO(REG_6326_3D_TEnable, prev->hwCapEnable);
654       smesa->clearTexCache = GL_FALSE;
655    }
656
657    /* Texture Setting */
658    if (smesa->GlobalFlag & CFLAG_TEXTURERESET)
659       MMIO(REG_6326_3D_TextureSet, prev->texture[0].hwTextureSet);
660
661    if (smesa->GlobalFlag & GFLAG_TEXTUREMIPMAP)
662       MMIO(REG_6326_3D_TextureWidthHeight, prev->texture[0].hwTexWidthHeight);
663
664   /*
665   MMIO(REG_3D_TextureTransparencyColorHigh, prev->texture[0].hwTextureClrHigh);
666   MMIO(REG_3D_TextureTransparencyColorLow, prev->texture[0].hwTextureClrLow);
667   */
668
669    if (smesa->GlobalFlag & GFLAG_TEXBORDERCOLOR)
670       MMIO(REG_6326_3D_TextureBorderColor, prev->texture[0].hwTextureBorderColor);
671
672    if (smesa->GlobalFlag & GFLAG_TEXTUREADDRESS) {
673       switch ((prev->texture[0].hwTextureSet & MASK_6326_TextureLevel) >> 8)
674       {
675       case 9:
676          MMIO(REG_6326_3D_TextureAddress9, prev->texture[0].texOffset9);
677          /* FALLTHROUGH */
678       case 8:
679          MMIO(REG_6326_3D_TextureAddress8, prev->texture[0].texOffset8);
680          MMIO(REG_6326_3D_TexturePitch89, prev->texture[0].texPitch89);
681          /* FALLTHROUGH */
682       case 7:
683          MMIO(REG_6326_3D_TextureAddress7, prev->texture[0].texOffset7);
684          /* FALLTHROUGH */
685       case 6:
686          MMIO(REG_6326_3D_TextureAddress6, prev->texture[0].texOffset6);
687          MMIO(REG_6326_3D_TexturePitch67, prev->texture[0].texPitch67);
688          /* FALLTHROUGH */
689       case 5:
690          MMIO(REG_6326_3D_TextureAddress5, prev->texture[0].texOffset5);
691          /* FALLTHROUGH */
692       case 4:
693          MMIO(REG_6326_3D_TextureAddress4, prev->texture[0].texOffset4);
694          MMIO(REG_6326_3D_TexturePitch45, prev->texture[0].texPitch45);
695          /* FALLTHROUGH */
696       case 3:
697          MMIO(REG_6326_3D_TextureAddress3, prev->texture[0].texOffset3);
698          /* FALLTHROUGH */
699       case 2:
700          MMIO(REG_6326_3D_TextureAddress2, prev->texture[0].texOffset2);
701          MMIO(REG_6326_3D_TexturePitch23, prev->texture[0].texPitch23);
702          /* FALLTHROUGH */
703       case 1:
704          MMIO(REG_6326_3D_TextureAddress1, prev->texture[0].texOffset1);
705          /* FALLTHROUGH */
706       case 0:
707          MMIO(REG_6326_3D_TextureAddress0, prev->texture[0].texOffset0);
708          MMIO(REG_6326_3D_TexturePitch01, prev->texture[0].texPitch01);
709          break;
710       }
711    }
712
713    /* texture environment */
714    if (smesa->GlobalFlag & GFLAG_TEXTUREENV) {
715       MMIO(REG_6326_3D_TextureBlendSet, prev->hwTexBlendSet);
716    }
717
718    smesa->GlobalFlag &= ~GFLAG_TEXTURE_STATES;
719 }