more work on GL_ARB_texture_compression
[mesa.git] / src / mesa / main / texobj.c
index 78691d24f2c5687a3bb9770a8d559e6174e06460..81833a3fa62020a1308fb2d290c3bded55a9d707 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id: texobj.c,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.1
+ * Version:  3.3
  * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2000  Brian Paul   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"),
  */
 
 
-
-
-
 #ifdef PC_HEADER
 #include "all.h"
 #else
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "glheader.h"
+#include "colortab.h"
 #include "context.h"
 #include "enums.h"
 #include "hash.h"
-#include "macros.h"
+#include "mem.h"
 #include "teximage.h"
 #include "texstate.h"
 #include "texobj.h"
 #include "types.h"
-#ifdef XFree86Server
-#include "GL/xf86glx.h"
-#endif
 #endif
 
 
@@ -55,7 +47,7 @@
  * table.
  * Input:  shared - the shared GL state structure to contain the texture object
  *         name - integer name for the texture object
- *         dimensions - either 1, 2 or 3
+ *         dimensions - either 1, 2, 3 or 6 (cube map)
  * Return:  pointer to new texture object
  */
 struct gl_texture_object *
@@ -64,12 +56,13 @@ gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
 {
    struct gl_texture_object *obj;
 
-   assert(dimensions <= 3);
+   ASSERT(dimensions <= 3 || dimensions == 6);
+
+   obj = CALLOC_STRUCT(gl_texture_object);
 
-   obj = (struct gl_texture_object *)
-                     calloc(1,sizeof(struct gl_texture_object));
    if (obj) {
       /* init the non-zero fields */
+      obj->RefCount = 1;
       obj->Name = name;
       obj->Dimensions = dimensions;
       obj->WrapS = GL_REPEAT;
@@ -81,23 +74,19 @@ gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
       obj->BaseLevel = 0;
       obj->MaxLevel = 1000;
       obj->MinMagThresh = 0.0F;
-      obj->Palette[0] = 255;
-      obj->Palette[1] = 255;
-      obj->Palette[2] = 255;
-      obj->Palette[3] = 255;
-      obj->PaletteSize = 1;
-      obj->PaletteIntFormat = GL_RGBA;
-      obj->PaletteFormat = GL_RGBA;
+      _mesa_init_colortable(&obj->Palette);
 
       /* insert into linked list */
       if (shared) {
+         _glthread_LOCK_MUTEX(shared->Mutex);
          obj->Next = shared->TexObjectList;
          shared->TexObjectList = obj;
+         _glthread_UNLOCK_MUTEX(shared->Mutex);
       }
 
       if (name > 0) {
          /* insert into hash table */
-         HashInsert(shared->TexObjects, name, obj);
+         _mesa_HashInsert(shared->TexObjects, name, obj);
       }
    }
    return obj;
@@ -125,6 +114,7 @@ void gl_free_texture_object( struct gl_shared_state *shared,
 
    /* unlink t from the linked list */
    if (shared) {
+      _glthread_LOCK_MUTEX(shared->Mutex);
       tprev = NULL;
       tcurr = shared->TexObjectList;
       while (tcurr) {
@@ -140,24 +130,27 @@ void gl_free_texture_object( struct gl_shared_state *shared,
          tprev = tcurr;
          tcurr = tcurr->Next;
       }
+      _glthread_UNLOCK_MUTEX(shared->Mutex);
    }
 
    if (t->Name) {
       /* remove from hash table */
-      HashRemove(shared->TexObjects, t->Name);
+      _mesa_HashRemove(shared->TexObjects, t->Name);
    }
 
-   /* free texture image */
+   _mesa_free_colortable_data(&t->Palette);
+
+   /* free texture images */
    {
       GLuint i;
       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
          if (t->Image[i]) {
-            gl_free_texture_image( t->Image[i] );
+            _mesa_free_texture_image( t->Image[i] );
          }
       }
    }
    /* free this object */
-   free( t );
+   FREE( t );
 }
 
 
@@ -166,12 +159,14 @@ void gl_free_texture_object( struct gl_shared_state *shared,
  * Examine a texture object to determine if it is complete or not.
  * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
  */
-void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_texture_object *t )
+void
+_mesa_test_texobj_completeness( const GLcontext *ctx,
+                                struct gl_texture_object *t )
 {
    t->Complete = GL_TRUE;  /* be optimistic */
 
    /* Always need level zero image */
-   if (!t->Image[0] || !t->Image[0]->Data) {
+   if (!t->Image[0]) {
       t->Complete = GL_FALSE;
       return;
    }
@@ -180,7 +175,7 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
    if (t->Dimensions==1) {
       t->P = t->Image[0]->WidthLog2;
    }
-   else if (t->Dimensions==2) {
+   else if (t->Dimensions == 2 || t->Dimensions == 6) {
       t->P = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2);
    }
    else if (t->Dimensions==3) {
@@ -210,10 +205,6 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
       /* Test dimension-independent attributes */
       for (i = minLevel; i <= maxLevel; i++) {
          if (t->Image[i]) {
-            if (!t->Image[i]->Data) {
-               t->Complete = GL_FALSE;
-               return;
-            }
             if (t->Image[i]->Format != t->Image[0]->Format) {
                t->Complete = GL_FALSE;
                return;
@@ -238,10 +229,6 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
                   t->Complete = GL_FALSE;
                   return;
                }
-               if (!t->Image[i]->Data) {
-                  t->Complete = GL_FALSE;
-                  return;
-               }
                if (t->Image[i]->Width2 != width ) {
                   t->Complete = GL_FALSE;
                   return;
@@ -328,12 +315,16 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
 }
 
 
+_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
+
 
 /*
  * Execute glGenTextures
  */
-void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *texName )
+void
+_mesa_GenTextures( GLsizei n, GLuint *texName )
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLuint first;
    GLint i;
 
@@ -343,7 +334,13 @@ void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *texName )
       return;
    }
 
-   first = HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
+
+   /*
+    * This must be atomic (generation and allocation of texture IDs)
+    */
+   _glthread_LOCK_MUTEX(GenTexturesLock);
+
+   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
 
    /* Return the texture names */
    for (i=0;i<n;i++) {
@@ -356,6 +353,8 @@ void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *texName )
       GLuint dims = 0;
       (void) gl_alloc_texture_object(ctx->Shared, name, dims);
    }
+
+   _glthread_UNLOCK_MUTEX(GenTexturesLock);
 }
 
 
@@ -363,8 +362,10 @@ void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *texName )
 /*
  * Execute glDeleteTextures
  */
-void gl_DeleteTextures( GLcontext *ctx, GLsizei n, const GLuint *texName)
+void
+_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLint i;
 
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures");
@@ -373,28 +374,31 @@ void gl_DeleteTextures( GLcontext *ctx, GLsizei n, const GLuint *texName)
       struct gl_texture_object *t;
       if (texName[i]>0) {
          t = (struct gl_texture_object *)
-            HashLookup(ctx->Shared->TexObjects, texName[i]);
+            _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
          if (t) {
+            /* First check if this texture is currently bound.
+             * If so, unbind it and decrement the reference count.
+             */
             GLuint u;
-            for (u=0; u<MAX_TEXTURE_UNITS; u++) {
+            for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
                struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
               GLuint d;
               for (d = 1 ; d <= 3 ; d++) {
-                 if (unit->CurrentD[d]==t) {
-                    unit->CurrentD[d] = ctx->Shared->DefaultD[d][u];
-                    ctx->Shared->DefaultD[d][u]->RefCount++;
+                 if (unit->CurrentD[d] == t) {
+                    unit->CurrentD[d] = ctx->Shared->DefaultD[d];
+                    ctx->Shared->DefaultD[d]->RefCount++;
                     t->RefCount--;
-                    assert( t->RefCount >= 0 );
+                    ASSERT( t->RefCount >= 0 );
                  }
               }
             }
 
-            /* tell device driver to delete texture */
-            if (ctx->Driver.DeleteTexture) {
-               (*ctx->Driver.DeleteTexture)( ctx, t );
-            }
-
-            if (t->RefCount==0) {
+            /* Decrement reference count and delete if zero */
+            t->RefCount--;
+            ASSERT( t->RefCount >= 0 );
+            if (t->RefCount == 0) {
+               if (ctx->Driver.DeleteTexture)
+                  (*ctx->Driver.DeleteTexture)( ctx, t );
                gl_free_texture_object(ctx->Shared, t);
             }
          }
@@ -407,13 +411,15 @@ void gl_DeleteTextures( GLcontext *ctx, GLsizei n, const GLuint *texName)
 /*
  * Execute glBindTexture
  */
-void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
+void
+_mesa_BindTexture( GLenum target, GLuint texName )
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLuint unit = ctx->Texture.CurrentUnit;
    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
    struct gl_texture_object *oldTexObj;
    struct gl_texture_object *newTexObj;
-   GLint dim;
+   GLuint dim;
 
    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
       fprintf(stderr, "glBindTexture %s %d\n",
@@ -421,30 +427,50 @@ void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
 
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
 
-   dim = target - GL_TEXTURE_1D;
-
-   if (dim < 0 || dim > 2) {
-      gl_error( ctx, GL_INVALID_ENUM, "glBindTexture" );
-      return;
+   switch (target) {
+      case GL_TEXTURE_1D:
+         dim = 1;
+         oldTexObj = texUnit->CurrentD[1];
+         break;
+      case GL_TEXTURE_2D:
+         dim = 2;
+         oldTexObj = texUnit->CurrentD[2];
+         break;
+      case GL_TEXTURE_3D:
+         dim = 3;
+         oldTexObj = texUnit->CurrentD[3];
+         break;
+      case GL_TEXTURE_CUBE_MAP_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap) {
+            dim = 6;
+            oldTexObj = texUnit->CurrentCubeMap;
+            break;
+         }
+         /* fallthrough */
+      default:
+         gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
+         return;
    }
 
-   dim++;
-   oldTexObj = texUnit->CurrentD[dim];
-
    if (oldTexObj->Name == texName)
       return;
 
-   if (texName == 0) 
-      newTexObj = ctx->Shared->DefaultD[unit][dim];
+   if (texName == 0) {
+      if (target == GL_TEXTURE_CUBE_MAP_ARB)
+         newTexObj = ctx->Shared->DefaultCubeMap;
+      else
+         newTexObj = ctx->Shared->DefaultD[dim];
+   }
    else {
-      struct HashTable *hash = ctx->Shared->TexObjects;
-      newTexObj = (struct gl_texture_object *) HashLookup(hash, texName);
+      struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
+      newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
 
       if (!newTexObj)
         newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim);
 
       if (newTexObj->Dimensions != dim) {
         if (newTexObj->Dimensions) {
+            /* the named texture object's dimensions don't match the target */
            gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
            return;
         }
@@ -452,9 +478,24 @@ void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
       }
    }
 
-   oldTexObj->RefCount--;
    newTexObj->RefCount++;
-   texUnit->CurrentD[dim] = newTexObj;
+
+   switch (target) {
+      case GL_TEXTURE_1D:
+         texUnit->CurrentD[1] = newTexObj;
+         break;
+      case GL_TEXTURE_2D:
+         texUnit->CurrentD[2] = newTexObj;
+         break;
+      case GL_TEXTURE_3D:
+         texUnit->CurrentD[3] = newTexObj;
+         break;
+      case GL_TEXTURE_CUBE_MAP_ARB:
+         texUnit->CurrentCubeMap = newTexObj;
+         break;
+      default:
+         gl_problem(ctx, "bad target in BindTexture");
+   }
 
    /* If we've changed the CurrentD[123] texture object then update the
     * ctx->Texture.Current pointer to point to the new texture object.
@@ -481,6 +522,17 @@ void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
    if (ctx->Driver.BindTexture) {
       (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
    }
+
+   if (oldTexObj->Name > 0) {
+      /* never delete default (id=0) texture objects */
+      oldTexObj->RefCount--;
+      if (oldTexObj->RefCount <= 0) {
+         if (ctx->Driver.DeleteTexture) {
+           (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
+        }
+         gl_free_texture_object(ctx->Shared, oldTexObj);
+      }
+   }
 }
 
 
@@ -488,10 +540,11 @@ void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
 /*
  * Execute glPrioritizeTextures
  */
-void gl_PrioritizeTextures( GLcontext *ctx,
-                            GLsizei n, const GLuint *texName,
-                            const GLclampf *priorities )
+void
+_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
+                          const GLclampf *priorities )
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLint i;
 
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures");
@@ -504,9 +557,12 @@ void gl_PrioritizeTextures( GLcontext *ctx,
       struct gl_texture_object *t;
       if (texName[i]>0) {
          t = (struct gl_texture_object *)
-            HashLookup(ctx->Shared->TexObjects, texName[i]);
+            _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
          if (t) {
             t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
+
+           if (ctx->Driver.PrioritizeTexture)
+              ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
          }
       }
    }
@@ -515,12 +571,13 @@ void gl_PrioritizeTextures( GLcontext *ctx,
 
 
 /*
- * Execute glAreTexturesResident
+ * Execute glAreTexturesResident 
  */
-GLboolean gl_AreTexturesResident( GLcontext *ctx, GLsizei n,
-                                  const GLuint *texName,
-                                  GLboolean *residences )
+GLboolean
+_mesa_AreTexturesResident( GLsizei n, const GLuint *texName,
+                           GLboolean *residences )
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLboolean resident = GL_TRUE;
    GLint i;
 
@@ -539,10 +596,12 @@ GLboolean gl_AreTexturesResident( GLcontext *ctx, GLsizei n,
          return GL_FALSE;
       }
       t = (struct gl_texture_object *)
-         HashLookup(ctx->Shared->TexObjects, texName[i]);
+         _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
       if (t) {
-         /* we consider all valid texture objects to be resident */
-         residences[i] = GL_TRUE;
+        if (ctx->Driver.IsTextureResident)
+           residences[i] = ctx->Driver.IsTextureResident( ctx, t );
+        else 
+           residences[i] = GL_TRUE;
       }
       else {
          gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" );
@@ -557,11 +616,13 @@ GLboolean gl_AreTexturesResident( GLcontext *ctx, GLsizei n,
 /*
  * Execute glIsTexture
  */
-GLboolean gl_IsTexture( GLcontext *ctx, GLuint texture )
+GLboolean
+_mesa_IsTexture( GLuint texture )
 {
+   GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures",
                                                  GL_FALSE);
-   if (texture>0 && HashLookup(ctx->Shared->TexObjects, texture)) {
+   if (texture>0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) {
       return GL_TRUE;
    }
    else {