+ * Do actual texture binding. All error checking should have been done prior
+ * to calling this function. Note that the texture target (1D, 2D, etc) is
+ * always specified by the texObj->TargetIndex.
+ *
+ * \param unit index of texture unit to update
+ * \param texObj the new texture object (cannot be NULL)
+ */
+static void
+bind_texture(struct gl_context *ctx,
+ unsigned unit,
+ struct gl_texture_object *texObj)
+{
+ struct gl_texture_unit *texUnit;
+ int targetIndex;
+
+ assert(unit < ARRAY_SIZE(ctx->Texture.Unit));
+ texUnit = &ctx->Texture.Unit[unit];
+
+ assert(texObj);
+ assert(valid_texture_object(texObj));
+
+ targetIndex = texObj->TargetIndex;
+ assert(targetIndex >= 0);
+ assert(targetIndex < NUM_TEXTURE_TARGETS);
+
+ /* Check if this texture is only used by this context and is already bound.
+ * If so, just return. For GL_OES_image_external, rebinding the texture
+ * always must invalidate cached resources.
+ */
+ if (targetIndex != TEXTURE_EXTERNAL_INDEX) {
+ bool early_out;
+ mtx_lock(&ctx->Shared->Mutex);
+ early_out = ((ctx->Shared->RefCount == 1)
+ && (texObj == texUnit->CurrentTex[targetIndex]));
+ mtx_unlock(&ctx->Shared->Mutex);
+ if (early_out) {
+ return;
+ }
+ }
+
+ /* flush before changing binding */
+ FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
+
+ /* If the refcount on the previously bound texture is decremented to
+ * zero, it'll be deleted here.
+ */
+ _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], texObj);
+
+ ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
+ unit + 1);
+
+ if (texObj->Name != 0)
+ texUnit->_BoundTextures |= (1 << targetIndex);
+ else
+ texUnit->_BoundTextures &= ~(1 << targetIndex);
+
+ /* Pass BindTexture call to device driver */
+ if (ctx->Driver.BindTexture) {
+ ctx->Driver.BindTexture(ctx, unit, texObj->Target, texObj);
+ }
+}
+
+
+/**
+ * Implement glBindTexture(). Do error checking, look-up or create a new
+ * texture object, then bind it in the current texture unit.