added GL_SGIX/SGIS_pixel_texture
[mesa.git] / src / mesa / main / texobj.c
index fae67d4fc7c831f1c7469b5827f42badda417b49..bafac922aaec611957e07dc2078d94d3e9336b6f 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id: texobj.c,v 1.8 1999/11/11 01:22:28 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * 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"),
@@ -56,10 +55,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;
@@ -84,13 +83,15 @@ gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
 
       /* 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;
@@ -118,6 +119,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) {
@@ -133,11 +135,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 */
@@ -145,7 +148,7 @@ void gl_free_texture_object( struct gl_shared_state *shared,
       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] );
          }
       }
    }
@@ -164,7 +167,7 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
    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;
    }
@@ -203,10 +206,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;
@@ -231,10 +230,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;
@@ -321,6 +316,8 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
 }
 
 
+_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
+
 
 /*
  * Execute glGenTextures
@@ -338,7 +335,13 @@ _mesa_GenTextures( 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++) {
@@ -351,6 +354,8 @@ _mesa_GenTextures( GLsizei n, GLuint *texName )
       GLuint dims = 0;
       (void) gl_alloc_texture_object(ctx->Shared, name, dims);
    }
+
+   _glthread_UNLOCK_MUTEX(GenTexturesLock);
 }
 
 
@@ -370,28 +375,31 @@ _mesa_DeleteTextures( 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);
             }
          }
@@ -420,14 +428,21 @@ _mesa_BindTexture( GLenum target, GLuint texName )
 
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
 
-   dim = (GLuint) (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)
@@ -436,14 +451,15 @@ _mesa_BindTexture( 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;
         }
@@ -515,7 +531,7 @@ _mesa_PrioritizeTextures( 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) {
             t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
 
@@ -554,7 +570,7 @@ _mesa_AreTexturesResident( GLsizei n, const GLuint *texName,
          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 );
@@ -580,7 +596,7 @@ _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 {