OpenGLES: remove texture copy workaround.

This path should no longer be necessary, now that Compatibility
validates out the cases where multiple views of the same texture
are required.

Note: it's not possible to turn this into an assert, since
(for example) Desktop GL always requires the creation of a new
texture view when binding the stencil-only aspect, for example.
Compat doesn't need this because validation doesn't allow binding
of both depth-only and stencil-only views of the same texture.

It might be possible to inspect the bindings at a higher level, and determine create a glTextureView if both depth-only and stencil-only
are used. But since the desktop GL backend is not shipping at
the moment and this doesn't apply to GLES, this is not a high priority.

Change-Id: I01966e3f5fcf48e2751743c0d825653a2b4a53bc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/190762
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Stephen White <senorblanco@chromium.org>
diff --git a/src/dawn/native/opengl/CommandBufferGL.cpp b/src/dawn/native/opengl/CommandBufferGL.cpp
index b615872..6ffe429 100644
--- a/src/dawn/native/opengl/CommandBufferGL.cpp
+++ b/src/dawn/native/opengl/CommandBufferGL.cpp
@@ -297,16 +297,6 @@
         for (BindingIndex bindingIndex{0}; bindingIndex < group->GetLayout()->GetBindingCount();
              ++bindingIndex) {
             const BindingInfo& bindingInfo = group->GetLayout()->GetBindingInfo(bindingIndex);
-
-            if (std::holds_alternative<TextureBindingInfo>(bindingInfo.bindingLayout)) {
-                TextureView* view = ToBackend(group->GetBindingAsTextureView(bindingIndex));
-                view->CopyIfNeeded();
-            }
-        }
-
-        for (BindingIndex bindingIndex{0}; bindingIndex < group->GetLayout()->GetBindingCount();
-             ++bindingIndex) {
-            const BindingInfo& bindingInfo = group->GetLayout()->GetBindingInfo(bindingIndex);
             MatchVariant(
                 bindingInfo.bindingLayout,
                 [&](const BufferBindingInfo& layout) {
@@ -418,7 +408,6 @@
                     gl.BindImageTexture(imageIndex, handle, view->GetBaseMipLevel(), isLayered,
                                         view->GetBaseArrayLayer(), access,
                                         texture->GetGLFormat().internalFormat);
-                    texture->Touch();
                 },
                 [](const InputAttachmentBindingInfo&) { DAWN_UNREACHABLE(); });
         }
@@ -525,7 +514,6 @@
             resolveView->BindToFramebuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0);
             gl.BlitFramebuffer(0, 0, renderPass->width, renderPass->height, 0, 0, renderPass->width,
                                renderPass->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
-            ToBackend(resolveView->GetTexture())->Touch();
         }
     }
 
@@ -669,7 +657,6 @@
                 DoTexSubImage(gl, dst, reinterpret_cast<void*>(src.offset), dataLayout,
                               copy->copySize);
                 gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
-                ToBackend(dst.texture)->Touch();
 
                 buffer->TrackUsage();
                 break;
@@ -811,7 +798,6 @@
                 CopyImageSubData(gl, src.aspect, srcTexture->GetHandle(), srcTexture->GetGLTarget(),
                                  src.mipLevel, src.origin, dstTexture->GetHandle(),
                                  dstTexture->GetGLTarget(), dst.mipLevel, dst.origin, copySize);
-                ToBackend(dst.texture)->Touch();
                 break;
             }
 
@@ -1283,17 +1269,6 @@
             case Command::EndRenderPass: {
                 mCommands.NextCommand<EndRenderPassCmd>();
 
-                for (auto i :
-                     IterateBitSet(renderPass->attachmentState->GetColorAttachmentsMask())) {
-                    TextureView* textureView =
-                        ToBackend(renderPass->colorAttachments[i].view.Get());
-                    ToBackend(textureView->GetTexture())->Touch();
-                }
-                if (renderPass->attachmentState->HasDepthStencilAttachment()) {
-                    TextureView* textureView =
-                        ToBackend(renderPass->depthStencilAttachment.view.Get());
-                    ToBackend(textureView->GetTexture())->Touch();
-                }
                 if (renderPass->attachmentState->GetSampleCount() > 1) {
                     ResolveMultisampledRenderTargets(gl, renderPass);
                 }
diff --git a/src/dawn/native/opengl/QueueGL.cpp b/src/dawn/native/opengl/QueueGL.cpp
index a2e1698..97af4e9 100644
--- a/src/dawn/native/opengl/QueueGL.cpp
+++ b/src/dawn/native/opengl/QueueGL.cpp
@@ -140,7 +140,6 @@
         DAWN_TRY(ToBackend(destination.texture)->EnsureSubresourceContentInitialized(range));
     }
     DoTexSubImage(ToBackend(GetDevice())->GetGL(), textureCopy, data, dataLayout, writeSizePixel);
-    ToBackend(destination.texture)->Touch();
     return {};
 }
 
diff --git a/src/dawn/native/opengl/TextureGL.cpp b/src/dawn/native/opengl/TextureGL.cpp
index 2f5518a..a936327 100644
--- a/src/dawn/native/opengl/TextureGL.cpp
+++ b/src/dawn/native/opengl/TextureGL.cpp
@@ -74,6 +74,12 @@
 bool RequiresCreatingNewTextureView(
     const TextureBase* texture,
     const UnpackedPtr<TextureViewDescriptor>& textureViewDescriptor) {
+    // Compatibility mode validation should prevent the need for creation of
+    // new texture views.
+    if (ToBackend(texture->GetDevice())->IsCompatibilityMode()) {
+        return false;
+    }
+
     constexpr wgpu::TextureUsage kShaderUsageNeedsView =
         wgpu::TextureUsage::StorageBinding | wgpu::TextureUsage::TextureBinding;
     constexpr wgpu::TextureUsage kUsageNeedsView =
@@ -180,14 +186,6 @@
     gl.TexParameteri(mTarget, GL_TEXTURE_MAX_LEVEL, levels - 1);
 }
 
-void Texture::Touch() {
-    mGenID++;
-}
-
-uint32_t Texture::GetGenID() const {
-    return mGenID;
-}
-
 Texture::Texture(Device* device, const UnpackedPtr<TextureDescriptor>& descriptor, GLuint handle)
     : TextureBase(device, descriptor), mHandle(handle) {
     mTarget = TargetForTextureViewDimension(GetCompatibilityTextureBindingViewDimension(),
@@ -517,7 +515,6 @@
         SetIsSubresourceContentInitialized(true, range);
         device->IncrementLazyClearCountForTesting();
     }
-    Touch();
     return {};
 }
 
@@ -554,8 +551,6 @@
                            descriptor->baseArrayLayer, descriptor->arrayLayerCount);
             mOwnsHandle = true;
         } else {
-            // Simulate glTextureView() with texture-to-texture copies.
-            mUseCopy = true;
             mHandle = 0;
         }
     }
@@ -630,43 +625,6 @@
     }
 }
 
-void TextureView::CopyIfNeeded() {
-    if (!mUseCopy) {
-        return;
-    }
-
-    const Texture* texture = ToBackend(GetTexture());
-    if (mGenID == texture->GetGenID()) {
-        return;
-    }
-
-    Device* device = ToBackend(GetDevice());
-    const OpenGLFunctions& gl = device->GetGL();
-    uint32_t srcLevel = GetBaseMipLevel();
-    uint32_t numLevels = GetLevelCount();
-
-    uint32_t width = GetSingleSubresourceVirtualSize().width;
-    uint32_t height = GetSingleSubresourceVirtualSize().height;
-    Extent3D size{width, height, GetLayerCount()};
-
-    if (mHandle == 0) {
-        gl.GenTextures(1, &mHandle);
-        gl.BindTexture(mTarget, mHandle);
-        AllocateTexture(gl, mTarget, texture->GetSampleCount(), numLevels, GetInternalFormat(),
-                        size);
-        mOwnsHandle = true;
-    }
-
-    Origin3D src{0, 0, GetBaseArrayLayer()};
-    Origin3D dst{0, 0, 0};
-    for (GLuint level = 0; level < numLevels; ++level) {
-        CopyImageSubData(gl, GetAspects(), texture->GetHandle(), texture->GetGLTarget(),
-                         srcLevel + level, src, mHandle, mTarget, level, dst, size);
-    }
-
-    mGenID = texture->GetGenID();
-}
-
 GLenum TextureView::GetInternalFormat() const {
     // Depth/stencil don't support reinterpretation, and the aspect is specified at
     // bind time. In that case, we use the base texture format.
diff --git a/src/dawn/native/opengl/TextureGL.h b/src/dawn/native/opengl/TextureGL.h
index f25d01f..f971dd4 100644
--- a/src/dawn/native/opengl/TextureGL.h
+++ b/src/dawn/native/opengl/TextureGL.h
@@ -46,8 +46,6 @@
     GLuint GetHandle() const;
     GLenum GetGLTarget() const;
     const GLFormat& GetGLFormat() const;
-    uint32_t GetGenID() const;
-    void Touch();
 
     MaybeError EnsureSubresourceContentInitialized(const SubresourceRange& range);
 
@@ -61,7 +59,6 @@
     GLuint mHandle;
     bool mOwnsHandle = false;
     GLenum mTarget;
-    uint32_t mGenID = 0;
 };
 
 class TextureView final : public TextureViewBase {
@@ -71,7 +68,6 @@
     GLuint GetHandle() const;
     GLenum GetGLTarget() const;
     void BindToFramebuffer(GLenum target, GLenum attachment, GLuint depthLayer = 0);
-    void CopyIfNeeded();
 
   private:
     ~TextureView() override;
@@ -82,8 +78,6 @@
     GLuint mHandle;
     GLenum mTarget;
     bool mOwnsHandle;
-    bool mUseCopy = false;
-    uint32_t mGenID = 0;
 };
 
 }  // namespace dawn::native::opengl
diff --git a/src/dawn/tests/end2end/DepthStencilSamplingTests.cpp b/src/dawn/tests/end2end/DepthStencilSamplingTests.cpp
index 285003f..16a3e3c 100644
--- a/src/dawn/tests/end2end/DepthStencilSamplingTests.cpp
+++ b/src/dawn/tests/end2end/DepthStencilSamplingTests.cpp
@@ -641,7 +641,7 @@
 // Repro test for crbug.com/dawn/1187 where sampling a depth texture returns values not in [0, 1]
 TEST_P(DepthStencilSamplingTest, CheckDepthTextureRange) {
     // TODO(crbug.com/dawn/1187): The test fails on ANGLE D3D11, investigate why.
-    DAWN_SUPPRESS_TEST_IF(IsANGLE() && IsWindows());
+    DAWN_SUPPRESS_TEST_IF(IsANGLED3D11());
 
     // TODO(crbug.com/dawn/2295): diagnose this failure on Pixel 4 OpenGLES
     DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm());
@@ -751,11 +751,6 @@
 // Test that sampling a depth/stencil texture at components 1, 2, and 3 yield 0, 0, and 1
 // respectively
 TEST_P(DepthStencilSamplingTest, SampleExtraComponents) {
-    // This test fails on ANGLE (both SwiftShader and D3D11).
-    DAWN_SUPPRESS_TEST_IF(IsANGLE());
-    // TODO(crbug.com/dawn/2295): diagnose this failure on Pixel 6 OpenGLES
-    DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsARM());
-
     wgpu::TextureFormat format = GetParam().mTextureFormat;
 
     DoSamplingExtraStencilComponentsRenderTest(TestAspectAndSamplerType::StencilAsUint, format,
@@ -961,12 +956,6 @@
 
 // Test that sampling a stencil texture with a render/compute pipeline works
 TEST_P(StencilSamplingTest, SampleStencilOnly) {
-    // This test fails on SwANGLE (although it passes on other ANGLE backends).
-    DAWN_TEST_UNSUPPORTED_IF(IsANGLE());
-
-    // TODO(crbug.com/dawn/2295): diagnose this failure on Pixel 6 OpenGLES
-    DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsARM());
-
     wgpu::TextureFormat format = GetParam().mTextureFormat;
 
     DoSamplingTest(TestAspectAndSamplerType::StencilAsUint,
diff --git a/src/dawn/tests/end2end/QueueTests.cpp b/src/dawn/tests/end2end/QueueTests.cpp
index d0b6038..9e9eba4 100644
--- a/src/dawn/tests/end2end/QueueTests.cpp
+++ b/src/dawn/tests/end2end/QueueTests.cpp
@@ -274,10 +274,9 @@
   protected:
     void SetUp() override {
         DawnTestWithParams::SetUp();
-        // TODO(crbug.com/dawn/2391): Stencil format is failing on ANGLE + SwiftShader, needs
+        // TODO(crbug.com/dawn/2391): Stencil8 format is failing on OpenGLES; needs
         // investigation.
-        DAWN_SUPPRESS_TEST_IF(IsANGLESwiftShader() &&
-                              utils::IsDepthOrStencilFormat(GetParam().mTextureFormat));
+        DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && GetParam().mTextureFormat == TextureFormat::Stencil8);
     }
 
     static DataSpec MinimumDataSpec(wgpu::Extent3D writeSize,
diff --git a/src/dawn/tests/end2end/ReadOnlyDepthStencilAttachmentTests.cpp b/src/dawn/tests/end2end/ReadOnlyDepthStencilAttachmentTests.cpp
index 9022c62..6b7b535 100644
--- a/src/dawn/tests/end2end/ReadOnlyDepthStencilAttachmentTests.cpp
+++ b/src/dawn/tests/end2end/ReadOnlyDepthStencilAttachmentTests.cpp
@@ -325,10 +325,8 @@
 class ReadOnlyStencilAttachmentTests : public ReadOnlyDepthStencilAttachmentTests {};
 
 TEST_P(ReadOnlyStencilAttachmentTests, SampleFromAttachment) {
-    // TODO(angleproject:8384): ASSERT is triggered in the ANGLE D3D11 backend likely because of
-    // the usage of the GL_STENCIL_INDEX8 format.
-    DAWN_SUPPRESS_TEST_IF(IsANGLED3D11() &&
-                          GetParam().mTextureFormat == wgpu::TextureFormat::Stencil8);
+    // TODO(anglebug.com/344950145): assert failed in rx::TextureStorage11::verifySwizzleExists
+    DAWN_SUPPRESS_TEST_IF(IsANGLED3D11());
 
     // stencilRefValue < stencilValue (stencilInitValue), so stencil test passes. The pipeline
     // samples from stencil buffer and writes into color buffer.
@@ -387,6 +385,9 @@
 
 // Test that using stencilReadOnly while modifying the depth aspect works.
 TEST_P(ReadOnlyDepthAndStencilAttachmentTests, ModifyDepthSampleStencil) {
+    // TODO(anglebug.com/344950145): assert failed in rx::TextureStorage11::verifySwizzleExists
+    DAWN_SUPPRESS_TEST_IF(IsANGLED3D11());
+
     // Stencil test is always true but the depth test passes only for the
     TestSpec spec1;
     spec1.readonlyAspects = wgpu::TextureAspect::StencilOnly;
@@ -476,6 +477,8 @@
 
 // Test sampling stencil with both the depth and stencil readonly.
 TEST_P(ReadOnlyDepthAndStencilAttachmentTests, BothReadOnlySampleStencil) {
+    // TODO(anglebug.com/344950145): assert failed in rx::TextureStorage11::verifySwizzleExists
+    DAWN_SUPPRESS_TEST_IF(IsANGLED3D11());
     // Sample the stencil while using both depth an stencil testing.
 
     // First render: depth test passes only for the bottom half, stencil passes.
diff --git a/src/dawn/tests/end2end/TextureViewTests.cpp b/src/dawn/tests/end2end/TextureViewTests.cpp
index 2120fc8..f6ea563 100644
--- a/src/dawn/tests/end2end/TextureViewTests.cpp
+++ b/src/dawn/tests/end2end/TextureViewTests.cpp
@@ -420,6 +420,7 @@
 
 // Test sampling from a 2D array texture view created on a 2D array texture.
 TEST_P(TextureViewSamplingTest, Texture2DArrayViewOn2DArrayTexture) {
+    DAWN_TEST_UNSUPPORTED_IF(IsCompatibilityMode());
     Texture2DArrayViewTest(6, 1, 2, 0);
 }
 
diff --git a/webgpu-cts/compat-expectations.txt b/webgpu-cts/compat-expectations.txt
index ffe7b57..6a9a692 100644
--- a/webgpu-cts/compat-expectations.txt
+++ b/webgpu-cts/compat-expectations.txt
@@ -178,6 +178,17 @@
 # bitcast ai_to_f32 failures; precision issue?
 crbug.com/dawn/0000 webgpu:shader,execution,expression,call,builtin,bitcast:ai_to_f32:* [ Failure ]
 
+# stencil-only tests failing after the removal of the texture copy workaround
+crbug.com/343735745 webgpu:api,operation,command_buffer,copyTextureToTexture:copy_depth_stencil:format="depth24plus-stencil8" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:offsets_and_sizes_copy_depth_stencil:format="depth24plus-stencil8";copyMethod="CopyB2T";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:offsets_and_sizes_copy_depth_stencil:format="depth24plus-stencil8";copyMethod="CopyT2B";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:offsets_and_sizes_copy_depth_stencil:format="depth24plus-stencil8";copyMethod="WriteTexture";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:offsets_and_sizes_copy_depth_stencil:format="stencil8";copyMethod="CopyB2T";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:rowsPerImage_and_bytesPerRow_depth_stencil:format="depth24plus-stencil8";copyMethod="CopyB2T";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:rowsPerImage_and_bytesPerRow_depth_stencil:format="depth24plus-stencil8";copyMethod="CopyT2B";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:rowsPerImage_and_bytesPerRow_depth_stencil:format="depth24plus-stencil8";copyMethod="WriteTexture";aspect="stencil-only" [ Failure ]
+crbug.com/343735745 webgpu:api,operation,command_buffer,image_copy:rowsPerImage_and_bytesPerRow_depth_stencil:format="stencil8";copyMethod="CopyB2T";aspect="stencil-only" [ Failure ]
+
 ### This section represents tests which may require CTS changes.
 
 # ANGLETextureSharing is not supported