+
+/**
+ * Check if the given texture object is bound to the current draw or
+ * read framebuffer. If so, Unbind it.
+ */
+static void
+unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
+{
+ const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
+ GLuint i;
+
+ for (i = 0; i < n; i++) {
+ struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
+ if (fb->Name) {
+ GLuint j;
+ for (j = 0; j < BUFFER_COUNT; j++) {
+ if (fb->Attachment[j].Type == GL_TEXTURE &&
+ fb->Attachment[j].Texture == texObj) {
+ _mesa_remove_attachment(ctx, fb->Attachment + j);
+ }
+ }
+ }
+ }
+}
+
+
+/**
+ * Check if the given texture object is bound to any texture image units and
+ * unbind it if so (revert to default textures).
+ */
+static void
+unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
+{
+ GLuint u;
+
+ for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
+ struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
+ if (texObj == unit->Current1D) {
+ _mesa_reference_texobj(&unit->Current1D, ctx->Shared->Default1D);
+ }
+ else if (texObj == unit->Current2D) {
+ _mesa_reference_texobj(&unit->Current2D, ctx->Shared->Default2D);
+ }
+ else if (texObj == unit->Current3D) {
+ _mesa_reference_texobj(&unit->Current3D, ctx->Shared->Default3D);
+ }
+ else if (texObj == unit->CurrentCubeMap) {
+ _mesa_reference_texobj(&unit->CurrentCubeMap, ctx->Shared->DefaultCubeMap);
+ }
+ else if (texObj == unit->CurrentRect) {
+ _mesa_reference_texobj(&unit->CurrentRect, ctx->Shared->DefaultRect);
+ }
+ else if (texObj == unit->Current1DArray) {
+ _mesa_reference_texobj(&unit->Current1DArray, ctx->Shared->Default1DArray);
+ }
+ else if (texObj == unit->Current2DArray) {
+ _mesa_reference_texobj(&unit->Current2DArray, ctx->Shared->Default2DArray);
+ }
+ }
+}
+
+