mesa/st: enable carry/borrow lowering pass
[mesa.git] / src / mesa / main / texobj.c
index a72b75317f990f8dee81a34e558289b9ec5b73de..2a82c2d4ee3f1be094b3550dbe687cfdd9a9c8dc 100644 (file)
@@ -61,6 +61,27 @@ _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
 }
 
 
+void
+_mesa_begin_texture_lookups(struct gl_context *ctx)
+{
+   _mesa_HashLockMutex(ctx->Shared->TexObjects);
+}
+
+
+void
+_mesa_end_texture_lookups(struct gl_context *ctx)
+{
+   _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
+}
+
+
+struct gl_texture_object *
+_mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id)
+{
+   return (struct gl_texture_object *)
+      _mesa_HashLookupLocked(ctx->Shared->TexObjects, id);
+}
+
 
 /**
  * Allocate and initialize a new texture object.  But don't put it into the
@@ -1093,18 +1114,20 @@ static void
 unbind_texobj_from_texunits(struct gl_context *ctx,
                             struct gl_texture_object *texObj)
 {
-   GLuint u, tex;
+   const gl_texture_index index = texObj->TargetIndex;
+   GLuint u;
+
+   if (texObj->Target == 0)
+      return;
 
    for (u = 0; u < ctx->Texture.NumCurrentTexUsed; u++) {
       struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
-      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
-         if (texObj == unit->CurrentTex[tex]) {
-            _mesa_reference_texobj(&unit->CurrentTex[tex],
-                                   ctx->Shared->DefaultTex[tex]);
-            ASSERT(unit->CurrentTex[tex]);
-            unit->_BoundTextures &= ~(1 << tex);
-            break;
-         }
+
+      if (texObj == unit->CurrentTex[index]) {
+         /* Bind the default texture for this unit/target */
+         _mesa_reference_texobj(&unit->CurrentTex[index],
+                                ctx->Shared->DefaultTex[index]);
+         unit->_BoundTextures &= ~(1 << index);
       }
    }
 }
@@ -1128,6 +1151,28 @@ unbind_texobj_from_image_units(struct gl_context *ctx,
    }
 }
 
+/**
+ * Unbinds all textures bound to the given texture image unit.
+ */
+static void
+unbind_textures_from_unit(struct gl_context *ctx, GLuint unit)
+{
+   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+
+   while (texUnit->_BoundTextures) {
+      const GLuint index = ffs(texUnit->_BoundTextures) - 1;
+      struct gl_texture_object *texObj = ctx->Shared->DefaultTex[index];
+
+      _mesa_reference_texobj(&texUnit->CurrentTex[index], texObj);
+
+      /* Pass BindTexture call to device driver */
+      if (ctx->Driver.BindTexture)
+         ctx->Driver.BindTexture(ctx, unit, 0, texObj);
+
+      texUnit->_BoundTextures &= ~(1 << index);
+      ctx->NewState |= _NEW_TEXTURE;
+   }
+}
 
 /**
  * Delete named textures.
@@ -1367,7 +1412,111 @@ _mesa_BindTexture( GLenum target, GLuint texName )
 
    /* Pass BindTexture call to device driver */
    if (ctx->Driver.BindTexture)
-      ctx->Driver.BindTexture(ctx, target, newTexObj);
+      ctx->Driver.BindTexture(ctx, ctx->Texture.CurrentUnit, target, newTexObj);
+}
+
+
+void GLAPIENTRY
+_mesa_BindTextures(GLuint first, GLsizei count, const GLuint *textures)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLint i;
+
+   /* The ARB_multi_bind spec says:
+    *
+    *     "An INVALID_OPERATION error is generated if <first> + <count>
+    *      is greater than the number of texture image units supported
+    *      by the implementation."
+    */
+   if (first + count > ctx->Const.MaxCombinedTextureImageUnits) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glBindTextures(first=%u + count=%d > the value of "
+                  "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
+                  first, count, ctx->Const.MaxCombinedTextureImageUnits);
+      return;
+   }
+
+   /* Flush before changing bindings */
+   FLUSH_VERTICES(ctx, 0);
+
+   ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
+                                         first + count);
+
+   if (textures) {
+      /* Note that the error semantics for multi-bind commands differ from
+       * those of other GL commands.
+       *
+       * The issues section in the ARB_multi_bind spec says:
+       *
+       *    "(11) Typically, OpenGL specifies that if an error is generated by
+       *          a command, that command has no effect.  This is somewhat
+       *          unfortunate for multi-bind commands, because it would require
+       *          a first pass to scan the entire list of bound objects for
+       *          errors and then a second pass to actually perform the
+       *          bindings.  Should we have different error semantics?
+       *
+       *       RESOLVED:  Yes.  In this specification, when the parameters for
+       *       one of the <count> binding points are invalid, that binding
+       *       point is not updated and an error will be generated.  However,
+       *       other binding points in the same command will be updated if
+       *       their parameters are valid and no other error occurs."
+       */
+
+      _mesa_begin_texture_lookups(ctx);
+
+      for (i = 0; i < count; i++) {
+         if (textures[i] != 0) {
+            struct gl_texture_unit *texUnit = &ctx->Texture.Unit[first + i];
+            struct gl_texture_object *current = texUnit->_Current;
+            struct gl_texture_object *texObj;
+
+            if (current && current->Name == textures[i])
+               texObj = current;
+            else
+               texObj = _mesa_lookup_texture_locked(ctx, textures[i]);
+
+            if (texObj && texObj->Target != 0) {
+               const gl_texture_index targetIndex = texObj->TargetIndex;
+
+               if (texUnit->CurrentTex[targetIndex] != texObj) {
+                  /* Do the actual binding.  The refcount on the previously
+                   * bound texture object will be decremented.  It will be
+                   * deleted if the count hits zero.
+                   */
+                  _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex],
+                                         texObj);
+
+                  texUnit->_BoundTextures |= (1 << targetIndex);
+                  ctx->NewState |= _NEW_TEXTURE;
+
+                  /* Pass the BindTexture call to the device driver */
+                  if (ctx->Driver.BindTexture)
+                     ctx->Driver.BindTexture(ctx, first + i,
+                                             texObj->Target, texObj);
+               }
+            } else {
+               /* The ARB_multi_bind spec says:
+                *
+                *     "An INVALID_OPERATION error is generated if any value
+                *      in <textures> is not zero or the name of an existing
+                *      texture object (per binding)."
+                */
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glBindTextures(textures[%d]=%u is not zero "
+                           "or the name of an existing texture object)",
+                           i, textures[i]);
+            }
+         } else {
+            unbind_textures_from_unit(ctx, first + i);
+         }
+      }
+
+      _mesa_end_texture_lookups(ctx);
+   } else {
+      /* Unbind all textures in the range <first> through <first>+<count>-1 */
+      for (i = 0; i < count; i++)
+         unbind_textures_from_unit(ctx, first + i);
+   }
 }