void D3DWrapper::WindowResized(unsigned int clientWidth, unsigned int clientHeight, unsigned int windowWidth, unsigned int windowHeight) { // Output debug information Utility::Logger::Instance().D("Resize: %u x %u\n", clientWidth, clientHeight); m_deviceContext->OMSetRenderTargets(0, NULL, NULL); // Release references to resources m_backBufferView = NULL; m_depthStencilView = NULL; m_depthStencilBuffer = NULL; // Resize the internal buffers HRESULT result = m_swapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH); if (FAILED(result)) throw DirectXErrorM(result, "Failed to resize buffers"); // Recreate view to internal back buffer. result = CreateBackBufferView(); if (FAILED(result)) throw DirectXErrorM(result, "Failed to recreate back buffer view"); // Recreate depth/stencil buffer result = CreateDepthStencilBuffer(m_backBufferDescription.Width, m_backBufferDescription.Height); if (FAILED(result)) throw DirectXErrorM(result, "Failed to recreate depth/stencil buffer"); m_deviceContext->OMSetRenderTargets(1, &m_backBufferView.Resource(), m_depthStencilView.Resource()); // Recreate the viewports UpdateViewports(m_viewports); }
D3DContext::D3DContext(ApplicationWindow* targetWindow, const Description& description) : mTargetWindow(targetWindow) { if (!CreateDeviceAndSwapChain(description)) throw std::runtime_error("Failed to create device and swap chain"); if (!CreateBackBufferView()) throw std::runtime_error("Failed to create a back buffer view"); if (!CreateDepthStencilBuffer(description.DepthBuffer)) throw std::runtime_error("Failed to create a depth/stencil buffer"); mDevice->OMSetRenderTargets(1, &mBackBufferView, mDepthStencilView); SetViewports(description.Viewports); SetActiveViewport(0); }
D3DWrapper::D3DWrapper(Window* targetWindow, const Description& description) : m_targetWindow(targetWindow) { HRESULT result = S_OK; m_targetWindow->AddEventListener(this); result = CreateDeviceAndSwapChain(description.m_displayMode, description.m_fullscreen); if (FAILED(result)) throw DirectXErrorM(result, "Failed to create device and swap chain"); result = CreateBackBufferView(); if (FAILED(result)) throw DirectXErrorM(result, "Failed to create back buffer view"); result = CreateDepthStencilBuffer(description.m_displayMode.m_width, description.m_displayMode.m_height); if (FAILED(result)) throw DirectXErrorM(result, "Failed to create depth stencil buffer/view"); m_deviceContext->OMSetRenderTargets(1, &m_backBufferView.Resource(), m_depthStencilView.Resource()); SetViewports(description.m_viewports); }
bool D3DContext::ResizeBuffers(Description::Buffer backBufferDescription, const Description::Buffer& depthBufferDescription) { backBufferDescription.Width = (backBufferDescription.Width == 0) ? mTargetWindow->GetClientWidth() : backBufferDescription.Width; backBufferDescription.Height = (backBufferDescription.Height == 0) ? mTargetWindow->GetClientHeight() : backBufferDescription.Height; SafeRelease(mBackBufferView); SafeRelease(mDepthStencilBuffer); SafeRelease(mDepthStencilView); if (FAILED(mSwapChain->ResizeBuffers(0, backBufferDescription.Width, backBufferDescription.Height, DXGI_FORMAT_UNKNOWN, 0))) return false; if (!CreateBackBufferView()) return false; if (!CreateDepthStencilBuffer(depthBufferDescription)) return false; mBackBufferSize = backBufferDescription; mDepthBufferSize = depthBufferDescription; return true; }