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