Diligent Engine  v.2.4.g
HLSLUtils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include <sstream>
31 
32 #include "BasicTypes.h"
33 #include "GraphicsTypes.h"
34 #include "Shader.h"
35 #include "RefCountedObjectImpl.hpp"
36 #include "Errors.hpp"
37 #include "DataBlobImpl.hpp"
38 
39 namespace Diligent
40 {
41 
43  const char* ExtraDefinitions = nullptr);
44 
46 
47 template <typename BlobType>
48 void HandleHLSLCompilerResult(bool CompilationSucceeded,
49  BlobType* pCompilerMsgBlob,
50  const std::string& ShaderSource,
51  const char* ShaderName,
52  IDataBlob** ppOutputLog) noexcept(false)
53 {
54  const char* CompilerMsg = pCompilerMsgBlob ? static_cast<const char*>(pCompilerMsgBlob->GetBufferPointer()) : nullptr;
55  const size_t CompilerMsgLen = CompilerMsg ? pCompilerMsgBlob->GetBufferSize() : 0;
56 
57  if (ppOutputLog != nullptr)
58  {
59  const auto ShaderSourceLen = ShaderSource.length();
60  auto* pOutputLogBlob = MakeNewRCObj<DataBlobImpl>{}(ShaderSourceLen + 1 + CompilerMsgLen + 1);
61 
62  auto* log = static_cast<char*>(pOutputLogBlob->GetDataPtr());
63 
64  if (CompilerMsg != nullptr)
65  memcpy(log, CompilerMsg, CompilerMsgLen);
66  log[CompilerMsgLen] = 0; // Explicitly set null terminator
67  log += CompilerMsgLen + 1;
68 
69  memcpy(log, ShaderSource.data(), ShaderSourceLen);
70  log[ShaderSourceLen] = 0;
71 
72  pOutputLogBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(ppOutputLog));
73  }
74 
75  if (!CompilationSucceeded || CompilerMsgLen != 0)
76  {
77  std::stringstream ss;
78  ss << (CompilationSucceeded ? "Compiler output for shader '" : "Failed to compile shader '")
79  << (ShaderName != nullptr ? ShaderName : "<unknown>")
80  << "'";
81  if (CompilerMsg != nullptr && CompilerMsgLen != 0)
82  {
83  ss << ":" << std::endl
84  << std::string{CompilerMsg, CompilerMsgLen};
85  }
86  else if (!CompilationSucceeded)
87  {
88  ss << " (no shader log available).";
89  }
90 
91  if (CompilationSucceeded)
92  LOG_INFO_MESSAGE(ss.str());
93  else
94  LOG_ERROR_AND_THROW(ss.str());
95  }
96 }
97 
98 } // namespace Diligent
LOG_ERROR_AND_THROW
#define LOG_ERROR_AND_THROW(...)
Definition: Errors.hpp:101
Diligent::IDataBlob
Base interface for a file stream.
Definition: DataBlob.h:52
Diligent::SHADER_TYPE
SHADER_TYPE
Describes the shader type.
Definition: GraphicsTypes.h:65
Diligent::HandleHLSLCompilerResult
void HandleHLSLCompilerResult(bool CompilationSucceeded, BlobType *pCompilerMsgBlob, const std::string &ShaderSource, const char *ShaderName, IDataBlob **ppOutputLog) noexcept(false)
Definition: HLSLUtils.hpp:48
Shader.h
Diligent::IObject
Base interface for all dynamic objects in the engine.
Definition: Object.h:41
DataBlobImpl.hpp
LOG_INFO_MESSAGE
#define LOG_INFO_MESSAGE(...)
Definition: Errors.hpp:124
BasicTypes.h
Diligent::ShaderVersion
struct ShaderVersion ShaderVersion
Definition: Shader.h:219
Diligent::ShaderCreateInfo
struct ShaderCreateInfo ShaderCreateInfo
Definition: Shader.h:352
Diligent::MakeNewRCObj
Definition: RefCountedObjectImpl.hpp:612
Diligent::GetHLSLProfileString
String GetHLSLProfileString(SHADER_TYPE ShaderType, ShaderVersion ShaderModel)
Definition: HLSLUtils.cpp:68
Diligent::String
std::basic_string< Char > String
String variable.
Definition: BasicTypes.h:66
GraphicsTypes.h
RefCountedObjectImpl.hpp
Diligent::BuildHLSLSourceString
String BuildHLSLSourceString(const ShaderCreateInfo &ShaderCI, const char *ExtraDefinitions=nullptr)
Definition: HLSLUtils.cpp:46
ShaderType
Uint16 ShaderType
Definition: DXBCUtils.cpp:70
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Errors.hpp