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; |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 26 | mBackBufferTexture = NULL; |
| 27 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 28 | mOffscreenTexture = NULL; |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 29 | mOffscreenRTView = NULL; |
| 30 | mDepthStencilTexture = NULL; |
| 31 | mDepthStencilDSView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 49 | if (mBackBufferTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 50 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 51 | mBackBufferTexture->Release(); |
| 52 | mBackBufferTexture = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 55 | if (mBackBufferRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 56 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 57 | mBackBufferRTView->Release(); |
| 58 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (mOffscreenTexture) |
| 62 | { |
| 63 | mOffscreenTexture->Release(); |
| 64 | mOffscreenTexture = NULL; |
| 65 | } |
| 66 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 67 | if (mOffscreenRTView) |
| 68 | { |
| 69 | mOffscreenRTView->Release(); |
| 70 | mOffscreenRTView = NULL; |
| 71 | } |
| 72 | |
| 73 | if (mDepthStencilTexture) |
| 74 | { |
| 75 | mDepthStencilTexture->Release(); |
| 76 | mDepthStencilTexture = NULL; |
| 77 | } |
| 78 | |
| 79 | if (mDepthStencilDSView) |
| 80 | { |
| 81 | mDepthStencilDSView->Release(); |
| 82 | mDepthStencilDSView = NULL; |
| 83 | } |
| 84 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 106 | if (mBackBufferTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 107 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 108 | mBackBufferTexture->Release(); |
| 109 | mBackBufferTexture = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 112 | if (mBackBufferRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 113 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 114 | mBackBufferRTView->Release(); |
| 115 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (mOffscreenTexture) |
| 119 | { |
| 120 | mOffscreenTexture->Release(); |
| 121 | mOffscreenTexture = NULL; |
| 122 | } |
| 123 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 124 | if (mOffscreenRTView) // TODO: Preserve the render target content |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 125 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 126 | mOffscreenRTView->Release(); |
| 127 | mOffscreenRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 128 | } |
| 129 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 130 | if (mDepthStencilTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 131 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 132 | mDepthStencilTexture->Release(); |
| 133 | mDepthStencilTexture = NULL; |
| 134 | } |
| 135 | |
| 136 | if (mDepthStencilDSView) |
| 137 | { |
| 138 | mDepthStencilDSView->Release(); |
| 139 | mDepthStencilDSView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 178 | result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mOffscreenRTView); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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); |
daniel@transgaming.com | 567b9cf | 2012-11-28 21:04:46 +0000 | [diff] [blame^] | 188 | swapChainDesc.BufferDesc.Width = backbufferWidth; |
| 189 | swapChainDesc.BufferDesc.Height = backbufferHeight; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 218 | result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 219 | ASSERT(SUCCEEDED(result)); |
| 220 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 221 | result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 240 | result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencilTexture); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 257 | result = device->CreateDepthStencilView(mDepthStencilTexture, NULL, &mDepthStencilDSView); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 287 | if (mOffscreenRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 288 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 289 | mOffscreenRTView->AddRef(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 290 | } |
| 291 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 292 | return mOffscreenRTView; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Increments refcount on view. |
| 296 | // caller must Release() the returned view |
| 297 | ID3D11DepthStencilView *SwapChain11::getDepthStencil() |
| 298 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 299 | if (mDepthStencilDSView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 300 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 301 | mDepthStencilDSView->AddRef(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 302 | } |
| 303 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 304 | return mDepthStencilDSView; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |