Port VertexFormatTests to WGSL

Bug: dawn:572
Change-Id: I68484defb240528b0a7dd0eced6012d9866f30fc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/44765
Auto-Submit: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2021-03-23 13:25:29 +00:00 committed by Commit Bot service account
parent 2be3db9624
commit 94b8b7408e
1 changed files with 85 additions and 83 deletions

View File

@ -24,7 +24,7 @@
// the vertex content is the same as what we expected. On success it outputs green, // the vertex content is the same as what we expected. On success it outputs green,
// otherwise red. // otherwise red.
constexpr uint32_t kRTSize = 400; constexpr uint32_t kRTSize = 1;
constexpr uint32_t kVertexNum = 3; constexpr uint32_t kVertexNum = 3;
std::vector<uint16_t> Float32ToFloat16(std::vector<float> data) { std::vector<uint16_t> Float32ToFloat16(std::vector<float> data) {
@ -199,23 +199,20 @@ class VertexFormatTest : public DawnTest {
bool isNormalized, bool isNormalized,
bool isUnsigned, bool isUnsigned,
uint32_t componentCount) { uint32_t componentCount) {
if (componentCount == 1) { std::string base;
if (isFloat || isNormalized) { if (isFloat || isNormalized) {
return "float"; base = "f32";
} else if (isUnsigned) { } else if (isUnsigned) {
return "uint"; base = "u32";
} else { } else {
return "int"; base = "i32";
}
} else {
if (isNormalized || isFloat) {
return "vec" + std::to_string(componentCount);
} else if (isUnsigned) {
return "uvec" + std::to_string(componentCount);
} else {
return "ivec" + std::to_string(componentCount);
} }
if (componentCount == 1) {
return base;
} }
return "vec" + std::to_string(componentCount) + "<" + base + ">";
} }
// The length of vertexData is fixed to 3, it aligns to triangle vertex number // The length of vertexData is fixed to 3, it aligns to triangle vertex number
@ -233,46 +230,45 @@ class VertexFormatTest : public DawnTest {
std::string variableType = std::string variableType =
ShaderTypeGenerator(isFloat, isNormalized, isUnsigned, componentCount); ShaderTypeGenerator(isFloat, isNormalized, isUnsigned, componentCount);
std::string expectedDataType = ShaderTypeGenerator(isFloat, isNormalized, isUnsigned, 1); std::string expectedDataType = ShaderTypeGenerator(isFloat, isNormalized, isUnsigned, 1);
std::ostringstream vs;
vs << "#version 450\n";
// layout(location = 0) in float/uint/int/ivecn/vecn/uvecn test; std::ostringstream vs;
vs << "layout(location = 0) in " << variableType << " test;\n"; vs << "[[location(0)]] var<in> test : " << variableType << ";\n";
vs << "layout(location = 0) out vec4 color;\n";
// Because x86 CPU using "extended // Because x86 CPU using "extended
// precision"(https://en.wikipedia.org/wiki/Extended_precision) during float // precision"(https://en.wikipedia.org/wiki/Extended_precision) during float
// math(https://developer.nvidia.com/sites/default/files/akamai/cuda/files/NVIDIA-CUDA-Floating-Point.pdf), // math(https://developer.nvidia.com/sites/default/files/akamai/cuda/files/NVIDIA-CUDA-Floating-Point.pdf),
// move normalization and Float16ToFloat32 into shader to generate // move normalization and Float16ToFloat32 into shader to generate
// expected value. // expected value.
vs << "float Float16ToFloat32(uint fp16) {\n"; vs << R"(
vs << " uint magic = (uint(254) - uint(15)) << 23;\n"; [[location(0)]] var<out> color : vec4<f32>;
vs << " uint was_inf_nan = (uint(127) + uint(16)) << 23;\n"; fn Float16ToFloat32(fp16 : u32) -> f32 {
vs << " uint fp32u;\n"; const magic : u32 = (254u - 15u) << 23u;
vs << " float fp32;\n"; const was_inf_nan : u32 = (127u + 16u) << 23u;
vs << " fp32u = (fp16 & 0x7FFF) << 13;\n"; var fp32u : u32 = (fp16 & 0x7FFFu) << 13u;
vs << " fp32 = uintBitsToFloat(fp32u) * uintBitsToFloat(magic);\n"; const fp32 : f32 = bitcast<f32>(fp32u) * bitcast<f32>(magic);
vs << " fp32u = floatBitsToUint(fp32);\n"; fp32u = bitcast<u32>(fp32);
vs << " if (fp32 >= uintBitsToFloat(was_inf_nan)) {\n"; if (fp32 >= bitcast<f32>(was_inf_nan)) {
vs << " fp32u |= uint(255) << 23;\n"; fp32u = fp32u | (255u << 23u);
vs << " }\n"; }
vs << " fp32u |= (fp16 & 0x8000) << 16;\n"; fp32u = fp32u | ((fp16 & 0x8000u) << 16u);
vs << " fp32 = uintBitsToFloat(fp32u);\n"; return bitcast<f32>(fp32u);
vs << " return fp32;\n"; }
vs << "}\n";
vs << "void main() {\n"; [[builtin(vertex_index)]] var<in> VertexIndex : u32;
[[builtin(position)]] var<out> Position : vec4<f32>;
// Hard code the triangle in the shader so that we don't have to add a vertex input for it. [[stage(vertex)]] fn main() -> void {
vs << " const vec2 pos[3] = vec2[3](vec2(-1.0f, 0.0f), vec2(-1.0f, 1.0f), vec2(0.0f, " const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
"1.0f));\n"; vec2<f32>(-1.0, -1.0),
vs << " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"; vec2<f32>( 2.0, 0.0),
vec2<f32>( 0.0, 2.0));
Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
)";
// Declare expected values. // Declare expected values.
vs << " " << expectedDataType << " expected[" + std::to_string(kVertexNum) + "]"; vs << "var expected : array<array<" << expectedDataType << ", "
vs << "[" + std::to_string(componentCount) + "];\n"; << std::to_string(componentCount) << ">, " << std::to_string(kVertexNum) << ">;";
// Assign each elements in expected values // Assign each elements in expected values
// e.g. expected[0][0] = uint(1); // e.g. expected[0][0] = u32(1u);
// expected[0][1] = uint(2); // expected[0][1] = u32(2u);
for (uint32_t i = 0; i < kVertexNum; ++i) { for (uint32_t i = 0; i < kVertexNum; ++i) {
for (uint32_t j = 0; j < componentCount; ++j) { for (uint32_t j = 0; j < componentCount; ++j) {
vs << " expected[" + std::to_string(i) + "][" + std::to_string(j) + "] = " vs << " expected[" + std::to_string(i) + "][" + std::to_string(j) + "] = "
@ -284,36 +280,38 @@ class VertexFormatTest : public DawnTest {
} else if (isNormalized) { } else if (isNormalized) {
// Move normalize operation into shader because of CPU and GPU precision // Move normalize operation into shader because of CPU and GPU precision
// different on float math. // different on float math.
vs << "max(float(" << std::to_string(expectedData[i * componentCount + j]) vs << "max(f32(" << std::to_string(expectedData[i * componentCount + j])
<< ") / " << std::to_string(std::numeric_limits<T>::max()) << ", -1.0));\n"; << ") / " << std::to_string(std::numeric_limits<T>::max())
<< ".0 , -1.0));\n";
} else if (isHalf) { } else if (isHalf) {
// Becasue Vulkan and D3D12 handle -0.0f through uintBitsToFloat have different // Becasue Vulkan and D3D12 handle -0.0f through bitcast have different
// result (Vulkan take -0.0f as -0.0 but D3D12 take -0.0f as 0), add workaround // result (Vulkan take -0.0f as -0.0 but D3D12 take -0.0f as 0), add workaround
// for -0.0f. // for -0.0f.
if (static_cast<uint16_t>(expectedData[i * componentCount + j]) == if (static_cast<uint16_t>(expectedData[i * componentCount + j]) ==
kNegativeZeroInHalf) { kNegativeZeroInHalf) {
vs << "-0.0f);\n"; vs << "-0.0);\n";
} else { } else {
vs << "Float16ToFloat32(" vs << "Float16ToFloat32(u32("
<< std::to_string(expectedData[i * componentCount + j]); << std::to_string(expectedData[i * componentCount + j]) << ")));\n";
vs << "));\n";
} }
} else if (isUnsigned) {
vs << std::to_string(expectedData[i * componentCount + j]) << "u);\n";
} else { } else {
vs << std::to_string(expectedData[i * componentCount + j]) << ");\n"; vs << std::to_string(expectedData[i * componentCount + j]) << ");\n";
} }
} }
} }
vs << " bool success = true;\n"; vs << " var success : bool = true;\n";
// Perform the checks by successively ANDing a boolean // Perform the checks by successively ANDing a boolean
for (uint32_t component = 0; component < componentCount; ++component) { for (uint32_t component = 0; component < componentCount; ++component) {
std::string suffix = componentCount == 1 ? "" : "[" + std::to_string(component) + "]"; std::string suffix = componentCount == 1 ? "" : "[" + std::to_string(component) + "]";
std::string testVal = "testVal" + std::to_string(component); std::string testVal = "testVal" + std::to_string(component);
std::string expectedVal = "expectedVal" + std::to_string(component); std::string expectedVal = "expectedVal" + std::to_string(component);
vs << " " << expectedDataType << " " << testVal << ";\n"; vs << " var " << testVal << " : " << expectedDataType << ";\n";
vs << " " << expectedDataType << " " << expectedVal << ";\n"; vs << " var " << expectedVal << " : " << expectedDataType << ";\n";
vs << " " << testVal << " = test" << suffix << ";\n"; vs << " " << testVal << " = test" << suffix << ";\n";
vs << " " << expectedVal << " = expected[gl_VertexIndex]" vs << " " << expectedVal << " = expected[VertexIndex]"
<< "[" << component << "];\n"; << "[" << component << "];\n";
if (!isInputTypeFloat) { // Integer / unsigned integer need to match exactly. if (!isInputTypeFloat) { // Integer / unsigned integer need to match exactly.
vs << " success = success && (" << testVal << " == " << expectedVal << ");\n"; vs << " success = success && (" << testVal << " == " << expectedVal << ");\n";
@ -321,35 +319,32 @@ class VertexFormatTest : public DawnTest {
// TODO(shaobo.yan@intel.com) : a difference of 8 ULPs is allowed in this test // TODO(shaobo.yan@intel.com) : a difference of 8 ULPs is allowed in this test
// because it is required on MacbookPro 11.5,AMD Radeon HD 8870M(on macOS 10.13.6), // because it is required on MacbookPro 11.5,AMD Radeon HD 8870M(on macOS 10.13.6),
// but that it might be possible to tighten. // but that it might be possible to tighten.
vs << " if (isnan(" << expectedVal << ")) {\n"; vs << " if (isNan(" << expectedVal << ")) {\n";
vs << " success = success && isnan(" << testVal << ");\n"; vs << " success = success && isNan(" << testVal << ");\n";
vs << " } else {\n"; vs << " } else {\n";
vs << " uint testValFloatToUint = floatBitsToUint(" << testVal << ");\n"; vs << " const testValFloatToUint : u32 = bitcast<u32>(" << testVal << ");\n";
vs << " uint expectedValFloatToUint = floatBitsToUint(" << expectedVal vs << " const expectedValFloatToUint : u32 = bitcast<u32>(" << expectedVal
<< ");\n"; << ");\n";
vs << " success = success && max(testValFloatToUint, " vs << " success = success && max(testValFloatToUint, "
"expectedValFloatToUint)"; "expectedValFloatToUint)";
vs << " - min(testValFloatToUint, expectedValFloatToUint) < uint(8);\n"; vs << " - min(testValFloatToUint, expectedValFloatToUint) < 8u;\n";
vs << " }\n"; vs << " }\n";
} }
} }
vs << " if (success) {\n"; vs << R"(
vs << " color = vec4(0.0f, 1.0f, 0.0f, 1.0f);\n"; if (success) {
vs << " } else {\n"; color = vec4<f32>(0.0, 1.0, 0.0, 1.0);
vs << " color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"; } else {
vs << " }\n"; color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
vs << "}\n"; }
})";
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, vs.str().c_str());
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, vs.str().c_str()); wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
[[location(0)]] var<in> color : vec4<f32>;
wgpu::ShaderModule fsModule = [[location(0)]] var<out> FragColor : vec4<f32>;
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[stage(fragment)]] fn main() -> void {
#version 450 FragColor = color;
layout(location = 0) in vec4 color;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = color;
})"); })");
uint32_t bytesPerComponents = BytesPerComponents(format); uint32_t bytesPerComponents = BytesPerComponents(format);
@ -375,11 +370,6 @@ class VertexFormatTest : public DawnTest {
void DoVertexFormatTest(wgpu::VertexFormat format, void DoVertexFormatTest(wgpu::VertexFormat format,
std::vector<VertexType> vertex, std::vector<VertexType> vertex,
std::vector<ExpectedType> expectedData) { std::vector<ExpectedType> expectedData) {
// TODO(crbug.com/tint/402): Unimplemented min / max
DAWN_SKIP_TEST_IF(
(IsFloatFormat(format) || IsHalfFormat(format) || IsNormalizedFormat(format)) &&
HasToggleEnabled("use_tint_generator"));
wgpu::RenderPipeline pipeline = MakeTestPipeline(format, expectedData); wgpu::RenderPipeline pipeline = MakeTestPipeline(format, expectedData);
wgpu::Buffer vertexBuffer = utils::CreateBufferFromData( wgpu::Buffer vertexBuffer = utils::CreateBufferFromData(
device, vertex.data(), vertex.size() * sizeof(VertexType), wgpu::BufferUsage::Vertex); device, vertex.data(), vertex.size() * sizeof(VertexType), wgpu::BufferUsage::Vertex);
@ -756,6 +746,9 @@ TEST_P(VertexFormatTest, Float16x2) {
// See http://crbug.com/dawn/259 // See http://crbug.com/dawn/259
DAWN_SKIP_TEST_IF(IsMetal() && IsIntel()); DAWN_SKIP_TEST_IF(IsMetal() && IsIntel());
// Fails on NVIDIA's Vulkan drivers on CQ but passes locally.
DAWN_SKIP_TEST_IF(IsVulkan() && IsNvidia());
std::vector<uint16_t> vertexData = std::vector<uint16_t> vertexData =
Float32ToFloat16(std::vector<float>({14.8f, -0.0f, 22.5f, 1.3f, +0.0f, -24.8f})); Float32ToFloat16(std::vector<float>({14.8f, -0.0f, 22.5f, 1.3f, +0.0f, -24.8f}));
@ -767,6 +760,9 @@ TEST_P(VertexFormatTest, Float16x4) {
// See http://crbug.com/dawn/259 // See http://crbug.com/dawn/259
DAWN_SKIP_TEST_IF(IsMetal() && IsIntel()); DAWN_SKIP_TEST_IF(IsMetal() && IsIntel());
// Fails on NVIDIA's Vulkan drivers on CQ but passes locally.
DAWN_SKIP_TEST_IF(IsVulkan() && IsNvidia());
std::vector<uint16_t> vertexData = Float32ToFloat16(std::vector<float>( std::vector<uint16_t> vertexData = Float32ToFloat16(std::vector<float>(
{+0.0f, -16.8f, 18.2f, -0.0f, 12.5f, 1.3f, 14.8f, -12.4f, 22.5f, -48.8f, 47.4f, -24.8f})); {+0.0f, -16.8f, 18.2f, -0.0f, 12.5f, 1.3f, 14.8f, -12.4f, 22.5f, -48.8f, 47.4f, -24.8f}));
@ -792,6 +788,9 @@ TEST_P(VertexFormatTest, Float32x2) {
// See http://crbug.com/dawn/259 // See http://crbug.com/dawn/259
DAWN_SKIP_TEST_IF(IsMetal() && IsIntel()); DAWN_SKIP_TEST_IF(IsMetal() && IsIntel());
// Fails on NVIDIA's Vulkan drivers on CQ but passes locally.
DAWN_SKIP_TEST_IF(IsVulkan() && IsNvidia());
std::vector<float> vertexData = {18.23f, -0.0f, +0.0f, +1.0f, 1.3f, -1.0f}; std::vector<float> vertexData = {18.23f, -0.0f, +0.0f, +1.0f, 1.3f, -1.0f};
DoVertexFormatTest(wgpu::VertexFormat::Float32x2, vertexData, vertexData); DoVertexFormatTest(wgpu::VertexFormat::Float32x2, vertexData, vertexData);
@ -802,6 +801,9 @@ TEST_P(VertexFormatTest, Float32x3) {
// See http://crbug.com/dawn/259 // See http://crbug.com/dawn/259
DAWN_SKIP_TEST_IF(IsMetal() && IsIntel()); DAWN_SKIP_TEST_IF(IsMetal() && IsIntel());
// Fails on NVIDIA's Vulkan drivers on CQ but passes locally.
DAWN_SKIP_TEST_IF(IsVulkan() && IsNvidia());
std::vector<float> vertexData = { std::vector<float> vertexData = {
+0.0f, -1.0f, -0.0f, 1.0f, 1.3f, 99.45f, 23.6f, -81.2f, 55.0f, +0.0f, -1.0f, -0.0f, 1.0f, 1.3f, 99.45f, 23.6f, -81.2f, 55.0f,
}; };