From 8804bc541ebb71b1646bc1c842c65c85e46d9ef3 Mon Sep 17 00:00:00 2001 From: Yizhou Jiang Date: Mon, 6 May 2019 01:53:16 +0000 Subject: [PATCH] Fix validation of mip map level Mip map level starts with the size of texture image and halves the size until a 1x1 dimension. BUG=chromium:954769 Change-Id: I59e51af216d65c721a3d2c2023306883e524820d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7000 Reviewed-by: Corentin Wallez Reviewed-by: Kai Ninomiya Commit-Queue: Yizhou Jiang --- src/dawn_native/Texture.cpp | 6 ++++-- .../validation/TextureValidationTests.cpp | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/dawn_native/Texture.cpp b/src/dawn_native/Texture.cpp index 633b943a53..7baf5dcde5 100644 --- a/src/dawn_native/Texture.cpp +++ b/src/dawn_native/Texture.cpp @@ -14,6 +14,8 @@ #include "dawn_native/Texture.h" +#include + #include "common/Assert.h" #include "common/Math.h" #include "dawn_native/Device.h" @@ -174,8 +176,8 @@ namespace dawn_native { return DAWN_VALIDATION_ERROR("Cannot create an empty texture"); } - if (Log2(descriptor->size.width) + 1 < descriptor->mipLevelCount || - Log2(descriptor->size.height) + 1 < descriptor->mipLevelCount) { + if (Log2(std::max(descriptor->size.width, descriptor->size.height)) + 1 < + descriptor->mipLevelCount) { return DAWN_VALIDATION_ERROR("Texture has too many mip levels"); } diff --git a/src/tests/unittests/validation/TextureValidationTests.cpp b/src/tests/unittests/validation/TextureValidationTests.cpp index 4d011829d7..20c2c41bfe 100644 --- a/src/tests/unittests/validation/TextureValidationTests.cpp +++ b/src/tests/unittests/validation/TextureValidationTests.cpp @@ -135,8 +135,8 @@ TEST_F(TextureValidationTest, MipLevelCount) { dawn::TextureDescriptor descriptor = defaultDescriptor; descriptor.size.width = 31; descriptor.size.height = 32; - // Mip level width: 31, 15, 7, 3, 1 - descriptor.mipLevelCount = 6; + // Mip level width: 31, 15, 7, 3, 1, 1 + descriptor.mipLevelCount = 7; ASSERT_DEVICE_ERROR(device.CreateTexture(&descriptor)); } @@ -146,8 +146,8 @@ TEST_F(TextureValidationTest, MipLevelCount) { dawn::TextureDescriptor descriptor = defaultDescriptor; descriptor.size.width = 32; descriptor.size.height = 31; - // Mip level height: 31, 15, 7, 3, 1 - descriptor.mipLevelCount = 6; + // Mip level height: 31, 15, 7, 3, 1, 1 + descriptor.mipLevelCount = 7; ASSERT_DEVICE_ERROR(device.CreateTexture(&descriptor)); } @@ -161,6 +161,17 @@ TEST_F(TextureValidationTest, MipLevelCount) { ASSERT_DEVICE_ERROR(device.CreateTexture(&descriptor)); } + + // Non square mip map halves the resolution until a 1x1 dimension. + { + dawn::TextureDescriptor descriptor = defaultDescriptor; + descriptor.size.width = 32; + descriptor.size.height = 8; + // Mip maps: 32 * 8, 16 * 4, 8 * 2, 4 * 1, 2 * 1, 1 * 1 + descriptor.mipLevelCount = 6; + + device.CreateTexture(&descriptor); + } } // Test that it is valid to destroy a texture