rearranged order of some functions
[mesa.git] / src / mesa / main / texobj.c
index e46b318a44bcea81a707a0c77b00a8adb08e5cc7..f782212a16c39ffc98b36a2e30a2076c6cce0ea0 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: texobj.c,v 1.5 1999/10/10 13:04:17 brianp Exp $ */
+/* $Id: texobj.c,v 1.14 2000/02/12 01:59:19 brianp 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"),
  * 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.
  */
-/* $XFree86: xc/lib/GL/mesa/src/texobj.c,v 1.3 1999/04/04 00:20:32 dawes Exp $ */
-
-
 
 
 #ifdef PC_HEADER
 #include "all.h"
 #else
-#ifndef XFree86Server
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#else
-#include "GL/xf86glx.h"
-#endif
+#include "glheader.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"
@@ -65,10 +56,10 @@ gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
 {
    struct gl_texture_object *obj;
 
-   assert(dimensions <= 3);
+   ASSERT(dimensions <= 3);
+
+   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;
@@ -83,23 +74,25 @@ 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;
+      obj->Palette.Table[0] = 255;
+      obj->Palette.Table[1] = 255;
+      obj->Palette.Table[2] = 255;
+      obj->Palette.Table[3] = 255;
+      obj->Palette.Size = 1;
+      obj->Palette.IntFormat = GL_RGBA;
+      obj->Palette.Format = GL_RGBA;
 
       /* 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;
@@ -127,6 +120,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) {
@@ -142,11 +136,12 @@ 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 */
@@ -159,7 +154,7 @@ void gl_free_texture_object( struct gl_shared_state *shared,
       }
    }
    /* free this object */
-   GL_FREE( t );
+   FREE( t );
 }
 
 
@@ -334,8 +329,10 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
 /*
  * 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;
 
@@ -345,7 +342,7 @@ void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *texName )
       return;
    }
 
-   first = HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
+   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
 
    /* Return the texture names */
    for (i=0;i<n;i++) {
@@ -365,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");
@@ -375,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) {
+                 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);
             }
          }
@@ -409,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",
@@ -423,14 +427,21 @@ 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;
+         break;
+      case GL_TEXTURE_2D:
+         dim = 2;
+         break;
+      case GL_TEXTURE_3D:
+         dim = 3;
+         break;
+      default:
+         gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
+         return;
    }
 
-   dim++;
    oldTexObj = texUnit->CurrentD[dim];
 
    if (oldTexObj->Name == texName)
@@ -439,14 +450,15 @@ void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texName )
    if (texName == 0) 
       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;
         }
@@ -501,10 +513,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");
@@ -517,7 +530,7 @@ 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 );
 
@@ -533,10 +546,11 @@ void gl_PrioritizeTextures( GLcontext *ctx,
 /*
  * 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;
 
@@ -555,7 +569,7 @@ 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) {
         if (ctx->Driver.IsTextureResident)
            residences[i] = ctx->Driver.IsTextureResident( ctx, t );
@@ -575,11 +589,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 {