OpenGL: Simplify lazy clear of render pass attachments

Bug: dawn:145
Change-Id: Ia175ebc5a74f7cc15584b9132e00f9089d5dc5b6
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/14983
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn_native/opengl/CommandBufferGL.cpp b/src/dawn_native/opengl/CommandBufferGL.cpp
index b8052d2..9bbf740 100644
--- a/src/dawn_native/opengl/CommandBufferGL.cpp
+++ b/src/dawn_native/opengl/CommandBufferGL.cpp
@@ -408,12 +408,13 @@
         auto TransitionForPass = [](const PassResourceUsage& usages) {
             for (size_t i = 0; i < usages.textures.size(); i++) {
                 Texture* texture = ToBackend(usages.textures[i]);
-                // We count the lazy clears for non output attachment textures in order to match the
-                // backdoor lazy clear counts in Vulkan and D3D12.
-                bool isLazyClear =
-                    !(usages.textureUsages[i] & wgpu::TextureUsage::OutputAttachment);
-                texture->EnsureSubresourceContentInitialized(
-                    0, texture->GetNumMipLevels(), 0, texture->GetArrayLayers(), isLazyClear);
+                // Clear textures that are not output attachments. Output attachments will be
+                // cleared in BeginRenderPass by setting the loadop to clear when the
+                // texture subresource has not been initialized before the render pass.
+                if (!(usages.textureUsages[i] & wgpu::TextureUsage::OutputAttachment)) {
+                    texture->EnsureSubresourceContentInitialized(0, texture->GetNumMipLevels(), 0,
+                                                                 texture->GetArrayLayers());
+                }
             }
         };
 
@@ -434,6 +435,8 @@
                 case Command::BeginRenderPass: {
                     auto* cmd = mCommands.NextCommand<BeginRenderPassCmd>();
                     TransitionForPass(passResourceUsages[nextPassNumber]);
+
+                    LazyClearRenderPassAttachments(cmd);
                     ExecuteRenderPass(cmd);
 
                     nextPassNumber++;
@@ -779,7 +782,6 @@
             for (uint32_t i :
                  IterateBitSet(renderPass->attachmentState->GetColorAttachmentsMask())) {
                 auto* attachmentInfo = &renderPass->colorAttachments[i];
-                TextureView* view = ToBackend(attachmentInfo->view.Get());
 
                 // Load op - color
                 // TODO(cwallez@chromium.org): Choose the clear function depending on the
@@ -791,30 +793,14 @@
                     gl.ClearBufferfv(GL_COLOR, i, &attachmentInfo->clearColor.r);
                 }
 
-                switch (attachmentInfo->storeOp) {
-                    case wgpu::StoreOp::Store: {
-                        view->GetTexture()->SetIsSubresourceContentInitialized(
-                            true, view->GetBaseMipLevel(), view->GetLevelCount(),
-                            view->GetBaseArrayLayer(), view->GetLayerCount());
-                    } break;
-
-                    case wgpu::StoreOp::Clear: {
-                        // TODO(natlee@microsoft.com): call glDiscard to do optimization
-                        view->GetTexture()->SetIsSubresourceContentInitialized(
-                            false, view->GetBaseMipLevel(), view->GetLevelCount(),
-                            view->GetBaseArrayLayer(), view->GetLayerCount());
-                    } break;
-
-                    default:
-                        UNREACHABLE();
-                        break;
+                if (attachmentInfo->storeOp == wgpu::StoreOp::Clear) {
+                    // TODO(natlee@microsoft.com): call glDiscard to do optimization
                 }
             }
 
             if (renderPass->attachmentState->HasDepthStencilAttachment()) {
                 auto* attachmentInfo = &renderPass->depthStencilAttachment;
                 const Format& attachmentFormat = attachmentInfo->view->GetTexture()->GetFormat();
-                TextureView* view = ToBackend(attachmentInfo->view.Get());
 
                 // Load op - depth/stencil
                 bool doDepthClear = attachmentFormat.HasDepth() &&
@@ -838,18 +824,6 @@
                     const GLint clearStencil = attachmentInfo->clearStencil;
                     gl.ClearBufferiv(GL_STENCIL, 0, &clearStencil);
                 }
-
-                if (attachmentInfo->depthStoreOp == wgpu::StoreOp::Store &&
-                    attachmentInfo->stencilStoreOp == wgpu::StoreOp::Store) {
-                    view->GetTexture()->SetIsSubresourceContentInitialized(
-                        true, view->GetBaseMipLevel(), view->GetLevelCount(),
-                        view->GetBaseArrayLayer(), view->GetLayerCount());
-                } else if (attachmentInfo->depthStoreOp == wgpu::StoreOp::Clear &&
-                           attachmentInfo->stencilStoreOp == wgpu::StoreOp::Clear) {
-                    view->GetTexture()->SetIsSubresourceContentInitialized(
-                        false, view->GetBaseMipLevel(), view->GetLevelCount(),
-                        view->GetBaseArrayLayer(), view->GetLayerCount());
-                }
             }
         }
 
diff --git a/src/dawn_native/opengl/TextureGL.cpp b/src/dawn_native/opengl/TextureGL.cpp
index 27c9c12..f75df56 100644
--- a/src/dawn_native/opengl/TextureGL.cpp
+++ b/src/dawn_native/opengl/TextureGL.cpp
@@ -299,14 +299,18 @@
                 gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
             }
         }
+        if (clearValue == TextureBase::ClearValue::Zero) {
+            SetIsSubresourceContentInitialized(true, baseMipLevel, levelCount, baseArrayLayer,
+                                               layerCount);
+            device->IncrementLazyClearCountForTesting();
+        }
         return {};
     }
 
     void Texture::EnsureSubresourceContentInitialized(uint32_t baseMipLevel,
                                                       uint32_t levelCount,
                                                       uint32_t baseArrayLayer,
-                                                      uint32_t layerCount,
-                                                      bool isLazyClear) {
+                                                      uint32_t layerCount) {
         if (!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
             return;
         }
@@ -314,11 +318,6 @@
                                              layerCount)) {
             GetDevice()->ConsumedError(ClearTexture(baseMipLevel, levelCount, baseArrayLayer,
                                                     layerCount, TextureBase::ClearValue::Zero));
-            if (isLazyClear) {
-                GetDevice()->IncrementLazyClearCountForTesting();
-            }
-            SetIsSubresourceContentInitialized(true, baseMipLevel, levelCount, baseArrayLayer,
-                                               layerCount);
         }
     }
 
diff --git a/src/dawn_native/opengl/TextureGL.h b/src/dawn_native/opengl/TextureGL.h
index b72c4fc..919e883 100644
--- a/src/dawn_native/opengl/TextureGL.h
+++ b/src/dawn_native/opengl/TextureGL.h
@@ -40,8 +40,7 @@
         void EnsureSubresourceContentInitialized(uint32_t baseMipLevel,
                                                  uint32_t levelCount,
                                                  uint32_t baseArrayLayer,
-                                                 uint32_t layerCount,
-                                                 bool isLazyClear = true);
+                                                 uint32_t layerCount);
 
       private:
         void DestroyImpl() override;