From a88ff24e94bba0efaa44efe6263bd1b698e58849 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 14 Sep 2020 17:45:59 -0700 Subject: Completed shader compilation refactoring (fixed https://github.com/DiligentGraphics/DiligentCore/issues/160) --- Graphics/ShaderTools/include/HLSLUtils.hpp | 53 ++++++++++++++++++++++++++++++ Graphics/ShaderTools/src/DXCompiler.cpp | 42 +++++------------------ 2 files changed, 62 insertions(+), 33 deletions(-) (limited to 'Graphics/ShaderTools') diff --git a/Graphics/ShaderTools/include/HLSLUtils.hpp b/Graphics/ShaderTools/include/HLSLUtils.hpp index 9abd1e7d..0e36c7b4 100644 --- a/Graphics/ShaderTools/include/HLSLUtils.hpp +++ b/Graphics/ShaderTools/include/HLSLUtils.hpp @@ -27,6 +27,8 @@ #pragma once +#include + #include "BasicTypes.h" #include "GraphicsTypes.h" #include "Shader.h" @@ -39,4 +41,55 @@ String BuildHLSLSourceString(const ShaderCreateInfo& ShaderCI, String GetHLSLProfileString(SHADER_TYPE ShaderType, ShaderVersion ShaderModel); +template +void HandleHLSLCompilerResult(bool CompilationSucceeded, + BlobType* pCompilerMsgBlob, + const std::string& ShaderSource, + const char* ShaderName, + IDataBlob** ppOutputLog) noexcept(false) +{ + const char* CompilerMsg = pCompilerMsgBlob ? static_cast(pCompilerMsgBlob->GetBufferPointer()) : nullptr; + const size_t CompilerMsgLen = CompilerMsg ? pCompilerMsgBlob->GetBufferSize() : 0; + + if (ppOutputLog != nullptr) + { + const auto ShaderSourceLen = ShaderSource.length(); + auto* pOutputLogBlob = MakeNewRCObj{}(ShaderSourceLen + 1 + CompilerMsgLen + 1); + + auto* log = static_cast(pOutputLogBlob->GetDataPtr()); + + if (CompilerMsg != nullptr) + memcpy(log, CompilerMsg, CompilerMsgLen); + log[CompilerMsgLen] = 0; // Explicitly set null terminator + log += CompilerMsgLen + 1; + + memcpy(log, ShaderSource.data(), ShaderSourceLen); + log[ShaderSourceLen] = 0; + + pOutputLogBlob->QueryInterface(IID_DataBlob, reinterpret_cast(ppOutputLog)); + } + + if (!CompilationSucceeded || CompilerMsgLen != 0) + { + std::stringstream ss; + ss << (CompilationSucceeded ? "Compiler output for shader '" : "Failed to compile shader '") + << (ShaderName != nullptr ? ShaderName : "") + << "'"; + if (CompilerMsg != nullptr && CompilerMsgLen != 0) + { + ss << ":" << std::endl + << CompilerMsg; + } + else if (!CompilationSucceeded) + { + ss << " (no shader log available)."; + } + + if (CompilationSucceeded) + LOG_INFO_MESSAGE(ss.str()); + else + LOG_ERROR_AND_THROW(ss.str()); + } +} + } // namespace Diligent diff --git a/Graphics/ShaderTools/src/DXCompiler.cpp b/Graphics/ShaderTools/src/DXCompiler.cpp index 3ce6066f..ad7a0023 100644 --- a/Graphics/ShaderTools/src/DXCompiler.cpp +++ b/Graphics/ShaderTools/src/DXCompiler.cpp @@ -459,8 +459,8 @@ void DXCompilerImpl::Compile(const ShaderCreateInfo& ShaderCI, #endif }; - CComPtr compiled; - CComPtr errors; + CComPtr pCompiledShader; + CComPtr pDxcLog; IDXCompiler::CompileAttribs CA; @@ -475,44 +475,20 @@ void DXCompilerImpl::Compile(const ShaderCreateInfo& ShaderCI, CA.pArgs = m_Target == DXCompilerTarget::Direct3D12 ? pDxbcArgs : pSpirvArgs; CA.ArgsCount = m_Target == DXCompilerTarget::Direct3D12 ? _countof(pDxbcArgs) : _countof(pSpirvArgs); CA.pShaderSourceStreamFactory = ShaderCI.pShaderSourceStreamFactory; - CA.ppBlobOut = &compiled; - CA.ppCompilerOutput = &errors; + CA.ppBlobOut = &pCompiledShader; + CA.ppCompilerOutput = &pDxcLog; auto result = Compile(CA); + HandleHLSLCompilerResult(result, pDxcLog.p, Source, ShaderCI.Desc.Name, ppCompilerOutput); - const size_t CompilerMsgLen = errors ? errors->GetBufferSize() : 0; - const char* CompilerMsg = CompilerMsgLen > 0 ? static_cast(errors->GetBufferPointer()) : nullptr; - - if (CompilerMsg != nullptr && ppCompilerOutput != nullptr) - { - auto* pOutputDataBlob = MakeNewRCObj()(Source.length() + 1 + CompilerMsgLen + 1); - char* DataPtr = static_cast(pOutputDataBlob->GetDataPtr()); - memcpy(DataPtr, CompilerMsg, CompilerMsgLen); - DataPtr[CompilerMsgLen] = 0; // Set null terminator as CompilerMsgLen may not account for it - memcpy(DataPtr + CompilerMsgLen + 1, Source.data(), Source.length() + 1); - pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast(ppCompilerOutput)); - } - - if (!result) - { - if (ppCompilerOutput != nullptr) - { - LOG_ERROR_AND_THROW("Failed to compile Vulkan shader \"", (ShaderCI.Desc.Name != nullptr ? ShaderCI.Desc.Name : ""), "\"."); - } - else - { - LOG_ERROR_AND_THROW("Failed to compile Vukan shader \"", (ShaderCI.Desc.Name != nullptr ? ShaderCI.Desc.Name : ""), "\":\n", (CompilerMsg != nullptr ? std::string(CompilerMsg, CompilerMsgLen) : "")); - } - } - - if (result && compiled && compiled->GetBufferSize() > 0) + if (result && pCompiledShader && pCompiledShader->GetBufferSize() > 0) { if (pByteCode != nullptr) - pByteCode->assign(static_cast(compiled->GetBufferPointer()), - static_cast(compiled->GetBufferPointer()) + compiled->GetBufferSize() / sizeof(uint32_t)); + pByteCode->assign(static_cast(pCompiledShader->GetBufferPointer()), + static_cast(pCompiledShader->GetBufferPointer()) + pCompiledShader->GetBufferSize() / sizeof(uint32_t)); if (ppByteCodeBlob != nullptr) - *ppByteCodeBlob = compiled.Detach(); + *ppByteCodeBlob = pCompiledShader.Detach(); } } -- cgit v1.2.3