Implement device.CreateErrorShaderModule()

This patch implements device.CreateErrorShaderModule() for Blink to
directly create an error shader module and insert validation and
compilation errors when anything wrong is detected.

Bug: dawn:1345
Test: dawn_unittests
Change-Id: I1415aaf0609febaff49c40ebbfedbbcefa2e5504
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/134104
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Jiawei Shao 2023-05-25 03:32:41 +00:00 committed by Dawn LUCI CQ
parent 0eb4d04d83
commit daffe42b6a
6 changed files with 70 additions and 18 deletions

View File

@ -1096,6 +1096,14 @@
{"name": "descriptor", "type": "shader module descriptor", "annotation": "const*"}
]
},
{
"name": "create error shader module",
"returns": "shader module",
"args": [
{"name": "descriptor", "type": "shader module descriptor", "annotation": "const*"},
{"name": "error message", "type": "char", "annotation": "const*", "length": "strlen"}
]
},
{
"name": "create swap chain",
"returns": "swap chain",

View File

@ -77,7 +77,7 @@ OwnedCompilationMessages::OwnedCompilationMessages() {
OwnedCompilationMessages::~OwnedCompilationMessages() = default;
void OwnedCompilationMessages::AddMessageForTesting(std::string message,
void OwnedCompilationMessages::AddMessage(std::string message,
wgpu::CompilationMessageType type,
uint64_t lineNum,
uint64_t linePos,

View File

@ -37,8 +37,7 @@ class OwnedCompilationMessages : public NonCopyable {
OwnedCompilationMessages();
~OwnedCompilationMessages();
void AddMessageForTesting(
std::string message,
void AddMessage(std::string message,
wgpu::CompilationMessageType type = wgpu::CompilationMessageType::Info,
uint64_t lineNum = 0,
uint64_t linePos = 0,

View File

@ -1248,6 +1248,21 @@ ShaderModuleBase* DeviceBase::APICreateShaderModule(const ShaderModuleDescriptor
return result.Detach();
}
ShaderModuleBase* DeviceBase::APICreateErrorShaderModule(const ShaderModuleDescriptor* descriptor,
const char* errorMessage) {
Ref<ShaderModuleBase> result =
ShaderModuleBase::MakeError(this, descriptor ? descriptor->label : nullptr);
std::unique_ptr<OwnedCompilationMessages> compilationMessages(
std::make_unique<OwnedCompilationMessages>());
compilationMessages->AddMessage(errorMessage, wgpu::CompilationMessageType::Error);
result->InjectCompilationMessages(std::move(compilationMessages));
std::unique_ptr<ErrorData> errorData =
DAWN_VALIDATION_ERROR("Error in calling %s.CreateShaderModule(%s).", this, descriptor);
ConsumeError(std::move(errorData));
return result.Detach();
}
SwapChainBase* DeviceBase::APICreateSwapChain(Surface* surface,
const SwapChainDescriptor* descriptor) {
Ref<SwapChainBase> result;

View File

@ -285,6 +285,8 @@ class DeviceBase : public RefCountedWithExternalCount {
ExternalTextureBase* APICreateExternalTexture(const ExternalTextureDescriptor* descriptor);
SamplerBase* APICreateSampler(const SamplerDescriptor* descriptor);
ShaderModuleBase* APICreateShaderModule(const ShaderModuleDescriptor* descriptor);
ShaderModuleBase* APICreateErrorShaderModule(const ShaderModuleDescriptor* descriptor,
const char* errorMessage);
SwapChainBase* APICreateSwapChain(Surface* surface, const SwapChainDescriptor* descriptor);
TextureBase* APICreateTexture(const TextureDescriptor* descriptor);

View File

@ -254,11 +254,10 @@ TEST_F(ShaderModuleValidationTest, GetCompilationMessages) {
native::ShaderModuleBase* shaderModuleBase = native::FromAPI(shaderModule.Get());
native::OwnedCompilationMessages* messages = shaderModuleBase->GetCompilationMessages();
messages->ClearMessages();
messages->AddMessageForTesting("Info Message");
messages->AddMessageForTesting("Warning Message", wgpu::CompilationMessageType::Warning);
messages->AddMessageForTesting("Error Message", wgpu::CompilationMessageType::Error, 3, 4);
messages->AddMessageForTesting("Complete Message", wgpu::CompilationMessageType::Info, 3, 4, 5,
6);
messages->AddMessage("Info Message");
messages->AddMessage("Warning Message", wgpu::CompilationMessageType::Warning);
messages->AddMessage("Error Message", wgpu::CompilationMessageType::Error, 3, 4);
messages->AddMessage("Complete Message", wgpu::CompilationMessageType::Info, 3, 4, 5, 6);
auto callback = [](WGPUCompilationInfoRequestStatus status, const WGPUCompilationInfo* info,
void* userdata) {
@ -728,5 +727,34 @@ TEST_F(ShaderModuleValidationTest, SourceToCodeMemberDeprecation) {
native::FromAPI(codeShader.Get())));
}
// Test creating an error shader module with device.CreateErrorShaderModule()
TEST_F(ShaderModuleValidationTest, CreateErrorShaderModule) {
wgpu::ShaderModuleWGSLDescriptor wgslDesc = {};
wgpu::ShaderModuleDescriptor descriptor = {};
descriptor.nextInChain = &wgslDesc;
wgslDesc.code = "@compute @workgroup_size(1) fn main() {}";
wgpu::ShaderModule errorShaderModule;
ASSERT_DEVICE_ERROR(errorShaderModule = device.CreateErrorShaderModule(
&descriptor, "Shader compilation error"));
auto callback = [](WGPUCompilationInfoRequestStatus status, const WGPUCompilationInfo* info,
void* userdata) {
ASSERT_EQ(WGPUCompilationInfoRequestStatus_Success, status);
ASSERT_NE(nullptr, info);
ASSERT_EQ(1u, info->messageCount);
const WGPUCompilationMessage* message = &info->messages[0];
ASSERT_STREQ("Shader compilation error", message->message);
ASSERT_EQ(WGPUCompilationMessageType_Error, message->type);
ASSERT_EQ(0u, message->lineNum);
ASSERT_EQ(0u, message->linePos);
};
errorShaderModule.GetCompilationInfo(callback, nullptr);
FlushWire();
}
} // anonymous namespace
} // namespace dawn