daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 2 | // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | 8dc8e27 | 2013-01-11 04:10:45 +0000 | [diff] [blame] | 18 | #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" |
shannon.woods@transgaming.com | 2570c34 | 2013-01-25 21:50:22 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 20 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 21 | namespace rx |
| 22 | { |
| 23 | |
| 24 | SwapChain11::SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle, |
| 25 | GLenum backBufferFormat, GLenum depthBufferFormat) |
| 26 | : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) |
| 27 | { |
| 28 | mSwapChain = NULL; |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 29 | mBackBufferTexture = NULL; |
| 30 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 31 | mOffscreenTexture = NULL; |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 32 | mOffscreenRTView = NULL; |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 33 | mOffscreenSRView = NULL; |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 34 | mDepthStencilTexture = NULL; |
| 35 | mDepthStencilDSView = NULL; |
daniel@transgaming.com | f06e539 | 2013-01-11 21:15:50 +0000 | [diff] [blame] | 36 | mQuadVB = NULL; |
| 37 | mPassThroughSampler = NULL; |
| 38 | mPassThroughIL = NULL; |
| 39 | mPassThroughVS = NULL; |
| 40 | mPassThroughPS = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 41 | mWidth = -1; |
| 42 | mHeight = -1; |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 43 | mAppCreatedShareHandle = mShareHandle != NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SwapChain11::~SwapChain11() |
| 47 | { |
| 48 | release(); |
| 49 | } |
| 50 | |
| 51 | void SwapChain11::release() |
| 52 | { |
| 53 | if (mSwapChain) |
| 54 | { |
| 55 | mSwapChain->Release(); |
| 56 | mSwapChain = NULL; |
| 57 | } |
| 58 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 59 | if (mBackBufferTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 60 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 61 | mBackBufferTexture->Release(); |
| 62 | mBackBufferTexture = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 63 | } |
| 64 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 65 | if (mBackBufferRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 66 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 67 | mBackBufferRTView->Release(); |
| 68 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (mOffscreenTexture) |
| 72 | { |
| 73 | mOffscreenTexture->Release(); |
| 74 | mOffscreenTexture = NULL; |
| 75 | } |
| 76 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 77 | if (mOffscreenRTView) |
| 78 | { |
| 79 | mOffscreenRTView->Release(); |
| 80 | mOffscreenRTView = NULL; |
| 81 | } |
| 82 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 83 | if (mOffscreenSRView) |
| 84 | { |
| 85 | mOffscreenSRView->Release(); |
| 86 | mOffscreenSRView = NULL; |
| 87 | } |
| 88 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 89 | if (mDepthStencilTexture) |
| 90 | { |
| 91 | mDepthStencilTexture->Release(); |
| 92 | mDepthStencilTexture = NULL; |
| 93 | } |
| 94 | |
| 95 | if (mDepthStencilDSView) |
| 96 | { |
| 97 | mDepthStencilDSView->Release(); |
| 98 | mDepthStencilDSView = NULL; |
| 99 | } |
| 100 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 101 | if (mQuadVB) |
| 102 | { |
| 103 | mQuadVB->Release(); |
| 104 | mQuadVB = NULL; |
| 105 | } |
| 106 | |
| 107 | if (mPassThroughSampler) |
| 108 | { |
| 109 | mPassThroughSampler->Release(); |
| 110 | mPassThroughSampler = NULL; |
| 111 | } |
| 112 | |
| 113 | if (mPassThroughIL) |
| 114 | { |
| 115 | mPassThroughIL->Release(); |
| 116 | mPassThroughIL = NULL; |
| 117 | } |
| 118 | |
| 119 | if (mPassThroughVS) |
| 120 | { |
| 121 | mPassThroughVS->Release(); |
| 122 | mPassThroughVS = NULL; |
| 123 | } |
| 124 | |
| 125 | if (mPassThroughPS) |
| 126 | { |
| 127 | mPassThroughPS->Release(); |
| 128 | mPassThroughPS = NULL; |
| 129 | } |
| 130 | |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 131 | if (!mAppCreatedShareHandle) |
| 132 | { |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 133 | mShareHandle = NULL; |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 134 | } |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | EGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) |
| 138 | { |
| 139 | ID3D11Device *device = mRenderer->getDevice(); |
| 140 | |
| 141 | if (device == NULL) |
| 142 | { |
| 143 | return EGL_BAD_ACCESS; |
| 144 | } |
| 145 | |
| 146 | // Release specific resources to free up memory for the new render target, while the |
| 147 | // old render target still exists for the purpose of preserving its contents. |
| 148 | if (mSwapChain) |
| 149 | { |
| 150 | mSwapChain->Release(); |
| 151 | mSwapChain = NULL; |
| 152 | } |
| 153 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 154 | if (mBackBufferTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 155 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 156 | mBackBufferTexture->Release(); |
| 157 | mBackBufferTexture = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 158 | } |
| 159 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 160 | if (mBackBufferRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 161 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 162 | mBackBufferRTView->Release(); |
| 163 | mBackBufferRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (mOffscreenTexture) |
| 167 | { |
| 168 | mOffscreenTexture->Release(); |
| 169 | mOffscreenTexture = NULL; |
| 170 | } |
| 171 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 172 | if (mOffscreenRTView) // TODO: Preserve the render target content |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 173 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 174 | mOffscreenRTView->Release(); |
| 175 | mOffscreenRTView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 176 | } |
| 177 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 178 | if (mOffscreenSRView) |
| 179 | { |
| 180 | mOffscreenSRView->Release(); |
| 181 | mOffscreenSRView = NULL; |
| 182 | } |
| 183 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 184 | if (mDepthStencilTexture) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 185 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 186 | mDepthStencilTexture->Release(); |
| 187 | mDepthStencilTexture = NULL; |
| 188 | } |
| 189 | |
| 190 | if (mDepthStencilDSView) |
| 191 | { |
| 192 | mDepthStencilDSView->Release(); |
| 193 | mDepthStencilDSView = NULL; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 194 | } |
| 195 | |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 196 | // If the app passed in a share handle, open the resource |
| 197 | // See EGL_ANGLE_d3d_share_handle_client_buffer |
| 198 | if (mAppCreatedShareHandle) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 199 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 200 | ID3D11Resource *tempResource11; |
| 201 | HRESULT result = device->OpenSharedResource(mShareHandle, __uuidof(ID3D11Resource), (void**)&tempResource11); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 202 | |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 203 | if (FAILED(result)) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 204 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 205 | ERR("Failed to open the swap chain pbuffer share handle: %08lX", result); |
| 206 | release(); |
| 207 | return EGL_BAD_PARAMETER; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 208 | } |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 209 | |
| 210 | result = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&mOffscreenTexture); |
| 211 | tempResource11->Release(); |
| 212 | |
| 213 | if (FAILED(result)) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 214 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 215 | ERR("Failed to query texture2d interface in pbuffer share handle: %08lX", result); |
| 216 | release(); |
| 217 | return EGL_BAD_PARAMETER; |
| 218 | } |
| 219 | |
| 220 | // Validate offscreen texture parameters |
| 221 | D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; |
| 222 | mOffscreenTexture->GetDesc(&offscreenTextureDesc); |
| 223 | |
| 224 | if (offscreenTextureDesc.Width != (UINT)backbufferWidth |
| 225 | || offscreenTextureDesc.Height != (UINT)backbufferHeight |
| 226 | || offscreenTextureDesc.Format != gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat) |
| 227 | || offscreenTextureDesc.MipLevels != 1 |
| 228 | || offscreenTextureDesc.ArraySize != 1) |
| 229 | { |
| 230 | ERR("Invalid texture parameters in the shared offscreen texture pbuffer"); |
| 231 | release(); |
| 232 | return EGL_BAD_PARAMETER; |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | const bool useSharedResource = !mWindow && mRenderer->getShareHandleSupport(); |
| 238 | |
| 239 | D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; |
| 240 | offscreenTextureDesc.Width = backbufferWidth; |
| 241 | offscreenTextureDesc.Height = backbufferHeight; |
| 242 | offscreenTextureDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); |
| 243 | offscreenTextureDesc.MipLevels = 1; |
| 244 | offscreenTextureDesc.ArraySize = 1; |
| 245 | offscreenTextureDesc.SampleDesc.Count = 1; |
| 246 | offscreenTextureDesc.SampleDesc.Quality = 0; |
| 247 | offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT; |
| 248 | offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; |
| 249 | offscreenTextureDesc.CPUAccessFlags = 0; |
| 250 | offscreenTextureDesc.MiscFlags = useSharedResource ? D3D11_RESOURCE_MISC_SHARED : 0; |
| 251 | |
| 252 | HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture); |
| 253 | |
| 254 | if (FAILED(result)) |
| 255 | { |
| 256 | ERR("Could not create offscreen texture: %08lX", result); |
| 257 | release(); |
| 258 | |
| 259 | if (isDeviceLostError(result)) |
| 260 | { |
| 261 | return EGL_CONTEXT_LOST; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | return EGL_BAD_ALLOC; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | d3d11::SetDebugName(mOffscreenTexture, "Offscreen texture"); |
| 270 | |
| 271 | // EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client |
| 272 | if (useSharedResource) |
| 273 | { |
| 274 | IDXGIResource *offscreenTextureResource = NULL; |
| 275 | result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource); |
| 276 | |
| 277 | // Fall back to no share handle on failure |
| 278 | if (FAILED(result)) |
| 279 | { |
| 280 | ERR("Could not query offscreen texture resource: %08lX", result); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | result = offscreenTextureResource->GetSharedHandle(&mShareHandle); |
| 285 | |
| 286 | if (FAILED(result)) |
| 287 | { |
| 288 | mShareHandle = NULL; |
| 289 | ERR("Could not get offscreen texture shared handle: %08lX", result); |
| 290 | } |
| 291 | } |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 295 | HRESULT result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mOffscreenRTView); |
| 296 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 297 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 298 | d3d11::SetDebugName(mOffscreenRTView, "Offscreen render target"); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 299 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 300 | result = device->CreateShaderResourceView(mOffscreenTexture, NULL, &mOffscreenSRView); |
| 301 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 302 | d3d11::SetDebugName(mOffscreenSRView, "Offscreen shader resource"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 303 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 304 | if (mWindow) |
| 305 | { |
shannon.woods@transgaming.com | 7d4b482 | 2013-01-25 21:52:27 +0000 | [diff] [blame] | 306 | // We cannot create a swap chain for an HWND that is owned by a different process |
| 307 | DWORD currentProcessId = GetCurrentProcessId(); |
| 308 | DWORD wndProcessId; |
| 309 | GetWindowThreadProcessId(mWindow, &wndProcessId); |
| 310 | |
| 311 | if (currentProcessId != wndProcessId) |
| 312 | { |
| 313 | ERR("Could not create swap chain, window owned by different process"); |
| 314 | release(); |
| 315 | return EGL_BAD_NATIVE_WINDOW; |
| 316 | } |
| 317 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 318 | IDXGIFactory *factory = mRenderer->getDxgiFactory(); |
| 319 | |
| 320 | DXGI_SWAP_CHAIN_DESC swapChainDesc = {0}; |
| 321 | swapChainDesc.BufferCount = 2; |
| 322 | swapChainDesc.BufferDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); |
daniel@transgaming.com | 567b9cf | 2012-11-28 21:04:46 +0000 | [diff] [blame] | 323 | swapChainDesc.BufferDesc.Width = backbufferWidth; |
| 324 | swapChainDesc.BufferDesc.Height = backbufferHeight; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 325 | swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; |
| 326 | swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; |
| 327 | swapChainDesc.BufferDesc.RefreshRate.Numerator = 0; |
| 328 | swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; |
| 329 | swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 330 | swapChainDesc.Flags = 0; |
| 331 | swapChainDesc.OutputWindow = mWindow; |
| 332 | swapChainDesc.SampleDesc.Count = 1; |
| 333 | swapChainDesc.SampleDesc.Quality = 0; |
| 334 | swapChainDesc.Windowed = TRUE; |
| 335 | |
| 336 | result = factory->CreateSwapChain(device, &swapChainDesc, &mSwapChain); |
| 337 | |
| 338 | if (FAILED(result)) |
| 339 | { |
| 340 | ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); |
| 341 | release(); |
| 342 | |
| 343 | if (isDeviceLostError(result)) |
| 344 | { |
| 345 | return EGL_CONTEXT_LOST; |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | return EGL_BAD_ALLOC; |
| 350 | } |
| 351 | } |
| 352 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 353 | result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 354 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 355 | d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture"); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 356 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 357 | result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 358 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 359 | d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target"); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (mDepthBufferFormat != GL_NONE) |
| 363 | { |
| 364 | D3D11_TEXTURE2D_DESC depthStencilDesc = {0}; |
| 365 | depthStencilDesc.Width = backbufferWidth; |
| 366 | depthStencilDesc.Height = backbufferHeight; |
| 367 | depthStencilDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mDepthBufferFormat); |
| 368 | depthStencilDesc.MipLevels = 1; |
| 369 | depthStencilDesc.ArraySize = 1; |
| 370 | depthStencilDesc.SampleDesc.Count = 1; |
| 371 | depthStencilDesc.SampleDesc.Quality = 0; |
| 372 | depthStencilDesc.Usage = D3D11_USAGE_DEFAULT; |
| 373 | depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; |
| 374 | depthStencilDesc.CPUAccessFlags = 0; |
| 375 | depthStencilDesc.MiscFlags = 0; |
| 376 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 377 | result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencilTexture); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 378 | if (FAILED(result)) |
| 379 | { |
| 380 | ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); |
| 381 | release(); |
| 382 | |
| 383 | if (isDeviceLostError(result)) |
| 384 | { |
| 385 | return EGL_CONTEXT_LOST; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | return EGL_BAD_ALLOC; |
| 390 | } |
| 391 | } |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 392 | d3d11::SetDebugName(mDepthStencilTexture, "Depth stencil texture"); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 393 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 394 | result = device->CreateDepthStencilView(mDepthStencilTexture, NULL, &mDepthStencilDSView); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 395 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 396 | d3d11::SetDebugName(mDepthStencilDSView, "Depth stencil view"); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 397 | } |
| 398 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 399 | D3D11_BUFFER_DESC vbDesc; |
shannon.woods@transgaming.com | f3d8207 | 2013-01-25 21:50:43 +0000 | [diff] [blame] | 400 | vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 401 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 402 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 403 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 404 | vbDesc.MiscFlags = 0; |
| 405 | vbDesc.StructureByteStride = 0; |
| 406 | |
| 407 | result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB); |
| 408 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 409 | d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 410 | |
| 411 | D3D11_SAMPLER_DESC samplerDesc; |
| 412 | samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; |
| 413 | samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 414 | samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 415 | samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 416 | samplerDesc.MipLODBias = 0.0f; |
| 417 | samplerDesc.MaxAnisotropy = 0; |
| 418 | samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; |
| 419 | samplerDesc.BorderColor[0] = 0.0f; |
| 420 | samplerDesc.BorderColor[1] = 0.0f; |
| 421 | samplerDesc.BorderColor[2] = 0.0f; |
| 422 | samplerDesc.BorderColor[3] = 0.0f; |
| 423 | samplerDesc.MinLOD = 0; |
| 424 | samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; |
| 425 | |
| 426 | result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); |
| 427 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 428 | d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 429 | |
| 430 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 431 | { |
| 432 | { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 433 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 434 | }; |
| 435 | |
| 436 | result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mPassThroughIL); |
| 437 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 438 | d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 439 | |
| 440 | result = device->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mPassThroughVS); |
| 441 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 442 | d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 443 | |
shannon.woods@transgaming.com | 2570c34 | 2013-01-25 21:50:22 +0000 | [diff] [blame] | 444 | result = device->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mPassThroughPS); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 445 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | ad3d827 | 2013-01-11 04:11:14 +0000 | [diff] [blame] | 446 | d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader"); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 447 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 448 | mWidth = backbufferWidth; |
| 449 | mHeight = backbufferHeight; |
| 450 | |
| 451 | return EGL_SUCCESS; |
| 452 | } |
| 453 | |
| 454 | // parameters should be validated/clamped by caller |
| 455 | EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
| 456 | { |
| 457 | if (!mSwapChain) |
| 458 | { |
| 459 | return EGL_SUCCESS; |
| 460 | } |
| 461 | |
| 462 | ID3D11Device *device = mRenderer->getDevice(); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 463 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 464 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 465 | // Set vertices |
| 466 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 467 | HRESULT result = deviceContext->Map(mQuadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 468 | if (FAILED(result)) |
| 469 | { |
| 470 | return EGL_BAD_ACCESS; |
| 471 | } |
| 472 | |
shannon.woods@transgaming.com | f3d8207 | 2013-01-25 21:50:43 +0000 | [diff] [blame] | 473 | d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 474 | |
| 475 | // Create a quad in homogeneous coordinates |
| 476 | float x1 = (x / mWidth) * 2.0f - 1.0f; |
| 477 | float y1 = ((mHeight - y - height) / mHeight) * 2.0f - 1.0f; |
| 478 | float x2 = ((x + width) / mWidth) * 2.0f - 1.0f; |
| 479 | float y2 = ((mHeight - y) / mHeight) * 2.0f - 1.0f; |
| 480 | |
| 481 | float u1 = x / float(mWidth); |
| 482 | float v1 = y / float(mHeight); |
| 483 | float u2 = (x + width) / float(mWidth); |
| 484 | float v2 = (y + height) / float(mHeight); |
| 485 | |
shannon.woods@transgaming.com | f3d8207 | 2013-01-25 21:50:43 +0000 | [diff] [blame] | 486 | d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v1); |
| 487 | d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2); |
| 488 | d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1); |
| 489 | d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 490 | |
| 491 | deviceContext->Unmap(mQuadVB, 0); |
| 492 | |
shannon.woods@transgaming.com | f3d8207 | 2013-01-25 21:50:43 +0000 | [diff] [blame] | 493 | static UINT stride = sizeof(d3d11::PositionTexCoordVertex); |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 494 | static UINT startIdx = 0; |
| 495 | deviceContext->IASetVertexBuffers(0, 1, &mQuadVB, &stride, &startIdx); |
| 496 | |
| 497 | // Apply state |
| 498 | deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); |
| 499 | |
| 500 | static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 501 | deviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF); |
| 502 | |
| 503 | deviceContext->RSSetState(NULL); |
| 504 | |
| 505 | // Apply shaders |
| 506 | deviceContext->IASetInputLayout(mPassThroughIL); |
| 507 | deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 508 | deviceContext->VSSetShader(mPassThroughVS, NULL, 0); |
| 509 | deviceContext->PSSetShader(mPassThroughPS, NULL, 0); |
| 510 | |
| 511 | // Apply render targets |
| 512 | deviceContext->OMSetRenderTargets(1, &mBackBufferRTView, NULL); |
| 513 | |
daniel@transgaming.com | 9799a2f | 2013-01-11 04:09:27 +0000 | [diff] [blame] | 514 | // Set the viewport |
| 515 | D3D11_VIEWPORT viewport; |
| 516 | viewport.TopLeftX = 0; |
| 517 | viewport.TopLeftY = 0; |
| 518 | viewport.Width = mWidth; |
| 519 | viewport.Height = mHeight; |
| 520 | viewport.MinDepth = 0.0f; |
| 521 | viewport.MaxDepth = 1.0f; |
| 522 | deviceContext->RSSetViewports(1, &viewport); |
| 523 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 524 | // Apply textures |
| 525 | deviceContext->PSSetShaderResources(0, 1, &mOffscreenSRView); |
| 526 | deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler); |
| 527 | |
| 528 | // Draw |
| 529 | deviceContext->Draw(4, 0); |
| 530 | mSwapChain->Present(0, 0); |
| 531 | |
| 532 | // Unbind |
| 533 | static ID3D11ShaderResourceView *const nullSRV = NULL; |
| 534 | deviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 535 | |
| 536 | static ID3D11RenderTargetView *const nullRTV = NULL; |
| 537 | deviceContext->OMSetRenderTargets(1, &nullRTV, NULL); |
| 538 | |
daniel@transgaming.com | e097047 | 2012-11-28 21:05:07 +0000 | [diff] [blame] | 539 | mRenderer->markAllStateDirty(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 540 | |
| 541 | return EGL_SUCCESS; |
| 542 | } |
| 543 | |
shannon.woods@transgaming.com | 5c25ed2 | 2013-01-25 21:49:51 +0000 | [diff] [blame] | 544 | // Increments refcount on texture. |
| 545 | // caller must Release() the returned texture |
| 546 | ID3D11Texture2D *SwapChain11::getOffscreenTexture() |
| 547 | { |
| 548 | if (mOffscreenTexture) |
| 549 | { |
| 550 | mOffscreenTexture->AddRef(); |
| 551 | } |
| 552 | |
| 553 | return mOffscreenTexture; |
| 554 | } |
| 555 | |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 556 | // Increments refcount on view. |
| 557 | // caller must Release() the returned view |
| 558 | ID3D11RenderTargetView *SwapChain11::getRenderTarget() |
| 559 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 560 | if (mOffscreenRTView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 561 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 562 | mOffscreenRTView->AddRef(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 563 | } |
| 564 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 565 | return mOffscreenRTView; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Increments refcount on view. |
| 569 | // caller must Release() the returned view |
shannon.woods@transgaming.com | 5c25ed2 | 2013-01-25 21:49:51 +0000 | [diff] [blame] | 570 | ID3D11ShaderResourceView *SwapChain11::getRenderTargetShaderResource() |
| 571 | { |
| 572 | if (mOffscreenSRView) |
| 573 | { |
| 574 | mOffscreenSRView->AddRef(); |
| 575 | } |
| 576 | |
| 577 | return mOffscreenSRView; |
| 578 | } |
| 579 | |
| 580 | // Increments refcount on view. |
| 581 | // caller must Release() the returned view |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 582 | ID3D11DepthStencilView *SwapChain11::getDepthStencil() |
| 583 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 584 | if (mDepthStencilDSView) |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 585 | { |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 586 | mDepthStencilDSView->AddRef(); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 587 | } |
| 588 | |
daniel@transgaming.com | c8c70ad | 2012-11-28 21:04:37 +0000 | [diff] [blame] | 589 | return mDepthStencilDSView; |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 590 | } |
| 591 | |
daniel@transgaming.com | d733bb8 | 2012-11-28 20:53:40 +0000 | [diff] [blame] | 592 | SwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain) |
| 593 | { |
| 594 | ASSERT(dynamic_cast<rx::SwapChain11*>(swapChain) != NULL); |
| 595 | return static_cast<rx::SwapChain11*>(swapChain); |
daniel@transgaming.com | 32fdf82 | 2012-11-28 20:53:30 +0000 | [diff] [blame] | 596 | } |
daniel@transgaming.com | d733bb8 | 2012-11-28 20:53:40 +0000 | [diff] [blame] | 597 | |
| 598 | } |