Add toggle for disabling lazy clear for mapped at creation buffers

Add a toggle to skip the memset for clearing mappedAtCreation buffers.
The buffer is considered initialized with this toggle so that we don't
clear it later using the GPU thereby preserving its contents.

Bug: chromium:1456492
Change-Id: I3ec0d2bbcf5b0814e095227836cb7b0d1e983c99
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/159028
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Auto-Submit: Sunny Sachanandani <sunnyps@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/src/dawn/native/Buffer.cpp b/src/dawn/native/Buffer.cpp
index 6749fa3..1114c9e 100644
--- a/src/dawn/native/Buffer.cpp
+++ b/src/dawn/native/Buffer.cpp
@@ -343,13 +343,16 @@
     }
 
     DeviceBase* device = GetDevice();
-    if (device->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
+    if (device->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse) &&
+        !device->IsToggleEnabled(Toggle::DisableLazyClearForMappedAtCreationBuffer)) {
         memset(ptr, uint8_t(0u), size);
-        SetIsDataInitialized();
         device->IncrementLazyClearCountForTesting();
     } else if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
         memset(ptr, uint8_t(1u), size);
     }
+    // Mark the buffer as initialized since we don't want to later clear it using the GPU since that
+    // would overwrite what the client wrote using the CPU.
+    SetIsDataInitialized();
 
     return {};
 }
diff --git a/src/dawn/native/Toggles.cpp b/src/dawn/native/Toggles.cpp
index 020aa96..feab124 100644
--- a/src/dawn/native/Toggles.cpp
+++ b/src/dawn/native/Toggles.cpp
@@ -71,6 +71,10 @@
       "Clears resource to zero on first usage. This initializes the resource so that no dirty bits "
       "from recycled memory is present in the new resource.",
       "https://crbug.com/dawn/145", ToggleStage::Device}},
+    {Toggle::DisableLazyClearForMappedAtCreationBuffer,
+     {"disable_lazy_clear_for_mapped_at_creation_buffer",
+      "Disable clearing buffers to zero for buffers which are mapped at creation.",
+      "https://crbug.com/dawn/145", ToggleStage::Device}},
     {Toggle::TurnOffVsync,
      {"turn_off_vsync",
       "Turn off vsync when rendering. In order to do performance test or run perf tests, turn off "
diff --git a/src/dawn/native/Toggles.h b/src/dawn/native/Toggles.h
index bd1ead7..3a05369 100644
--- a/src/dawn/native/Toggles.h
+++ b/src/dawn/native/Toggles.h
@@ -45,6 +45,7 @@
     NonzeroClearResourcesOnCreationForTesting,
     AlwaysResolveIntoZeroLevelAndLayer,
     LazyClearResourceOnFirstUse,
+    DisableLazyClearForMappedAtCreationBuffer,
     TurnOffVsync,
     UseTemporaryBufferInCompressedTextureToTextureCopy,
     UseD3D12ResourceHeapTier2,