From 14f20b0ab38d5996d2c2716156912491783d93fa Mon Sep 17 00:00:00 2001 From: Jiawei Shao Date: Tue, 9 Jun 2020 17:09:04 +0000 Subject: [PATCH] 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 Reviewed-by: Kai Ninomiya --- src/dawn_native/Buffer.h | 2 + src/dawn_native/metal/BufferMTL.h | 2 + src/dawn_native/metal/BufferMTL.mm | 16 ++++++ src/tests/BUILD.gn | 1 + .../end2end/NonzeroBufferCreationTests.cpp | 54 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 src/tests/end2end/NonzeroBufferCreationTests.cpp diff --git a/src/dawn_native/Buffer.h b/src/dawn_native/Buffer.h index ec20452d8c..b0e45f9543 100644 --- a/src/dawn_native/Buffer.h +++ b/src/dawn_native/Buffer.h @@ -39,6 +39,8 @@ namespace dawn_native { }; 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 afd123e9a9..98bab96244 100644 --- a/src/dawn_native/metal/BufferMTL.h +++ b/src/dawn_native/metal/BufferMTL.h @@ -43,6 +43,8 @@ namespace dawn_native { namespace metal { bool IsMapWritable() const override; MaybeError MapAtCreationImpl(uint8_t** mappedPointer) override; + void ClearBuffer(BufferBase::ClearValue clearValue); + id mMtlBuffer = nil; }; diff --git a/src/dawn_native/metal/BufferMTL.mm b/src/dawn_native/metal/BufferMTL.mm index 6da0fd9c80..4d00d69341 100644 --- a/src/dawn_native/metal/BufferMTL.mm +++ b/src/dawn_native/metal/BufferMTL.mm @@ -71,6 +71,10 @@ namespace dawn_native { namespace metal { return DAWN_OUT_OF_MEMORY_ERROR("Buffer allocation failed"); } + if (GetDevice()->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) { + ClearBuffer(BufferBase::ClearValue::NonZero); + } + return {}; } @@ -113,4 +117,16 @@ namespace dawn_native { namespace metal { 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 b55bde4702..01b3042b7e 100644 --- a/src/tests/BUILD.gn +++ b/src/tests/BUILD.gn @@ -277,6 +277,7 @@ source_set("dawn_end2end_tests_sources") { "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 0000000000..088c10a219 --- /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 + +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 expectedData(kSize, static_cast(1u)); + EXPECT_BUFFER_U32_RANGE_EQ(reinterpret_cast(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 expectedData(kSize, static_cast(1u)); + EXPECT_BUFFER_U32_RANGE_EQ(reinterpret_cast(expectedData.data()), buffer, 0, + kSize / sizeof(uint32_t)); +} + +DAWN_INSTANTIATE_TEST(NonzeroBufferCreationTests, + MetalBackend({"nonzero_clear_resources_on_creation_for_testing"}));