Effect::Effect(ID3D11Device * pDevice, const std::wstring &fileName) :m_pShader(nullptr) { #if DEBUG_SHADER DWORD shaderFlags = 0; //这些一般在shader编译器中设置的选项 #if defined(DEBUG) || defined(_DEBUG) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Blob * compiledShader = nullptr; ID3D10Blob * compilationMsgs = nullptr; HRESULT hr = D3DX11CompileFromFile(fileName.c_str(), nullptr, nullptr, nullptr, "fx_5_0", shaderFlags, 0, nullptr, &compiledShader, &compilationMsgs, nullptr); //这些信息是编译错误信息 if (compilationMsgs != 0) { MessageBoxA(0, (char *)compilationMsgs->GetBufferPointer(), 0, 0); ReleaseCOM(compilationMsgs); } //Even if there are no compliation msgs,check to make sure there no other errors. if (FAILED(hr)) { DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, pDevice, &m_pShader)); //Done with compiled shader ReleaseCOM(compiledShader); /************************************************************************/ #else std::ifstream fin(fileName, std::ios::binary); fin.seekg(0, std::ios_base::end);//定位至文件末尾,以求长度 int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShaderFile(size); fin.read(&compiledShaderFile[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShaderFile[0], size, 0, pDevice, &m_pShader)); #endif }
ID3DX11Effect* Assignment5::InitialiseShader(const std::wstring& a_FilePath) { DWORD shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = 0; ID3D10Blob* compilationMsgs = 0; HRESULT hr = D3DX11CompileFromFile(std::wstring(L"FX/" + a_FilePath).c_str(), 0, 0, 0, "fx_5_0", shaderFlags, 0, 0, &compiledShader, &compilationMsgs, 0); // compilationMsgs can store errors or warnings. if(compilationMsgs != 0) { MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0); ReleaseCOM(compilationMsgs); } // Even if there are no compilationMsgs, check to make sure there were no other errors. if(FAILED(hr)) { DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } ID3DX11Effect* shader; HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &shader)); // Done with compiled shader. ReleaseCOM(compiledShader); return shader; }
void LightDemo::InitFX() { std::ifstream fin("fx/Lighting.fxo", std::ios::binary); OC_ASSERT(fin.good()); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, m_dxDevice.Get(), m_fx.GetAddressOf())); m_tech = m_fx->GetTechniqueByName("LightTech"); m_fxWorldViewProj = m_fx->GetVariableByName("gWorldViewProj")->AsMatrix(); m_fxWorld = m_fx->GetVariableByName("gWorld")->AsMatrix(); m_fxWorldInvTranspose = m_fx->GetVariableByName("gWorldInvTranspose")->AsMatrix(); m_fxEyePosW = m_fx->GetVariableByName("gEyePosW")->AsVector(); m_fxDirLight = m_fx->GetVariableByName("gDirLight"); m_fxPointLight = m_fx->GetVariableByName("gPointLight"); m_fxSpotLight = m_fx->GetVariableByName("gSpotLight"); m_fxMaterial = m_fx->GetVariableByName("gMaterial"); }
void BoxApp::BuildFX() { DWORD shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = 0; ID3D10Blob* compilationMsgs = 0; HRESULT hr = D3DX11CompileFromFile(L"FX/color.fx", 0, 0, 0, "fx_5_0", shaderFlags, 0, 0, &compiledShader, &compilationMsgs, 0); // compilationMsgs can store errors or warnings. if( compilationMsgs != 0 ) { MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0); ReleaseCOM(compilationMsgs); } // Even if there are no compilationMsgs, check to make sure there were no other errors. if(FAILED(hr)) { // DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &mFX)); // Done with compiled shader. ReleaseCOM(compiledShader); mTech = mFX->GetTechniqueByName("ColorTech"); mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix(); }
HRESULT Effect::CreateEffectFromMemory(ID3D11Device* device, const std::wstring& filename) { std::ifstream fin(filename.c_str(), std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); if (size == -1) return STG_E_FILENOTFOUND; std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); 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 return D3DX11CreateEffectFromMemory(&compiledShader[0], size, dwShaderFlags, device, &(m_Effect)); }
bool TSRD3D11Effect::CompileFromMemory( unsigned char* _pByteCode, unsigned int _uiByteLength ) { #if defined( D3D11_RENDERER ) D3DCreateBlob( _uiByteLength, &m_pEffectBuffer ); #else D3D10CreateBlob( _uiByteLength, &m_pEffectBuffer ); #endif memcpy( m_pEffectBuffer->GetBufferPointer(), _pByteCode, _uiByteLength ); // create the actual effect from the blob and start inspecting the constants used inside it.. #ifdef D3D10_RENDERER HRESULT hr = D3D10CreateEffectFromMemory( m_pEffectBuffer->GetBufferPointer(), m_pEffectBuffer->GetBufferSize(), 0, g_pD3DDevice, 0, &m_pD3DEffect ); #endif #ifdef D3D11_RENDERER HRESULT hr = D3DX11CreateEffectFromMemory( m_pEffectBuffer->GetBufferPointer(), m_pEffectBuffer->GetBufferSize(), 0, g_pD3DDevice, &m_pD3DEffect ); #endif if ( FAILED( hr ) ) { TSRPrintln( "Failed to create effect from memory" ); } BindConstants(); return true; }
HRESULT SoftParticles::LoadEffectFromFile( ID3D11Device* pd3dDevice, WCHAR* szFileName, ID3DX11Effect** ppEffect ) { HRESULT hr = S_OK; // Compile the effect file ID3DBlob* pBlobFX = NULL; ID3DBlob* pErrorBlob = NULL; hr = D3DX11CompileFromFile(szFileName, NULL, NULL, NULL, "fx_5_0", NULL, NULL, NULL, &pBlobFX, &pErrorBlob, NULL); if (FAILED(hr)) { char* err = (char*)pErrorBlob->GetBufferPointer(); SAFE_RELEASE(pErrorBlob); return hr; } // Create the effect hr = D3DX11CreateEffectFromMemory(pBlobFX->GetBufferPointer(), pBlobFX->GetBufferSize(), 0, pd3dDevice, ppEffect); if( FAILED(hr) ) { OutputDebugString( TEXT("Failed to load effect file.") ); return hr; } SAFE_RELEASE(pBlobFX); SAFE_RELEASE(pErrorBlob); return S_OK; }
HRESULT c_post_effect::compile_shader(const std::wstring& fx_file_name) { HRESULT hr; d3d11_blob_ptr effect_blob, error_blob; V(D3DX11CompileFromFile(fx_file_name.c_str(), // effect file name NULL, // defines NULL, // includes NULL, // function name "fx_5_0", // profile D3D10_SHADER_DEBUG, // compile flag NULL, // compile flag NULL, // thread pump &effect_blob, // effect buffer &error_blob, // error buffer NULL)); if (m_effect) m_effect = NULL; V(D3DX11CreateEffectFromMemory(effect_blob->GetBufferPointer(), // effect buffer pointer effect_blob->GetBufferSize(), 0, m_render_sys_context->get_d3d11_device(), &m_effect)); return S_OK; }
bool BlurShaderH::Initalize(ID3D11Device* pd3dDevice) { blurSizeH = (1.0f/400); ID3D10Blob* pBlob = NULL; ID3D10Blob* pErrorBlob = NULL; DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Il settaggio di questo flag permette di eseguire il debug diretto dello shader, senza // impattare in modo marcato sulle performance. dwShaderFlags |= D3DCOMPILE_DEBUG; #endif // Creazione effetto in g_pEffect, caricandolo dal file fx HRESULT hr = D3DX11CompileFromFile(L"Shaders/blur_h.fx", NULL, NULL,NULL, "fx_5_0", dwShaderFlags, 0, NULL, &pBlob, &pErrorBlob, NULL); if( FAILED( hr ) ) { MessageBox(NULL, L"Problema nel caricamento di blur_h.fx.", L"Error", MB_OK ); if (pErrorBlob) { MessageBoxA(0, (char*)pErrorBlob->GetBufferPointer(), 0, 0); } return false; } D3DX11CreateEffectFromMemory(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, pd3dDevice ,&m_Effect); // Trova la tecnica definita nel .fx - nota come un effetto.fx può avere diverse tecniche. m_Technique = m_Effect->GetTechniqueByName( "Blur_h" ); //Definiamo gli input da agganciare (bind) al vertex shader //In questo caso passeremo solo le informazioni sulla posizione con un vertex buffer D3D11_INPUT_ELEMENT_DESC layout[] = { {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Creiamo e configuriamo il layout di input D3DX11_PASS_DESC PassDesc; m_Technique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); hr = pd3dDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_VertexLayout ); if( FAILED( hr ) ) return false; m_Effect->GetVariableByName("blurSizeH")->AsScalar()->SetFloat(blurSizeH); return true; }
void RenderInterfaceDx11::initEffect() { //Debug DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif //Send blob to gather debug data ID3DBlob* pBlobEffect = NULL; ID3DBlob* pBlobErrors = NULL; HRESULT hr = 0; hr = D3DX11CompileFromFile( L"../shaders/effect.fx", NULL, NULL, "", "fx_5_0", shaderFlags, NULL, NULL, &pBlobEffect, &pBlobErrors, NULL ); // If compilation of the .fx-file fails. Present compilation errors to the user char* l_pError = NULL; if( FAILED(hr) ) { char msg[20000]; strcpy_s(msg, sizeof(msg), (char*)pBlobErrors->GetBufferPointer()); OutputDebugStringA(msg); // A as in ASCII. dx11 conversion MessageBoxA(GetDesktopWindow(), msg, "Effect compilation error", MB_OK | MB_ICONERROR); // A as in ASCII. dx11 conversion return; } if(FAILED(hr = D3DX11CreateEffectFromMemory( pBlobEffect->GetBufferPointer(), pBlobEffect->GetBufferSize(), shaderFlags, m_device, &m_effect ))) { MessageBoxA(0, "Cannot create effect from memory.", "D3DX11CreateEffectFromMemory error", MB_OK | MB_ICONERROR); // A as in ASCII. dx11 conversion return; } //hr = D3DX11CompileFromFile(L"../shaders/effect.fx", 0, 0, "fx_4_0", shaderFlags, 0, // m_device, 0, 0, &m_effect, &compilationErrors, 0); // the FAILED() macro modifies the hr, this copy is used with the HR() macro }
void Model::init_shader(ID3D11Device *pD3D11Device, HWND hWnd) { DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _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; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 // Read the D3DX effect file HRESULT hr = S_OK; D3DX11CompileEffectFromFile(L"model.fx", nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, 0, pD3D11Device, &m_pEffect, nullptr); #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN(DXUTCompileFromFile(L"Tutorial11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, 0, &pEffectBuffer)); hr = D3DX11CreateEffectFromMemory(pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &m_pEffect); SAFE_RELEASE(pEffectBuffer); if (FAILED(hr)) return hr; #endif m_pEffectTechnique = m_pEffect->GetTechniqueByName("ModelTech"); m_pWorld = m_pEffect->GetVariableByName("g_World")->AsMatrix(); m_pView = m_pEffect->GetVariableByName("g_View")->AsMatrix(); m_pProj = m_pEffect->GetVariableByName("g_Proj")->AsMatrix(); // Done with compiled shader. D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0} }; D3DX11_PASS_DESC passDesc; m_pEffectTechnique->GetPassByIndex(0)->GetDesc(&passDesc); pD3D11Device->CreateInputLayout(vertexDesc, 2, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &m_pInputLayout); }
void Renderable::createEffect(const char* filename, ID3D11Device* _dxDev, ID3DX11Effect** fx) { std::vector<char> compiledShader(0); // shaders if (!loadEffect(filename, compiledShader)) MessageBoxA(0, "Error Loading Effect", 0, 0); HRESULT hr = D3DX11CreateEffectFromMemory(&compiledShader[0], compiledShader.size(), 0, _dxDev, fx); if (FAILED(hr)) MessageBoxA(0, "Error creating FX", 0, 0); }
HRESULT Shader::Init( ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename, const D3D11_INPUT_ELEMENT_DESC* inputElementDesc, unsigned int numElements ) { this->zDevice = device; this->zDeviceContext = deviceContext; this->zNumElements = numElements; this->zFilename = filename; this->zInputElementDesc = (D3D11_INPUT_ELEMENT_DESC*)inputElementDesc; HRESULT hr = S_OK; ID3DBlob* mBlobEffect = NULL; ID3DBlob* mBlobErrors = NULL; DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined(DEBUG) || defined(_DEBUG) dwShaderFlags |= D3D10_SHADER_DEBUG; #endif D3DX11CompileFromFile(filename, NULL, NULL, "", "fx_5_0", dwShaderFlags, NULL, NULL, &mBlobEffect, &mBlobErrors, &hr); if (FAILED(hr)) { throw(ShaderException( __FILE__, __LINE__, __FUNCTION__, "Effect compilation error, file: " + zFilename)); } if (FAILED(hr = D3DX11CreateEffectFromMemory( mBlobEffect->GetBufferPointer(), mBlobEffect->GetBufferSize(), dwShaderFlags, this->zDevice, &this->zEffect))) { throw(ShaderException( __FILE__, __LINE__, __FUNCTION__, "Cannot create effect from memory, file: " + zFilename)); } SAFE_RELEASE(mBlobEffect); SAFE_RELEASE(mBlobErrors); return this->SetTechnique(0); }
JF::Effect::Effect(ID3D11Device* device, const std::wstring& filename) : mFX(0) { auto fin = JFAPI::FILE::Open(filename.c_str(), eFILE_OPERATION::READ); size_t size = JFAPI::FILE::GetLength(fin); std::vector<char> compiledShader(size); JFAPI::FILE::Read(fin, size, &compiledShader[0]); JFAPI::FILE::Close(fin); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, &mFX)); }
void Effects::InitAll(ID3D11Device* device) { ID3D10Blob* blob; ID3D10Blob *effectBytecode; ID3DX11Effect *effect; //D3DX11CompileEffectFromFile HR(D3DX11CompileFromFile(L"FX/Billboard.fx", NULL, NULL, "Light2Tex", "fx_5_0", 0, 0, 0, &effectBytecode, &blob, NULL)); HRESULT hr = D3DX11CreateEffectFromMemory(effectBytecode->GetBufferPointer(), effectBytecode->GetBufferSize(), 0, device, &effect); BasicFX = new BasicEffect(device, L"FX/UberShader.fxo"); SkyFX = new SkyEffect(device, L"FX/Sky.fxo"); BillboardFX = new BillboardEffect(device, L"FX/Billboard.fx", effect); //BillboardFX->mFX = effect; }
Effect::Effect(ID3D11Device* device, const std::wstring& filename) : mFX(0) { std::ifstream fin(filename, std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, &mFX)); }
void InitDirect3DApp::BuildEffect() { DWORD shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = 0; ID3D10Blob* compilationErrors = 0; HRESULT hr = D3DX11CompileFromFile(L"FX/color.fx", // Name of the .fx file 0, // Advanced option 0, // Advanced option 0, // Using the effects framework so set it to null "fx_5_0", // Shader version we are using shaderFlags, // Flags to specify how the code should be compiled 0, // Advanced effect compilation options 0, // Advanced option to compile the shader asynchronously &compiledShader, // Pointer to the compiled shader &compilationErrors, // Pointer to compilation errors if there are any 0); // Return error code for compiling asynchronously // Check for any compilation errors if (compilationErrors != 0) { MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0); ReleaseCOM(compilationErrors); } // Check for any other errors with compiling the shader if (FAILED(hr)) { DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } // Shader compiled successfully, time to create the effect HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &effect)); ReleaseCOM(compiledShader); // Get the technique location from the effect technique = effect->GetTechniqueByName("ColorTechnique"); // Get the World View Proj Matrix location from the effect worldViewProj = effect->GetVariableByName("WorldViewProj")->AsMatrix(); }
bool MyApp::createEffect(const char* filename, ID3DX11Effect** fx) { // Load and create the pre-compiled shader code std::vector<char> compiledShader(0); if (!loadEffects(filename, compiledShader)) { OutputDebugStringA("Error: effect file not loaded"); return false; } HRESULT hr = D3DX11CreateEffectFromMemory(&compiledShader[0], compiledShader.size(), 0, _dxDev, fx); if (FAILED(hr)) return false; return true; }
void BoxApp::BuildFX() { DWORD shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = 0; ID3D10Blob* compilationMsgs = 0; HRESULT hr = D3DX11CompileFromFile(L"FX/Lighting.fx", 0, 0, 0, "fx_5_0", shaderFlags, 0, 0, &compiledShader, &compilationMsgs, 0); // compilationMsgs can store errors or warnings. if( compilationMsgs != 0 ) { MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0); ReleaseCOM(compilationMsgs); } // Even if there are no compilationMsgs, check to make sure there were no other errors. if(FAILED(hr)) { DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &mFX)); // Done with compiled shader. ReleaseCOM(compiledShader); mTech = mFX->GetTechniqueByName("LightTech"); mTech2 = mFX->GetTechniqueByName("IllumTech"); mTechSkyBox = mFX->GetTechniqueByName("SkyBoxTech"); mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix(); mfxWorld = mFX->GetVariableByName("gWorld")->AsMatrix(); mfxWorldInvTranspose = mFX->GetVariableByName("gWorldInvTranspose")->AsMatrix(); mfxEyePosW = mFX->GetVariableByName("gEyePosW")->AsVector(); mfxDirLight = mFX->GetVariableByName("gDirLight"); mfxPointLight = mFX->GetVariableByName("gPointLight"); mfxPointLight2 = mFX->GetVariableByName("gPointLight2"); mfxSpotLight = mFX->GetVariableByName("gSpotLight"); mfxMaterial = mFX->GetVariableByName("gMaterial"); diffuseMap = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource(); }
Effect::Effect(ID3D11Device* device, const std::string& filename) : m_fx(nullptr) { std::ifstream fin(filename, std::ios::binary); OC_ASSERT(fin.good()); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, m_fx.GetAddressOf())); }
void Effect::CreateDXEffectFromFile(const string& filepath) { ID3D11Device* device = GraphicsManager::Get()->GetDevice(); U32 flags = 0; #ifdef _DEBUG flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_WARNINGS_ARE_ERRORS; #endif ID3D10Blob* effectBlob = nullptr; ID3D10Blob* error = nullptr; try { HRESULT hr; while(FAILED(D3DX11CompileFromFile(filepath.c_str(), nullptr, nullptr, nullptr, "fx_5_0", flags, 0, nullptr, &effectBlob, &error, &hr))) { if (error) { U8* begin = (U8*) error->GetBufferPointer(); U8* end = begin + error->GetBufferSize(); string message(begin, end); Logger::Get()->Log(message, Logger::Error); #ifdef _DEBUG I32 retVal = Logger::Get()->ShowMessage("Effect compilation error", message.c_str()); if (retVal != IDRETRY) { throw DXException(hr, message.c_str()); } #else throw Exception("DirectX: Cannot compile effect file '" + filepath + "'. Check log file for more details."); #endif } } _ASSERT(effectBlob); DXCall(D3DX11CreateEffectFromMemory(effectBlob->GetBufferPointer(), effectBlob->GetBufferSize(), 0, device, &dx11Effect)); ReleaseCOM(effectBlob); ReleaseCOM(error); } catch (const Exception& exception) { ReleaseCOM(effectBlob); ReleaseCOM(error); throw exception; } catch (...) { _ASSERT(false); } }
void Shape::LoadFX() { std::ifstream fin("fx/color.fxo", std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, mD3DDevice, &mFX)); mTech = mFX->GetTechniqueByName("ColorTech"); mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix(); }
bool CScene::LoadTechnique() { HRESULT hr; ID3DBlob* pEffectBlob; ID3DBlob* pErrors; // This strangely typed variable collects any errors when compiling the effect file DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; // These "flags" are used to set the compiler options DWORD flags2 = D3DCOMPILE_DEBUG | D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY; //Compile Technique file hr = D3DX11CompileFromFile( L"./Shaders/Technique.fx", NULL, NULL, NULL, "fx_5_0", dwShaderFlags, 0, NULL, &pEffectBlob, &pErrors, &hr ); if( FAILED( hr ) ) { if( pErrors != 0 ) MessageBox( NULL, CA2CT( reinterpret_cast<char*>(pErrors->GetBufferPointer()) ), L"Error", MB_OK ); // Compiler error: display error message else MessageBox( NULL, L"Error loading Vertex Shader FX file. Ensure your FX file is in the same folder as this executable.", L"Error", MB_OK ); // No error message - probably file not found return false; } uint32_t SHADER_DEBUG = 1; uint32_t SHADER_RELEASE = 0; LPVOID bufferPointer = pEffectBlob->GetBufferPointer(); SIZE_T bufferSize = pEffectBlob->GetBufferSize(); hr = D3DX11CreateEffectFromMemory( bufferPointer, bufferSize, 0, mpd3dDevice, &Effect ); if( FAILED( hr ) ) { MessageBox( NULL, L"Error creating effects", L"Error", MB_OK ); return false; } return true; }
HRESULT MyCreateEffectFromFile(TCHAR *fName, DWORD dwShaderFlags, ID3D11Device *pDevice, ID3DX11Effect **pEffect) { char fNameChar[1024]; CharToOem(fName, fNameChar); ID3DXBuffer *pShader; HRESULT hr; ID3DXBuffer *pErrors; if (FAILED(D3DXCompileShaderFromFileA(fNameChar, NULL, NULL, NULL, "fx_5_0", 0, &pShader, &pErrors, NULL))) { MessageBoxA(NULL, (char *)pErrors->GetBufferPointer(), "Error", MB_OK); return E_FAIL; } V_RETURN(D3DX11CreateEffectFromMemory(pShader->GetBufferPointer(), pShader->GetBufferSize(), dwShaderFlags, pDevice, pEffect)); SAFE_RELEASE(pErrors); SAFE_RELEASE(pShader); return S_OK; }
int EffectSystemD3D11::CreateEffect(const char *path) { // 先把char转成wchar_t int wchar_size = MultiByteToWideChar(CP_ACP, 0, path, -1, nullptr, 0); wchar_t *wchar_path = new wchar_t[wchar_size]; ZeroMemory(wchar_path, wchar_size); MultiByteToWideChar(CP_ACP, 0, path, -1, wchar_path, wchar_size); // compile DWORD flags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef PE_DEBUG_MODE flags |= D3DCOMPILE_DEBUG; #endif ID3DBlob *effectBlob = nullptr; ID3DBlob *errorBlob = nullptr; // dx11只能使用fx_5_0 HRESULT hr = D3DCompileFromFile(wchar_path, nullptr, nullptr, nullptr, "fx_5_0", flags, 0, &effectBlob, &errorBlob); if (FAILED(hr)) { if (errorBlob != nullptr){ LogSystem::GetInstance().Log("编译%s出错, D3D Error:%s", path, (char*)errorBlob->GetBufferPointer()); errorBlob->Release(); } return -1; } if (errorBlob != nullptr){ errorBlob->Release(); } // create effect ID3DX11Effect *effect; hr = D3DX11CreateEffectFromMemory(effectBlob->GetBufferPointer(), effectBlob->GetBufferSize(), 0, mDevice, &effect); if (FAILED(hr)) { LogSystem::GetInstance().Log("创建%s出错", path); return -1; } mEffects.push_back(effect); return (mEffects.size() - 1); }
HRESULT DecorationSystem::LoadShaders() { DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) dwShaderFlags |= D3D10_SHADER_DEBUG; // dwShaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION; #endif CComPtr<ID3DBlob> pShader; CompileShaderFromFile("Data/Shaders/Grass.fx", NULL, NULL,"fx_5_0", &pShader); D3DX11CreateEffectFromMemory(pShader->GetBufferPointer(), pShader->GetBufferSize(), dwShaderFlags, D3D11Dev(), &m_pGrassEffect); pShader.Detach(); GET_EFFECT_VAR( m_pGrassEffect, "World", MATRIX, m_pfxWorld); GET_EFFECT_VAR( m_pGrassEffect, "View", MATRIX, m_pfxView); GET_EFFECT_VAR( m_pGrassEffect, "Proj", MATRIX, m_pfxProj); GET_EFFECT_VAR( m_pGrassEffect, "g_fTime", SCALAR, m_fTimeVar); GET_EFFECT_VAR( m_pGrassEffect, "txDiffuse", SHADER_RESOURCE, m_pGrassSRV); return S_OK; }
HRESULT LoadEffectFromFile( ID3D11Device* pd3dDevice, const char* szFileName, ID3DX11Effect** ppEffect, const D3D10_SHADER_MACRO *pDefines /*= NULL*/, LPCSTR pProfile/*="fx_5_0"*/ ) { HRESULT hr = S_OK; //DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY; // 동시 적용못함. DWORD dwShaderFlags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_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 |= D3D10_SHADER_DEBUG | D3D10_SHADER_SKIP_OPTIMIZATION; #endif // Compile the effect file ID3DBlob* pBlobFX = NULL; ID3DBlob* pErrorBlob = NULL; hr = D3DX11CompileFromFile(szFileName, pDefines, NULL, NULL, pProfile, dwShaderFlags, NULL, NULL, &pBlobFX, &pErrorBlob, NULL); if (FAILED(hr)) { if(pErrorBlob != NULL) OutputDebugStringA((char*)pErrorBlob->GetBufferPointer()); SAFE_RELEASE(pErrorBlob); return hr; } // Create the effect hr = D3DX11CreateEffectFromMemory(pBlobFX->GetBufferPointer(), pBlobFX->GetBufferSize(), 0, pd3dDevice, ppEffect); if( FAILED(hr) ) { OutputDebugString( TEXT("Failed to load effect file.") ); return hr; } SAFE_RELEASE(pBlobFX); SAFE_RELEASE(pErrorBlob); return S_OK; }
void D3DRenderer::InitEffects() { ID3D10Blob* compiledShader = 0; ID3D10Blob* compilationMsgs = 0; HRESULT hr = D3DX11CompileFromFile(L"Effects/color.fx", 0, 0, 0, "fx_5_0", 0, 0, 0, &compiledShader, &compilationMsgs, 0); if (compilationMsgs !=0) { MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0 ,0); ReleaseCOM(compilationMsgs); } if (FAILED(hr)) { DXTrace(__FILE__,(DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true); } HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &mEffect)); ReleaseCOM(compiledShader); mETech = mEffect->GetTechniqueByName("ColorTech"); }
HRESULT MyCreateEffectFromCompiledFile(TCHAR *fName, DWORD dwShaderFlags, ID3D11Device *pDevice, ID3DX11Effect **pEffect) { HRESULT hr; char filename[MAX_PATH]; wcstombs_s(NULL,filename,MAX_PATH,fName,MAX_PATH); FILE * pFile; errno_t error; if((error = fopen_s(&pFile,filename,"rb")) != 0) { MessageBoxA(NULL,"Could not find compiled effect file","Error",MB_OK); return S_FALSE; } // obtain file size: fseek (pFile , 0 , SEEK_END); int lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: char* buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) return S_FALSE; // copy the file into the buffer: size_t result = fread (buffer,sizeof(char),lSize,pFile); if ((int)result != lSize) return S_FALSE; //create the effect V_RETURN(D3DX11CreateEffectFromMemory((void*) buffer, result*sizeof(char) , dwShaderFlags, pDevice, pEffect)); fclose (pFile); free (buffer); return S_OK; }
HRESULT Shader::Init(Device3D* device3D, const char* filename, const D3D11_INPUT_ELEMENT_DESC* inputElementDesc, unsigned int numElements) { mDevice3D = device3D; HRESULT hr = S_OK; ID3DBlob* pBlobEffect = NULL; ID3DBlob* pBlobErrors = NULL; DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined(DEBUG) || defined(_DEBUG) dwShaderFlags |= D3D10_SHADER_DEBUG; #endif hr = D3DX11CompileFromFile( filename, NULL, NULL, "", "fx_5_0", dwShaderFlags, NULL, NULL, &pBlobEffect, &pBlobErrors, NULL ); char* l_pError = NULL; if( FAILED(hr) ) { char msg[20000]; strcpy_s(msg, sizeof(msg), (char*)pBlobErrors->GetBufferPointer()); OutputDebugString(msg); MessageBox(GetDesktopWindow(), msg, "Effect compilation error", MB_OK | MB_ICONERROR); return hr; } if(FAILED(hr = D3DX11CreateEffectFromMemory( pBlobEffect->GetBufferPointer(), pBlobEffect->GetBufferSize(), dwShaderFlags, mDevice3D->GetDevice(), &m_pEffect ))) { MessageBox(0, "Cannot create effect from memory.", "D3DX11CreateEffectFromMemory error", MB_OK | MB_ICONERROR); return hr; } m_pTechnique = m_pEffect->GetTechniqueByIndex(0); if(inputElementDesc) { D3DX11_PASS_DESC PassDesc; m_pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); if(FAILED(hr = mDevice3D->GetDevice()->CreateInputLayout( inputElementDesc, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pInputLayout ))) { MessageBox(0, "Cannot create input layout.", "CreateInputLayout error", MB_OK | MB_ICONERROR); return hr; } } return hr; }