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