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>
This commit is contained in:
Jiawei Shao 2020-06-09 17:09:04 +00:00 committed by Commit Bot service account
parent b6c8855e3a
commit 14f20b0ab3
5 changed files with 75 additions and 0 deletions

View File

@ -39,6 +39,8 @@ namespace dawn_native {
};
public:
enum class ClearValue { Zero, NonZero };
BufferBase(DeviceBase* device, const BufferDescriptor* descriptor);
static BufferBase* MakeError(DeviceBase* device);

View File

@ -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<MTLBuffer> mMtlBuffer = nil;
};

View File

@ -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

View File

@ -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",

View File

@ -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"}));