diff --git a/src/dawn/native/d3d/ShaderUtils.cpp b/src/dawn/native/d3d/ShaderUtils.cpp index a0e13d47cb..8557878355 100644 --- a/src/dawn/native/d3d/ShaderUtils.cpp +++ b/src/dawn/native/d3d/ShaderUtils.cpp @@ -347,41 +347,52 @@ void DumpCompiledShader(Device* device, const CompiledShader& compiledShader, uint32_t compileFlags) { std::ostringstream dumpedMsg; - dumpedMsg << "/* Dumped generated HLSL */" << std::endl - << compiledShader.hlslSource << std::endl; + // The HLSL may be empty if compilation failed. + if (!compiledShader.hlslSource.empty()) { + dumpedMsg << "/* Dumped generated HLSL */" << std::endl + << compiledShader.hlslSource << std::endl; + } - if (device->IsToggleEnabled(Toggle::UseDXC)) { - dumpedMsg << "/* Dumped disassembled DXIL */" << std::endl; - const Blob& shaderBlob = compiledShader.shaderBlob; - ComPtr dxcBlob; - ComPtr disassembly; - if (FAILED(device->GetDxcLibrary()->CreateBlobWithEncodingFromPinned( - shaderBlob.Data(), shaderBlob.Size(), 0, &dxcBlob)) || - FAILED(device->GetDxcCompiler()->Disassemble(dxcBlob.Get(), &disassembly))) { - dumpedMsg << "DXC disassemble failed" << std::endl; + // The blob may be empty if FXC/DXC compilation failed. + const Blob& shaderBlob = compiledShader.shaderBlob; + if (!shaderBlob.Empty()) { + if (device->IsToggleEnabled(Toggle::UseDXC)) { + dumpedMsg << "/* Dumped disassembled DXIL */" << std::endl; + ComPtr dxcBlob; + ComPtr disassembly; + if (FAILED(device->GetDxcLibrary()->CreateBlobWithEncodingFromPinned( + shaderBlob.Data(), shaderBlob.Size(), 0, &dxcBlob)) || + FAILED(device->GetDxcCompiler()->Disassemble(dxcBlob.Get(), &disassembly))) { + dumpedMsg << "DXC disassemble failed" << std::endl; + } else { + dumpedMsg << std::string_view( + static_cast(disassembly->GetBufferPointer()), + disassembly->GetBufferSize()); + } } else { - dumpedMsg << std::string_view(static_cast(disassembly->GetBufferPointer()), - disassembly->GetBufferSize()); - } - } else { - dumpedMsg << "/* FXC compile flags */ " << std::endl - << CompileFlagsToStringFXC(compileFlags) << std::endl; - dumpedMsg << "/* Dumped disassembled DXBC */" << std::endl; - ComPtr disassembly; - const Blob& shaderBlob = compiledShader.shaderBlob; - UINT flags = - // Some literals are printed as floats with precision(6) which is not enough - // precision for values very close to 0, so always print literals as hex values. - D3D_DISASM_PRINT_HEX_LITERALS; - if (FAILED(device->GetFunctions()->d3dDisassemble(shaderBlob.Data(), shaderBlob.Size(), - flags, nullptr, &disassembly))) { - dumpedMsg << "D3D disassemble failed" << std::endl; - } else { - dumpedMsg << std::string_view(static_cast(disassembly->GetBufferPointer()), - disassembly->GetBufferSize()); + dumpedMsg << "/* FXC compile flags */ " << std::endl + << CompileFlagsToStringFXC(compileFlags) << std::endl; + dumpedMsg << "/* Dumped disassembled DXBC */" << std::endl; + ComPtr disassembly; + UINT flags = + // Some literals are printed as floats with precision(6) which is not enough + // precision for values very close to 0, so always print literals as hex values. + D3D_DISASM_PRINT_HEX_LITERALS; + if (FAILED(device->GetFunctions()->d3dDisassemble(shaderBlob.Data(), shaderBlob.Size(), + flags, nullptr, &disassembly))) { + dumpedMsg << "D3D disassemble failed" << std::endl; + } else { + dumpedMsg << std::string_view( + static_cast(disassembly->GetBufferPointer()), + disassembly->GetBufferSize()); + } } } - device->EmitLog(WGPULoggingType_Info, dumpedMsg.str().c_str()); + + std::string logMessage = dumpedMsg.str(); + if (!logMessage.empty()) { + device->EmitLog(WGPULoggingType_Info, logMessage.c_str()); + } } } // namespace dawn::native::d3d diff --git a/src/dawn/native/d3d11/ShaderModuleD3D11.cpp b/src/dawn/native/d3d11/ShaderModuleD3D11.cpp index 23fd202645..f0092ffbdb 100644 --- a/src/dawn/native/d3d11/ShaderModuleD3D11.cpp +++ b/src/dawn/native/d3d11/ShaderModuleD3D11.cpp @@ -158,13 +158,20 @@ ResultOrError ShaderModule::Compile( req.hlsl.limits = LimitsForCompilationRequest::Create(limits.v1); CacheResult compiledShader; - DAWN_TRY_LOAD_OR_RUN(compiledShader, device, std::move(req), d3d::CompiledShader::FromBlob, - d3d::CompileShader); + MaybeError compileError = [&]() -> MaybeError { + DAWN_TRY_LOAD_OR_RUN(compiledShader, device, std::move(req), d3d::CompiledShader::FromBlob, + d3d::CompileShader); + return {}; + }(); - if (req.hlsl.dumpShaders) { + if (device->IsToggleEnabled(Toggle::DumpShaders)) { d3d::DumpCompiledShader(device, *compiledShader, compileFlags); } + if (compileError.IsError()) { + return {compileError.AcquireError()}; + } + device->GetBlobCache()->EnsureStored(compiledShader); // Clear the hlslSource. It is only used for logging and should not be used diff --git a/src/dawn/native/d3d12/ShaderModuleD3D12.cpp b/src/dawn/native/d3d12/ShaderModuleD3D12.cpp index beeddb852a..b284bb0365 100644 --- a/src/dawn/native/d3d12/ShaderModuleD3D12.cpp +++ b/src/dawn/native/d3d12/ShaderModuleD3D12.cpp @@ -207,13 +207,20 @@ ResultOrError ShaderModule::Compile( req.hlsl.limits = LimitsForCompilationRequest::Create(limits.v1); CacheResult compiledShader; - DAWN_TRY_LOAD_OR_RUN(compiledShader, device, std::move(req), d3d::CompiledShader::FromBlob, - d3d::CompileShader); + MaybeError compileError = [&]() -> MaybeError { + DAWN_TRY_LOAD_OR_RUN(compiledShader, device, std::move(req), d3d::CompiledShader::FromBlob, + d3d::CompileShader); + return {}; + }(); if (device->IsToggleEnabled(Toggle::DumpShaders)) { d3d::DumpCompiledShader(device, *compiledShader, compileFlags); } + if (compileError.IsError()) { + return {compileError.AcquireError()}; + } + device->GetBlobCache()->EnsureStored(compiledShader); // Clear the hlslSource. It is only used for logging and should not be used