daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // SwapChain11.cpp: Implements a back-end specific class for the D3D11 swap chain. |
| 8 | |
| 9 | #include "libGLESv2/renderer/SwapChain11.h" |
| 10 | |
| 11 | #include "common/debug.h" |
| 12 | #include "libGLESv2/utilities.h" |
| 13 | #include "libGLESv2/renderer/renderer11_utils.h" |
| 14 | #include "libGLESv2/renderer/Renderer11.h" |
| 15 | #include "libGLESv2/Context.h" |
| 16 | #include "libGLESv2/main.h" |
| 17 | |
| 18 | namespace rx |
| 19 | { |
| 20 | |
| 21 | SwapChain11::SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle, |
| 22 | GLenum backBufferFormat, GLenum depthBufferFormat) |
| 23 | : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) |
| 24 | { |
| 25 | mSwapChain = NULL; |
| 26 | mBackBuffer = NULL; |
| 27 | mBackBufferView = NULL; |
| 28 | mRenderTargetView = NULL; |
| 29 | mDepthStencil = NULL; |
| 30 | mDepthStencilView = NULL; |
| 31 | mOffscreenTexture = NULL; |
| 32 | mWidth = -1; |
| 33 | mHeight = -1; |
| 34 | } |
| 35 | |
| 36 | SwapChain11::~SwapChain11() |
| 37 | { |
| 38 | release(); |
| 39 | } |
| 40 | |
| 41 | void SwapChain11::release() |
| 42 | { |
| 43 | if (mSwapChain) |
| 44 | { |
| 45 | mSwapChain->Release(); |
| 46 | mSwapChain = NULL; |
| 47 | } |
| 48 | |
| 49 | if (mBackBuffer) |
| 50 | { |
| 51 | mBackBuffer->Release(); |
| 52 | mBackBuffer = NULL; |
| 53 | } |
| 54 | |
| 55 | if (mBackBufferView) |
| 56 | { |
| 57 | mBackBufferView->Release(); |
| 58 | mBackBufferView = NULL; |
| 59 | } |
| 60 | |
| 61 | if (mRenderTargetView) |
| 62 | { |
| 63 | mRenderTargetView->Release(); |
| 64 | mRenderTargetView = NULL; |
| 65 | } |
| 66 | |
| 67 | if (mDepthStencil) |
| 68 | { |
| 69 | mDepthStencil->Release(); |
| 70 | mDepthStencil = NULL; |
| 71 | } |
| 72 | |
| 73 | if (mDepthStencilView) |
| 74 | { |
| 75 | mDepthStencilView->Release(); |
| 76 | mDepthStencilView = NULL; |
| 77 | } |
| 78 | |
| 79 | if (mOffscreenTexture) |
| 80 | { |
| 81 | mOffscreenTexture->Release(); |
| 82 | mOffscreenTexture = NULL; |
| 83 | } |
| 84 | |
| 85 | if (mWindow) |
| 86 | mShareHandle = NULL; |
| 87 | } |
| 88 | |
| 89 | EGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) |
| 90 | { |
| 91 | ID3D11Device *device = mRenderer->getDevice(); |
| 92 | |
| 93 | if (device == NULL) |
| 94 | { |
| 95 | return EGL_BAD_ACCESS; |
| 96 | } |
| 97 | |
| 98 | // Release specific resources to free up memory for the new render target, while the |
| 99 | // old render target still exists for the purpose of preserving its contents. |
| 100 | if (mSwapChain) |
| 101 | { |
| 102 | mSwapChain->Release(); |
| 103 | mSwapChain = NULL; |
| 104 | } |
| 105 | |
| 106 | if (mBackBuffer) |
| 107 | { |
| 108 | mBackBuffer->Release(); |
| 109 | mBackBuffer = NULL; |
| 110 | } |
| 111 | |
| 112 | if (mBackBufferView) |
| 113 | { |
| 114 | mBackBufferView->Release(); |
| 115 | mBackBufferView = NULL; |
| 116 | } |
| 117 | |
| 118 | if (mRenderTargetView) // TODO: Preserve the render target content |
| 119 | { |
| 120 | mRenderTargetView->Release(); |
| 121 | mRenderTargetView = NULL; |
| 122 | } |
| 123 | |
| 124 | if (mOffscreenTexture) |
| 125 | { |
| 126 | mOffscreenTexture->Release(); |
| 127 | mOffscreenTexture = NULL; |
| 128 | } |
| 129 | |
| 130 | if (mDepthStencil) |
| 131 | { |
| 132 | mDepthStencil->Release(); |
| 133 | mDepthStencil = NULL; |
| 134 | } |
| 135 | |
| 136 | if (mDepthStencilView) |
| 137 | { |
| 138 | mDepthStencilView->Release(); |
| 139 | mDepthStencilView = NULL; |
| 140 | } |
| 141 | |
| 142 | HANDLE *pShareHandle = NULL; |
| 143 | if (!mWindow && mRenderer->getShareHandleSupport()) |
| 144 | { |
| 145 | pShareHandle = &mShareHandle; |
| 146 | } |
| 147 | |
| 148 | D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; |
| 149 | offscreenTextureDesc.Width = backbufferWidth; |
| 150 | offscreenTextureDesc.Height = backbufferHeight; |
| 151 | offscreenTextureDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); |
| 152 | offscreenTextureDesc.MipLevels = 1; |
| 153 | offscreenTextureDesc.ArraySize = 1; |
| 154 | offscreenTextureDesc.SampleDesc.Count = 1; |
| 155 | offscreenTextureDesc.SampleDesc.Quality = 0; |
| 156 | offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT; |
| 157 | offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET; |
| 158 | offscreenTextureDesc.CPUAccessFlags = 0; |
| 159 | offscreenTextureDesc.MiscFlags = 0; // D3D11_RESOURCE_MISC_SHARED |
| 160 | |
| 161 | HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture); |
| 162 | |
| 163 | if (FAILED(result)) |
| 164 | { |
| 165 | ERR("Could not create offscreen texture: %08lX", result); |
| 166 | release(); |
| 167 | |
| 168 | if (isDeviceLostError(result)) |
| 169 | { |
| 170 | return EGL_CONTEXT_LOST; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | return EGL_BAD_ALLOC; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mRenderTargetView); |
| 179 | ASSERT(SUCCEEDED(result)); |
| 180 | |
| 181 | if (mWindow) |
| 182 | { |
| 183 | IDXGIFactory *factory = mRenderer->getDxgiFactory(); |
| 184 | |
| 185 | DXGI_SWAP_CHAIN_DESC swapChainDesc = {0}; |
| 186 | swapChainDesc.BufferCount = 2; |
| 187 | swapChainDesc.BufferDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); |
| 188 | swapChainDesc.BufferDesc.Width = 1; |
| 189 | swapChainDesc.BufferDesc.Height = 1; |
| 190 | swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; |
| 191 | swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; |
| 192 | swapChainDesc.BufferDesc.RefreshRate.Numerator = 0; |
| 193 | swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; |
| 194 | swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 195 | swapChainDesc.Flags = 0; |
| 196 | swapChainDesc.OutputWindow = mWindow; |
| 197 | swapChainDesc.SampleDesc.Count = 1; |
| 198 | swapChainDesc.SampleDesc.Quality = 0; |
| 199 | swapChainDesc.Windowed = TRUE; |
| 200 | |
| 201 | result = factory->CreateSwapChain(device, &swapChainDesc, &mSwapChain); |
| 202 | |
| 203 | if (FAILED(result)) |
| 204 | { |
| 205 | ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); |
| 206 | release(); |
| 207 | |
| 208 | if (isDeviceLostError(result)) |
| 209 | { |
| 210 | return EGL_CONTEXT_LOST; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | return EGL_BAD_ALLOC; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBuffer); |
| 219 | ASSERT(SUCCEEDED(result)); |
| 220 | |
| 221 | result = device->CreateRenderTargetView(mBackBuffer, NULL, &mBackBufferView); |
| 222 | ASSERT(SUCCEEDED(result)); |
| 223 | } |
| 224 | |
| 225 | if (mDepthBufferFormat != GL_NONE) |
| 226 | { |
| 227 | D3D11_TEXTURE2D_DESC depthStencilDesc = {0}; |
| 228 | depthStencilDesc.Width = backbufferWidth; |
| 229 | depthStencilDesc.Height = backbufferHeight; |
| 230 | depthStencilDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mDepthBufferFormat); |
| 231 | depthStencilDesc.MipLevels = 1; |
| 232 | depthStencilDesc.ArraySize = 1; |
| 233 | depthStencilDesc.SampleDesc.Count = 1; |
| 234 | depthStencilDesc.SampleDesc.Quality = 0; |
| 235 | depthStencilDesc.Usage = D3D11_USAGE_DEFAULT; |
| 236 | depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; |
| 237 | depthStencilDesc.CPUAccessFlags = 0; |
| 238 | depthStencilDesc.MiscFlags = 0; |
| 239 | |
| 240 | result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencil); |
| 241 | |
| 242 | if (FAILED(result)) |
| 243 | { |
| 244 | ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); |
| 245 | release(); |
| 246 | |
| 247 | if (isDeviceLostError(result)) |
| 248 | { |
| 249 | return EGL_CONTEXT_LOST; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | return EGL_BAD_ALLOC; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | result = device->CreateDepthStencilView(mDepthStencil, NULL, &mDepthStencilView); |
| 258 | ASSERT(SUCCEEDED(result)); |
| 259 | } |
| 260 | |
| 261 | mWidth = backbufferWidth; |
| 262 | mHeight = backbufferHeight; |
| 263 | |
| 264 | return EGL_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | // parameters should be validated/clamped by caller |
| 268 | EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
| 269 | { |
| 270 | if (!mSwapChain) |
| 271 | { |
| 272 | return EGL_SUCCESS; |
| 273 | } |
| 274 | |
| 275 | ID3D11Device *device = mRenderer->getDevice(); |
| 276 | |
| 277 | // TODO |
| 278 | UNIMPLEMENTED(); |
| 279 | |
| 280 | return EGL_SUCCESS; |
| 281 | } |
| 282 | |
| 283 | // Increments refcount on view. |
| 284 | // caller must Release() the returned view |
| 285 | ID3D11RenderTargetView *SwapChain11::getRenderTarget() |
| 286 | { |
| 287 | if (mRenderTargetView) |
| 288 | { |
| 289 | mRenderTargetView->AddRef(); |
| 290 | } |
| 291 | |
| 292 | return mRenderTargetView; |
| 293 | } |
| 294 | |
| 295 | // Increments refcount on view. |
| 296 | // caller must Release() the returned view |
| 297 | ID3D11DepthStencilView *SwapChain11::getDepthStencil() |
| 298 | { |
| 299 | if (mDepthStencilView) |
| 300 | { |
| 301 | mDepthStencilView->AddRef(); |
| 302 | } |
| 303 | |
| 304 | return mDepthStencilView; |
| 305 | } |
| 306 | |
| 307 | // Increments refcount on texture. |
| 308 | // caller must Release() the returned texture |
| 309 | ID3D11Texture2D *SwapChain11::getOffscreenTexture() |
| 310 | { |
| 311 | if (mOffscreenTexture) |
| 312 | { |
| 313 | mOffscreenTexture->AddRef(); |
| 314 | } |
| 315 | |
| 316 | return mOffscreenTexture; |
| 317 | } |
| 318 | |
daniel@transgaming.com | d733bb8 | 2012-11-28 20:53:40 +0000 | [diff] [blame] | 319 | SwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain) |
| 320 | { |
| 321 | ASSERT(dynamic_cast<rx::SwapChain11*>(swapChain) != NULL); |
| 322 | return static_cast<rx::SwapChain11*>(swapChain); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 323 | } |
daniel@transgaming.com | d733bb8 | 2012-11-28 20:53:40 +0000 | [diff] [blame] | 324 | |
| 325 | } |
| 326 | |