Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d / d3d11 / SwapChain11.cpp
1 //
2 // Copyright (c) 2012-2014 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/d3d/d3d11/SwapChain11.h"
10 #include "libGLESv2/renderer/d3d/d3d11/renderer11_utils.h"
11 #include "libGLESv2/renderer/d3d/d3d11/formatutils11.h"
12 #include "libGLESv2/renderer/d3d/d3d11/Renderer11.h"
13
14 // Precompiled shaders
15 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/passthrough2d11vs.h"
16 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/passthroughrgba2d11ps.h"
17
18 #include "common/features.h"
19 #include "common/NativeWindow.h"
20
21 namespace rx
22 {
23
24 SwapChain11::SwapChain11(Renderer11 *renderer, rx::NativeWindow nativeWindow, HANDLE shareHandle,
25                          GLenum backBufferFormat, GLenum depthBufferFormat)
26     : mRenderer(renderer),
27       SwapChain(nativeWindow, shareHandle, backBufferFormat, depthBufferFormat)
28 {
29     mSwapChain = NULL;
30     mBackBufferTexture = NULL;
31     mBackBufferRTView = NULL;
32     mOffscreenTexture = NULL;
33     mOffscreenRTView = NULL;
34     mOffscreenSRView = NULL;
35     mDepthStencilTexture = NULL;
36     mDepthStencilDSView = NULL;
37     mDepthStencilSRView = NULL;
38     mQuadVB = NULL;
39     mPassThroughSampler = NULL;
40     mPassThroughIL = NULL;
41     mPassThroughVS = NULL;
42     mPassThroughPS = NULL;
43     mWidth = -1;
44     mHeight = -1;
45     mSwapInterval = 0;
46     mAppCreatedShareHandle = mShareHandle != NULL;
47     mPassThroughResourcesInit = false;
48 }
49
50 SwapChain11::~SwapChain11()
51 {
52     release();
53 }
54
55 void SwapChain11::release()
56 {
57     SafeRelease(mSwapChain);
58     SafeRelease(mBackBufferTexture);
59     SafeRelease(mBackBufferRTView);
60     SafeRelease(mOffscreenTexture);
61     SafeRelease(mOffscreenRTView);
62     SafeRelease(mOffscreenSRView);
63     SafeRelease(mDepthStencilTexture);
64     SafeRelease(mDepthStencilDSView);
65     SafeRelease(mDepthStencilSRView);
66     SafeRelease(mQuadVB);
67     SafeRelease(mPassThroughSampler);
68     SafeRelease(mPassThroughIL);
69     SafeRelease(mPassThroughVS);
70     SafeRelease(mPassThroughPS);
71
72     if (!mAppCreatedShareHandle)
73     {
74         mShareHandle = NULL;
75     }
76 }
77
78 void SwapChain11::releaseOffscreenTexture()
79 {
80     SafeRelease(mOffscreenTexture);
81     SafeRelease(mOffscreenRTView);
82     SafeRelease(mOffscreenSRView);
83     SafeRelease(mDepthStencilTexture);
84     SafeRelease(mDepthStencilDSView);
85     SafeRelease(mDepthStencilSRView);
86 }
87
88 EGLint SwapChain11::resetOffscreenTexture(int backbufferWidth, int backbufferHeight)
89 {
90     ID3D11Device *device = mRenderer->getDevice();
91
92     ASSERT(device != NULL);
93
94     // D3D11 does not allow zero size textures
95     ASSERT(backbufferWidth >= 1);
96     ASSERT(backbufferHeight >= 1);
97
98     // Preserve the render target content
99     ID3D11Texture2D *previousOffscreenTexture = mOffscreenTexture;
100     if (previousOffscreenTexture)
101     {
102         previousOffscreenTexture->AddRef();
103     }
104     const int previousWidth = mWidth;
105     const int previousHeight = mHeight;
106
107     releaseOffscreenTexture();
108
109     const d3d11::TextureFormat &backbufferFormatInfo = d3d11::GetTextureFormatInfo(mBackBufferFormat);
110
111     // If the app passed in a share handle, open the resource
112     // See EGL_ANGLE_d3d_share_handle_client_buffer
113     if (mAppCreatedShareHandle)
114     {
115         ID3D11Resource *tempResource11;
116         HRESULT result = device->OpenSharedResource(mShareHandle, __uuidof(ID3D11Resource), (void**)&tempResource11);
117
118         if (FAILED(result))
119         {
120             ERR("Failed to open the swap chain pbuffer share handle: %08lX", result);
121             release();
122             return EGL_BAD_PARAMETER;
123         }
124
125         result = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&mOffscreenTexture);
126         SafeRelease(tempResource11);
127
128         if (FAILED(result))
129         {
130             ERR("Failed to query texture2d interface in pbuffer share handle: %08lX", result);
131             release();
132             return EGL_BAD_PARAMETER;
133         }
134
135         // Validate offscreen texture parameters
136         D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0};
137         mOffscreenTexture->GetDesc(&offscreenTextureDesc);
138
139         if (offscreenTextureDesc.Width != (UINT)backbufferWidth ||
140             offscreenTextureDesc.Height != (UINT)backbufferHeight ||
141             offscreenTextureDesc.Format != backbufferFormatInfo.texFormat ||
142             offscreenTextureDesc.MipLevels != 1 ||
143             offscreenTextureDesc.ArraySize != 1)
144         {
145             ERR("Invalid texture parameters in the shared offscreen texture pbuffer");
146             release();
147             return EGL_BAD_PARAMETER;
148         }
149     }
150     else
151     {
152         const bool useSharedResource = !mNativeWindow.getNativeWindow() && mRenderer->getShareHandleSupport();
153
154         D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0};
155         offscreenTextureDesc.Width = backbufferWidth;
156         offscreenTextureDesc.Height = backbufferHeight;
157         offscreenTextureDesc.Format = backbufferFormatInfo.texFormat;
158         offscreenTextureDesc.MipLevels = 1;
159         offscreenTextureDesc.ArraySize = 1;
160         offscreenTextureDesc.SampleDesc.Count = 1;
161         offscreenTextureDesc.SampleDesc.Quality = 0;
162         offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT;
163         offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
164         offscreenTextureDesc.CPUAccessFlags = 0;
165         offscreenTextureDesc.MiscFlags = useSharedResource ? D3D11_RESOURCE_MISC_SHARED : 0;
166
167         HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture);
168
169         if (FAILED(result))
170         {
171             ERR("Could not create offscreen texture: %08lX", result);
172             release();
173
174             if (d3d11::isDeviceLostError(result))
175             {
176                 return EGL_CONTEXT_LOST;
177             }
178             else
179             {
180                 return EGL_BAD_ALLOC;
181             }
182         }
183
184         d3d11::SetDebugName(mOffscreenTexture, "Offscreen back buffer texture");
185
186         // EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client
187         if (useSharedResource)
188         {
189             IDXGIResource *offscreenTextureResource = NULL;
190             result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource);
191
192             // Fall back to no share handle on failure
193             if (FAILED(result))
194             {
195                 ERR("Could not query offscreen texture resource: %08lX", result);
196             }
197             else
198             {
199                 result = offscreenTextureResource->GetSharedHandle(&mShareHandle);
200                 SafeRelease(offscreenTextureResource);
201
202                 if (FAILED(result))
203                 {
204                     mShareHandle = NULL;
205                     ERR("Could not get offscreen texture shared handle: %08lX", result);
206                 }
207             }
208         }
209     }
210
211
212     D3D11_RENDER_TARGET_VIEW_DESC offscreenRTVDesc;
213     offscreenRTVDesc.Format = backbufferFormatInfo.rtvFormat;
214     offscreenRTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
215     offscreenRTVDesc.Texture2D.MipSlice = 0;
216
217     HRESULT result = device->CreateRenderTargetView(mOffscreenTexture, &offscreenRTVDesc, &mOffscreenRTView);
218     ASSERT(SUCCEEDED(result));
219     d3d11::SetDebugName(mOffscreenRTView, "Offscreen back buffer render target");
220
221     D3D11_SHADER_RESOURCE_VIEW_DESC offscreenSRVDesc;
222     offscreenSRVDesc.Format = backbufferFormatInfo.srvFormat;
223     offscreenSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
224     offscreenSRVDesc.Texture2D.MostDetailedMip = 0;
225     offscreenSRVDesc.Texture2D.MipLevels = -1;
226
227     result = device->CreateShaderResourceView(mOffscreenTexture, &offscreenSRVDesc, &mOffscreenSRView);
228     ASSERT(SUCCEEDED(result));
229     d3d11::SetDebugName(mOffscreenSRView, "Offscreen back buffer shader resource");
230
231     const d3d11::TextureFormat &depthBufferFormatInfo = d3d11::GetTextureFormatInfo(mDepthBufferFormat);
232
233     if (mDepthBufferFormat != GL_NONE)
234     {
235         D3D11_TEXTURE2D_DESC depthStencilTextureDesc;
236         depthStencilTextureDesc.Width = backbufferWidth;
237         depthStencilTextureDesc.Height = backbufferHeight;
238         depthStencilTextureDesc.Format = depthBufferFormatInfo.texFormat;
239         depthStencilTextureDesc.MipLevels = 1;
240         depthStencilTextureDesc.ArraySize = 1;
241         depthStencilTextureDesc.SampleDesc.Count = 1;
242         depthStencilTextureDesc.SampleDesc.Quality = 0;
243         depthStencilTextureDesc.Usage = D3D11_USAGE_DEFAULT;
244         depthStencilTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
245         depthStencilTextureDesc.CPUAccessFlags = 0;
246         depthStencilTextureDesc.MiscFlags = 0;
247
248         result = device->CreateTexture2D(&depthStencilTextureDesc, NULL, &mDepthStencilTexture);
249         if (FAILED(result))
250         {
251             ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result);
252             release();
253
254             if (d3d11::isDeviceLostError(result))
255             {
256                 return EGL_CONTEXT_LOST;
257             }
258             else
259             {
260                 return EGL_BAD_ALLOC;
261             }
262         }
263         d3d11::SetDebugName(mDepthStencilTexture, "Offscreen depth stencil texture");
264
265         D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilDesc;
266         depthStencilDesc.Format = depthBufferFormatInfo.dsvFormat;
267         depthStencilDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
268         depthStencilDesc.Flags = 0;
269         depthStencilDesc.Texture2D.MipSlice = 0;
270
271         result = device->CreateDepthStencilView(mDepthStencilTexture, &depthStencilDesc, &mDepthStencilDSView);
272         ASSERT(SUCCEEDED(result));
273         d3d11::SetDebugName(mDepthStencilDSView, "Offscreen depth stencil view");
274
275         D3D11_SHADER_RESOURCE_VIEW_DESC depthStencilSRVDesc;
276         depthStencilSRVDesc.Format = depthBufferFormatInfo.srvFormat;
277         depthStencilSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
278         depthStencilSRVDesc.Texture2D.MostDetailedMip = 0;
279         depthStencilSRVDesc.Texture2D.MipLevels = -1;
280
281         result = device->CreateShaderResourceView(mDepthStencilTexture, &depthStencilSRVDesc, &mDepthStencilSRView);
282         ASSERT(SUCCEEDED(result));
283         d3d11::SetDebugName(mDepthStencilSRView, "Offscreen depth stencil shader resource");
284     }
285
286     mWidth = backbufferWidth;
287     mHeight = backbufferHeight;
288
289     if (previousOffscreenTexture != NULL)
290     {
291         D3D11_BOX sourceBox = {0};
292         sourceBox.left = 0;
293         sourceBox.right = std::min(previousWidth, mWidth);
294         sourceBox.top = std::max(previousHeight - mHeight, 0);
295         sourceBox.bottom = previousHeight;
296         sourceBox.front = 0;
297         sourceBox.back = 1;
298
299         ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
300         const int yoffset = std::max(mHeight - previousHeight, 0);
301         deviceContext->CopySubresourceRegion(mOffscreenTexture, 0, 0, yoffset, 0, previousOffscreenTexture, 0, &sourceBox);
302
303         SafeRelease(previousOffscreenTexture);
304
305         if (mSwapChain)
306         {
307             swapRect(0, 0, mWidth, mHeight);
308         }
309     }
310
311     return EGL_SUCCESS;
312 }
313
314 EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
315 {
316     ID3D11Device *device = mRenderer->getDevice();
317
318     if (device == NULL)
319     {
320         return EGL_BAD_ACCESS;
321     }
322
323     // EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains
324     if (backbufferWidth < 1 || backbufferHeight < 1)
325     {
326         return EGL_SUCCESS;
327     }
328
329     // Can only call resize if we have already created our swap buffer and resources
330     ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView);
331
332     SafeRelease(mBackBufferTexture);
333     SafeRelease(mBackBufferRTView);
334
335     // Resize swap chain
336     DXGI_SWAP_CHAIN_DESC desc;
337     mSwapChain->GetDesc(&desc);
338     const d3d11::TextureFormat &backbufferFormatInfo = d3d11::GetTextureFormatInfo(mBackBufferFormat);
339     HRESULT result = mSwapChain->ResizeBuffers(desc.BufferCount, backbufferWidth, backbufferHeight, backbufferFormatInfo.texFormat, 0);
340
341     if (FAILED(result))
342     {
343         ERR("Error resizing swap chain buffers: 0x%08X", result);
344         release();
345
346         if (d3d11::isDeviceLostError(result))
347         {
348             return EGL_CONTEXT_LOST;
349         }
350         else
351         {
352             return EGL_BAD_ALLOC;
353         }
354     }
355
356     result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
357     ASSERT(SUCCEEDED(result));
358     if (SUCCEEDED(result))
359     {
360         d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
361     }
362
363     result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView);
364     ASSERT(SUCCEEDED(result));
365     if (SUCCEEDED(result))
366     {
367         d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target");
368     }
369
370     return resetOffscreenTexture(backbufferWidth, backbufferHeight);
371 }
372
373 EGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval)
374 {
375     ID3D11Device *device = mRenderer->getDevice();
376
377     if (device == NULL)
378     {
379         return EGL_BAD_ACCESS;
380     }
381
382     // Release specific resources to free up memory for the new render target, while the
383     // old render target still exists for the purpose of preserving its contents.
384     SafeRelease(mSwapChain);
385     SafeRelease(mBackBufferTexture);
386     SafeRelease(mBackBufferRTView);
387
388     mSwapInterval = static_cast<unsigned int>(swapInterval);
389     if (mSwapInterval > 4)
390     {
391         // IDXGISwapChain::Present documentation states that valid sync intervals are in the [0,4] range
392         return EGL_BAD_PARAMETER;
393     }
394
395     // EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains
396     if (backbufferWidth < 1 || backbufferHeight < 1)
397     {
398         releaseOffscreenTexture();
399         return EGL_SUCCESS;
400     }
401
402     if (mNativeWindow.getNativeWindow())
403     {
404         const d3d11::TextureFormat &backbufferFormatInfo = d3d11::GetTextureFormatInfo(mBackBufferFormat);
405
406         HRESULT result = mNativeWindow.createSwapChain(device, mRenderer->getDxgiFactory(),
407                                                backbufferFormatInfo.texFormat,
408                                                backbufferWidth, backbufferHeight, &mSwapChain);
409
410         if (FAILED(result))
411         {
412             ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result);
413             release();
414
415             if (d3d11::isDeviceLostError(result))
416             {
417                 return EGL_CONTEXT_LOST;
418             }
419             else
420             {
421                 return EGL_BAD_ALLOC;
422             }
423         }
424
425         result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
426         ASSERT(SUCCEEDED(result));
427         d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
428
429         result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView);
430         ASSERT(SUCCEEDED(result));
431         d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target");
432     }
433
434     // If we are resizing the swap chain, we don't wish to recreate all the static resources
435     if (!mPassThroughResourcesInit)
436     {
437         mPassThroughResourcesInit = true;
438         initPassThroughResources();
439     }
440
441     return resetOffscreenTexture(backbufferWidth, backbufferHeight);
442 }
443
444 void SwapChain11::initPassThroughResources()
445 {
446     ID3D11Device *device = mRenderer->getDevice();
447
448     ASSERT(device != NULL);
449
450     // Make sure our resources are all not allocated, when we create
451     ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL);
452     ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL);
453
454     D3D11_BUFFER_DESC vbDesc;
455     vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
456     vbDesc.Usage = D3D11_USAGE_DYNAMIC;
457     vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
458     vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
459     vbDesc.MiscFlags = 0;
460     vbDesc.StructureByteStride = 0;
461
462     HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB);
463     ASSERT(SUCCEEDED(result));
464     d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer");
465
466     D3D11_SAMPLER_DESC samplerDesc;
467     samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
468     samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
469     samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
470     samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
471     samplerDesc.MipLODBias = 0.0f;
472     samplerDesc.MaxAnisotropy = 0;
473     samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
474     samplerDesc.BorderColor[0] = 0.0f;
475     samplerDesc.BorderColor[1] = 0.0f;
476     samplerDesc.BorderColor[2] = 0.0f;
477     samplerDesc.BorderColor[3] = 0.0f;
478     samplerDesc.MinLOD = 0;
479     samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
480
481     result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler);
482     ASSERT(SUCCEEDED(result));
483     d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler");
484
485     D3D11_INPUT_ELEMENT_DESC quadLayout[] =
486     {
487         { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
488         { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
489     };
490
491     result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), &mPassThroughIL);
492     ASSERT(SUCCEEDED(result));
493     d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout");
494
495     result = device->CreateVertexShader(g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), NULL, &mPassThroughVS);
496     ASSERT(SUCCEEDED(result));
497     d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader");
498
499     result = device->CreatePixelShader(g_PS_PassthroughRGBA2D, sizeof(g_PS_PassthroughRGBA2D), NULL, &mPassThroughPS);
500     ASSERT(SUCCEEDED(result));
501     d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader");
502 }
503
504 // parameters should be validated/clamped by caller
505 EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height)
506 {
507     if (!mSwapChain)
508     {
509         return EGL_SUCCESS;
510     }
511
512     ID3D11Device *device = mRenderer->getDevice();
513     ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
514
515     // Set vertices
516     D3D11_MAPPED_SUBRESOURCE mappedResource;
517     HRESULT result = deviceContext->Map(mQuadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
518     if (FAILED(result))
519     {
520         return EGL_BAD_ACCESS;
521     }
522
523     d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
524
525     // Create a quad in homogeneous coordinates
526     float x1 = (x / float(mWidth)) * 2.0f - 1.0f;
527     float y1 = (y / float(mHeight)) * 2.0f - 1.0f;
528     float x2 = ((x + width) / float(mWidth)) * 2.0f - 1.0f;
529     float y2 = ((y + height) / float(mHeight)) * 2.0f - 1.0f;
530
531     float u1 = x / float(mWidth);
532     float v1 = y / float(mHeight);
533     float u2 = (x + width) / float(mWidth);
534     float v2 = (y + height) / float(mHeight);
535
536     d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v1);
537     d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2);
538     d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1);
539     d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2);
540
541     deviceContext->Unmap(mQuadVB, 0);
542
543     static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
544     static UINT startIdx = 0;
545     deviceContext->IASetVertexBuffers(0, 1, &mQuadVB, &stride, &startIdx);
546
547     // Apply state
548     deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
549
550     static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
551     deviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
552
553     deviceContext->RSSetState(NULL);
554
555     // Apply shaders
556     deviceContext->IASetInputLayout(mPassThroughIL);
557     deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
558     deviceContext->VSSetShader(mPassThroughVS, NULL, 0);
559     deviceContext->PSSetShader(mPassThroughPS, NULL, 0);
560     deviceContext->GSSetShader(NULL, NULL, 0);
561
562     // Apply render targets
563     mRenderer->setOneTimeRenderTarget(mBackBufferRTView);
564
565     // Set the viewport
566     D3D11_VIEWPORT viewport;
567     viewport.TopLeftX = 0;
568     viewport.TopLeftY = 0;
569     viewport.Width = mWidth;
570     viewport.Height = mHeight;
571     viewport.MinDepth = 0.0f;
572     viewport.MaxDepth = 1.0f;
573     deviceContext->RSSetViewports(1, &viewport);
574
575     // Apply textures
576     mRenderer->setShaderResource(gl::SAMPLER_PIXEL, 0, mOffscreenSRView);
577     deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler);
578
579     // Draw
580     deviceContext->Draw(4, 0);
581
582 #if ANGLE_VSYNC == ANGLE_DISABLED
583     result = mSwapChain->Present(0, 0);
584 #else
585     result = mSwapChain->Present(mSwapInterval, 0);
586 #endif
587
588     if (result == DXGI_ERROR_DEVICE_REMOVED)
589     {
590         HRESULT removedReason = device->GetDeviceRemovedReason();
591         UNUSED_TRACE_VARIABLE(removedReason);
592         ERR("Present failed: the D3D11 device was removed: 0x%08X", removedReason);
593         return EGL_CONTEXT_LOST;
594     }
595     else if (result == DXGI_ERROR_DEVICE_RESET)
596     {
597         ERR("Present failed: the D3D11 device was reset from a bad command.");
598         return EGL_CONTEXT_LOST;
599     }
600     else if (FAILED(result))
601     {
602         ERR("Present failed with error code 0x%08X", result);
603     }
604
605     // Unbind
606     mRenderer->setShaderResource(gl::SAMPLER_PIXEL, 0, NULL);
607
608     mRenderer->unapplyRenderTargets();
609     mRenderer->markAllStateDirty();
610
611     return EGL_SUCCESS;
612 }
613
614 ID3D11Texture2D *SwapChain11::getOffscreenTexture()
615 {
616     return mOffscreenTexture;
617 }
618
619 ID3D11RenderTargetView *SwapChain11::getRenderTarget()
620 {
621     return mOffscreenRTView;
622 }
623
624 ID3D11ShaderResourceView *SwapChain11::getRenderTargetShaderResource()
625 {
626     return mOffscreenSRView;
627 }
628
629 ID3D11DepthStencilView *SwapChain11::getDepthStencil()
630 {
631     return mDepthStencilDSView;
632 }
633
634 ID3D11ShaderResourceView * SwapChain11::getDepthStencilShaderResource()
635 {
636     return mDepthStencilSRView;
637 }
638
639 ID3D11Texture2D *SwapChain11::getDepthStencilTexture()
640 {
641     return mDepthStencilTexture;
642 }
643
644 SwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain)
645 {
646     ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain11*, swapChain));
647     return static_cast<rx::SwapChain11*>(swapChain);
648 }
649
650 void SwapChain11::recreate()
651 {
652     // possibly should use this method instead of reset
653 }
654
655 }