[native] Eagerly erase from mDirtyStateTextures

In ResourceTableBase, eagerly erase textures from mDirtyStateTextures
when their last slot is removed in SetEntry or when they are destroyed
in OnTextureDestroyed, rather than lazily skipping missing entries in
MakeResourcesVisibleExcept.

This avoids holding dangling TextureBase* pointers in
mDirtyStateTextures, guarantees that every dirty texture is in
mTextureState with at least one slot, and simplifies state tracking for
the upcoming SetResourceUsage change.

Bug: 579994653
Change-Id: I19fb5c9500d30c3067ff2ce4dbce9ac95b20983d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/356595
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/dawn/native/ResourceTable.cpp b/src/dawn/native/ResourceTable.cpp
index f9b210d..c29db92 100644
--- a/src/dawn/native/ResourceTable.cpp
+++ b/src/dawn/native/ResourceTable.cpp
@@ -502,6 +502,7 @@
                 mTextureState.erase(currTexture);
                 // Also remove the mapping of texture to this table
                 currTexture->RemoveResourceTableUse(this);
+                mDirtyStateTextures.erase(currTexture);
             }
         }
     } else if (auto sampler = GetRef<SamplerBase>(state.resource)) {
@@ -570,12 +571,14 @@
     Updates updates;
 
     auto HandleDirtyTexture = [&](TextureBase* texture) {
-        // The texture may not be in mTextureState if, for example, it was destroyed.
+        // A texture is removed from mDirtyStateTextures whenever it is removed from mTextureState
+        // (e.g. when its last slot is removed or when it is destroyed).
         auto iter = mTextureState.find(texture);
-        if (iter == mTextureState.end()) {
-            return;
-        }
+        DAWN_ASSERT(iter != mTextureState.end());
         auto& textureState = iter->second;
+        // A texture is only added to mDirtyStateTextures when it has at least one slot, and removed
+        // from it when it has no more slots.
+        DAWN_ASSERT(!textureState.slots.empty());
 
         // Update visible flag
         bool visible =
@@ -699,6 +702,7 @@
         MarkStateDirty(slot);
     }
     mTextureState.erase(iter);
+    mDirtyStateTextures.erase(texture);
 }
 
 }  // namespace dawn::native