HRESULT D3D9RenderImpl::Initialize(HWND hDisplayWindow) { m_hDisplayWindow = hDisplayWindow; m_pD3D9 = Direct3DCreate9( D3D_SDK_VERSION ); if(!m_pD3D9) { return E_FAIL; } HR(m_pD3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &m_displayMode)); D3DCAPS9 deviceCaps; HR(m_pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &deviceCaps)); DWORD dwBehaviorFlags = D3DCREATE_MULTITHREADED; if( deviceCaps.VertexProcessingCaps != 0 ) dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING; else dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; HR(GetPresentParams(&m_presentParams, TRUE)); HR(m_pD3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hDisplayWindow, dwBehaviorFlags, &m_presentParams, &m_pDevice)); return CreateResources(); }
/* Creates the necessary D3D objects */ void D3DRenderer::InitD3D(HWND hWnd) { mD3D = Direct3DCreate9(D3D_SDK_VERSION); // Create the D3D object, which is needed to create the D3DDevice. if(mD3D == NULL) { // TODO : Error reporting return; } HRESULT hr; D3DPRESENT_PARAMETERS d3dpp; GetPresentParams(d3dpp); D3DCAPS9 caps; hr = mD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); DWORD vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; D3DDEVTYPE devType = D3DDEVTYPE_HAL; if(caps.VertexProcessingCaps == 0) { vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; } hr = mD3D->CreateDevice( D3DADAPTER_DEFAULT, devType, hWnd, vertexProcessing, &d3dpp, &mD3DDevice); if(FAILED(hr)) { // TODO : Error reporting return; } }
// Recover from a lost device bool D3D9Renderer::Reset() { XTRACE_FUNCTION; if (!CanReset()) { return false; } PreReset(); D3DPRESENT_PARAMETERS Params; GetPresentParams(Params); XTRACE_BEGIN(ResetDevice); const HRESULT hr = m_D3DDevice->Reset(&Params); if (hr != D3D_OK) { return false; } XTRACE_END; m_DeviceLost = false; PostReset(); return true; }
void D3D9Renderer::Initialize() { XTRACE_FUNCTION; RendererCommon::Initialize(); STATIC_HASHED_STRING(Render); CATPRINTF(sRender, 1, "Creating Direct3D system...\n"); ASSERT(!m_D3D); m_D3D = Direct3DCreate9(D3D_SDK_VERSION); ASSERT(m_D3D); DEBUGPRINTF("Initializing Direct3D...\n"); STATICHASH(Renderer); STATICHASH(TestCaps); const bool TestCaps = ConfigManager::GetBool(sTestCaps, false, sRenderer); if (TestCaps) { TestCapabilities(); } D3DPRESENT_PARAMETERS D3DParams; GetPresentParams(D3DParams); CATPRINTF(sRender, 1, "Creating Direct3D device...\n"); HRESULT Result = 0; #ifdef USE_NVPERFHUD Result = m_D3D->CreateDevice(m_D3D->GetAdapterCount() - 1, D3DDEVTYPE_REF, m_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DParams, &m_D3DDevice); #else Result = m_D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_FPU_PRESERVE | D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DParams, &m_D3DDevice); if (Result != D3D_OK) { CATPRINTF( sRender, 1, "CreateDevice returned 0x%08X, trying again with software processing\n", Result); Result = m_D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_FPU_PRESERVE | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DParams, &m_D3DDevice); } #endif CATPRINTF(sRender, 1, "CreateDevice returned 0x%08X\n", Result); ASSERT(Result == D3D_OK); CATPRINTF(sRender, 1, "Device created.\n"); CATPRINTF(sRender, 1, "Creating default render target...\n"); CreateDefaultRenderTarget(); CATPRINTF(sRender, 1, "Default render target created.\n"); CATPRINTF(sRender, 1, "Initializing render state.\n"); // I never want to use built-in lighting. All other render state should be // driven by materials. { const HRESULT Result = m_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); DEBUGASSERT(Result == D3D_OK); Unused(Result); } CATPRINTF(sRender, 1, "Render state initialized.\n"); CATPRINTF(sRender, 1, "Direct3D initialized.\n"); }
/* Called when a drawing pass has ended */ void D3DRenderer::OnDrawEnd() { mDeviceLost = false; mD3DDevice->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 0); mD3DDevice->EndScene(); HRESULT result = 0; result = mD3DDevice->TestCooperativeLevel(); if(result == D3DERR_DEVICELOST) return; if(result == D3DERR_DEVICENOTRESET) { mDeviceLost = true; // Release the existing swap chains SwapChainMap::iterator swapChainIt = mSwapChains.begin(); for(; swapChainIt != mSwapChains.end(); swapChainIt++) { IDirect3DSwapChain9* pSwapChain = swapChainIt->second.mD3DSwapChain; if(pSwapChain) { pSwapChain->Release(); } } // Release the shaders ShaderMap::iterator shaderIt = mShaders.begin(); for(; shaderIt != mShaders.end(); shaderIt++) { (*shaderIt).second.mEffect->Release(); } // Release the vbuffer mVertexBuffer->Release(); mVertexBuffer = NULL; // Release the vertex decl mVertexDeclaration->Release(); mVertexDeclaration = NULL; // Reset the device D3DPRESENT_PARAMETERS d3dpp; GetPresentParams(d3dpp); HRESULT hr = mD3DDevice->Reset(&d3dpp); if(FAILED(hr)) { printf("FAILED\n"); } // Recreate the swap chains swapChainIt = mSwapChains.begin(); for(; swapChainIt != mSwapChains.end(); swapChainIt++) { SwapChain swapChain = swapChainIt->second; CreateContext(swapChain.mWindow, swapChain.mWidth, swapChain.mHeight, swapChainIt->first); } // Create the Vertex Buffer CreateBuffers(); // Recreate the shaders ShaderMap tmp = mShaders; mShaders.clear(); CreateShaders(); shaderIt = tmp.begin(); for(; shaderIt != tmp.end(); shaderIt++) { Shader& shader = shaderIt->second; if(shader.mSource != "") LoadShader(shader.mName.c_str(), shader.mSource.c_str()); } // By setting mContext to zero, we force the back-buffer changes in the next frame long oldContext = mContext; mContext = 0; SetContext(oldContext); } SwapChainMap::iterator it = mSwapChains.find(mContext); if(it != mSwapChains.end()) { IDirect3DSwapChain9* pSwapChain = it->second.mD3DSwapChain; if(pSwapChain) result = pSwapChain->Present(NULL, NULL, NULL, NULL, 0); } }