bool Texture2D::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { LOGWARNING("Texture creation while device is lost"); return true; } IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice(); // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead if (usage_ & D3DUSAGE_DEPTHSTENCIL && !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, usage_, D3DRTYPE_TEXTURE)) { if (!device || FAILED(device->CreateDepthStencilSurface( width_, height_, (D3DFORMAT)format_, D3DMULTISAMPLE_NONE, 0, FALSE, (IDirect3DSurface9**)&renderSurface_->surface_, 0))) { LOGERROR("Could not create depth-stencil surface"); return false; } levels_ = 1; } else { if (!device || FAILED(graphics_->GetImpl()->GetDevice()->CreateTexture( width_, height_, requestedLevels_, usage_, (D3DFORMAT)format_, (D3DPOOL)pool_, (IDirect3DTexture9**)&object_, 0))) { LOGERROR("Could not create texture"); return false; } levels_ = ((IDirect3DTexture9*)object_)->GetLevelCount(); if (usage_ & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) ((IDirect3DTexture9*)object_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_); } return true; }
DepthStencilbuffer::DepthStencilbuffer(int width, int height, GLsizei samples) { IDirect3DDevice9 *device = getDevice(); mDepthStencil = NULL; int supportedSamples = getContext()->getNearestSupportedSamples(D3DFMT_D24S8, samples); if (supportedSamples == -1) { error(GL_OUT_OF_MEMORY); return; } HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, es2dx::GetMultisampleTypeFromSamples(supportedSamples), 0, FALSE, &mDepthStencil, 0); if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) { error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); if (mDepthStencil) { setSize(width, height); mFormat = GL_DEPTH24_STENCIL8_OES; mD3DFormat = D3DFMT_D24S8; mSamples = supportedSamples; } else { setSize(0, 0); mFormat = GL_RGBA4; //default format mD3DFormat = D3DFMT_UNKNOWN; mSamples = 0; } }
/// @copydoc D3D9DeviceResetListener::OnPostReset() void D3D9DepthStencilSurface::OnPostReset( D3D9Renderer* pRenderer ) { HELIUM_ASSERT( pRenderer ); IDirect3DDevice9* pDevice = pRenderer->GetD3DDevice(); HELIUM_ASSERT( pDevice ); D3DMULTISAMPLE_TYPE multisampleType = D3DMULTISAMPLE_NONE; if( m_multisampleCountMinusOne != 0 ) { multisampleType = static_cast< D3DMULTISAMPLE_TYPE >( D3DMULTISAMPLE_2_SAMPLES + m_multisampleCountMinusOne - 1 ); } HELIUM_ASSERT( !m_pSurface ); HELIUM_D3D9_VERIFY( pDevice->CreateDepthStencilSurface( m_width, m_height, ( m_bStencil ? D3DFMT_D24S8 : D3DFMT_D24X8 ), multisampleType, 0, FALSE, &m_pSurface, NULL ) ); }
DepthStencilbuffer::DepthStencilbuffer(int width, int height, GLsizei samples) { IDirect3DDevice9 *device = getDevice(); mDepthStencil = NULL; int supportedSamples = getContext()->getNearestSupportedSamples(D3DFMT_D24S8, samples); if (supportedSamples == -1) { error(GL_OUT_OF_MEMORY); return; } if (width > 0 && height > 0) { HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, es2dx::GetMultisampleTypeFromSamples(supportedSamples), 0, FALSE, &mDepthStencil, 0); if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) { error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); } mWidth = width; mHeight = height; mInternalFormat = GL_DEPTH24_STENCIL8_OES; mD3DFormat = D3DFMT_D24S8; mSamples = supportedSamples; }
EGLint SwapChain9::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) { IDirect3DDevice9 *device = mRenderer->getDevice(); if (device == NULL) { return EGL_BAD_ACCESS; } // Evict all non-render target textures to system memory and release all resources // before reallocating them to free up as much video memory as possible. device->EvictManagedResources(); HRESULT result; // Release specific resources to free up memory for the new render target, while the // old render target still exists for the purpose of preserving its contents. if (mSwapChain) { mSwapChain->Release(); mSwapChain = NULL; } if (mBackBuffer) { mBackBuffer->Release(); mBackBuffer = NULL; } if (mOffscreenTexture) { mOffscreenTexture->Release(); mOffscreenTexture = NULL; } if (mDepthStencil) { mDepthStencil->Release(); mDepthStencil = NULL; } HANDLE *pShareHandle = NULL; if (!mWindow && mRenderer->getShareHandleSupport()) { pShareHandle = &mShareHandle; } result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET, gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT, &mOffscreenTexture, pShareHandle); if (FAILED(result)) { ERR("Could not create offscreen texture: %08lX", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } IDirect3DSurface9 *oldRenderTarget = mRenderTarget; result = mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); ASSERT(SUCCEEDED(result)); if (oldRenderTarget) { RECT rect = { 0, 0, mWidth, mHeight }; if (rect.right > static_cast<LONG>(backbufferWidth)) { rect.right = backbufferWidth; } if (rect.bottom > static_cast<LONG>(backbufferHeight)) { rect.bottom = backbufferHeight; } mRenderer->endScene(); result = device->StretchRect(oldRenderTarget, &rect, mRenderTarget, &rect, D3DTEXF_NONE); ASSERT(SUCCEEDED(result)); oldRenderTarget->Release(); } if (mWindow) { D3DPRESENT_PARAMETERS presentParameters = {0}; presentParameters.AutoDepthStencilFormat = gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat); presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat); presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = mWindow; presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented presentParameters.PresentationInterval = convertInterval(swapInterval); presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; presentParameters.BackBufferWidth = backbufferWidth; presentParameters.BackBufferHeight = backbufferHeight; // http://crbug.com/140239 // http://crbug.com/143434 // // Some AMD/Intel switchable systems / drivers appear to round swap chain surfaces to a multiple of 64 pixels in width // when using the integrated Intel. This rounds the width up rather than down. // // Some non-switchable AMD GPUs / drivers do not respect the source rectangle to Present. Therefore, when the vendor ID // is not Intel, the back buffer width must be exactly the same width as the window or horizontal scaling will occur. if (mRenderer->getAdapterVendor() == VENDOR_ID_INTEL) { presentParameters.BackBufferWidth = (presentParameters.BackBufferWidth + 63) / 64 * 64; } result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL || result == D3DERR_DEVICELOST); ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer); ASSERT(SUCCEEDED(result)); InvalidateRect(mWindow, NULL, FALSE); } if (mDepthBufferFormat != GL_NONE) { result = device->CreateDepthStencilSurface(backbufferWidth, backbufferHeight, gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat), D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL); ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } } mWidth = backbufferWidth; mHeight = backbufferHeight; mSwapInterval = swapInterval; return EGL_SUCCESS; }
RenderTarget9::RenderTarget9(Renderer *renderer, GLsizei width, GLsizei height, GLenum internalFormat, GLsizei samples) { mRenderer = Renderer9::makeRenderer9(renderer); mRenderTarget = NULL; D3DFORMAT renderFormat = gl_d3d9::GetRenderFormat(internalFormat, mRenderer); int supportedSamples = mRenderer->getNearestSupportedSamples(renderFormat, samples); if (supportedSamples == -1) { gl::error(GL_OUT_OF_MEMORY); return; } HRESULT result = D3DERR_INVALIDCALL; GLuint clientVersion = mRenderer->getCurrentClientVersion(); if (width > 0 && height > 0) { IDirect3DDevice9 *device = mRenderer->getDevice(); bool requiresInitialization = false; if (gl::GetDepthBits(internalFormat, clientVersion) > 0 || gl::GetStencilBits(internalFormat, clientVersion) > 0) { result = device->CreateDepthStencilSurface(width, height, renderFormat, gl_d3d9::GetMultisampleType(supportedSamples), 0, FALSE, &mRenderTarget, NULL); } else { requiresInitialization = gl_d3d9::RequiresTextureDataInitialization(internalFormat); result = device->CreateRenderTarget(width, height, renderFormat, gl_d3d9::GetMultisampleType(supportedSamples), 0, FALSE, &mRenderTarget, NULL); } if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_INVALIDARG || result == E_OUTOFMEMORY) { gl::error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); if (requiresInitialization) { // This format requires that the data be initialized before the render target can be used // Unfortunately this requires a Get call on the d3d device but it is far better than having // to mark the render target as lockable and copy data to the gpu. IDirect3DSurface9 *prevRenderTarget = NULL; device->GetRenderTarget(0, &prevRenderTarget); device->SetRenderTarget(0, mRenderTarget); device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 0.0f, 0); device->SetRenderTarget(0, prevRenderTarget); } } mWidth = width; mHeight = height; mDepth = 1; mInternalFormat = internalFormat; mSamples = supportedSamples; mActualFormat = d3d9_gl::GetInternalFormat(renderFormat); }
bool Texture2D::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { ATOMIC_LOGWARNING("Texture creation while device is lost"); return true; } unsigned pool = usage_ > TEXTURE_STATIC ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED; unsigned d3dUsage = 0; switch (usage_) { case TEXTURE_DYNAMIC: d3dUsage |= D3DUSAGE_DYNAMIC; break; case TEXTURE_RENDERTARGET: d3dUsage |= D3DUSAGE_RENDERTARGET; break; case TEXTURE_DEPTHSTENCIL: d3dUsage |= D3DUSAGE_DEPTHSTENCIL; break; default: break; } IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice(); // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead if (usage_ == TEXTURE_DEPTHSTENCIL && !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, d3dUsage, D3DRTYPE_TEXTURE)) { HRESULT hr = device->CreateDepthStencilSurface( (UINT)width_, (UINT)height_, (D3DFORMAT)format_, D3DMULTISAMPLE_NONE, 0, FALSE, (IDirect3DSurface9**)&renderSurface_->surface_, 0); if (FAILED(hr)) { ATOMIC_SAFE_RELEASE(renderSurface_->surface_); ATOMIC_LOGD3DERROR("Could not create depth-stencil surface", hr); return false; } levels_ = 1; } else { HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture( (UINT)width_, (UINT)height_, requestedLevels_, d3dUsage, (D3DFORMAT)format_, (D3DPOOL)pool, (IDirect3DTexture9**)&object_, 0); if (FAILED(hr)) { ATOMIC_SAFE_RELEASE(object_.ptr_); ATOMIC_LOGD3DERROR("Could not create texture", hr); return false; } levels_ = ((IDirect3DTexture9*)object_.ptr_)->GetLevelCount(); if (usage_ >= TEXTURE_RENDERTARGET) { hr = ((IDirect3DTexture9*)object_.ptr_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_); if (FAILED(hr)) ATOMIC_LOGD3DERROR("Could not get rendertarget surface", hr); } } return true; }
bool Texture2D::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } if (multiSample_ > 1 && !autoResolve_) { URHO3D_LOGWARNING("Multisampled texture without autoresolve is not supported on Direct3D9"); autoResolve_ = true; } GraphicsImpl* impl = graphics_->GetImpl(); unsigned pool = usage_ > TEXTURE_STATIC ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED; unsigned d3dUsage = 0; switch (usage_) { case TEXTURE_DYNAMIC: d3dUsage |= D3DUSAGE_DYNAMIC; break; case TEXTURE_RENDERTARGET: d3dUsage |= D3DUSAGE_RENDERTARGET; if (requestedLevels_ != 1) { // Check mipmap autogeneration support if (impl->CheckFormatSupport((D3DFORMAT)format_, D3DUSAGE_AUTOGENMIPMAP, D3DRTYPE_TEXTURE)) { requestedLevels_ = 0; d3dUsage |= D3DUSAGE_AUTOGENMIPMAP; } else requestedLevels_ = 1; } break; case TEXTURE_DEPTHSTENCIL: d3dUsage |= D3DUSAGE_DEPTHSTENCIL; // No mipmaps for depth-stencil textures requestedLevels_ = 1; break; default: break; } if (multiSample_ > 1) { // Fall back to non-multisampled if unsupported multisampling mode if (!impl->CheckMultiSampleSupport((D3DFORMAT)format_, multiSample_)) { multiSample_ = 1; autoResolve_ = false; } } IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice(); // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead // Multisampled surfaces need also to be created this way if (usage_ == TEXTURE_DEPTHSTENCIL && (multiSample_ > 1 || !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, d3dUsage, D3DRTYPE_TEXTURE))) { HRESULT hr = device->CreateDepthStencilSurface( (UINT)width_, (UINT)height_, (D3DFORMAT)format_, (multiSample_ > 1) ? (D3DMULTISAMPLE_TYPE)multiSample_ : D3DMULTISAMPLE_NONE, 0, FALSE, (IDirect3DSurface9**)&renderSurface_->surface_, nullptr); if (FAILED(hr)) { URHO3D_LOGD3DERROR("Could not create depth-stencil surface", hr); URHO3D_SAFE_RELEASE(renderSurface_->surface_); return false; } levels_ = 1; } else { HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture( (UINT)width_, (UINT)height_, requestedLevels_, d3dUsage, (D3DFORMAT)format_, (D3DPOOL)pool, (IDirect3DTexture9**)&object_, nullptr); if (FAILED(hr)) { URHO3D_LOGD3DERROR("Could not create texture", hr); URHO3D_SAFE_RELEASE(object_.ptr_); return false; } levels_ = ((IDirect3DTexture9*)object_.ptr_)->GetLevelCount(); // Create the multisampled rendertarget for rendering to if necessary if (usage_ == TEXTURE_RENDERTARGET && multiSample_ > 1) { HRESULT hr = device->CreateRenderTarget( (UINT)width_, (UINT)height_, (D3DFORMAT)format_, (D3DMULTISAMPLE_TYPE)multiSample_, 0, FALSE, (IDirect3DSurface9**)&renderSurface_->surface_, nullptr); if (FAILED(hr)) { URHO3D_LOGD3DERROR("Could not create multisampled rendertarget surface", hr); URHO3D_SAFE_RELEASE(renderSurface_->surface_); return false; } } else if (usage_ >= TEXTURE_RENDERTARGET) { // Else use the texture surface directly for rendering hr = ((IDirect3DTexture9*)object_.ptr_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_); if (FAILED(hr)) { URHO3D_LOGD3DERROR("Could not get rendertarget surface", hr); URHO3D_SAFE_RELEASE(renderSurface_->surface_); return false; } } } return true; }
void Surface::resetSwapChain(int backbufferWidth, int backbufferHeight) { IDirect3DDevice9 *device = mDisplay->getDevice(); if (device == NULL) { return; } // Evict all non-render target textures to system memory and release all resources // before reallocating them to free up as much video memory as possible. device->EvictManagedResources(); release(); D3DPRESENT_PARAMETERS presentParameters = {0}; HRESULT result; presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat; presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = mConfig->mRenderTargetFormat; presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = getWindowHandle(); presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented presentParameters.PresentationInterval = mPresentInterval; presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; presentParameters.BackBufferWidth = backbufferWidth; presentParameters.BackBufferHeight = backbufferHeight; if (mWindow) { result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); } else { HANDLE *pShareHandle = NULL; if (mDisplay->isD3d9exDevice()) { pShareHandle = &mShareHandle; } result = device->CreateTexture(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, 1, D3DUSAGE_RENDERTARGET, presentParameters.BackBufferFormat, D3DPOOL_DEFAULT, &mOffscreenTexture, pShareHandle); } if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); release(); return error(EGL_BAD_ALLOC); } result = device->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, presentParameters.MultiSampleQuality, FALSE, &mDepthStencil, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Could not create depthstencil surface for new swap chain: %08lX", result); release(); return error(EGL_BAD_ALLOC); } if (mWindow) { mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mRenderTarget); InvalidateRect(mWindow, NULL, FALSE); } else { mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); } mWidth = presentParameters.BackBufferWidth; mHeight = presentParameters.BackBufferHeight; mPresentIntervalDirty = false; }
bool Surface::resetSwapChain(int backbufferWidth, int backbufferHeight) { IDirect3DDevice9 *device = mDisplay->getDevice(); if (device == NULL) { return false; } // Evict all non-render target textures to system memory and release all resources // before reallocating them to free up as much video memory as possible. device->EvictManagedResources(); release(); D3DPRESENT_PARAMETERS presentParameters = {0}; HRESULT result; bool useFlipEx = (LOWORD(GetVersion()) >= 0x61) && mDisplay->isD3d9ExDevice(); // FlipEx causes unseemly stretching when resizing windows AND when one // draws outside of the WM_PAINT callback. While this is seldom a problem in // single process applications, it is particuarly noticeable in multiprocess // applications. Therefore, if the creator process of our window is not in // the current process, disable use of FlipEx. DWORD windowPID; GetWindowThreadProcessId(mWindow, &windowPID); if(windowPID != GetCurrentProcessId()) useFlipEx = false; presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat; // We set BackBufferCount = 1 even when we use D3DSWAPEFFECT_FLIPEX. // We do this because DirectX docs are a bit vague whether to set this to 1 // or 2. The runtime seems to accept 1, so we speculate that either it is // forcing it to 2 without telling us, or better, doing something smart // behind the scenes knowing that we don't need more. presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = mConfig->mRenderTargetFormat; presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = getWindowHandle(); presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented presentParameters.PresentationInterval = mPresentInterval; // Use flipEx on Win7 or greater. if(useFlipEx) presentParameters.SwapEffect = D3DSWAPEFFECT_FLIPEX; else presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; presentParameters.BackBufferWidth = backbufferWidth; presentParameters.BackBufferHeight = backbufferHeight; if (mWindow) { result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); } else { HANDLE *pShareHandle = NULL; if (mDisplay->isD3d9ExDevice()) { pShareHandle = &mShareHandle; } result = device->CreateTexture(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, 1, D3DUSAGE_RENDERTARGET, presentParameters.BackBufferFormat, D3DPOOL_DEFAULT, &mOffscreenTexture, pShareHandle); } if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); release(); return error(EGL_BAD_ALLOC, false); } if (mConfig->mDepthStencilFormat != D3DFMT_UNKNOWN) { result = device->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, presentParameters.MultiSampleQuality, FALSE, &mDepthStencil, NULL); } if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Could not create depthstencil surface for new swap chain: %08lX", result); release(); return error(EGL_BAD_ALLOC, false); } if (mWindow) { mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mRenderTarget); InvalidateRect(mWindow, NULL, FALSE); } else { mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); } mWidth = presentParameters.BackBufferWidth; mHeight = presentParameters.BackBufferHeight; mPresentIntervalDirty = false; return true; }
void Surface::resetSwapChain() { IDirect3DDevice9 *device = mDisplay->getDevice(); D3DPRESENT_PARAMETERS presentParameters = {0}; presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat; presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = mConfig->mRenderTargetFormat; presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = getWindowHandle(); presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented presentParameters.PresentationInterval = Display::convertInterval(mConfig->mMinSwapInterval); presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; RECT windowRect; if (!GetClientRect(getWindowHandle(), &windowRect)) { ASSERT(false); return; } presentParameters.BackBufferWidth = windowRect.right - windowRect.left; presentParameters.BackBufferHeight = windowRect.bottom - windowRect.top; IDirect3DSwapChain9 *swapChain = NULL; HRESULT result = device->CreateAdditionalSwapChain(&presentParameters, &swapChain); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Could not create additional swap chains: %08lX", result); return error(EGL_BAD_ALLOC); } IDirect3DSurface9 *depthStencilSurface = NULL; result = device->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); swapChain->Release(); ERR("Could not create depthstencil surface for new swap chain: %08lX", result); return error(EGL_BAD_ALLOC); } IDirect3DSurface9 *renderTarget = NULL; result = device->CreateRenderTarget(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, presentParameters.BackBufferFormat, presentParameters.MultiSampleType, presentParameters.MultiSampleQuality, FALSE, &renderTarget, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); swapChain->Release(); depthStencilSurface->Release(); ERR("Could not create render target surface for new swap chain: %08lX", result); return error(EGL_BAD_ALLOC); } ASSERT(SUCCEEDED(result)); IDirect3DTexture9 *flipTexture = NULL; result = device->CreateTexture(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, 1, D3DUSAGE_RENDERTARGET, presentParameters.BackBufferFormat, D3DPOOL_DEFAULT, &flipTexture, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); swapChain->Release(); depthStencilSurface->Release(); renderTarget->Release(); ERR("Could not create flip texture for new swap chain: %08lX", result); return error(EGL_BAD_ALLOC); } IDirect3DSurface9 *backBuffer = NULL; swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backBuffer); if (mSwapChain) mSwapChain->Release(); if (mDepthStencil) mDepthStencil->Release(); if (mBackBuffer) mBackBuffer->Release(); if (mRenderTarget) mRenderTarget->Release(); if (mFlipTexture) mFlipTexture->Release(); mWidth = presentParameters.BackBufferWidth; mHeight = presentParameters.BackBufferHeight; mSwapChain = swapChain; mDepthStencil = depthStencilSurface; mBackBuffer = backBuffer; mRenderTarget = renderTarget; mFlipTexture = flipTexture; // The flip state block recorded mFlipTexture so it is now invalid. releaseRecordedState(device); }