예제 #1
0
//================================================================================================
bool DXShader11::tryLoadBinThenCompile(LPCWSTR pSrcFile, 
									   LPCSTR pFuncName,
									   const D3D_SHADER_MACRO * pDefines)
{
	//First try loading from bin
	DAnsiStr strBinFile;
	strBinFile.copyFromW(pSrcFile);
	strBinFile =  PS::FILESTRINGUTILS::ChangeFileExt(strBinFile, DAnsiStr(".bin"));

	if(loadFromBinary(strBinFile.ptr())	)
		return true;

	//Could not find bin so try compiling the HLSL code and save bin afterwards.
	HRESULT res = compile(pSrcFile, pFuncName, pDefines, true);	
	return SUCCEEDED(res);
}
예제 #2
0
HRESULT DXShader11::compile( LPCWSTR pSrcFile, 
							 LPCSTR pFuncName,
							 const D3D_SHADER_MACRO * pDefines,
							 bool bSaveByteCode)
{
	HRESULT hr;
    
	if(!PS::FILESTRINGUTILS::FileExists( pSrcFile))
		return E_FAIL;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

    // We generally prefer to use the higher CS shader profile when possible as CS 5.0 is better performance on 11-class hardware
	LPCSTR pProfile = ( m_pDXDevice->getDevice()->GetFeatureLevel() >= D3D_FEATURE_LEVEL_11_0 ) ? "cs_5_0" : "cs_4_0";

    ID3DBlob* pErrorBlob = NULL;
    ID3DBlob* pBlob = NULL;
	hr = D3DX11CompileFromFile(pSrcFile, pDefines, NULL, pFuncName, pProfile, 
						       dwShaderFlags, NULL, NULL, &pBlob, &pErrorBlob, NULL );
    if ( FAILED(hr) )
    {
        if ( pErrorBlob )
		{
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
			ReportError((const char*)pErrorBlob->GetBufferPointer());
		}

        SAFE_RELEASE( pErrorBlob );
        SAFE_RELEASE( pBlob );    

        return hr;
    }    

	hr = m_pDXDevice->getDevice()->CreateComputeShader( pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &m_pComputeShader );
	if(SUCCEEDED(hr))
	{
		m_bCompiled = true;
		if(bSaveByteCode)
		{
			DAnsiStr strBinFile;
			strBinFile.copyFromW(pSrcFile);
			strBinFile =  PS::FILESTRINGUTILS::ChangeFileExt(strBinFile, DAnsiStr(".bin"));
			ofstream ofs(strBinFile.ptr(), ios::out | ios::trunc | ios::binary);
			if(!ofs.is_open())
				return false;

			ofs.write( reinterpret_cast<const char*>(pBlob->GetBufferPointer()), pBlob->GetBufferSize());
			ofs.close();
		}
	}


#if defined(DEBUG) || defined(PROFILE)
	if ( m_pComputeShader )
		(m_pComputeShader)->SetPrivateData( WKPDID_D3DDebugObjectName, lstrlenA(pFuncName), pFuncName);
#endif

    SAFE_RELEASE( pErrorBlob );
    SAFE_RELEASE( pBlob );

    return hr;
}