summaryrefslogtreecommitdiffstats
path: root/Graphics/ShaderTools
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-09-15 00:45:59 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-09-15 00:45:59 +0000
commita88ff24e94bba0efaa44efe6263bd1b698e58849 (patch)
tree87ff71de5f567dec8303eb9aadefb38a63759099 /Graphics/ShaderTools
parentRefactoring shader compilation - part III (diff)
downloadDiligentCore-a88ff24e94bba0efaa44efe6263bd1b698e58849.tar.gz
DiligentCore-a88ff24e94bba0efaa44efe6263bd1b698e58849.zip
Completed shader compilation refactoring (fixed https://github.com/DiligentGraphics/DiligentCore/issues/160)
Diffstat (limited to 'Graphics/ShaderTools')
-rw-r--r--Graphics/ShaderTools/include/HLSLUtils.hpp53
-rw-r--r--Graphics/ShaderTools/src/DXCompiler.cpp42
2 files changed, 62 insertions, 33 deletions
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 <sstream>
+
#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 <typename BlobType>
+void HandleHLSLCompilerResult(bool CompilationSucceeded,
+ BlobType* pCompilerMsgBlob,
+ const std::string& ShaderSource,
+ const char* ShaderName,
+ IDataBlob** ppOutputLog) noexcept(false)
+{
+ const char* CompilerMsg = pCompilerMsgBlob ? static_cast<const char*>(pCompilerMsgBlob->GetBufferPointer()) : nullptr;
+ const size_t CompilerMsgLen = CompilerMsg ? pCompilerMsgBlob->GetBufferSize() : 0;
+
+ if (ppOutputLog != nullptr)
+ {
+ const auto ShaderSourceLen = ShaderSource.length();
+ auto* pOutputLogBlob = MakeNewRCObj<DataBlobImpl>{}(ShaderSourceLen + 1 + CompilerMsgLen + 1);
+
+ auto* log = static_cast<char*>(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<IObject**>(ppOutputLog));
+ }
+
+ if (!CompilationSucceeded || CompilerMsgLen != 0)
+ {
+ std::stringstream ss;
+ ss << (CompilationSucceeded ? "Compiler output for shader '" : "Failed to compile shader '")
+ << (ShaderName != nullptr ? ShaderName : "<unknown>")
+ << "'";
+ 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<IDxcBlob> compiled;
- CComPtr<IDxcBlob> errors;
+ CComPtr<IDxcBlob> pCompiledShader;
+ CComPtr<IDxcBlob> 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<const char*>(errors->GetBufferPointer()) : nullptr;
-
- if (CompilerMsg != nullptr && ppCompilerOutput != nullptr)
- {
- auto* pOutputDataBlob = MakeNewRCObj<DataBlobImpl>()(Source.length() + 1 + CompilerMsgLen + 1);
- char* DataPtr = static_cast<char*>(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<IObject**>(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) : "<no compiler log available>"));
- }
- }
-
- if (result && compiled && compiled->GetBufferSize() > 0)
+ if (result && pCompiledShader && pCompiledShader->GetBufferSize() > 0)
{
if (pByteCode != nullptr)
- pByteCode->assign(static_cast<uint32_t*>(compiled->GetBufferPointer()),
- static_cast<uint32_t*>(compiled->GetBufferPointer()) + compiled->GetBufferSize() / sizeof(uint32_t));
+ pByteCode->assign(static_cast<uint32_t*>(pCompiledShader->GetBufferPointer()),
+ static_cast<uint32_t*>(pCompiledShader->GetBufferPointer()) + pCompiledShader->GetBufferSize() / sizeof(uint32_t));
if (ppByteCodeBlob != nullptr)
- *ppByteCodeBlob = compiled.Detach();
+ *ppByteCodeBlob = pCompiledShader.Detach();
}
}