HRESULT Technique::setDomainShader(LPCWSTR filename, LPCSTR entryPoint, LPCSTR shaderModel, ID3D11Device* g_pd3dDevice){ HRESULT hr = S_OK; 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 ID3DBlob* pDSBlob = nullptr; ID3DBlob* pErrorBlob = nullptr; hr = D3DCompileFromFile(filename, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint, shaderModel, dwShaderFlags, 0, &pDSBlob, &pErrorBlob); if (FAILED(hr)) { if (pErrorBlob) { std::ofstream fout("Shader_Debug.txt"); fout << reinterpret_cast<const char*>(pErrorBlob->GetBufferPointer()); OutputDebugStringA(reinterpret_cast<const char*>(pErrorBlob->GetBufferPointer())); pErrorBlob->Release(); } } if (pErrorBlob) pErrorBlob->Release(); hr = g_pd3dDevice->CreateDomainShader(pDSBlob->GetBufferPointer(), pDSBlob->GetBufferSize(), nullptr, &g_pDomainShader); if (FAILED(hr)) { pDSBlob->Release(); } pDSBlob->Release(); return hr; }
void D3D::CreateShaders() { //create vertex shader ID3DBlob* pVS = nullptr; D3DCompileFromFile(L"VertexShader.hlsl", NULL, NULL, "main", "vs_5_0", 0, NULL, &pVS, NULL); device->CreateVertexShader(pVS->GetBufferPointer(), pVS->GetBufferSize(), nullptr, &vertexShader); //create input layout (verified using vertex shader) D3D11_INPUT_ELEMENT_DESC inputDesc[] = { { "SV_POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 2, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; device->CreateInputLayout(inputDesc, 3, pVS->GetBufferPointer(), pVS->GetBufferSize(), &inputLayout); pVS->Release(); //create pixel shader ID3DBlob* pPS = nullptr; D3DCompileFromFile(L"PixelShader.hlsl", NULL, NULL, "main", "ps_5_0", 0, NULL, &pPS, NULL); device->CreatePixelShader(pPS->GetBufferPointer(), pPS->GetBufferSize(), nullptr, &pixelShader); pPS->Release(); }
HRESULT CompileComputeShader(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ ID3D11Device* device, _Outptr_ ID3DBlob** blob) { if (!srcFile || !entryPoint || !device || !blob) return E_INVALIDARG; *blob = nullptr; UINT flags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) flags |= D3DCOMPILE_DEBUG; #endif const D3D_SHADER_MACRO defines[] = { "EXAMPLE_DEFINE", "1", NULL, NULL }; ID3DBlob* shaderBlob = nullptr; ID3DBlob* errorBlob = nullptr; HRESULT hr = D3DCompileFromFile(srcFile, defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint, "cs_5_0", flags, 0, &shaderBlob, &errorBlob); if (FAILED(hr)) { if (errorBlob) { OutputDebugStringA((char*)errorBlob->GetBufferPointer()); errorBlob->Release(); } if (shaderBlob) shaderBlob->Release(); return hr; } *blob = shaderBlob; return hr; }
int TechniqueHLSL::insertHullShader(std::string shaderDir, std::string shaderName) { if(shaderDir.empty() || shaderName.empty() ) { return -1; } std::string shaderPath = shaderDir + ":" + shaderName; for(unsigned int i=0; i < hullShaders->size(); i++) { if(shaderPath.size() == hullShaders->at(i).name.size()) { if( hullShaders->at(i).name == shaderPath ) { return i; } } } ID3DBlob* shaderBlob = NULL; HRESULT hr; GraphicsDX11* g = GraphicsDX11::getInstance(); hr = g->compileShader(shaderDir.c_str(),shaderName.c_str(),"hs_5_0",&shaderBlob ); if(FAILED(hr)) { std::string text = "Failed to compile " + shaderName + " ( " + shaderDir + " )"; MessageBox( NULL, text.c_str(),"Shader Error",MB_OK); PostQuitMessage(0); return -1; } ID3D11HullShader* shader; hr = device->CreateHullShader(shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), NULL, &shader); if( FAILED(hr) ) { hr = g->compileShader(shaderDir.c_str(),shaderName.c_str(),"hs_4_0",&shaderBlob ); if(FAILED(hr)) { std::string text = "Failed to compile " + shaderName + " ( " + shaderDir + " )"; MessageBox( NULL, text.c_str(),"Shader Error",MB_OK); PostQuitMessage(0); return -1; } hr = device->CreateHullShader(shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), NULL, &shader); if(FAILED(hr)) { std::string text = "Failed to create " + shaderName + " ( " + shaderDir + " )"; MessageBox( NULL, text.c_str(),"Shader Error",MB_OK); PostQuitMessage(0); return -1; } std::cout << "Failed to create " << shaderPath << " using shader model 5. Reverting to shader model 4." << std::endl; } shaderBlob->Release(); HSstruct hs; hs.name = shaderPath; hs.hullShader = shader; hullShaders->push_back(hs); return hullShaders->size()-1; }
void D3D11Mesh::LoadAndBuildMeshFromOBJFile(LPCWSTR filename, D3D11ShaderProgram* shader) { Triangle* mesh; m_numtriangles = importOBJMesh(filename, &mesh); m_shader = shader; unsigned int offset = sizeof(Vector3); D3D11_INPUT_ELEMENT_DESC vertlayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offset, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 2*offset, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; ID3D11Device* pDevice = shader->GetDevice(); ID3DBlob* pVSBlob = shader->GetVSBlob(); HRESULT hr = pDevice->CreateInputLayout(vertlayout, ARRAYSIZE(vertlayout), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &m_vertexLayout); D3D11_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(Vertex)*m_numtriangles*3; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA initData; ZeroMemory(&initData, sizeof(initData)); initData.pSysMem = &(mesh[0].m_vertices[0].m_position[0]); pDevice->CreateBuffer(&bd, &initData, &m_vertexBuffer); delete [] mesh; }
_Use_decl_annotations_ HRESULT D3DX11CompileEffectFromMemory( LPCVOID pData, SIZE_T DataLength, LPCSTR srcName, const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, UINT HLSLFlags, UINT FXFlags, ID3D11Device *pDevice, ID3DX11Effect **ppEffect, ID3DBlob **ppErrors ) { if ( !pData || !DataLength || !pDevice || !ppEffect ) return E_INVALIDARG; if ( FXFlags & D3DCOMPILE_EFFECT_CHILD_EFFECT ) { DPF(0, "Effect pools (i.e. D3DCOMPILE_EFFECT_CHILD_EFFECT) not supported" ); return E_NOTIMPL; } ID3DBlob *blob = nullptr; HRESULT hr = D3DCompile( pData, DataLength, srcName, pDefines, pInclude, "", "fx_5_0", HLSLFlags, FXFlags, &blob, ppErrors ); if ( FAILED(hr) ) { DPF(0, "D3DCompile of fx_5_0 profile failed: %08X", hr ); SAFE_RELEASE( blob ); return hr; } hr = S_OK; VN( *ppEffect = new CEffect( FXFlags & D3DX11_EFFECT_RUNTIME_VALID_FLAGS ) ); VH( ((CEffect*)(*ppEffect))->LoadEffect(blob->GetBufferPointer(), static_cast<uint32_t>( blob->GetBufferSize() ) ) ); SAFE_RELEASE( blob ); VH( ((CEffect*)(*ppEffect))->BindToDevice(pDevice, (srcName) ? srcName : "D3DX11Effect" ) ); lExit: if (FAILED(hr)) { SAFE_RELEASE(*ppEffect); } return hr; }
bool scPixelShader::LoadImpl( ID3D11Device* device ) { // ·ÀÖ¹ÄÚ´æй¶ if (mPixelShader) mPixelShader->Release(); ID3DBlob* buffer = 0; if (!Compile(&buffer)) return false; HRESULT hr = device->CreatePixelShader(buffer->GetBufferPointer(), buffer->GetBufferSize(), 0, &mPixelShader); if (FAILED(hr)) { if (buffer) buffer->Release(); scErrMsg("!!!Error creating pixel shader."); return false; } buffer->Release(); return true; }
bool CSGravity::initialize(ID3D11Device* device, Particle* particles, int maxParticles){ ID3DBlob* csBuffer; if(!D3D11Init::CompileD3DShader(_csFilePath, "CS_Main", "cs_5_0", &csBuffer)){ MessageBox(NULL, "Could not compile compute shader!", "Error", 0); if(csBuffer){ csBuffer->Release(); } return false; } HRESULT result = device->CreateComputeShader(csBuffer->GetBufferPointer(), csBuffer->GetBufferSize(), 0, &_computeShader); if(FAILED(result)){ MessageBox(NULL, "Could not create compute shader!", "Error", 0); return false; } calculateThreadGroups(maxParticles); return createInputBuffer(device, particles, maxParticles) && createConstantBuffers(device, maxParticles); }
// ------------------------------------------------------------------------------------------------ BasicShader::BasicShader(ID3D11Device* device) : Shader( Shaders::BasicShader ) { // create cbuffers. m_cbPerFrame.Construct(device); m_cbPerObject.Construct(device); // compile shaders ID3DBlob* vsBlob = CompileShaderFromResource(L"BasicShader.hlsl", "VS","vs_4_0", NULL); ID3DBlob* psBlob = CompileShaderFromResource(L"BasicShader.hlsl", "PS","ps_4_0", NULL); assert(vsBlob && psBlob); m_vsShader = GpuResourceFactory::CreateVertexShader(vsBlob); m_psShader = GpuResourceFactory::CreatePixelShader(psBlob); assert(m_vsShader && m_psShader); // create input layout m_vertexLayout = GpuResourceFactory::CreateInputLayout(vsBlob, VertexFormat::VF_P); assert(m_vertexLayout); // release the blobs vsBlob->Release(); psBlob->Release(); }
void Effect::ReadShaderFile(std::wstring filename, ID3DBlob **blob, char* target, char* entryPoint) { HRESULT hr; ID3DBlob* errMsg; hr = D3DCompileFromFile( filename.c_str(), nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint, target, D3DCOMPILE_DEBUG, 0, blob, &errMsg ); #ifdef DEBUG if (errMsg) { Debug::Log((char*)errMsg->GetBufferPointer()); errMsg->Release(); } #endif HR(hr); }
bool TextureShader::CompileShaders() { HRESULT hr; ID3DBlob *errorsBlob = nullptr; m_vsBlob = nullptr; m_psBlob = nullptr; // compile vertex shader from file hr = D3DCompileFromFile(L"simple.vs", nullptr, nullptr, "TextureVertexShader", "vs_4_0", D3DCOMPILE_DEBUG, 0, &m_vsBlob, &errorsBlob); if (FAILED(hr)) { DebugOut("Vertex Shader compilation failed!\n"); if (errorsBlob != nullptr) { DebugOut("ID3DBlob error: %s\n", (char *)errorsBlob->GetBufferPointer()); errorsBlob->Release(); errorsBlob = nullptr; } return false; } // compile pixel shader from file hr = D3DCompileFromFile(L"simple.ps", nullptr, nullptr, "TexturePixelShader", "ps_4_0", D3DCOMPILE_DEBUG, 0, &m_psBlob, &errorsBlob); if (FAILED(hr)) { DebugOut("Pixel shader compilation failed!\n"); if (errorsBlob != nullptr) { DebugOut("ID3DBlob error: %s\n", (char *)errorsBlob->GetBufferPointer()); errorsBlob->Release(); errorsBlob = nullptr; } return false; } return true; };
static HRESULT CompileShader( PCWCHAR file, LPCSTR entryPoint, LPCSTR shaderModel, ID3DBlob** ppBlob ) { HRESULT hr = S_OK; DWORD dwFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG dwFlags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3DBlob* pErr = nullptr; hr = D3DCompileFromFile( file, nullptr, nullptr, entryPoint, shaderModel, dwFlags, 0, ppBlob, &pErr ); if ( FAILED( hr ) ) { if ( pErr ) { OutputDebugStringA( reinterpret_cast<const char*>( pErr->GetBufferPointer() ) ); pErr->Release(); } return hr; } if ( pErr ) pErr->Release(); return S_OK; }
//--------------------------------------------------------------------------- ShadowMapGen::ShadowMapGen( ID3D11Device* device ) : m_rc( NULL ), m_pSurface( NULL ), m_vertexShader( NULL ), m_layoutP( NULL ), m_rasterState(NULL) { //GpuResourceFactory // compile and create vertex shader. ID3DBlob* vsBlob = CompileShaderFromResource(L"ShadowMapGen.hlsl","VSMain","vs_4_0", NULL); assert(vsBlob); m_vertexShader = GpuResourceFactory::CreateVertexShader(vsBlob); assert(m_vertexShader); // create input layout m_layoutP = GpuResourceFactory::CreateInputLayout(vsBlob, VertexFormat::VF_P); assert(m_layoutP); vsBlob->Release(); // create raster state. D3D11_RASTERIZER_DESC rsDcr = RSCache::Inst()->GetDefaultRsDcr(); rsDcr.AntialiasedLineEnable = FALSE; rsDcr.MultisampleEnable = FALSE; rsDcr.DepthBias = 25000; rsDcr.SlopeScaledDepthBias = 1.0f; rsDcr.DepthBiasClamp = 0.f; HRESULT hr = device->CreateRasterizerState( &rsDcr, &m_rasterState ); Logger::IsFailureLog(hr, L"CreateRasterizerState"); assert(m_rasterState); // create constant buffers. m_cbPerFrame.Construct(device); m_cbPerDraw.Construct(device); }
bool Shader::createShader(wchar_t* namevs, wchar_t* nameps, ID3D11Device* pDevice) { HRESULT hr = S_OK; ID3DBlob* vertexShaderBuffer = nullptr; hr = m_compileShaderFromFile(namevs, "VS", "vs_4_0", &vertexShaderBuffer); if(FAILED(hr)) { Log::get()->err("Не удалось загрузить вершинный шейдер %ls", namevs); return false; } ID3DBlob* pixelShaderBuffer = nullptr; hr = m_compileShaderFromFile(nameps, "PS", "ps_4_0", &pixelShaderBuffer); if(FAILED(hr)) { Log::get()->err("Не удалось загрузить пиксельный шейдер %ls", nameps); return false; } hr = pDevice->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_pVertexShader); if(FAILED(hr)) { Log::get()->err("Не удалось создать вершинный шейдер"); return false; } hr = pDevice->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pPixelShader); if(FAILED(hr)) { Log::get()->err("Не удалось создать пиксельный шейдер"); return false; } hr = pDevice->CreateInputLayout(m_pLayoutFormat, m_numLayout, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_pLayout); if(FAILED(hr)) { Log::get()->err("Не удалось создать формат ввода"); return false; } _DELETE_ARRAY(m_pLayoutFormat); _RELEASE(vertexShaderBuffer); _RELEASE(pixelShaderBuffer); return true; }
//**************************************************************************// // Compile the shader file. These files aren't pre-compiled (well, not // // here, and are compiled on he fly). // //**************************************************************************// HRESULT VertexAndPixelShaders::CompileShaderFromFile(WCHAR* szFileName, // File Name LPCSTR szEntryPoint, // Name of shader LPCSTR szShaderModel, // Shader model ID3DBlob** ppBlobOut) // Blob returned { HRESULT hr = S_OK; // find the file WCHAR str[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, szFileName)); 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 ID3DBlob* pErrorBlob; hr = D3DX11CompileFromFile(str, NULL, NULL, szEntryPoint, szShaderModel, dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL); if (FAILED(hr)) { WCHAR errorCharsW[200]; if (pErrorBlob != NULL) { charStrToWideChar(errorCharsW, (char *)pErrorBlob->GetBufferPointer()); MessageBox(0, errorCharsW, L"Error", 0); } if (pErrorBlob) pErrorBlob->Release(); return hr; } SAFE_RELEASE(pErrorBlob); return S_OK; }
//-------------------------------------------------------------------------------------- // Compile the shader //-------------------------------------------------------------------------------------- HRESULT CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut) { HRESULT hr = S_OK; 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 ID3DBlob* pErrorBlob; hr = D3DCompileFromFile(szFileName, NULL, NULL, szEntryPoint, szShaderModel, dwShaderFlags, 0, ppBlobOut, &pErrorBlob); if (FAILED(hr)) { if (pErrorBlob != NULL) OutputDebugStringA((char*)pErrorBlob->GetBufferPointer()); return hr; } return S_OK; }
ID3DX11Effect* LoadEffect(ID3D11Device* device, wchar_t* filename) { HRESULT hr; ID3DX11Effect* effect; ID3DBlob* errors = 0; hr = LoadEffectFromFile(device,filename,&effect); if( FAILED( hr ) ) { if(0 != errors) MessageBoxA ( 0, (LPCSTR)errors->GetBufferPointer(), "Error", MB_OK ); else MessageBoxA ( 0, "error from D3DX11CreateEffectFromFile == 0" , "Error", MB_OK ); SAFE_RELEASE(errors); exit(0); } else if( 0 != errors) { OutputDebugStringA((LPCSTR)errors->GetBufferPointer()); SAFE_RELEASE(errors); } return effect; }
D3D12RootSignature::D3D12RootSignature(_In_ Device& DeviceObj) { D3D12_ROOT_SIGNATURE_DESC RootSignatureDesc; RootSignatureDesc.NumParameters = 0; RootSignatureDesc.pParameters = nullptr; RootSignatureDesc.NumStaticSamplers = 0; RootSignatureDesc.pStaticSamplers = nullptr; RootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; ID3DBlob* RootSignatureBlob = nullptr; ID3DBlob* ErrorBlob = nullptr; HRESULT hr = D3D12SerializeRootSignature(&RootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &RootSignatureBlob, &ErrorBlob); ETERNAL_ASSERT(hr == S_OK); hr = static_cast<D3D12Device&>(DeviceObj).GetD3D12Device()->CreateRootSignature( DeviceObj.GetDeviceMask(), RootSignatureBlob->GetBufferPointer(), RootSignatureBlob->GetBufferSize(), __uuidof(ID3D12RootSignature), (void**)&_RootSignature ); ETERNAL_ASSERT(hr == S_OK); }
void PixelShader::Initialize(GraphicsSystem& gs, const wchar_t* pFileName, const char* pEntryPoint, const char* pPixelShaderModel) { DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if 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. shaderFlags |= D3DCOMPILE_DEBUG; #endif ID3DBlob* pShaderBlob = nullptr; ID3DBlob* pErrorBlob = nullptr; HRESULT hr = D3DCompileFromFile(pFileName, nullptr, nullptr, pEntryPoint, pPixelShaderModel, shaderFlags, 0, &pShaderBlob, &pErrorBlob); if (FAILED(hr) && pErrorBlob != nullptr) { OutputDebugStringA((char*)pErrorBlob->GetBufferPointer()); } SafeRelease(pErrorBlob); // Create pixel buffer hr = gs.GetDevice()->CreatePixelShader(pShaderBlob->GetBufferPointer(), pShaderBlob->GetBufferSize(), nullptr, &mpPixelShader); SafeRelease(pShaderBlob); }
HRESULT Sky::createShaderFromFile(const std::wstring& shaderPath, ID3D11Device* device, const bool reload) { HRESULT hr; releaseShader(); if (reload) { ID3DBlob* blob; if (D3DX11CompileEffectFromFile(shaderPath.c_str(), nullptr, nullptr, D3DCOMPILE_DEBUG, 0, device, &s_effect, &blob) != S_OK) { char* buffer = reinterpret_cast<char*>(blob->GetBufferPointer()); OutputDebugStringA(buffer); return E_FAIL; } } else { V_RETURN(D3DX11CreateEffectFromFile(shaderPath.c_str(), 0, device, &s_effect)); } s_worldViewProj = s_effect->GetVariableByName("g_mWorldViewProj")->AsMatrix(); // One float3, used as postion and cubetexture coordinate D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA } }; D3DX11_PASS_DESC pd; s_effect->GetTechniqueByIndex(0)->GetPassByIndex(0)->GetDesc(&pd); V_RETURN(device->CreateInputLayout(layout, sizeof(layout) / sizeof(D3D11_INPUT_ELEMENT_DESC), pd.pIAInputSignature, pd.IAInputSignatureSize, &s_inputLayout)); return S_OK; }
ShaderClass* LoadShader(ID3D11Device* d3dDevice, const std::wstring& fileName, const std::string& entryPoint, const std::string& _profile) { ID3DBlob* pShaderBlob = nullptr; ID3DBlob* pErrorBlob = nullptr; string profile = _profile; if (profile == "latest") profile = GetLatestProfile<ShaderClass>(); UINT flags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG flags |= D3DCOMPILE_DEBUG; #endif // No macros used HRESULT hResult = D3DCompileFromFile(fileName, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint.c_str(), profile.c_str(), flags, 0, *pShaderBlob, *pErrorBlob); if (FAILED(hResult)) { if (pErrorBlob) { string error = (char *)pErrorBlob->GetBufferPointer(); OutputDebugStringA(error.c_str()); SafeRelease(pErrorBlob); SafeRelease(pShaderBlob); } return nullptr; } ShaderClass pShader = CreateShader<ShaderClass>(d3dDevice, pShaderBlob, nullptr); SafeRelease(pShaderBlob); SafeRelease(pErrorBlob); return pShader; }
bool ShapesDemo::CompileShader(LPWSTR filepath, LPSTR entry, LPSTR model, ID3DBlob** buffer){ DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined(DEBUG) || defined(_DEBUG) shaderFlags |= D3DCOMPILE_DEBUG; #endif ID3DBlob* errorBuffer = 0; HRESULT result = D3DX11CompileFromFile(filepath, 0, 0, entry, model, shaderFlags, 0, 0, buffer, &errorBuffer, 0); if(FAILED(result)){ if( errorBuffer != 0 ) { OutputDebugStringA( ( char* )errorBuffer->GetBufferPointer( ) ); errorBuffer->Release( ); } return false; } if( errorBuffer != 0 ) errorBuffer->Release( ); return true; }
bool CEffectShader::LoadShader(const std::string &Filename, const std::string &EntryPoint, const std::string &ShaderModel, ID3DBlob **BlobOut) { HRESULT hr = S_OK; DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) dwShaderFlags |= D3DCOMPILE_DEBUG; #endif ID3DBlob* pErrorBlob; hr = D3DX11CompileFromFile(Filename.c_str(), m_ShaderMacros, NULL, EntryPoint.c_str(), ShaderModel.c_str(), dwShaderFlags, 0, NULL, BlobOut, &pErrorBlob, NULL); if (FAILED(hr)) { if (pErrorBlob != NULL) OutputDebugStringA((char*)pErrorBlob->GetBufferPointer()); if (pErrorBlob) pErrorBlob->Release(); return false; } if (pErrorBlob) pErrorBlob->Release(); return true; }
//-------------------------------------------------------------------------------------- // Вспомогательная функция для компиляции шейдеров в D3DX11 //-------------------------------------------------------------------------------------- HRESULT CompileShaderFromFile(LPCSTR szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut) { HRESULT hr = S_OK; DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; ID3DBlob* pErrorBlob; hr = D3DX11CompileFromFile(szFileName, NULL, NULL, szEntryPoint, szShaderModel, dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL); if(FAILED(hr)) { if(pErrorBlob != NULL) OutputDebugStringA((char*)pErrorBlob->GetBufferPointer()); if(pErrorBlob) pErrorBlob->Release(); return hr; } if(pErrorBlob) pErrorBlob->Release(); return S_OK; }
HRESULT CompileShaderFromFile(std::wstring fileName, char* entryPoint, char* shaderModel, ID3DBlob*& blob) { HRESULT hr = S_OK; DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG dwShaderFlags |= D3DCOMPILE_DEBUG; #else dwShaderFlags |= D3DCOMPILE_OPTIMIZATION_LEVEL3; #endif ID3DBlob* errorBlob = nullptr; hr = D3DCompileFromFile( fileName.c_str(), nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint, shaderModel, dwShaderFlags, 0, &blob, &errorBlob ); if (FAILED(hr)) { if (errorBlob != nullptr) { OutputDebugStringA(static_cast<char*>(errorBlob->GetBufferPointer())); } } if (errorBlob != nullptr) { errorBlob->Release(); } return hr; }
int Load() { D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; HRESULT res; ID3DBlob *vsBuffer = nullptr; if (!dx4s::utils::ShaderCompile("shader.fx", "VS_Main", "vs_4_0", &vsBuffer)) return -1; res = d3dDevice->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), 0, &VS); if (FAILED(res)) { if (vsBuffer) vsBuffer->Release(); return -1; } res = d3dDevice->CreateInputLayout(inputLayoutDesc, ARRAYSIZE(inputLayoutDesc), vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &inputLayout); if (FAILED(res)) return -1; ID3DBlob *psBuffer = nullptr; if (!dx4s::utils::ShaderCompile("shader.fx", "PS_Main", "ps_4_0", &psBuffer)) return -1; res = d3dDevice->CreatePixelShader(psBuffer->GetBufferPointer(), psBuffer->GetBufferSize(), 0, &PS); if (FAILED(res)) return -1; XMFLOAT3 vertexPos[] = { XMFLOAT3(0.5f, 0.5f, 0.5f), XMFLOAT3(0.5f, -0.5f, 0.5f), XMFLOAT3(-0.5f, -0.5f, 0.5f), }; D3D11_BUFFER_DESC BufferDesc = { 0 }; BufferDesc.ByteWidth = sizeof(XMFLOAT3) * 3; BufferDesc.Usage = D3D11_USAGE_DEFAULT; BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA subData = { 0 }; subData.pSysMem = vertexPos; res = d3dDevice->CreateBuffer(&BufferDesc, &subData, &vertexBuffer); if (FAILED(res)) return -1; return 0; }
HRESULT CFontShader::Init() { D3D11_INPUT_ELEMENT_DESC tInputLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; UINT iElementNum = ARRAYSIZE(tInputLayout); DWORD swFlag = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) swFlag |= D3D10_SHADER_DEBUG; #endif ID3DBlob* pBlob = NULL, *pErrorBlob = NULL; ID3D11Device* pDevice = m_pDevice->GetDevice(); if (SUCCEEDED(D3DX11CompileFromFile(L"../bin/Data/Fx/VertexFont.fx", NULL, NULL, "VS", "vs_4_0", swFlag, 0, NULL, &pBlob, &pErrorBlob, NULL))) { pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &m_pVertexShader); pDevice->CreateInputLayout( tInputLayout, iElementNum, pBlob->GetBufferPointer(), pBlob->GetBufferSize(), &m_pVertexLayout); } else { if (pErrorBlob) { FAILED_CHECK_MSG(E_FAIL, L"셰이더 컴파일 실패"); } else { FAILED_CHECK_MSG(E_FAIL, L"셰이더 파일이 존재하지 않습니다."); } return E_FAIL; } pBlob = pErrorBlob = NULL; if (SUCCEEDED(D3DX11CompileFromFile(L"../bin/Data/Fx/VertexFont.fx", NULL, NULL, "PS", "ps_4_0", swFlag, 0, NULL, &pBlob, &pErrorBlob, NULL))) { pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &m_pPixelShader); } else { if (pErrorBlob) { FAILED_CHECK_MSG(E_FAIL, L"셰이더 컴파일 실패"); } else { FAILED_CHECK_MSG(E_FAIL, L"셰이더 파일이 존재하지 않습니다."); } return E_FAIL; } ::Safe_Release(pBlob); ::Safe_Release(pErrorBlob); return S_OK; }
GraphicsPipelineStateDesc GraphicsPipelineStateDesc::Simple( const D3D12_INPUT_LAYOUT_DESC & inputLayout, const RootSignature & rootSig, const Shader & vs, const Shader & ps) { ID3DBlob* vsBlob = vs.GetBlob(); ID3DBlob* psBlob = ps.GetBlob(); GraphicsPipelineStateDesc psoDesc; ZeroMemory(&psoDesc, sizeof(psoDesc)); psoDesc.InputLayout = inputLayout; psoDesc.pRootSignature = rootSig.Get(); psoDesc.VS = { reinterpret_cast<BYTE*>(vsBlob->GetBufferPointer()), vsBlob->GetBufferSize() }; psoDesc.PS = { reinterpret_cast<BYTE*>(psBlob->GetBufferPointer()), psBlob->GetBufferSize() }; /* psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK; psoDesc.RasterizerState.FrontCounterClockwise = FALSE; psoDesc.RasterizerState.DepthBias = D3D12_DEFAULT_DEPTH_BIAS; psoDesc.RasterizerState.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP; psoDesc.RasterizerState.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; psoDesc.RasterizerState.DepthClipEnable = TRUE; psoDesc.RasterizerState.MultisampleEnable = FALSE; psoDesc.RasterizerState.AntialiasedLineEnable = FALSE; psoDesc.RasterizerState.ForcedSampleCount = 0; psoDesc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF; */ psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT); psoDesc.BlendState.AlphaToCoverageEnable = FALSE; psoDesc.BlendState.IndependentBlendEnable = FALSE; const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc = { FALSE,FALSE, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_LOGIC_OP_NOOP, D3D12_COLOR_WRITE_ENABLE_ALL, }; for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) { psoDesc.BlendState.RenderTarget[i] = defaultRenderTargetBlendDesc; } psoDesc.DepthStencilState.DepthEnable = FALSE; psoDesc.DepthStencilState.StencilEnable = FALSE; psoDesc.SampleMask = UINT_MAX; psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; psoDesc.NumRenderTargets = 1; psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; psoDesc.SampleDesc.Count = 1; return psoDesc; }
ID3D11VertexShader* LoadVertexShaderFile(ID3D11Device* pd3dDevice, const void* pShaderFileData, ID3DBlob** ppBlobOut, char *pFuntionName, bool bBinary) { HRESULT hr = S_OK; ID3D11VertexShader* pVertexShader; ID3DBlob* pBlob = NULL; DWORD dwSize = 0; LPCVOID lpData = NULL; if (bBinary == false) { if (pFuntionName == 0) { if (FAILED(hr = CompileShaderFromFile((TCHAR*)pShaderFileData, "VS", "vs_5_0", &pBlob))) { return nullptr; } }else { if (FAILED(hr = CompileShaderFromFile((TCHAR*)pShaderFileData, pFuntionName, "vs_5_0", &pBlob))) { return nullptr; } } dwSize = pBlob->GetBufferSize(); lpData = pBlob->GetBufferPointer(); } else { pBlob = *ppBlobOut; if (pBlob == nullptr) return nullptr; dwSize = pBlob->GetBufferSize(); lpData = pBlob->GetBufferPointer(); } if (FAILED(hr = pd3dDevice->CreateVertexShader(lpData, dwSize, NULL, &pVertexShader))) { pBlob->Release(); return nullptr; } if (ppBlobOut == nullptr) pBlob->Release(); else *ppBlobOut = pBlob; return pVertexShader; };
void ShaderCompiler::compileProgram(IO* io, const ProgramDesc& programDesc, std::weak_ptr<DX12DeviceContext> context) { auto fmt = boost::format("Compiling program \"%1%\"") % programDesc.name; LOGS_INDENT_START << boost::str(fmt); Program prog; for (auto shaderDesc : programDesc.shaders) { auto fmt = boost::format("Path: %1%, Type: %2%, Entry: %3%") % shaderDesc.path % shaderDesc.type % shaderDesc.main; LOGS << boost::str(fmt); auto buffer = io->loadFile(shaderDesc.path); ID3DBlob* shaderBlob = nullptr; ID3DBlob* errorBlob = nullptr; auto hr = D3DCompile(buffer.c_str(), buffer.size(), shaderDesc.path.c_str(), nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, shaderDesc.main.c_str(), getDXShaderType(shaderDesc.type).c_str(), shaderDesc.flags, 0, &shaderBlob, &errorBlob); if (FAILED(hr)) { if (errorBlob != nullptr) { LOGE << "Compilation failed" << std::endl << static_cast<char*>(errorBlob->GetBufferPointer()); errorBlob->Release(); } if (shaderBlob != nullptr) shaderBlob->Release(); fmt = boost::format("Shader compilation failed: %1%") % shaderDesc.path; throw new std::runtime_error(boost::str(fmt)); } D3D12_SHADER_BYTECODE bc; bc.BytecodeLength = shaderBlob->GetBufferSize(); bc.pShaderBytecode = shaderBlob->GetBufferPointer(); if (shaderDesc.type == "vs") prog.vs = bc; else if (shaderDesc.type == "ps") prog.ps = bc; getShaderResources(shaderBlob, context); } LOGS_INDENT_END << "Program done."; }