diff options
| author | azhirnov <zh1dron@gmail.com> | 2020-09-08 19:24:03 +0000 |
|---|---|---|
| committer | azhirnov <zh1dron@gmail.com> | 2020-09-08 19:54:42 +0000 |
| commit | cb92debd446581a094941e923d3a973d5450018f (patch) | |
| tree | 45e07a592e72d3525d31f2eb18a3d58cf77a1d03 /Graphics | |
| parent | added DXC license (diff) | |
| download | DiligentCore-cb92debd446581a094941e923d3a973d5450018f.tar.gz DiligentCore-cb92debd446581a094941e923d3a973d5450018f.zip | |
removed m_isDXIL flag, detect DXIL by bytecode
Diffstat (limited to 'Graphics')
7 files changed, 68 insertions, 58 deletions
diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderResourcesD3D12.hpp b/Graphics/GraphicsEngineD3D12/include/ShaderResourcesD3D12.hpp index 88c1cdc5..23f9882b 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderResourcesD3D12.hpp +++ b/Graphics/GraphicsEngineD3D12/include/ShaderResourcesD3D12.hpp @@ -92,7 +92,7 @@ class ShaderResourcesD3D12 final : public ShaderResources { public: // Loads shader resources from the compiled shader bytecode - ShaderResourcesD3D12(ID3DBlob* pShaderBytecode, bool isDXIL, const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix); + ShaderResourcesD3D12(ID3DBlob* pShaderBytecode, const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix); // clang-format off ShaderResourcesD3D12 (const ShaderResourcesD3D12&) = delete; diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderD3D12Impl.cpp index 3c740b29..d14eb122 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderD3D12Impl.cpp @@ -65,7 +65,7 @@ ShaderD3D12Impl::ShaderD3D12Impl(IReferenceCounters* pRefCounters, // Load shader resources auto& Allocator = GetRawAllocator(); auto* pRawMem = ALLOCATE(Allocator, "Allocator for ShaderResources", ShaderResourcesD3D12, 1); - auto* pResources = new (pRawMem) ShaderResourcesD3D12(m_pShaderByteCode, m_isDXIL, m_Desc, ShaderCI.UseCombinedTextureSamplers ? ShaderCI.CombinedSamplerSuffix : nullptr); + auto* pResources = new (pRawMem) ShaderResourcesD3D12(m_pShaderByteCode, m_Desc, ShaderCI.UseCombinedTextureSamplers ? ShaderCI.CombinedSamplerSuffix : nullptr); m_pShaderResources.reset(pResources, STDDeleterRawMem<ShaderResourcesD3D12>(Allocator)); } diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderResourcesD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderResourcesD3D12.cpp index f7488afe..bcb34226 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderResourcesD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderResourcesD3D12.cpp @@ -32,13 +32,12 @@ #include "ShaderD3DBase.hpp" #include "ShaderBase.hpp" #include "DXILUtils.hpp" -#include "dxc/dxcapi.h" namespace Diligent { -ShaderResourcesD3D12::ShaderResourcesD3D12(ID3DBlob* pShaderBytecode, bool isDXIL, const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix) : +ShaderResourcesD3D12::ShaderResourcesD3D12(ID3DBlob* pShaderBytecode, const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix) : ShaderResources{ShdrDesc.ShaderType} { class NewResourceHandler @@ -56,25 +55,9 @@ ShaderResourcesD3D12::ShaderResourcesD3D12(ID3DBlob* pShaderBytecode, bool isDXI CComPtr<ID3D12ShaderReflection> pShaderReflection; - HRESULT hr; - - if (isDXIL) - { - const uint32_t DFCC_DXIL = uint32_t('D') | (uint32_t('X') << 8) | (uint32_t('I') << 16) | (uint32_t('L') << 24); - CComPtr<IDxcContainerReflection> pReflection; - UINT32 shaderIdx; - hr = D3D12DxcCreateInstance(CLSID_DxcContainerReflection, IID_PPV_ARGS(&pReflection)); - CHECK_D3D_RESULT_THROW(hr, "Failed to create shader reflection instance"); - hr = pReflection->Load(reinterpret_cast<IDxcBlob*>(pShaderBytecode)); - CHECK_D3D_RESULT_THROW(hr, "Failed to load shader reflection from bytecode"); - hr = pReflection->FindFirstPartKind(DFCC_DXIL, &shaderIdx); - CHECK_D3D_RESULT_THROW(hr, "Failed to find DXIL part"); - hr = pReflection->GetPartReflection(shaderIdx, __uuidof(pShaderReflection), reinterpret_cast<void**>(&pShaderReflection)); - CHECK_D3D_RESULT_THROW(hr, "Failed to get the shader reflection"); - } - else + if (!DxcGetShaderReflection(reinterpret_cast<IDxcBlob*>(pShaderBytecode), &pShaderReflection)) { - hr = D3DReflect(pShaderBytecode->GetBufferPointer(), pShaderBytecode->GetBufferSize(), __uuidof(pShaderReflection), reinterpret_cast<void**>(&pShaderReflection)); + HRESULT hr = D3DReflect(pShaderBytecode->GetBufferPointer(), pShaderBytecode->GetBufferSize(), __uuidof(pShaderReflection), reinterpret_cast<void**>(&pShaderReflection)); CHECK_D3D_RESULT_THROW(hr, "Failed to get the shader reflection"); } diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.hpp b/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.hpp index 2667b639..3de8b38e 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.hpp +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.hpp @@ -44,7 +44,6 @@ public: protected: CComPtr<ID3DBlob> m_pShaderByteCode; - bool m_isDXIL; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp index a93bb5ec..8df5330a 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp @@ -311,27 +311,28 @@ static HRESULT CompileShader(const char* Source, } // namespace -ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, ShaderVersion ShaderModel, bool IsD3D12) : - m_isDXIL{false} +ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, ShaderVersion ShaderModel, bool IsD3D12) { if (ShaderCI.Source || ShaderCI.FilePath) { DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from the source code or a file"); DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from the source code or a file"); + bool IsDXIL = false; + // validate compiler type switch (ShaderCI.ShaderCompiler) { // clang-format off - case SHADER_COMPILER_DEFAULT: m_isDXIL = false; break; - case SHADER_COMPILER_DXC: m_isDXIL = true; break; - case SHADER_COMPILER_FXC: m_isDXIL = false; break; + case SHADER_COMPILER_DEFAULT: IsDXIL = false; break; + case SHADER_COMPILER_DXC: IsDXIL = true; break; + case SHADER_COMPILER_FXC: IsDXIL = false; break; // clang-format on - default: UNEXPECTED("Unsupported shader compiler"); m_isDXIL = false; + default: UNEXPECTED("Unsupported shader compiler"); } // validate shader model - if (m_isDXIL) + if (IsDXIL) { ShaderModel = (ShaderModel.Major >= 6 ? ShaderModel : ShaderVersion{6, 0}); @@ -346,10 +347,10 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, ShaderVersion Sha ShaderModel = MaxSM; } else - m_isDXIL = false; + IsDXIL = false; } - if (!m_isDXIL) + if (!IsDXIL) { ShaderModel = (ShaderModel.Major < 6 ? ShaderModel : (IsD3D12 ? ShaderVersion{5, 1} : ShaderVersion{5, 0})); } @@ -401,7 +402,7 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, ShaderVersion Sha CComPtr<ID3DBlob> errors; HRESULT hr; - if (m_isDXIL) + if (IsDXIL) hr = CompileDxilShader(ShaderSource.c_str(), ShaderSource.length(), ShaderCI, strShaderProfile.c_str(), &m_pShaderByteCode, &errors); else hr = CompileShader(ShaderSource.c_str(), ShaderSource.length(), ShaderCI, strShaderProfile.c_str(), &m_pShaderByteCode, &errors); diff --git a/Graphics/HLSLTools/include/DXILUtils.hpp b/Graphics/HLSLTools/include/DXILUtils.hpp index acc32187..3f0b4a67 100644 --- a/Graphics/HLSLTools/include/DXILUtils.hpp +++ b/Graphics/HLSLTools/include/DXILUtils.hpp @@ -36,6 +36,9 @@ struct DxcDefine; struct IDxcBlob; +// defined in d3d12shader.h +struct ID3D12ShaderReflection; + namespace Diligent { @@ -67,14 +70,13 @@ bool DxcCompile(DXCompilerTarget Target, std::vector<uint32_t> DXILtoSPIRV(const ShaderCreateInfo& Attribs, const char* ExtraDefinitions, - IDataBlob** ppCompilerOutput); + IDataBlob** ppCompilerOutput) noexcept(false); #if D3D12_SUPPORTED -// calls DxcCreateInstance -HRESULT D3D12DxcCreateInstance( - _In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID* ppv); +// Returns false if pShaderBytecode hasn't DXIL bytecode. +// Throws exception on error. +bool DxcGetShaderReflection(IDxcBlob* pShaderBytecode, + ID3D12ShaderReflection** ppShaderReflection) noexcept(false); #endif } // namespace Diligent
\ No newline at end of file diff --git a/Graphics/HLSLTools/src/DXILUtils.cpp b/Graphics/HLSLTools/src/DXILUtils.cpp index 45fec173..cc343414 100644 --- a/Graphics/HLSLTools/src/DXILUtils.cpp +++ b/Graphics/HLSLTools/src/DXILUtils.cpp @@ -42,11 +42,12 @@ // Platforms that has DXCompiler. #if defined(PLATFORM_WIN32) || defined(PLATFORM_UNIVERSAL_WINDOWS) || defined(PLATFORM_LINUX) -# include "dxc/dxcapi.h" -# ifdef PLATFORM_LINUX -# undef _countof +# if D3D12_SUPPORTED +# include <d3d12shader.h> # endif +# include "dxc/dxcapi.h" + # include "DataBlobImpl.hpp" # include "RefCntAutoPtr.hpp" @@ -270,20 +271,6 @@ private: } // namespace -# if D3D12_SUPPORTED -HRESULT D3D12DxcCreateInstance( - _In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID* ppv) -{ - DXCompilerImpl* DxCompiler = DXILCompilerLib(); - if (DxCompiler != nullptr && DxCompiler->CreateInstance != nullptr) - return DxCompiler->CreateInstance(rclsid, riid, ppv); - else - return E_NOTIMPL; -} -# endif - bool DxcLoadLibrary(DXCompilerTarget Target, const char* name) { DXCompilerImpl* DxCompiler = nullptr; @@ -471,6 +458,44 @@ bool DxcCompile(DXCompilerTarget Target, return true; } +# if D3D12_SUPPORTED +# define FOURCC(a, b, c, d) (uint32_t{((d) << 24) | ((c) << 16) | ((b) << 8) | (a)}) + +bool DxcGetShaderReflection(IDxcBlob* pShaderBytecode, + ID3D12ShaderReflection** ppShaderReflection) noexcept(false) +{ + HRESULT hr; + auto DXCompiler = DXILCompilerLib(); + bool IsDXIL = false; + + if (DXCompiler != nullptr && DXCompiler->CreateInstance != nullptr) + { + const uint32_t DFCC_DXIL = FOURCC('D', 'X', 'I', 'L'); + CComPtr<IDxcContainerReflection> pReflection; + UINT32 shaderIdx; + + hr = DXCompiler->CreateInstance(CLSID_DxcContainerReflection, IID_PPV_ARGS(&pReflection)); + if (FAILED(hr)) + LOG_ERROR_AND_THROW("Failed to create shader reflection instance"); + + hr = pReflection->Load(pShaderBytecode); + if (FAILED(hr)) + LOG_ERROR_AND_THROW("Failed to load shader reflection from bytecode"); + + hr = pReflection->FindFirstPartKind(DFCC_DXIL, &shaderIdx); + IsDXIL = SUCCEEDED(hr); + if (IsDXIL) + { + hr = pReflection->GetPartReflection(shaderIdx, __uuidof(*ppShaderReflection), reinterpret_cast<void**>(ppShaderReflection)); + if (FAILED(hr)) + LOG_ERROR_AND_THROW("Failed to get the shader reflection"); + } + } + return IsDXIL; +} +# endif + + # if VULKAN_SUPPORTED // Implemented in GLSLSourceBuilder.cpp const char* GetShaderTypeDefines(SHADER_TYPE Type); @@ -489,7 +514,7 @@ static const char g_HLSLDefinitions[] = std::vector<uint32_t> DXILtoSPIRV(const ShaderCreateInfo& Attribs, const char* ExtraDefinitions, - IDataBlob** ppCompilerOutput) + IDataBlob** ppCompilerOutput) noexcept(false) { RefCntAutoPtr<IDataBlob> pFileData(MakeNewRCObj<DataBlobImpl>()(0)); @@ -667,7 +692,7 @@ bool DxcCompile(DXCompilerTarget Target, std::vector<uint32_t> DXILtoSPIRV(const ShaderCreateInfo& Attribs, const char* ExtraDefinitions, - IDataBlob** ppCompilerOutput) + IDataBlob** ppCompilerOutput) noexcept(false) { return {}; } |
