Diligent Engine  v.2.4.g
DXCompiler.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 <vector>
31 #include <memory>
32 
33 #include "Constants.h"
34 #include "Shader.h"
35 #include "DataBlob.h"
36 #include "ResourceBindingMap.hpp"
37 
38 // defined in dxcapi.h
39 struct DxcDefine;
40 struct IDxcBlob;
41 
42 // defined in d3d12shader.h
43 struct ID3D12ShaderReflection;
44 
45 namespace Diligent
46 {
47 
48 enum class DXCompilerTarget
49 {
50  Direct3D12, // compiles to DXIL
51  Vulkan, // compiles to SPIRV
52 };
53 
56 {
57 public:
58  virtual ~IDXCompiler() {}
59 
60  virtual ShaderVersion GetMaxShaderModel() = 0;
61 
62  virtual bool IsLoaded() = 0;
63 
64  virtual void GetVersion(Uint32& MajorVersion, Uint32& MinorVersion) const = 0;
65 
67  {
68  const char* Source = nullptr;
70  const wchar_t* EntryPoint = nullptr;
71  const wchar_t* Profile = nullptr;
72  const DxcDefine* pDefines = nullptr;
74  const wchar_t** pArgs = nullptr;
77  IDxcBlob** ppBlobOut = nullptr;
78  IDxcBlob** ppCompilerOutput = nullptr;
79  };
80  virtual bool Compile(const CompileAttribs& Attribs) = 0;
81 
82  virtual void Compile(const ShaderCreateInfo& ShaderCI,
83  ShaderVersion ShaderModel,
84  const char* ExtraDefinitions,
85  IDxcBlob** ppByteCodeBlob,
86  std::vector<uint32_t>* pByteCode,
87  IDataBlob** ppCompilerOutput) noexcept(false) = 0;
88 
89 
92 
95 
104  virtual bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
105  IDxcBlob* pSrcBytecode,
106  IDxcBlob** ppDstByteCode) = 0;
107 
109  virtual void GetD3D12ShaderReflection(IDxcBlob* pShaderBytecode,
110  ID3D12ShaderReflection** ppShaderReflection) = 0;
111 };
112 
113 // Use this function to load the DX Compiler library.
114 // pLibraryName is an optional path to the library. If not provided, default
115 // path is used.
116 std::unique_ptr<IDXCompiler> CreateDXCompiler(DXCompilerTarget Target, Uint32 APIVersion, const char* pLibraryName);
117 
118 bool IsDXILBytecode(const void* pBytecode, size_t Size);
119 
120 } // namespace Diligent
Diligent::IShaderSourceInputStreamFactory
Shader source stream factory interface.
Definition: Shader.h:134
Diligent::IDXCompiler::GetMaxShaderModel
virtual ShaderVersion GetMaxShaderModel()=0
Diligent::IDXCompiler::Compile
virtual bool Compile(const CompileAttribs &Attribs)=0
Diligent::IDXCompiler::CompileAttribs::Source
const char * Source
Definition: DXCompiler.hpp:68
Diligent::IDataBlob
Base interface for a file stream.
Definition: DataBlob.h:52
Diligent::DXCompilerTarget::Vulkan
@ Vulkan
Diligent::ShaderVersion
Shader version.
Definition: Shader.h:177
Diligent::IDXCompiler::CompileAttribs::SourceLength
Uint32 SourceLength
Definition: DXCompiler.hpp:69
Diligent::ResourceBinding::BindInfo
Definition: ResourceBindingMap.hpp:41
Diligent::IDXCompiler::TResourceBindingMap
ResourceBinding::TMap TResourceBindingMap
Definition: DXCompiler.hpp:91
Shader.h
Diligent::IDXCompiler::CompileAttribs
Definition: DXCompiler.hpp:66
Diligent::IDXCompiler::GetD3D12ShaderReflection
virtual void GetD3D12ShaderReflection(IDxcBlob *pShaderBytecode, ID3D12ShaderReflection **ppShaderReflection)=0
Attempts to extract shader reflection from the bytecode using DXC.
Diligent::DXCompilerTarget::Direct3D12
@ Direct3D12
Diligent::IDXCompiler::CompileAttribs::pArgs
const wchar_t ** pArgs
Definition: DXCompiler.hpp:74
Diligent::DXBCUtils::BindInfo
ResourceBinding::BindInfo BindInfo
Definition: DXBCUtils.hpp:40
Constants.h
Diligent::ShaderCreateInfo
Shader creation attributes.
Definition: Shader.h:241
Diligent::CreateDXCompiler
std::unique_ptr< IDXCompiler > CreateDXCompiler(DXCompilerTarget Target, Uint32 APIVersion, const char *pLibraryName)
Definition: DXCompiler.cpp:283
MinorVersion
Uint8 MinorVersion
Definition: DXBCUtils.cpp:68
Diligent::IDXCompiler::CompileAttribs::EntryPoint
const wchar_t * EntryPoint
Definition: DXCompiler.hpp:70
DataBlob.h
Diligent::IDXCompiler::CompileAttribs::DefinesCount
Uint32 DefinesCount
Definition: DXCompiler.hpp:73
Diligent::IDXCompiler::RemapResourceBindings
virtual bool RemapResourceBindings(const TResourceBindingMap &ResourceMap, IDxcBlob *pSrcBytecode, IDxcBlob **ppDstByteCode)=0
Remaps resource bindings (shader registers and spaces) in the source byte code using the resource bin...
Diligent::ResourceBinding::TMap
std::unordered_map< HashMapStringKey, BindInfo, HashMapStringKey::Hasher > TMap
A mapping from the resource name to the binding (shader register, space, array size).
Definition: ResourceBindingMap.hpp:67
Diligent::IDXCompiler
DXC compiler interface.
Definition: DXCompiler.hpp:55
Diligent::IDXCompiler::CompileAttribs::pDefines
const DxcDefine * pDefines
Definition: DXCompiler.hpp:72
Diligent::IDXCompiler::CompileAttribs::ppBlobOut
IDxcBlob ** ppBlobOut
Definition: DXCompiler.hpp:77
Diligent::IDXCompiler::CompileAttribs::Profile
const wchar_t * Profile
Definition: DXCompiler.hpp:71
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::IDXCompiler::~IDXCompiler
virtual ~IDXCompiler()
Definition: DXCompiler.hpp:58
Diligent::IDXCompiler::CompileAttribs::pShaderSourceStreamFactory
IShaderSourceInputStreamFactory * pShaderSourceStreamFactory
Definition: DXCompiler.hpp:76
ResourceBindingMap.hpp
Diligent::IDXCompiler::IsLoaded
virtual bool IsLoaded()=0
Diligent::IDXCompiler::CompileAttribs::ArgsCount
Uint32 ArgsCount
Definition: DXCompiler.hpp:75
MajorVersion
Uint8 MajorVersion
Definition: DXBCUtils.cpp:69
Diligent::IsDXILBytecode
bool IsDXILBytecode(const void *pBytecode, size_t Size)
Definition: DXCompiler.cpp:1762
Diligent::DXCompilerTarget
DXCompilerTarget
Definition: DXCompiler.hpp:48
Diligent::IDXCompiler::GetVersion
virtual void GetVersion(Uint32 &MajorVersion, Uint32 &MinorVersion) const =0
Diligent::IDXCompiler::CompileAttribs::ppCompilerOutput
IDxcBlob ** ppCompilerOutput
Definition: DXCompiler.hpp:78
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37