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