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 Shader::AddShader(const String& name, const String& filepath) { if (FAILED(D3DX11CompileEffectFromFile( filepath, NULL, NULL, 0U, 0U, Window::Device(), &Access(name).effect, NULL))) { throw std::exception("シェーダーファイルのコンパイルに失敗しました"); } }
void EffectHelper::init(ID3D11Device *pD3D11Device, std::string effectFile) { 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"tutorial11.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("Render"); std::vector<D3D11_INPUT_ELEMENT_DESC> vInputLayoutDesc; D3D11_INPUT_ELEMENT_DESC inputLayoutDesc; inputLayoutDesc.SemanticName = "POSITION"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 0; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); inputLayoutDesc.SemanticName = "NORMAL"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 12; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); inputLayoutDesc.SemanticName = "TEXCOORD"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 24; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); // Create the input layout D3DX11_PASS_DESC PassDesc; auto numElements = vInputLayoutDesc.size(); m_pEffectTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); pD3D11Device->CreateInputLayout(&vInputLayoutDesc[0], numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pInputLayout); }