From cf7ef96b20bf3f91a9ee623f94800d617bc6ff3c Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Tue, 27 Mar 2018 18:46:47 -0700 Subject: Added shader compiler log output --- .../include/HLSLDefinitions.fxh | 1 + .../include/HLSLDefinitions_inc.fxh | 1 + .../GraphicsEngineD3DBase/src/ShaderD3DBase.cpp | 79 +++++++++++++--------- 3 files changed, 48 insertions(+), 33 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions.fxh b/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions.fxh index df6edfa0..44ed3641 100644 --- a/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions.fxh +++ b/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions.fxh @@ -74,3 +74,4 @@ float2x2 MatrixFromRows(float2 row0, float2 row1) } #endif // _HLSL_DEFINITIONS_ + diff --git a/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions_inc.fxh b/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions_inc.fxh index 3eb36067..72bfc1f8 100644 --- a/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions_inc.fxh +++ b/Graphics/GraphicsEngineD3DBase/include/HLSLDefinitions_inc.fxh @@ -74,3 +74,4 @@ "}\n" "\n" "#endif // _HLSL_DEFINITIONS_\n" +"\n" diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp index 31332e3e..5820245b 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp @@ -57,7 +57,7 @@ public: return E_FAIL; } - RefCntAutoPtr pFileData( MakeNewRCObj()(0) ); + RefCntAutoPtr pFileData( MakeNewRCObj()(0) ); pSourceStream->Read( pFileData ); *ppData = pFileData->GetDataPtr(); *pBytes = static_cast( pFileData->GetSize() ); @@ -75,7 +75,7 @@ public: private: IShaderSourceInputStreamFactory *m_pStreamFactory; - std::unordered_map< LPCVOID, RefCntAutoPtr > m_DataBlobs; + std::unordered_map< LPCVOID, RefCntAutoPtr > m_DataBlobs; }; HRESULT CompileShader( const char* Source, @@ -83,7 +83,8 @@ HRESULT CompileShader( const char* Source, const D3D_SHADER_MACRO* pDefines, IShaderSourceInputStreamFactory *pIncludeStreamFactory, LPCSTR profile, - ID3DBlob **ppBlobOut ) + ID3DBlob **ppBlobOut, + ID3DBlob **ppCompilerOutput) { DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) @@ -98,37 +99,25 @@ HRESULT CompileShader( const char* Source, // dwShaderFlags |= D3D10_SHADER_OPTIMIZATION_LEVEL3; #endif HRESULT hr; - do - { - CComPtr errors; +// do +// { auto SourceLen = strlen(Source); D3DIncludeImpl IncludeImpl(pIncludeStreamFactory); - hr = D3DCompile( Source, SourceLen, NULL, pDefines, &IncludeImpl, strFunctionName, profile, dwShaderFlags, 0, ppBlobOut, &errors ); + hr = D3DCompile( Source, SourceLen, NULL, pDefines, &IncludeImpl, strFunctionName, profile, dwShaderFlags, 0, ppBlobOut, ppCompilerOutput); - if( FAILED(hr) || errors ) - { - std::wstringstream errorss; - ComErrorDesc ErrDesc(hr); - if( FAILED(hr) ) - Diligent::FormatMsg( errorss, "Failed to compile shader\n" ); - else - Diligent::FormatMsg( errorss, "Shader compiler output:\n" ); - Diligent::FormatMsg( errorss, ErrDesc.Get(), "\n" ); - if( errors ) - Diligent::FormatMsg( errorss, (char*)errors->GetBufferPointer() ); - auto ErrorDesc = errorss.str(); - OutputDebugStringW( ErrorDesc.c_str() ); - if( FAILED(hr) -#if PLATFORM_WIN32 - && IDRETRY != MessageBoxW( NULL, ErrorDesc.c_str() , L"FX Error", MB_ICONERROR | (Source == nullptr ? MB_ABORTRETRYIGNORE : 0) ) -#endif - ) - { - break; - } - } - } while( FAILED(hr) ); +// if( FAILED(hr) || errors ) +// { +// if( FAILED(hr) +//#if PLATFORM_WIN32 +// && IDRETRY != MessageBoxW( NULL, L"Failed to compile shader", L"FX Error", MB_ICONERROR | (Source == nullptr ? MB_ABORTRETRYIGNORE : 0) ) +//#endif +// ) +// { +// break; +// } +// } +// } while( FAILED(hr) ); return hr; } @@ -178,7 +167,7 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) VERIFY(CreationAttribs.pShaderSourceStreamFactory, "Input stream factory is null"); RefCntAutoPtr pSourceStream; CreationAttribs.pShaderSourceStreamFactory->CreateInputStream(CreationAttribs.FilePath, &pSourceStream); - RefCntAutoPtr pFileData(MakeNewRCObj()(0)); + RefCntAutoPtr pFileData(MakeNewRCObj()(0)); if (pSourceStream == nullptr) LOG_ERROR_AND_THROW("Failed to open shader source file"); pSourceStream->Read(pFileData); @@ -201,8 +190,32 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) } VERIFY(CreationAttribs.EntryPoint != nullptr, "Entry point must not be null"); - CHECK_D3D_RESULT_THROW(CompileShader(ShaderSource.c_str(), CreationAttribs.EntryPoint, pDefines, CreationAttribs.pShaderSourceStreamFactory, strShaderProfile.c_str(), &m_pShaderByteCode), - "Failed to compile the shader"); + CComPtr errors; + auto hr = CompileShader(ShaderSource.c_str(), CreationAttribs.EntryPoint, pDefines, CreationAttribs.pShaderSourceStreamFactory, strShaderProfile.c_str(), &m_pShaderByteCode, &errors); + + const char *CompilerMsg = errors ? reinterpret_cast(errors->GetBufferPointer()) : nullptr; + if(CompilerMsg != nullptr && CreationAttribs.ppCompilerOutput != nullptr) + { + auto ErrorMsgLen = strlen(CompilerMsg); + auto *pOutputDataBlob = MakeNewRCObj()(ErrorMsgLen + 1 + ShaderSource.length() + 1); + char* DataPtr = reinterpret_cast(pOutputDataBlob->GetDataPtr()); + memcpy(DataPtr, CompilerMsg, ErrorMsgLen+1); + memcpy(DataPtr + ErrorMsgLen + 1, ShaderSource.data(), ShaderSource.length() + 1); + pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast(CreationAttribs.ppCompilerOutput)); + } + + if(FAILED(hr)) + { + ComErrorDesc ErrDesc(hr); + if(CreationAttribs.ppCompilerOutput != nullptr) + { + LOG_ERROR_AND_THROW("Failed to compile D3D shader (", ErrDesc.Get(), ")."); + } + else + { + LOG_ERROR_AND_THROW("Failed to compile D3D shader (", ErrDesc.Get(), "):\n", CompilerMsg != nullptr ? CompilerMsg : ""); + } + } } else if (CreationAttribs.ByteCode) { -- cgit v1.2.3