void DX11::BackbufferSurface::Resize(ID3D11Texture2D * texture, unsigned width, unsigned height) { this->width = width; this->height = height; device->CreateRenderTargetView(texture, 0, &renderTargetView); CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 1, D3D11_BIND_DEPTH_STENCIL); ID3D11Texture2D* depthStencilBuffer; device->CreateTexture2D( &depthStencilTextureDesc, 0, &depthStencilBuffer); device->CreateDepthStencilView( depthStencilBuffer, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2DMS), &depthStencilView); depthStencilBuffer->Release(); viewport = CD3D11_VIEWPORT( 0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height)); }
void DX11::TextureSurface::OnResetDevice(Gpu::Api * gpu) { OnLostDevice(gpu); unsigned width = unsigned(viewport.Width); unsigned height = unsigned(viewport.Height); bool typeless = (format == Gpu::DrawSurface::Format_Typeless); if(relativeWindow) { gpu->GetBackbufferSize(width, height, relativeWindow); width = unsigned(widthFactor * float(width)); height = unsigned(heightFactor * float(height)); } viewport = CD3D11_VIEWPORT(0.0f, 0.0f, float(width), float(height)); static const DXGI_FORMAT SURFACE_FORMAT_TO_DXGI_FORMAT[Format_Total] = { DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R11G11B10_FLOAT, DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R24G8_TYPELESS }; DXGI_FORMAT dxgiFormat = SURFACE_FORMAT_TO_DXGI_FORMAT[format]; unsigned mipLevels = generateMips ? min(width, height) / 4 : 1; D3D11_BIND_FLAG bindFlag = typeless ? D3D11_BIND_DEPTH_STENCIL : D3D11_BIND_RENDER_TARGET; ID3D11Texture2D * texture2D = 0; CD3D11_TEXTURE2D_DESC textureDesc( dxgiFormat, width, height, 1, mipLevels, // MIP LEVELS - *MUST NEVER* BE GREATER THAN log2(width) OR log2(height) bindFlag | D3D11_BIND_SHADER_RESOURCE); device->CreateTexture2D(&textureDesc, 0, &texture2D); if(typeless) dxgiFormat = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; ID3D11ShaderResourceView * shaderView; CD3D11_SHADER_RESOURCE_VIEW_DESC shaderViewDesc( texture2D, D3D11_SRV_DIMENSION_TEXTURE2D, dxgiFormat, 0, mipLevels); device->CreateShaderResourceView(texture2D, &shaderViewDesc, &shaderView); if(!typeless) { CD3D11_RENDER_TARGET_VIEW_DESC renderViewDesc( texture2D, D3D11_RTV_DIMENSION_TEXTURE2D, dxgiFormat); device->CreateRenderTargetView(texture2D, &renderViewDesc, &renderTargetView); CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 1, D3D11_BIND_DEPTH_STENCIL); device->CreateTexture2D(&depthStencilTextureDesc, 0, &depthStencilTexture); device->CreateDepthStencilView( depthStencilTexture, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D), &depthStencilView); } else { device->CreateDepthStencilView( texture2D, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D, DXGI_FORMAT_D24_UNORM_S8_UINT), &depthStencilView); } texture = new DX11::Texture(texture2D, shaderView, textureDesc); Clear(); }
int ContextD3D11::CreateWindowSizeDependentResources(uint32_t width, uint32_t height) { ID3D11RenderTargetView* nullViews[] = {nullptr}; device_context_->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr); SafeRelease(&render_target_view_); SafeRelease(&depth_stencil_view_); device_context_->Flush(); //RECT rc; //GetClientRect( window_handle_, &rc ); width_ = width;//rc.right - rc.left; height_ = height;//rc.bottom - rc.top; if(swap_chain_ != nullptr) { swap_chain_->ResizeBuffers(2, static_cast<UINT>(width_),static_cast<UINT>(height_), DXGI_FORMAT_B8G8R8A8_UNORM, 0); } else { // Otherwise, create a new one using the same adapter as the existing Direct3D device. DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0}; swapChainDesc.Width = static_cast<UINT>(width_); // Match the size of the window. swapChainDesc.Height = static_cast<UINT>(height_); swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format. swapChainDesc.Stereo = false; swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling. swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency. swapChainDesc.Scaling = DXGI_SCALING_NONE; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect. swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; IDXGIDevice1* dxgiDevice; device_->QueryInterface(__uuidof(dxgiDevice),(void**)&dxgiDevice); //IDXGIAdapter* dxgiAdapter; //dxgiDevice->GetAdapter(&dxgiAdapter); IDXGIFactory2* dxgiFactory; adaptor_->GetParent(__uuidof(IDXGIFactory2), (void**)&dxgiFactory ); DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreen_desc; memset(&fullscreen_desc,0,sizeof(fullscreen_desc)); fullscreen_desc.Windowed = true; dxgiFactory->CreateSwapChainForHwnd(device_,window_handle_, &swapChainDesc, &fullscreen_desc,nullptr, &swap_chain_); dxgiDevice->SetMaximumFrameLatency(1); SafeRelease(&dxgiFactory); //SafeRelease(&dxgiAdapter); SafeRelease(&dxgiDevice); } ID3D11Texture2D* backBuffer; swap_chain_->GetBuffer(0,__uuidof(ID3D11Texture2D),(void**)&backBuffer); device_->CreateRenderTargetView(backBuffer,nullptr,&render_target_view_); SafeRelease(&backBuffer); // Create a depth stencil view. CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, static_cast<UINT>(width_), static_cast<UINT>(height_), 1, 0, D3D11_BIND_DEPTH_STENCIL ); ID3D11Texture2D* depth_stencil_tex; device_->CreateTexture2D(&depthStencilTextureDesc,nullptr,&depth_stencil_tex); CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D); device_->CreateDepthStencilView(depth_stencil_tex,&depthStencilViewDesc,&depth_stencil_view_); SafeRelease(&depth_stencil_tex); SafeRelease(&default_depth_state); D3D11_DEPTH_STENCIL_DESC depthStencilDesc; // Initialize the description of the stencil state. ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc)); depthStencilDesc.DepthEnable = true; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthStencilDesc.StencilEnable = true; depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; depthStencilDesc.StencilWriteMask = 0xFF; // Stencil operations if pixel is front-facing. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Stencil operations if pixel is back-facing. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; device_->CreateDepthStencilState(&depthStencilDesc, &default_depth_state); SetDepthState(null,0); // Set the rendering viewport to target the entire window. CD3D11_VIEWPORT viewport(0.0f,0.0f,width_,height_); device_context_->RSSetViewports(1, &viewport); device_context_->OMSetRenderTargets( 1, &render_target_view_, depth_stencil_view_ ); return S_OK; }