Add nonzero_clear_resources_on_creation_for_testing toggle to opengl

Forces texture to clear to nonzero on creation to test the logic of
lazy clearing.

Bug: dawn:145
Change-Id: Ib1f6e8f961927008e3c09960ad0219f975d86cc6
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7240
Commit-Queue: Natasha Lee <natlee@microsoft.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp
index a287f48..df45d5c 100644
--- a/src/dawn_native/opengl/DeviceGL.cpp
+++ b/src/dawn_native/opengl/DeviceGL.cpp
@@ -33,6 +33,9 @@
 
     Device::Device(AdapterBase* adapter, const DeviceDescriptor* descriptor)
         : DeviceBase(adapter, descriptor) {
+        if (descriptor != nullptr) {
+            ApplyToggleOverrides(descriptor);
+        }
     }
 
     Device::~Device() {
diff --git a/src/dawn_native/opengl/TextureGL.cpp b/src/dawn_native/opengl/TextureGL.cpp
index 34fa9f3..03203d2 100644
--- a/src/dawn_native/opengl/TextureGL.cpp
+++ b/src/dawn_native/opengl/TextureGL.cpp
@@ -166,6 +166,18 @@
         // The texture is not complete if it uses mipmapping and not all levels up to
         // MAX_LEVEL have been defined.
         glTexParameteri(mTarget, GL_TEXTURE_MAX_LEVEL, levels - 1);
+
+        if (GetDevice()->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
+            static constexpr uint32_t MAX_TEXEL_SIZE = 16;
+            ASSERT(TextureFormatPixelSize(GetFormat()) <= MAX_TEXEL_SIZE);
+            GLubyte clearColor[MAX_TEXEL_SIZE];
+            std::fill(clearColor, clearColor + MAX_TEXEL_SIZE, 255);
+
+            // TODO(natlee@microsoft.com): clear all subresources
+            for (uint32_t i = 0; i < GL_TEXTURE_MAX_LEVEL; i++) {
+                glClearTexImage(mHandle, i, formatInfo.format, formatInfo.type, clearColor);
+            }
+        }
     }
 
     Texture::Texture(Device* device,
diff --git a/src/tests/end2end/NonzeroTextureCreationTests.cpp b/src/tests/end2end/NonzeroTextureCreationTests.cpp
index 28a9b4d..3053d13 100644
--- a/src/tests/end2end/NonzeroTextureCreationTests.cpp
+++ b/src/tests/end2end/NonzeroTextureCreationTests.cpp
@@ -95,5 +95,7 @@
 }

 

 DAWN_INSTANTIATE_TEST(NonzeroTextureCreationTests,

+                      ForceWorkaround(OpenGLBackend,

+                                      "nonzero_clear_resources_on_creation_for_testing"),

                       ForceWorkaround(VulkanBackend,

                                       "nonzero_clear_resources_on_creation_for_testing"));