mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-17 17:05:31 +00:00
Clear Vulkan Textures at first usage
This prevents dirty textures to be used when memory is recycled while destroying/creating textures. If a texture is not cleared at load, it will be cleared to 0 before it is used. Bug: dawn:145 Change-Id: Ia3f02427478fb48649089829186ccb377caa1912 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/6960 Commit-Queue: Natasha Lee <natlee@microsoft.com> Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
031fbbbaa1
commit
28232ce9f5
@@ -82,9 +82,11 @@ const DawnTestParam OpenGLBackend(dawn_native::BackendType::OpenGL);
|
||||
const DawnTestParam VulkanBackend(dawn_native::BackendType::Vulkan);
|
||||
|
||||
DawnTestParam ForceWorkarounds(const DawnTestParam& originParam,
|
||||
std::initializer_list<const char*> forceEnabledWorkarounds) {
|
||||
std::initializer_list<const char*> forceEnabledWorkarounds,
|
||||
std::initializer_list<const char*> forceDisabledWorkarounds) {
|
||||
DawnTestParam newTestParam = originParam;
|
||||
newTestParam.forceEnabledWorkarounds = forceEnabledWorkarounds;
|
||||
newTestParam.forceDisabledWorkarounds = forceDisabledWorkarounds;
|
||||
return newTestParam;
|
||||
}
|
||||
|
||||
@@ -310,8 +312,12 @@ void DawnTest::SetUp() {
|
||||
for (const char* forceEnabledWorkaround : GetParam().forceEnabledWorkarounds) {
|
||||
ASSERT(gTestEnv->GetInstance()->GetToggleInfo(forceEnabledWorkaround) != nullptr);
|
||||
}
|
||||
for (const char* forceDisabledWorkaround : GetParam().forceDisabledWorkarounds) {
|
||||
ASSERT(gTestEnv->GetInstance()->GetToggleInfo(forceDisabledWorkaround) != nullptr);
|
||||
}
|
||||
dawn_native::DeviceDescriptor deviceDescriptor;
|
||||
deviceDescriptor.forceEnabledToggles = GetParam().forceEnabledWorkarounds;
|
||||
deviceDescriptor.forceDisabledToggles = GetParam().forceDisabledWorkarounds;
|
||||
backendDevice = backendAdapter.CreateDevice(&deviceDescriptor);
|
||||
|
||||
backendProcs = dawn_native::GetProcs();
|
||||
|
||||
@@ -70,6 +70,7 @@ struct DawnTestParam {
|
||||
dawn_native::BackendType backendType;
|
||||
|
||||
std::vector<const char*> forceEnabledWorkarounds;
|
||||
std::vector<const char*> forceDisabledWorkarounds;
|
||||
};
|
||||
|
||||
// Shorthands for backend types used in the DAWN_INSTANTIATE_TEST
|
||||
@@ -79,7 +80,8 @@ extern const DawnTestParam OpenGLBackend;
|
||||
extern const DawnTestParam VulkanBackend;
|
||||
|
||||
DawnTestParam ForceWorkarounds(const DawnTestParam& originParam,
|
||||
std::initializer_list<const char*> forceEnabledWorkarounds);
|
||||
std::initializer_list<const char*> forceEnabledWorkarounds,
|
||||
std::initializer_list<const char*> forceDisabledWorkarounds = {});
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
|
||||
@@ -96,8 +96,11 @@ TEST_P(NonzeroTextureCreationTests, ArrayLayerClears) {
|
||||
|
||||
DAWN_INSTANTIATE_TEST(NonzeroTextureCreationTests,
|
||||
ForceWorkarounds(D3D12Backend,
|
||||
{"nonzero_clear_resources_on_creation_for_testing"}),
|
||||
{"nonzero_clear_resources_on_creation_for_testing"},
|
||||
{"lazy_clear_resource_on_first_use"}),
|
||||
ForceWorkarounds(OpenGLBackend,
|
||||
{"nonzero_clear_resources_on_creation_for_testing"}),
|
||||
{"nonzero_clear_resources_on_creation_for_testing"},
|
||||
{"lazy_clear_resource_on_first_use"}),
|
||||
ForceWorkarounds(VulkanBackend,
|
||||
{"nonzero_clear_resources_on_creation_for_testing"}));
|
||||
{"nonzero_clear_resources_on_creation_for_testing"},
|
||||
{"lazy_clear_resource_on_first_use"}));
|
||||
|
||||
260
src/tests/end2end/TextureZeroInitTests.cpp
Normal file
260
src/tests/end2end/TextureZeroInitTests.cpp
Normal file
@@ -0,0 +1,260 @@
|
||||
// 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 "utils/ComboRenderPipelineDescriptor.h"
|
||||
#include "utils/DawnHelpers.h"
|
||||
|
||||
class TextureZeroInitTest : public DawnTest {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
DawnTest::SetUp();
|
||||
}
|
||||
dawn::TextureDescriptor CreateTextureDescriptor(uint32_t mipLevelCount,
|
||||
uint32_t arrayLayerCount,
|
||||
dawn::TextureUsageBit usage) {
|
||||
dawn::TextureDescriptor descriptor;
|
||||
descriptor.dimension = dawn::TextureDimension::e2D;
|
||||
descriptor.size.width = kSize;
|
||||
descriptor.size.height = kSize;
|
||||
descriptor.size.depth = 1;
|
||||
descriptor.arrayLayerCount = arrayLayerCount;
|
||||
descriptor.sampleCount = 1;
|
||||
descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
|
||||
descriptor.mipLevelCount = mipLevelCount;
|
||||
descriptor.usage = usage;
|
||||
return descriptor;
|
||||
}
|
||||
dawn::TextureViewDescriptor CreateTextureViewDescriptor(uint32_t baseMipLevel,
|
||||
uint32_t baseArrayLayer) {
|
||||
dawn::TextureViewDescriptor descriptor;
|
||||
descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
|
||||
descriptor.baseArrayLayer = baseArrayLayer;
|
||||
descriptor.arrayLayerCount = 1;
|
||||
descriptor.baseMipLevel = baseMipLevel;
|
||||
descriptor.mipLevelCount = 1;
|
||||
descriptor.dimension = dawn::TextureViewDimension::e2D;
|
||||
return descriptor;
|
||||
}
|
||||
constexpr static uint32_t kSize = 128;
|
||||
};
|
||||
|
||||
// This tests that the code path of CopyTextureToBuffer clears correctly to Zero after first usage
|
||||
TEST_P(TextureZeroInitTest, RecycleTextureMemoryClear) {
|
||||
dawn::TextureDescriptor descriptor = CreateTextureDescriptor(
|
||||
1, 1, dawn::TextureUsageBit::OutputAttachment | dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture texture = device.CreateTexture(&descriptor);
|
||||
|
||||
// Texture's first usage is in EXPECT_PIXEL_RGBA8_EQ's call to CopyTextureToBuffer
|
||||
RGBA8 filledWithZeros(0, 0, 0, 0);
|
||||
EXPECT_PIXEL_RGBA8_EQ(filledWithZeros, texture, 0, 0);
|
||||
}
|
||||
|
||||
// Test that non-zero mip level clears subresource to Zero after first use
|
||||
// This goes through the BeginRenderPass's code path
|
||||
TEST_P(TextureZeroInitTest, MipMapClearsToZero) {
|
||||
dawn::TextureDescriptor descriptor = CreateTextureDescriptor(
|
||||
4, 1, dawn::TextureUsageBit::OutputAttachment | dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture texture = device.CreateTexture(&descriptor);
|
||||
|
||||
dawn::TextureViewDescriptor viewDescriptor = CreateTextureViewDescriptor(2, 0);
|
||||
dawn::TextureView view = texture.CreateView(&viewDescriptor);
|
||||
|
||||
utils::BasicRenderPass renderPass =
|
||||
utils::BasicRenderPass(kSize, kSize, texture, dawn::TextureFormat::R8G8B8A8Unorm);
|
||||
|
||||
renderPass.renderPassInfo.cColorAttachmentsInfoPtr[0]->attachment = view;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
// Texture's first usage is in BeginRenderPass's call to RecordRenderPass
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
|
||||
pass.EndPass();
|
||||
}
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
uint32_t mipSize = kSize >> 2;
|
||||
std::vector<RGBA8> expected(mipSize * mipSize, {0, 0, 0, 0});
|
||||
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected.data(), renderPass.color, 0, 0, mipSize, mipSize, 2, 0);
|
||||
}
|
||||
|
||||
// Test that non-zero array layers clears subresource to Zero after first use.
|
||||
// This goes through the BeginRenderPass's code path
|
||||
TEST_P(TextureZeroInitTest, ArrayLayerClearsToZero) {
|
||||
dawn::TextureDescriptor descriptor = CreateTextureDescriptor(
|
||||
1, 4, dawn::TextureUsageBit::OutputAttachment | dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture texture = device.CreateTexture(&descriptor);
|
||||
|
||||
dawn::TextureViewDescriptor viewDescriptor = CreateTextureViewDescriptor(0, 2);
|
||||
dawn::TextureView view = texture.CreateView(&viewDescriptor);
|
||||
|
||||
utils::BasicRenderPass renderPass =
|
||||
utils::BasicRenderPass(kSize, kSize, texture, dawn::TextureFormat::R8G8B8A8Unorm);
|
||||
|
||||
renderPass.renderPassInfo.cColorAttachmentsInfoPtr[0]->attachment = view;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
|
||||
pass.EndPass();
|
||||
}
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
std::vector<RGBA8> expected(kSize * kSize, {0, 0, 0, 0});
|
||||
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected.data(), renderPass.color, 0, 0, kSize, kSize, 0, 2);
|
||||
}
|
||||
|
||||
// This tests CopyBufferToTexture fully overwrites copy so lazy init is not needed.
|
||||
// TODO(natlee@microsoft.com): Add backdoor to dawn native to query the number of zero-inited
|
||||
// subresources
|
||||
TEST_P(TextureZeroInitTest, CopyBufferToTexture) {
|
||||
dawn::TextureDescriptor descriptor = CreateTextureDescriptor(
|
||||
4, 1,
|
||||
dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled |
|
||||
dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture texture = device.CreateTexture(&descriptor);
|
||||
|
||||
std::vector<uint8_t> data(4 * kSize * kSize, 100);
|
||||
dawn::Buffer stagingBuffer = utils::CreateBufferFromData(
|
||||
device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
|
||||
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
|
||||
dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
|
||||
dawn::Extent3D copySize = {kSize, kSize, 1};
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
std::vector<RGBA8> expected(kSize * kSize, {100, 100, 100, 100});
|
||||
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected.data(), texture, 0, 0, kSize, kSize, 0, 0);
|
||||
}
|
||||
|
||||
// Test for a copy only to a subset of the subresource, lazy init is necessary to clear the other
|
||||
// half.
|
||||
TEST_P(TextureZeroInitTest, CopyBufferToTextureHalf) {
|
||||
dawn::TextureDescriptor descriptor = CreateTextureDescriptor(
|
||||
4, 1,
|
||||
dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled |
|
||||
dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture texture = device.CreateTexture(&descriptor);
|
||||
|
||||
std::vector<uint8_t> data(4 * kSize * kSize, 100);
|
||||
dawn::Buffer stagingBuffer = utils::CreateBufferFromData(
|
||||
device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
|
||||
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
|
||||
dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
|
||||
dawn::Extent3D copySize = {kSize / 2, kSize, 1};
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
std::vector<RGBA8> expected100((kSize / 2) * kSize, {100, 100, 100, 100});
|
||||
std::vector<RGBA8> expectedZeros((kSize / 2) * kSize, {0, 0, 0, 0});
|
||||
// first half filled with 100, by the buffer data
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected100.data(), texture, 0, 0, kSize / 2, kSize, 0, 0);
|
||||
// second half should be cleared
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expectedZeros.data(), texture, kSize / 2, 0, kSize / 2, kSize, 0, 0);
|
||||
}
|
||||
|
||||
// This tests CopyTextureToTexture fully overwrites copy so lazy init is not needed.
|
||||
TEST_P(TextureZeroInitTest, CopyTextureToTexture) {
|
||||
dawn::TextureDescriptor srcDescriptor = CreateTextureDescriptor(
|
||||
1, 1, dawn::TextureUsageBit::Sampled | dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture srcTexture = device.CreateTexture(&srcDescriptor);
|
||||
|
||||
dawn::TextureCopyView srcTextureCopyView =
|
||||
utils::CreateTextureCopyView(srcTexture, 0, 0, {0, 0, 0});
|
||||
|
||||
dawn::TextureDescriptor dstDescriptor = CreateTextureDescriptor(
|
||||
1, 1,
|
||||
dawn::TextureUsageBit::OutputAttachment | dawn::TextureUsageBit::TransferDst |
|
||||
dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture dstTexture = device.CreateTexture(&dstDescriptor);
|
||||
|
||||
dawn::TextureCopyView dstTextureCopyView =
|
||||
utils::CreateTextureCopyView(dstTexture, 0, 0, {0, 0, 0});
|
||||
|
||||
dawn::Extent3D copySize = {kSize, kSize, 1};
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
encoder.CopyTextureToTexture(&srcTextureCopyView, &dstTextureCopyView, ©Size);
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
std::vector<RGBA8> expected(kSize * kSize, {0, 0, 0, 0});
|
||||
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected.data(), srcTexture, 0, 0, kSize, kSize, 0, 0);
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expected.data(), dstTexture, 0, 0, kSize, kSize, 0, 0);
|
||||
}
|
||||
|
||||
// This Tests the CopyTextureToTexture's copy only to a subset of the subresource, lazy init is
|
||||
// necessary to clear the other half.
|
||||
TEST_P(TextureZeroInitTest, CopyTextureToTextureHalf) {
|
||||
dawn::TextureDescriptor srcDescriptor = CreateTextureDescriptor(
|
||||
1, 1,
|
||||
dawn::TextureUsageBit::Sampled | dawn::TextureUsageBit::TransferSrc |
|
||||
dawn::TextureUsageBit::TransferDst);
|
||||
dawn::Texture srcTexture = device.CreateTexture(&srcDescriptor);
|
||||
|
||||
// fill srcTexture with 100
|
||||
{
|
||||
std::vector<uint8_t> data(4 * kSize * kSize, 100);
|
||||
dawn::Buffer stagingBuffer =
|
||||
utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()),
|
||||
dawn::BufferUsageBit::TransferSrc);
|
||||
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
|
||||
dawn::TextureCopyView textureCopyView =
|
||||
utils::CreateTextureCopyView(srcTexture, 0, 0, {0, 0, 0});
|
||||
dawn::Extent3D copySize = {kSize, kSize, 1};
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
}
|
||||
|
||||
dawn::TextureCopyView srcTextureCopyView =
|
||||
utils::CreateTextureCopyView(srcTexture, 0, 0, {0, 0, 0});
|
||||
|
||||
dawn::TextureDescriptor dstDescriptor = CreateTextureDescriptor(
|
||||
1, 1,
|
||||
dawn::TextureUsageBit::OutputAttachment | dawn::TextureUsageBit::TransferDst |
|
||||
dawn::TextureUsageBit::TransferSrc);
|
||||
dawn::Texture dstTexture = device.CreateTexture(&dstDescriptor);
|
||||
|
||||
dawn::TextureCopyView dstTextureCopyView =
|
||||
utils::CreateTextureCopyView(dstTexture, 0, 0, {0, 0, 0});
|
||||
dawn::Extent3D copySize = {kSize / 2, kSize, 1};
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
encoder.CopyTextureToTexture(&srcTextureCopyView, &dstTextureCopyView, ©Size);
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
std::vector<RGBA8> expectedWithZeros((kSize / 2) * kSize, {0, 0, 0, 0});
|
||||
std::vector<RGBA8> expectedWith100(kSize * kSize, {100, 100, 100, 100});
|
||||
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expectedWith100.data(), srcTexture, 0, 0, kSize, kSize, 0, 0);
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expectedWith100.data(), dstTexture, 0, 0, kSize / 2, kSize, 0, 0);
|
||||
EXPECT_TEXTURE_RGBA8_EQ(expectedWithZeros.data(), dstTexture, kSize / 2, 0, kSize / 2, kSize, 0,
|
||||
0);
|
||||
}
|
||||
|
||||
DAWN_INSTANTIATE_TEST(TextureZeroInitTest,
|
||||
ForceWorkarounds(VulkanBackend,
|
||||
{"nonzero_clear_resources_on_creation_for_testing"}));
|
||||
@@ -41,12 +41,6 @@ TEST_F(ToggleValidationTest, QueryToggleInfo) {
|
||||
|
||||
// Tests overriding toggles when creating a device works correctly.
|
||||
TEST_F(ToggleValidationTest, OverrideToggleUsage) {
|
||||
// Dawn unittests use null Adapters, so there should be no toggles that are enabled by default
|
||||
{
|
||||
std::vector<const char*> toggleNames = dawn_native::GetTogglesUsed(device.Get());
|
||||
ASSERT_EQ(0u, toggleNames.size());
|
||||
}
|
||||
|
||||
// Create device with a valid name of a toggle
|
||||
{
|
||||
const char* kValidToggleName = "emulate_store_and_msaa_resolve";
|
||||
@@ -55,18 +49,30 @@ TEST_F(ToggleValidationTest, OverrideToggleUsage) {
|
||||
|
||||
DawnDevice deviceWithToggle = adapter.CreateDevice(&descriptor);
|
||||
std::vector<const char*> toggleNames = dawn_native::GetTogglesUsed(deviceWithToggle);
|
||||
ASSERT_EQ(1u, toggleNames.size());
|
||||
ASSERT_EQ(0, strcmp(kValidToggleName, toggleNames[0]));
|
||||
bool validToggleExists = false;
|
||||
for (const char* toggle : toggleNames) {
|
||||
if (strcmp(toggle, kValidToggleName) == 0) {
|
||||
validToggleExists = true;
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(validToggleExists, true);
|
||||
}
|
||||
|
||||
// Create device with an invalid toggle name
|
||||
{
|
||||
const char* kInvalidToggleName = "!@#$%^&*";
|
||||
dawn_native::DeviceDescriptor descriptor;
|
||||
descriptor.forceEnabledToggles.push_back("!@#$%^&*");
|
||||
descriptor.forceEnabledToggles.push_back(kInvalidToggleName);
|
||||
|
||||
DawnDevice deviceWithToggle = adapter.CreateDevice(&descriptor);
|
||||
std::vector<const char*> toggleNames = dawn_native::GetTogglesUsed(deviceWithToggle);
|
||||
ASSERT_EQ(0u, toggleNames.size());
|
||||
bool InvalidToggleExists = false;
|
||||
for (const char* toggle : toggleNames) {
|
||||
if (strcmp(toggle, kInvalidToggleName) == 0) {
|
||||
InvalidToggleExists = true;
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(InvalidToggleExists, false);
|
||||
}
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
Reference in New Issue
Block a user