Metal: Enable nonzero_clear_resources_on_creation_for_testing on buffer
This patch enables nonzero_clear_resources_on_creation_for_testing
toggle on buffer on Metal backends as a preparation of supporting buffer
lazy-initializations in Dawn.
BUG=dawn:414
TEST=dawn_end2end_tests
Change-Id: Ia7106482922aeb0521affa206f459d40938a0131
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/22861
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn_native/Buffer.h b/src/dawn_native/Buffer.h
index ec20452..b0e45f9 100644
--- a/src/dawn_native/Buffer.h
+++ b/src/dawn_native/Buffer.h
@@ -39,6 +39,8 @@
};
public:
+ enum class ClearValue { Zero, NonZero };
+
BufferBase(DeviceBase* device, const BufferDescriptor* descriptor);
static BufferBase* MakeError(DeviceBase* device);
diff --git a/src/dawn_native/metal/BufferMTL.h b/src/dawn_native/metal/BufferMTL.h
index afd123e..98bab96 100644
--- a/src/dawn_native/metal/BufferMTL.h
+++ b/src/dawn_native/metal/BufferMTL.h
@@ -43,6 +43,8 @@
bool IsMapWritable() const override;
MaybeError MapAtCreationImpl(uint8_t** mappedPointer) override;
+ void ClearBuffer(BufferBase::ClearValue clearValue);
+
id<MTLBuffer> mMtlBuffer = nil;
};
diff --git a/src/dawn_native/metal/BufferMTL.mm b/src/dawn_native/metal/BufferMTL.mm
index 6da0fd9..4d00d69 100644
--- a/src/dawn_native/metal/BufferMTL.mm
+++ b/src/dawn_native/metal/BufferMTL.mm
@@ -71,6 +71,10 @@
return DAWN_OUT_OF_MEMORY_ERROR("Buffer allocation failed");
}
+ if (GetDevice()->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
+ ClearBuffer(BufferBase::ClearValue::NonZero);
+ }
+
return {};
}
@@ -113,4 +117,16 @@
mMtlBuffer = nil;
}
+ void Buffer::ClearBuffer(BufferBase::ClearValue clearValue) {
+ // TODO(jiawei.shao@intel.com): support buffer lazy-initialization to 0.
+ ASSERT(clearValue == BufferBase::ClearValue::NonZero);
+ const uint8_t clearBufferValue = 1;
+
+ Device* device = ToBackend(GetDevice());
+ CommandRecordingContext* commandContext = device->GetPendingCommandContext();
+ [commandContext->EnsureBlit() fillBuffer:mMtlBuffer
+ range:NSMakeRange(0, GetSize())
+ value:clearBufferValue];
+ }
+
}} // namespace dawn_native::metal
diff --git a/src/tests/BUILD.gn b/src/tests/BUILD.gn
index b55bde4..01b3042 100644
--- a/src/tests/BUILD.gn
+++ b/src/tests/BUILD.gn
@@ -277,6 +277,7 @@
"end2end/GpuMemorySynchronizationTests.cpp",
"end2end/IndexFormatTests.cpp",
"end2end/MultisampledRenderingTests.cpp",
+ "end2end/NonzeroBufferCreationTests.cpp",
"end2end/NonzeroTextureCreationTests.cpp",
"end2end/ObjectCachingTests.cpp",
"end2end/OpArrayLengthTests.cpp",
diff --git a/src/tests/end2end/NonzeroBufferCreationTests.cpp b/src/tests/end2end/NonzeroBufferCreationTests.cpp
new file mode 100644
index 0000000..088c10a
--- /dev/null
+++ b/src/tests/end2end/NonzeroBufferCreationTests.cpp
@@ -0,0 +1,54 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "tests/DawnTest.h"
+
+#include <vector>
+
+class NonzeroBufferCreationTests : public DawnTest {};
+
+// Verify that each byte of the buffer has all been initialized to 1 with the toggle enabled when it
+// is created with CopyDst usage.
+TEST_P(NonzeroBufferCreationTests, BufferCreationWithCopyDstUsage) {
+ constexpr uint32_t kSize = 32u;
+
+ wgpu::BufferDescriptor descriptor;
+ descriptor.size = kSize;
+ descriptor.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;
+
+ wgpu::Buffer buffer = device.CreateBuffer(&descriptor);
+
+ std::vector<uint8_t> expectedData(kSize, static_cast<uint8_t>(1u));
+ EXPECT_BUFFER_U32_RANGE_EQ(reinterpret_cast<uint32_t*>(expectedData.data()), buffer, 0,
+ kSize / sizeof(uint32_t));
+}
+
+// Verify that each byte of the buffer has all been initialized to 1 with the toggle enabled when it
+// is created without CopyDst usage.
+TEST_P(NonzeroBufferCreationTests, BufferCreationWithoutCopyDstUsage) {
+ constexpr uint32_t kSize = 32u;
+
+ wgpu::BufferDescriptor descriptor;
+ descriptor.size = kSize;
+ descriptor.usage = wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc;
+
+ wgpu::Buffer buffer = device.CreateBuffer(&descriptor);
+
+ std::vector<uint8_t> expectedData(kSize, static_cast<uint8_t>(1u));
+ EXPECT_BUFFER_U32_RANGE_EQ(reinterpret_cast<uint32_t*>(expectedData.data()), buffer, 0,
+ kSize / sizeof(uint32_t));
+}
+
+DAWN_INSTANTIATE_TEST(NonzeroBufferCreationTests,
+ MetalBackend({"nonzero_clear_resources_on_creation_for_testing"}));