mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-16 08:27:05 +00:00
Validate SPIR-V code when creating ShaderModules
This integrates spirv-val in dawn_native so that regular and WebGPU-specific validation of shaders is done. Also adds tests to check OpUndef is correctly rejected so we know WebGPU-specific validation is working. Change-Id: If49d276c98bca8cd3c6c1a420903fe34923a2942
This commit is contained in:
committed by
Corentin Wallez
parent
ae62847f1c
commit
21d8438ad6
@@ -53,6 +53,7 @@ list(APPEND UNITTEST_SOURCES
|
||||
${VALIDATION_TESTS_DIR}/PushConstantsValidationTests.cpp
|
||||
${VALIDATION_TESTS_DIR}/RenderPassDescriptorValidationTests.cpp
|
||||
${VALIDATION_TESTS_DIR}/RenderPipelineValidationTests.cpp
|
||||
${VALIDATION_TESTS_DIR}/ShaderModuleValidationTests.cpp
|
||||
${VALIDATION_TESTS_DIR}/VertexBufferValidationTests.cpp
|
||||
${VALIDATION_TESTS_DIR}/ValidationTest.cpp
|
||||
${VALIDATION_TESTS_DIR}/ValidationTest.h
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// Copyright 2018 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/unittests/validation/ValidationTest.h"
|
||||
|
||||
#include "utils/DawnHelpers.h"
|
||||
|
||||
class ShaderModuleValidationTest : public ValidationTest {
|
||||
};
|
||||
|
||||
// Test case with a simpler shader that should successfully be created
|
||||
TEST_F(ShaderModuleValidationTest, CreationSuccess) {
|
||||
const char* shader = R"(
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint Fragment %main "main" %fragColor
|
||||
OpExecutionMode %main OriginUpperLeft
|
||||
OpSource GLSL 450
|
||||
OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
|
||||
OpSourceExtension "GL_GOOGLE_include_directive"
|
||||
OpName %main "main"
|
||||
OpName %fragColor "fragColor"
|
||||
OpDecorate %fragColor Location 0
|
||||
%void = OpTypeVoid
|
||||
%3 = OpTypeFunction %void
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_ptr_Output_v4float = OpTypePointer Output %v4float
|
||||
%fragColor = OpVariable %_ptr_Output_v4float Output
|
||||
%float_1 = OpConstant %float 1
|
||||
%float_0 = OpConstant %float 0
|
||||
%12 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_1
|
||||
%main = OpFunction %void None %3
|
||||
%5 = OpLabel
|
||||
OpStore %fragColor %12
|
||||
OpReturn
|
||||
OpFunctionEnd)";
|
||||
|
||||
utils::CreateShaderModuleFromASM(device, shader);
|
||||
}
|
||||
|
||||
// Test case with a shader with OpUndef to test WebGPU-specific validation
|
||||
TEST_F(ShaderModuleValidationTest, OpUndef) {
|
||||
const char* shader = R"(
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint Fragment %main "main" %fragColor
|
||||
OpExecutionMode %main OriginUpperLeft
|
||||
OpSource GLSL 450
|
||||
OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
|
||||
OpSourceExtension "GL_GOOGLE_include_directive"
|
||||
OpName %main "main"
|
||||
OpName %fragColor "fragColor"
|
||||
OpDecorate %fragColor Location 0
|
||||
%void = OpTypeVoid
|
||||
%3 = OpTypeFunction %void
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_ptr_Output_v4float = OpTypePointer Output %v4float
|
||||
%fragColor = OpVariable %_ptr_Output_v4float Output
|
||||
%float_1 = OpConstant %float 1
|
||||
%float_0 = OpConstant %float 0
|
||||
%12 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_1
|
||||
%main = OpFunction %void None %3
|
||||
%5 = OpLabel
|
||||
%6 = OpUndef %v4float
|
||||
OpStore %fragColor %12
|
||||
OpReturn
|
||||
OpFunctionEnd)";
|
||||
|
||||
// Notice "%6 = OpUndef %v4float" above
|
||||
ASSERT_DEVICE_ERROR(utils::CreateShaderModuleFromASM(device, shader));
|
||||
|
||||
std::string error = GetLastDeviceErrorMessage();
|
||||
ASSERT_NE(error.find("OpUndef"), std::string::npos);
|
||||
}
|
||||
@@ -69,6 +69,9 @@ bool ValidationTest::EndExpectDeviceError() {
|
||||
mExpectError = false;
|
||||
return mError;
|
||||
}
|
||||
std::string ValidationTest::GetLastDeviceErrorMessage() const {
|
||||
return mDeviceErrorMessage;
|
||||
}
|
||||
|
||||
dawn::RenderPassDescriptor ValidationTest::CreateSimpleRenderPass() {
|
||||
dawn::TextureDescriptor descriptor;
|
||||
@@ -91,14 +94,16 @@ dawn::RenderPassDescriptor ValidationTest::CreateSimpleRenderPass() {
|
||||
}
|
||||
|
||||
void ValidationTest::OnDeviceError(const char* message, dawnCallbackUserdata userdata) {
|
||||
auto self = reinterpret_cast<ValidationTest*>(static_cast<uintptr_t>(userdata));
|
||||
self->mDeviceErrorMessage = message;
|
||||
|
||||
// Skip this one specific error that is raised when a builder is used after it got an error
|
||||
// this is important because we don't want to wrap all creation tests in ASSERT_DEVICE_ERROR.
|
||||
// Yes the error message is misleading.
|
||||
if (std::string(message) == "Builder cannot be used after GetResult") {
|
||||
if (self->mDeviceErrorMessage == "Builder cannot be used after GetResult") {
|
||||
return;
|
||||
}
|
||||
|
||||
auto self = reinterpret_cast<ValidationTest*>(static_cast<uintptr_t>(userdata));
|
||||
ASSERT_TRUE(self->mExpectError) << "Got unexpected device error: " << message;
|
||||
ASSERT_FALSE(self->mError) << "Got two errors in expect block";
|
||||
self->mError = true;
|
||||
|
||||
@@ -47,6 +47,7 @@ class ValidationTest : public testing::Test {
|
||||
|
||||
void StartExpectDeviceError();
|
||||
bool EndExpectDeviceError();
|
||||
std::string GetLastDeviceErrorMessage() const;
|
||||
|
||||
dawn::RenderPassDescriptor CreateSimpleRenderPass();
|
||||
|
||||
@@ -66,6 +67,7 @@ class ValidationTest : public testing::Test {
|
||||
|
||||
private:
|
||||
static void OnDeviceError(const char* message, dawnCallbackUserdata userdata);
|
||||
std::string mDeviceErrorMessage;
|
||||
bool mExpectError = false;
|
||||
bool mError = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user