void RenderSystem::FreeResources() { ReleaseCom(_depthStencilView); ReleaseCom(_swapChain); ReleaseCom(_renderTargetView); ReleaseCom(_device); ReleaseCom(_context); }
DirectInput::~DirectInput() { ReleaseCom(mDInput); mKeyboard->Unacquire(); mMouse->Unacquire(); ReleaseCom(mKeyboard); ReleaseCom(mMouse); }
void D3DApp::OnResize() { assert(md3dImmediateContext); assert(md3dDevice); assert(mSwapChain); ReleaseCom(mRenderTargetView); ReleaseCom(mDepthStencilView); ReleaseCom(mDepthStencilBuffer); HR(mSwapChain->ResizeBuffers(1, mClientWidth, mClientHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0)); ID3D11Texture2D* backBuffer; HR(mSwapChain->GetBuffer(0, COMID_P(backBuffer))); HR(md3dDevice->CreateRenderTargetView(backBuffer, NULL, &mRenderTargetView)); ReleaseCom(backBuffer); D3D11_TEXTURE2D_DESC depthStencilDesc; depthStencilDesc.Width = mClientWidth; depthStencilDesc.Height = mClientHeight; depthStencilDesc.MipLevels = 1; depthStencilDesc.ArraySize = 1; depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; if (mEnable4xMsaa) { depthStencilDesc.SampleDesc.Count = 4; depthStencilDesc.SampleDesc.Quality = m4xMsaaQuality - 1; } else { depthStencilDesc.SampleDesc.Count = 1; depthStencilDesc.SampleDesc.Quality = 0; } depthStencilDesc.Usage = D3D11_USAGE_DEFAULT; depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthStencilDesc.CPUAccessFlags = 0; depthStencilDesc.MiscFlags = 0; HR(md3dDevice->CreateTexture2D(&depthStencilDesc, 0, &mDepthStencilBuffer)); HR(md3dDevice->CreateDepthStencilView(mDepthStencilBuffer, 0, &mDepthStencilView)); md3dImmediateContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView); mScreenViewport.TopLeftX = 0; mScreenViewport.TopLeftY = 0; mScreenViewport.Width = static_cast<float>(mClientWidth); mScreenViewport.Height = static_cast<float>(mClientHeight); mScreenViewport.MinDepth = 0.0f; mScreenViewport.MaxDepth = 1.0f; md3dImmediateContext->RSSetViewports(1, &mScreenViewport); }
static IDXGIFactory* FactoryFromDevice(ID3D11Device *device) { assert(device); IDXGIDevice *dxgiDevice = nullptr; DEBUG_HR(device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice))); IDXGIAdapter *dxgiAdapter = nullptr; DEBUG_HR(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&dxgiAdapter))); IDXGIFactory *dxgiFactory = nullptr; DEBUG_HR(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&dxgiFactory))); ReleaseCom(dxgiAdapter); ReleaseCom(dxgiDevice); return dxgiFactory; }
void D3D12Render::LogAdapters() { UINT i = 0; IDXGIAdapter* adapter = nullptr; std::vector<IDXGIAdapter*> adapterList; while(m_pFactory->EnumAdapters(i, &adapter) != DXGI_ERROR_NOT_FOUND) { DXGI_ADAPTER_DESC desc; adapter->GetDesc(&desc); std::wstring text = L"***Adapter: "; text += desc.Description; text += L"\n"; OutputDebugString(text.c_str()); adapterList.push_back(adapter); ++i; } for(size_t i = 0; i < adapterList.size(); ++i) { LogAdapterOutputs(adapterList[i]); ReleaseCom(adapterList[i]); } }
LightingApp::~LightingApp() { ReleaseCom(mLandVB); ReleaseCom(mLandIB); ReleaseCom(mWavesVB); ReleaseCom(mWavesIB); ReleaseCom(mFX); ReleaseCom(mInputLayout); }
Error::E RenderSystem::CreateSwapChain(const Window &window) { assert(_device); _swapDesc = kDefaultSwapDesc; _swapDesc.SampleDesc.Count = 4; _swapDesc.SampleDesc.Quality = _msaaQualityLevel - 1; _swapDesc.OutputWindow = window.GetHandle(); _swapDesc.BufferDesc.Width = window.GetWidth(); _swapDesc.BufferDesc.Height = window.GetHeight(); IDXGIFactory *factory = FactoryFromDevice(_device); DEBUG_HR(factory->CreateSwapChain(_device, &_swapDesc, &_swapChain)); ReleaseCom(factory); return Error::OK; }
D3DApp::~D3DApp() { ReleaseCom(mRenderTargetView); ReleaseCom(mDepthStencilView); ReleaseCom(mSwapChain); ReleaseCom(mDepthStencilBuffer); if (md3dImmediateContext) md3dImmediateContext->ClearState(); ReleaseCom(md3dImmediateContext); ReleaseCom(md3dDevice); }
Error::E RenderSystem::Initialize(const Window &window) { Error::E err = Error::OK; err = CreateDeviceAndContext(); if (err != Error::OK) { FreeResources(); return err; } err = CreateSwapChain(window); if (err != Error::OK) { FreeResources(); return err; } ID3D11Texture2D *backBuffer; DEBUG_HR(_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer))); DEBUG_HR(_device->CreateRenderTargetView(backBuffer, NULL, &_renderTargetView)); ReleaseCom(backBuffer); err = CreateDepthStencilBuffer(); if (err != Error::OK) { FreeResources(); return err; } _context->OMSetRenderTargets(1, &_renderTargetView, _depthStencilView); D3D11_VIEWPORT vp; vp.TopLeftX = 0; vp.TopLeftY = 0; vp.Width = (float)window.GetWidth(); vp.Height = (float)window.GetHeight(); vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; _context->RSSetViewports(1, &vp); _initOk = true; return Error::OK; }
Error::E RenderSystem::CreateDepthStencilBuffer() { D3D11_TEXTURE2D_DESC bd; bd.Width = _swapDesc.BufferDesc.Width; bd.Height = _swapDesc.BufferDesc.Height; bd.MipLevels = 1; bd.ArraySize = 1; bd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; bd.SampleDesc = _swapDesc.SampleDesc; bd.Usage = D3D11_USAGE_DEFAULT; bd.BindFlags = D3D11_BIND_DEPTH_STENCIL; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; ID3D11Texture2D *buffer; DEBUG_HR(_device->CreateTexture2D(&bd, NULL, &buffer)); DEBUG_HR(_device->CreateDepthStencilView(buffer, 0, &_depthStencilView)); ReleaseCom(buffer); return Error::OK; }
void D3D12Render::LogAdapterOutputs(IDXGIAdapter* adapter) { UINT i = 0; IDXGIOutput* output = nullptr; while(adapter->EnumOutputs(i, &output) != DXGI_ERROR_NOT_FOUND) { DXGI_OUTPUT_DESC desc; output->GetDesc(&desc); std::wstring text = L"***Output: "; text += desc.DeviceName; text += L"\n"; OutputDebugString(text.c_str()); LogOutputDisplayModes(output, m_BackBufferFormat); ReleaseCom(output); ++i; } }
bool D3DApp::InitDirect3D() { UINT createDeviceFlags = 0; #if defined(DEBUG) || defined(_DEBUG) createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_FEATURE_LEVEL featureLevel; HRESULT hr = D3D11CreateDevice( 0, md3dDriverType, 0, createDeviceFlags, 0, 0, D3D11_SDK_VERSION, &md3dDevice, &featureLevel, &md3dImmediateContext); if (FAILED(hr)) { LogPrint(L"D3D11CreateDevice Failed."); return FALSE; } if (featureLevel != D3D_FEATURE_LEVEL_11_0) { LogPrint(L"Direct3D Feature Level 11 unsupported!"); return FALSE; } HR(md3dDevice->CheckMultisampleQualityLevels(DXGI_FORMAT_B8G8R8A8_UNORM, 4, &m4xMsaaQuality)); assert(m4xMsaaQuality > 0); DXGI_SWAP_CHAIN_DESC sd; sd.BufferDesc.Width = mClientWidth; sd.BufferDesc.Height = mClientHeight; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; if (mEnable4xMsaa) { sd.SampleDesc.Count = 4; sd.SampleDesc.Quality = m4xMsaaQuality - 1; } else { sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; } sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferCount = 1; sd.OutputWindow = mhMainWnd; sd.Windowed = TRUE; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.Flags = 0; IDXGIDevice*dxgiDevice = 0; HR(md3dDevice->QueryInterface(COMID_P(dxgiDevice))); IDXGIAdapter* dxgiAdapter = 0; HR(dxgiDevice->GetParent(COMID_P(dxgiAdapter))); IDXGIFactory* dxgiFactory = 0; HR(dxgiAdapter->GetParent(COMID_P(dxgiFactory))); HR(dxgiFactory->CreateSwapChain(md3dDevice, &sd, &mSwapChain)); ReleaseCom(dxgiDevice); ReleaseCom(dxgiAdapter); ReleaseCom(dxgiFactory); OnResize(); return true; }