fix GL_BACK color material bug
[mesa.git] / src / mesa / drivers / glide / fxddtex.c
index 3745312923c6ab790b8f922241ab940c033775a1..dd2bf4456163e0f71dce69d7ef4d227396b6f560 100644 (file)
@@ -95,9 +95,9 @@ static void fxTexInvalidate(GLcontext *ctx, struct gl_texture_object *tObj)
   fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
   tfxTexInfo *ti;
 
-  fxTMMoveOutTM(fxMesa,tObj); /* TO DO: SLOW but easy to write */
-
   ti=fxTMGetTexInfo(tObj);
+  if (ti->isInTM)   fxTMMoveOutTM(fxMesa,tObj); /* TO DO: SLOW but easy to write */
+
   ti->validated=GL_FALSE;
   fxMesa->new_state|=FX_NEW_TEXTURING;
   ctx->Driver.RenderStart = fxSetupFXUnits;
@@ -325,124 +325,154 @@ void fxDDTexParam(GLcontext *ctx, GLenum target, struct gl_texture_object *tObj,
 
 void fxDDTexDel(GLcontext *ctx, struct gl_texture_object *tObj)
 {
-  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
-  tfxTexInfo *ti=fxTMGetTexInfo(tObj);
+  fxMesaContext fxMesa = FX_CONTEXT(ctx);
+  tfxTexInfo *ti = fxTMGetTexInfo(tObj);
 
-  if (MESA_VERBOSE&VERBOSE_DRIVER) {
-     fprintf(stderr,"fxmesa: fxDDTexDel(%d,%x)\n",tObj->Name,(GLuint)ti);
+  if (MESA_VERBOSE & VERBOSE_DRIVER) {
+     fprintf(stderr, "fxmesa: fxDDTexDel(%d,%p)\n", tObj->Name, ti);
   }
 
-  if(!ti)
+  if (!ti)
     return;
 
-  fxTMFreeTexture(fxMesa,tObj);
+  fxTMFreeTexture(fxMesa, tObj);
 
   FREE(ti);
-  tObj->DriverData=NULL;
+  tObj->DriverData = NULL;
 
-  ctx->NewState|=NEW_TEXTURING;
+  ctx->NewState |= NEW_TEXTURING;
 }
 
-void fxDDTexPalette(GLcontext *ctx, struct gl_texture_object *tObj)
-{
-  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
-  int i;
-  FxU32 r,g,b,a;
-  tfxTexInfo *ti;
 
-  if(tObj) {  
-     if (MESA_VERBOSE&VERBOSE_DRIVER) {
-       fprintf(stderr,"fxmesa: fxDDTexPalette(%d,%x)\n",tObj->Name,(GLuint)tObj->DriverData);
-     }
 
-    if(tObj->Palette.Format!=GL_RGBA) {
-#ifndef FX_SILENT
-      fprintf(stderr,"fx Driver: unsupported palette format in texpalette()\n");
-#endif
-      return;
-    }
+/*
+ * Convert a gl_color_table texture palette to Glide's format.
+ */
+static void
+convertPalette(FxU32 data[256], const struct gl_color_table *table)
+{
+  const GLubyte *tableUB = (const GLubyte *) table->Table;
+  GLint width = table->Size;
+  FxU32 r, g, b, a;
+  GLint i;
 
-    if(tObj->Palette.Size>256) {
-#ifndef FX_SILENT
-      fprintf(stderr,"fx Driver: unsupported palette size in texpalette()\n");
-#endif
-      return;
-    }
+  ASSERT(table->TableType == GL_UNSIGNED_BYTE);
 
-    if (!tObj->DriverData)
-      tObj->DriverData=fxAllocTexObjData(fxMesa);
-  
-    ti=fxTMGetTexInfo(tObj);
-
-    for(i=0;i<tObj->Palette.Size;i++) {
-      r=tObj->Palette.Table[i*4];
-      g=tObj->Palette.Table[i*4+1];
-      b=tObj->Palette.Table[i*4+2];
-      a=tObj->Palette.Table[i*4+3];
-      ti->palette.data[i]=(a<<24)|(r<<16)|(g<<8)|b;
-    }
+  switch (table->Format) {
+    case GL_INTENSITY:
+      for (i = 0; i < width; i++) {
+        r = tableUB[i];
+        g = tableUB[i];
+        b = tableUB[i];
+        a = tableUB[i];
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+    case GL_LUMINANCE:
+      for (i = 0; i < width; i++) {
+        r = tableUB[i];
+        g = tableUB[i];
+        b = tableUB[i];
+        a = 255;
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+    case GL_ALPHA:
+      for (i = 0; i < width; i++) {
+        r = g = b = 255;
+        a = tableUB[i];
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+    case GL_LUMINANCE_ALPHA:
+      for (i = 0; i < width; i++) {
+        r = g = b = tableUB[i*2+0];
+        a = tableUB[i*2+1];
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+    case GL_RGB:
+      for (i = 0; i < width; i++) {
+        r = tableUB[i*3+0];
+        g = tableUB[i*3+1];
+        b = tableUB[i*3+2];
+        a = 255;
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+    case GL_RGBA:
+      for (i = 0; i < width; i++) {
+        r = tableUB[i*4+0];
+        g = tableUB[i*4+1];
+        b = tableUB[i*4+2];
+        a = tableUB[i*4+3];
+        data[i] = (a << 24) | (r << 16) | (g << 8) | b;
+      }
+      break;
+  }
+}
 
-    fxTexInvalidate(ctx,tObj);
-  } else {
-     if (MESA_VERBOSE&VERBOSE_DRIVER) {
-       fprintf(stderr,"fxmesa: fxDDTexPalette(global)\n");
-     }
-    if(ctx->Texture.Palette.Format!=GL_RGBA) {
-#ifndef FX_SILENT
-      fprintf(stderr,"fx Driver: unsupported palette format in texpalette()\n");
-#endif
-      return;
-    }
 
-    if(ctx->Texture.Palette.Size>256) {
-#ifndef FX_SILENT
-      fprintf(stderr,"fx Driver: unsupported palette size in texpalette()\n");
-#endif
-      return;
+void fxDDTexPalette(GLcontext *ctx, struct gl_texture_object *tObj)
+{
+  fxMesaContext fxMesa = FX_CONTEXT(ctx);
+
+  if (tObj) {
+    /* per-texture palette */
+    tfxTexInfo *ti;
+    if (MESA_VERBOSE & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxmesa: fxDDTexPalette(%d,%x)\n",
+              tObj->Name, (GLuint) tObj->DriverData);
     }
-
-    for(i=0;i<ctx->Texture.Palette.Size;i++) {
-      r=ctx->Texture.Palette.Table[i*4];
-      g=ctx->Texture.Palette.Table[i*4+1];
-      b=ctx->Texture.Palette.Table[i*4+2];
-      a=ctx->Texture.Palette.Table[i*4+3];
-      fxMesa->glbPalette.data[i]=(a<<24)|(r<<16)|(g<<8)|b;
+    if (!tObj->DriverData)
+      tObj->DriverData = fxAllocTexObjData(fxMesa);
+    ti = fxTMGetTexInfo(tObj);
+    convertPalette(ti->palette.data, &tObj->Palette);
+    fxTexInvalidate(ctx, tObj);
+  }
+  else {
+    /* global texture palette */
+    if (MESA_VERBOSE & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxmesa: fxDDTexPalette(global)\n");
     }
-
-    fxMesa->new_state|=FX_NEW_TEXTURING;
+    convertPalette(fxMesa->glbPalette.data, &ctx->Texture.Palette);
+    fxMesa->new_state |= FX_NEW_TEXTURING;
     ctx->Driver.RenderStart = fxSetupFXUnits;
   }
 }
 
+
 void fxDDTexUseGlbPalette(GLcontext *ctx, GLboolean state)
 {
-  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
+  fxMesaContext fxMesa = FX_CONTEXT(ctx);
 
   if (MESA_VERBOSE&VERBOSE_DRIVER) {
      fprintf(stderr,"fxmesa: fxDDTexUseGlbPalette(%d)\n",state);
   }
 
-  if(state) {
-    fxMesa->haveGlobalPaletteTexture=1;
+  if (state) {
+    fxMesa->haveGlobalPaletteTexture = 1;
 
-    FX_grTexDownloadTable(GR_TMU0,GR_TEXTABLE_PALETTE,&(fxMesa->glbPalette));
+    FX_grTexDownloadTable(GR_TMU0,GR_TEXTABLE_PALETTE, &(fxMesa->glbPalette));
     if (fxMesa->haveTwoTMUs)
-       FX_grTexDownloadTable(GR_TMU1,GR_TEXTABLE_PALETTE,&(fxMesa->glbPalette));
-  } else {
-    fxMesa->haveGlobalPaletteTexture=0;
+       FX_grTexDownloadTable(GR_TMU1, GR_TEXTABLE_PALETTE, &(fxMesa->glbPalette));
+  }
+  else {
+    fxMesa->haveGlobalPaletteTexture = 0;
 
-    if((ctx->Texture.Unit[0].Current==ctx->Texture.Unit[0].CurrentD[2]) &&
-       (ctx->Texture.Unit[0].Current!=NULL)) {
-      struct gl_texture_object *tObj=ctx->Texture.Unit[0].Current;
+    if ((ctx->Texture.Unit[0].Current == ctx->Texture.Unit[0].CurrentD[2]) &&
+        (ctx->Texture.Unit[0].Current != NULL)) {
+      struct gl_texture_object *tObj = ctx->Texture.Unit[0].Current;
 
       if (!tObj->DriverData)
-        tObj->DriverData=fxAllocTexObjData(fxMesa);
+        tObj->DriverData = fxAllocTexObjData(fxMesa);
   
-      fxTexInvalidate(ctx,tObj);
+      fxTexInvalidate(ctx, tObj);
     }
   }
 }
 
+
 static int logbase2(int n)
 {
   GLint i = 1;
@@ -1048,7 +1078,7 @@ GLboolean fxDDTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
   }
 
   if (ti->validated && ti->isInTM)
-    fxTMReloadSubMipMapLevel(fxMesa, texObj, level, yoffset, height);
+    fxTMReloadMipMapLevel(fxMesa, texObj, level);
   else
     fxTexInvalidate(ctx, texObj);