dawn-cmake/src/tests/unittests/MathTests.cpp
Corentin Wallez 431d618961 Add most WebGPU texture formats on Vulkan
This adds the formats to dawn.json, implements support in the Vulkan
backend and adds tests performing basic sampling checks for all formats.

The R8UnormSrgb and RG8UnormSrgb formats skipped because they are not
required in Vulkan (and RG8UnormSrgb is in fact not supported on the
machine used for developing this CL). A PR will be sent to the WebGPU
repo to remove the from the initial list of formats.

The RG11B10Float and RGB10A2Unorm formats of WebGPU are replaced with
B10GR11Float and A2RGB10Unorm that are the formats exposed by Vulkan. It
is likely that all APIs implement them with components stored in that
order.

Each format except depth-stencil ones is tested by uploading some
interesting texel data and checking that sampling from the texture
produces correct results. The goal is to make sure that backends don't
make a mistake in the giant switch statements. There was no effort made
to check the hardware implementation of the formats.

Tests will later be extended to cover rendering and clearing operations
as well as multisample resolve.

It isn't clear if depth-stencil format will support TRANSFER operations
in WebGPU so these are left untested for now.

BUG=dawn:128

Change-Id: I78ac5bf77b57398155551e6db3de50b478d69452
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8363
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2019-07-01 09:58:07 +00:00

164 lines
4.5 KiB
C++

// Copyright 2017 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 <gtest/gtest.h>
#include "common/Math.h"
#include <cmath>
// Tests for ScanForward
TEST(Math, ScanForward) {
// Test extrema
ASSERT_EQ(ScanForward(1), 0u);
ASSERT_EQ(ScanForward(0x80000000), 31u);
// Test with more than one bit set.
ASSERT_EQ(ScanForward(256), 8u);
ASSERT_EQ(ScanForward(256 + 32), 5u);
ASSERT_EQ(ScanForward(1024 + 256 + 32), 5u);
}
// Tests for Log2
TEST(Math, Log2) {
// Test extrema
ASSERT_EQ(Log2(1), 0u);
ASSERT_EQ(Log2(0xFFFFFFFF), 31u);
// Test boundary between two logs
ASSERT_EQ(Log2(0x80000000), 31u);
ASSERT_EQ(Log2(0x7FFFFFFF), 30u);
ASSERT_EQ(Log2(16), 4u);
ASSERT_EQ(Log2(15), 3u);
}
// Tests for IsPowerOfTwo
TEST(Math, IsPowerOfTwo) {
ASSERT_TRUE(IsPowerOfTwo(1));
ASSERT_TRUE(IsPowerOfTwo(2));
ASSERT_FALSE(IsPowerOfTwo(3));
ASSERT_TRUE(IsPowerOfTwo(0x8000000));
ASSERT_FALSE(IsPowerOfTwo(0x8000400));
}
// Tests for AlignPtr
TEST(Math, AlignPtr) {
constexpr size_t kTestAlignment = 8;
char buffer[kTestAlignment * 4];
for (size_t i = 0; i < 2 * kTestAlignment; ++i) {
char* unaligned = &buffer[i];
char* aligned = AlignPtr(unaligned, kTestAlignment);
ASSERT_GE(aligned - unaligned, 0);
ASSERT_LT(static_cast<size_t>(aligned - unaligned), kTestAlignment);
ASSERT_EQ(reinterpret_cast<uintptr_t>(aligned) & (kTestAlignment -1), 0u);
}
}
// Tests for Align
TEST(Math, Align) {
// 0 aligns to 0
ASSERT_EQ(Align(0, 4), 0u);
ASSERT_EQ(Align(0, 256), 0u);
ASSERT_EQ(Align(0, 512), 0u);
// Multiples align to self
ASSERT_EQ(Align(8, 8), 8u);
ASSERT_EQ(Align(16, 8), 16u);
ASSERT_EQ(Align(24, 8), 24u);
ASSERT_EQ(Align(256, 256), 256u);
ASSERT_EQ(Align(512, 256), 512u);
ASSERT_EQ(Align(768, 256), 768u);
// Alignment with 1 is self
for (uint32_t i = 0; i < 128; ++i) {
ASSERT_EQ(Align(i, 1), i);
}
// Everything in the range (align, 2*align] aligns to 2*align
for (uint32_t i = 1; i <= 64; ++i) {
ASSERT_EQ(Align(64 + i, 64), 128u);
}
}
// Tests for IsPtrAligned
TEST(Math, IsPtrAligned) {
constexpr size_t kTestAlignment = 8;
char buffer[kTestAlignment * 4];
for (size_t i = 0; i < 2 * kTestAlignment; ++i) {
char* unaligned = &buffer[i];
char* aligned = AlignPtr(unaligned, kTestAlignment);
ASSERT_EQ(IsPtrAligned(unaligned, kTestAlignment), unaligned == aligned);
}
}
// Tests for IsAligned
TEST(Math, IsAligned) {
// 0 is aligned
ASSERT_TRUE(IsAligned(0, 4));
ASSERT_TRUE(IsAligned(0, 256));
ASSERT_TRUE(IsAligned(0, 512));
// Multiples are aligned
ASSERT_TRUE(IsAligned(8, 8));
ASSERT_TRUE(IsAligned(16, 8));
ASSERT_TRUE(IsAligned(24, 8));
ASSERT_TRUE(IsAligned(256, 256));
ASSERT_TRUE(IsAligned(512, 256));
ASSERT_TRUE(IsAligned(768, 256));
// Alignment with 1 is always aligned
for (uint32_t i = 0; i < 128; ++i) {
ASSERT_TRUE(IsAligned(i, 1));
}
// Everything in the range (align, 2*align) is not aligned
for (uint32_t i = 1; i < 64; ++i) {
ASSERT_FALSE(IsAligned(64 + i, 64));
}
}
// Tests for float32 to float16 conversion
TEST(Math, Float32ToFloat16) {
ASSERT_EQ(Float32ToFloat16(0.0f), 0x0000);
ASSERT_EQ(Float32ToFloat16(-0.0f), 0x8000);
ASSERT_EQ(Float32ToFloat16(INFINITY), 0x7C00);
ASSERT_EQ(Float32ToFloat16(-INFINITY), 0xFC00);
// Check that NaN is converted to a value in one of the float16 NaN ranges
uint16_t nan16 = Float32ToFloat16(NAN);
ASSERT_TRUE(nan16 > 0xFC00 || (nan16 < 0x8000 && nan16 > 0x7C00));
ASSERT_EQ(Float32ToFloat16(1.0f), 0x3C00);
}
// Tests for SRGBToLinear
TEST(Math, SRGBToLinear) {
ASSERT_EQ(SRGBToLinear(0.0f), 0.0f);
ASSERT_EQ(SRGBToLinear(1.0f), 1.0f);
ASSERT_EQ(SRGBToLinear(-1.0f), 0.0f);
ASSERT_EQ(SRGBToLinear(2.0f), 1.0f);
ASSERT_FLOAT_EQ(SRGBToLinear(0.5f), 0.21404114f);
}