1bdf2d80f459d43f5a6df074431bacf41584f81b7shannon.woods@transgaming.com#include "precompiled.h"
232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com//
3ed36abd83e40bb1c0f4af9ea7314906b86ea3cdddaniel@transgaming.com// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// Use of this source code is governed by a BSD-style license that can be
532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// found in the LICENSE file.
632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com//
732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// SwapChain11.cpp: Implements a back-end specific class for the D3D11 swap chain.
932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
1032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com#include "libGLESv2/renderer/SwapChain11.h"
1132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
1232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com#include "libGLESv2/renderer/renderer11_utils.h"
1332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com#include "libGLESv2/renderer/Renderer11.h"
148dc8e277b4343a73ddbb890c4c8613ace6cc8b1cdaniel@transgaming.com#include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h"
152570c34d4ba9058b1544381d6b21a920b405e07ashannon.woods@transgaming.com#include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h"
16e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
1732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comnamespace rx
1832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
1932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
2032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comSwapChain11::SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle,
2132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com                         GLenum backBufferFormat, GLenum depthBufferFormat)
2232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat)
2332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
2432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    mSwapChain = NULL;
25c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    mBackBufferTexture = NULL;
26c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    mBackBufferRTView = NULL;
2732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    mOffscreenTexture = NULL;
28c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    mOffscreenRTView = NULL;
29e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    mOffscreenSRView = NULL;
30c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    mDepthStencilTexture = NULL;
31c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    mDepthStencilDSView = NULL;
32f06e539adbbfe39b4d59d91a0edb340cc934566adaniel@transgaming.com    mQuadVB = NULL;
33f06e539adbbfe39b4d59d91a0edb340cc934566adaniel@transgaming.com    mPassThroughSampler = NULL;
34f06e539adbbfe39b4d59d91a0edb340cc934566adaniel@transgaming.com    mPassThroughIL = NULL;
35f06e539adbbfe39b4d59d91a0edb340cc934566adaniel@transgaming.com    mPassThroughVS = NULL;
36f06e539adbbfe39b4d59d91a0edb340cc934566adaniel@transgaming.com    mPassThroughPS = NULL;
3732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    mWidth = -1;
3832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    mHeight = -1;
39ad74a4b10a90d0353ff941ad792874f1e1440374shannon.woods@transgaming.com    mSwapInterval = 0;
40c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    mAppCreatedShareHandle = mShareHandle != NULL;
41211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    mPassThroughResourcesInit = false;
4232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
4332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
4432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comSwapChain11::~SwapChain11()
4532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
4632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    release();
4732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
4832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
4932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comvoid SwapChain11::release()
5032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
5132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    if (mSwapChain)
5232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
5332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        mSwapChain->Release();
5432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        mSwapChain = NULL;
5532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
5632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
57c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mBackBufferTexture)
5832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
59c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mBackBufferTexture->Release();
60c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mBackBufferTexture = NULL;
6132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
6232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
63c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mBackBufferRTView)
6432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
65c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mBackBufferRTView->Release();
66c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mBackBufferRTView = NULL;
6732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
68c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com
69c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mOffscreenTexture)
7032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
71c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenTexture->Release();
72c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenTexture = NULL;
7332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
7432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
75c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mOffscreenRTView)
7632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
77c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenRTView->Release();
78c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenRTView = NULL;
7932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
8032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
81e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mOffscreenSRView)
82e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
83e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mOffscreenSRView->Release();
84e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mOffscreenSRView = NULL;
85e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
86e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
87c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mDepthStencilTexture)
8832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
89c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilTexture->Release();
90c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilTexture = NULL;
9132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
9232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
93c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mDepthStencilDSView)
9432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
95c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilDSView->Release();
96c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilDSView = NULL;
9732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
9832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
99e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mQuadVB)
100e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
101e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mQuadVB->Release();
102e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mQuadVB = NULL;
103e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
104e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
105e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mPassThroughSampler)
106e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
107e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughSampler->Release();
108e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughSampler = NULL;
109e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
110e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
111e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mPassThroughIL)
112e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
113e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughIL->Release();
114e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughIL = NULL;
115e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
116e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
117e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mPassThroughVS)
118e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
119e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughVS->Release();
120e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughVS = NULL;
121e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
122e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
123e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mPassThroughPS)
124e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
125e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughPS->Release();
126e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mPassThroughPS = NULL;
127e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
128e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
129c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    if (!mAppCreatedShareHandle)
130c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    {
13132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        mShareHandle = NULL;
132c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    }
13332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
13432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
13530f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.comvoid SwapChain11::releaseOffscreenTexture()
13632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
13732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    if (mOffscreenTexture)
13832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
13932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        mOffscreenTexture->Release();
14032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        mOffscreenTexture = NULL;
14132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
14232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
14330f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    if (mOffscreenRTView)
144c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    {
145c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenRTView->Release();
146c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenRTView = NULL;
147c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    }
148c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com
149e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (mOffscreenSRView)
150e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
151e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mOffscreenSRView->Release();
152e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        mOffscreenSRView = NULL;
153e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
154e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
155c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mDepthStencilTexture)
15632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
157c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilTexture->Release();
158c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilTexture = NULL;
15932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
16032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
161c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mDepthStencilDSView)
16232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
163c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilDSView->Release();
164c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mDepthStencilDSView = NULL;
16532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
16630f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com}
16730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
16830f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.comEGLint SwapChain11::resetOffscreenTexture(int backbufferWidth, int backbufferHeight)
16930f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com{
17030f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    ID3D11Device *device = mRenderer->getDevice();
17130f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
17230f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    ASSERT(device != NULL);
17330f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
17430f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    // D3D11 does not allow zero size textures
17530f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    ASSERT(backbufferWidth >= 1);
17630f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    ASSERT(backbufferHeight >= 1);
17730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
17830f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    // Preserve the render target content
17930f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    ID3D11Texture2D *previousOffscreenTexture = mOffscreenTexture;
18030f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    if (previousOffscreenTexture)
18130f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    {
18230f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        previousOffscreenTexture->AddRef();
18330f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    }
18430f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    const int previousWidth = mWidth;
18530f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    const int previousHeight = mHeight;
18630f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
18730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    releaseOffscreenTexture();
18832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
189c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    // If the app passed in a share handle, open the resource
190c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    // See EGL_ANGLE_d3d_share_handle_client_buffer
191c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    if (mAppCreatedShareHandle)
19232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
193c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        ID3D11Resource *tempResource11;
194c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        HRESULT result = device->OpenSharedResource(mShareHandle, __uuidof(ID3D11Resource), (void**)&tempResource11);
19532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
196c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        if (FAILED(result))
197c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        {
198c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            ERR("Failed to open the swap chain pbuffer share handle: %08lX", result);
199c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            release();
200c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            return EGL_BAD_PARAMETER;
201c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        }
20232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
203c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        result = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&mOffscreenTexture);
204c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        tempResource11->Release();
20532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
206c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        if (FAILED(result))
207c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        {
208c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            ERR("Failed to query texture2d interface in pbuffer share handle: %08lX", result);
209c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            release();
210c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            return EGL_BAD_PARAMETER;
211c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        }
21232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
213c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        // Validate offscreen texture parameters
214c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0};
215c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        mOffscreenTexture->GetDesc(&offscreenTextureDesc);
216c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
217c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        if (offscreenTextureDesc.Width != (UINT)backbufferWidth
218c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            || offscreenTextureDesc.Height != (UINT)backbufferHeight
219c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            || offscreenTextureDesc.Format != gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat)
220c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            || offscreenTextureDesc.MipLevels != 1
221c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            || offscreenTextureDesc.ArraySize != 1)
222c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        {
223c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            ERR("Invalid texture parameters in the shared offscreen texture pbuffer");
224c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            release();
225c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            return EGL_BAD_PARAMETER;
226c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        }
227c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    }
228c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    else
229c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    {
230c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        const bool useSharedResource = !mWindow && mRenderer->getShareHandleSupport();
231c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
232c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0};
233c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.Width = backbufferWidth;
234c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.Height = backbufferHeight;
235c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat);
236c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.MipLevels = 1;
237c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.ArraySize = 1;
238c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.SampleDesc.Count = 1;
239c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.SampleDesc.Quality = 0;
240c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT;
241c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
242c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.CPUAccessFlags = 0;
243c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        offscreenTextureDesc.MiscFlags = useSharedResource ? D3D11_RESOURCE_MISC_SHARED : 0;
244c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
245c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture);
246c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
247c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        if (FAILED(result))
24832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        {
249c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            ERR("Could not create offscreen texture: %08lX", result);
250c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            release();
251c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
252f5f5949101cbf4df3927810bfd9ca233619494c4shannon.woods@transgaming.com            if (d3d11::isDeviceLostError(result))
253c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            {
254c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                return EGL_CONTEXT_LOST;
255c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            }
256c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            else
257c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            {
258c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                return EGL_BAD_ALLOC;
259c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            }
26032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        }
261c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
262c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        d3d11::SetDebugName(mOffscreenTexture, "Offscreen texture");
263c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
264c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        // EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client
265c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com        if (useSharedResource)
26632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        {
267c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            IDXGIResource *offscreenTextureResource = NULL;
268c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource);
269c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
270c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            // Fall back to no share handle on failure
271c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            if (FAILED(result))
272c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            {
273c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                ERR("Could not query offscreen texture resource: %08lX", result);
274c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            }
275c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            else
276c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            {
277c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                result = offscreenTextureResource->GetSharedHandle(&mShareHandle);
278c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
279c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                if (FAILED(result))
280c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                {
281c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                    mShareHandle = NULL;
282c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                    ERR("Could not get offscreen texture shared handle: %08lX", result);
283c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com                }
284c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com            }
28532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        }
28632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
28732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
288c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com    HRESULT result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mOffscreenRTView);
289c60c5215ebab426654d1670282d15652667e38dashannon.woods@transgaming.com
29032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    ASSERT(SUCCEEDED(result));
291ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mOffscreenRTView, "Offscreen render target");
29232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
293e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    result = device->CreateShaderResourceView(mOffscreenTexture, NULL, &mOffscreenSRView);
294e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
295ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mOffscreenSRView, "Offscreen shader resource");
296e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
297ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (mDepthBufferFormat != GL_NONE)
298ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
299ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        D3D11_TEXTURE2D_DESC depthStencilDesc = {0};
300ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.Width = backbufferWidth;
301ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.Height = backbufferHeight;
302ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mDepthBufferFormat);
303ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.MipLevels = 1;
304ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.ArraySize = 1;
305ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.SampleDesc.Count = 1;
306ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.SampleDesc.Quality = 0;
307ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
308ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
309ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.CPUAccessFlags = 0;
310ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        depthStencilDesc.MiscFlags = 0;
311ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
312ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencilTexture);
313ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        if (FAILED(result))
314ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        {
315ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result);
316ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            release();
317ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
318ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            if (d3d11::isDeviceLostError(result))
319ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            {
320ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com                return EGL_CONTEXT_LOST;
321ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            }
322ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            else
323ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            {
324ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com                return EGL_BAD_ALLOC;
325ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com            }
326ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        }
327ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        d3d11::SetDebugName(mDepthStencilTexture, "Depth stencil texture");
328ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
329ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        result = device->CreateDepthStencilView(mDepthStencilTexture, NULL, &mDepthStencilDSView);
330ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        ASSERT(SUCCEEDED(result));
331ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        d3d11::SetDebugName(mDepthStencilDSView, "Depth stencil view");
332ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
333ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
334ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    mWidth = backbufferWidth;
335ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    mHeight = backbufferHeight;
336ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
33730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    if (previousOffscreenTexture != NULL)
33830f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    {
33930f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        D3D11_BOX sourceBox = {0};
34030f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.left = 0;
34130f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.right = std::min(previousWidth, mWidth);
34230f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.top = std::max(previousHeight - mHeight, 0);
34330f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.bottom = previousHeight;
34430f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.front = 0;
34530f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        sourceBox.back = 1;
34630f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
34730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
34830f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        const int yoffset = std::max(mHeight - previousHeight, 0);
34930f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        deviceContext->CopySubresourceRegion(mOffscreenTexture, 0, 0, yoffset, 0, previousOffscreenTexture, 0, &sourceBox);
35030f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
35130f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        previousOffscreenTexture->Release();
35230f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
35330f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        if (mSwapChain)
35430f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        {
35530f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com            swapRect(0, 0, mWidth, mHeight);
35630f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com        }
35730f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com    }
35830f52c6c5ad8f593bbfb91b3e31bc91e505feb3ashannon.woods@transgaming.com
359ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    return EGL_SUCCESS;
360ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com}
361ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
362c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.comEGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
363c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com{
364c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    ID3D11Device *device = mRenderer->getDevice();
365c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
366c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (device == NULL)
367c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
368c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        return EGL_BAD_ACCESS;
369c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
370c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
371c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    // Can only call resize if we have already created our swap buffer and resources
372c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView);
373c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
374c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (mBackBufferTexture)
375c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
376c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        mBackBufferTexture->Release();
377c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        mBackBufferTexture = NULL;
378c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
379c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
380c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (mBackBufferRTView)
381c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
382c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        mBackBufferRTView->Release();
383c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        mBackBufferRTView = NULL;
384c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
385c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
386c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    // Resize swap chain
387c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    DXGI_FORMAT backbufferDXGIFormat = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat);
388c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    HRESULT result = mSwapChain->ResizeBuffers(2, backbufferWidth, backbufferHeight, backbufferDXGIFormat, 0);
389c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
390c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (FAILED(result))
391c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
392c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        ERR("Error resizing swap chain buffers: 0x%08X", result);
393c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        release();
394c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
395c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        if (d3d11::isDeviceLostError(result))
396c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        {
397c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com            return EGL_CONTEXT_LOST;
398c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        }
399c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        else
400c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        {
401c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com            return EGL_BAD_ALLOC;
402c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        }
403c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
404c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
405c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
406c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    ASSERT(SUCCEEDED(result));
407c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (SUCCEEDED(result))
408c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
409c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
410c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
411c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
412c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView);
413c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    ASSERT(SUCCEEDED(result));
414c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    if (SUCCEEDED(result))
415c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    {
416c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com        d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target");
417c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    }
418c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
419c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com    return resetOffscreenTexture(backbufferWidth, backbufferHeight);
420c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com}
421c71ca75ad647335876ed6961ebf461203df4caddshannon.woods@transgaming.com
422ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.comEGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval)
423ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com{
424ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    ID3D11Device *device = mRenderer->getDevice();
425ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
426ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (device == NULL)
427ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
428ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        return EGL_BAD_ACCESS;
429ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
430ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
431ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    // Release specific resources to free up memory for the new render target, while the
432ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    // old render target still exists for the purpose of preserving its contents.
433ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (mSwapChain)
434ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
435ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mSwapChain->Release();
436ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mSwapChain = NULL;
437ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
438ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
439ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (mBackBufferTexture)
440ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
441ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mBackBufferTexture->Release();
442ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mBackBufferTexture = NULL;
443ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
444ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
445ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (mBackBufferRTView)
446ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
447ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mBackBufferRTView->Release();
448ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        mBackBufferRTView = NULL;
449ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
450ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
451ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    mSwapInterval = static_cast<unsigned int>(swapInterval);
452ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    if (mSwapInterval > 4)
453ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    {
454ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        // IDXGISwapChain::Present documentation states that valid sync intervals are in the [0,4] range
455ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        return EGL_BAD_PARAMETER;
456ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    }
457ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com
4588c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com    // EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains
4598c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com    if (backbufferWidth < 1 || backbufferHeight < 1)
4608c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com    {
4618c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com        releaseOffscreenTexture();
4628c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com        return EGL_SUCCESS;
4638c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com    }
4648c6d9df2f486d4245bc44be08530d432bce08593shannon.woods@transgaming.com
46532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    if (mWindow)
46632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
4677d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        // We cannot create a swap chain for an HWND that is owned by a different process
4687d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        DWORD currentProcessId = GetCurrentProcessId();
4697d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        DWORD wndProcessId;
4707d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        GetWindowThreadProcessId(mWindow, &wndProcessId);
4717d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com
4727d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        if (currentProcessId != wndProcessId)
4737d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        {
4747d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com            ERR("Could not create swap chain, window owned by different process");
4757d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com            release();
4767d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com            return EGL_BAD_NATIVE_WINDOW;
4777d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com        }
4787d4b482a933d48b93182fbf2d5e0f55fd3b60683shannon.woods@transgaming.com
47932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        IDXGIFactory *factory = mRenderer->getDxgiFactory();
48032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
48132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        DXGI_SWAP_CHAIN_DESC swapChainDesc = {0};
48232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferCount = 2;
48332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat);
484567b9cfd56524b38c83e5f2ec5c9201601f32494daniel@transgaming.com        swapChainDesc.BufferDesc.Width = backbufferWidth;
485567b9cfd56524b38c83e5f2ec5c9201601f32494daniel@transgaming.com        swapChainDesc.BufferDesc.Height = backbufferHeight;
48632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
48732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
48832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
48932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
49032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
49132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.Flags = 0;
49232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.OutputWindow = mWindow;
49332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.SampleDesc.Count = 1;
49432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.SampleDesc.Quality = 0;
49532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        swapChainDesc.Windowed = TRUE;
49632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
497ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com        HRESULT result = factory->CreateSwapChain(device, &swapChainDesc, &mSwapChain);
49832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
49932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        if (FAILED(result))
50032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        {
50132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result);
50232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            release();
50332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
504f5f5949101cbf4df3927810bfd9ca233619494c4shannon.woods@transgaming.com            if (d3d11::isDeviceLostError(result))
50532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            {
50632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com                return EGL_CONTEXT_LOST;
50732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            }
50832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            else
50932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            {
51032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com                return EGL_BAD_ALLOC;
51132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com            }
51232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        }
51332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
514c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
51532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        ASSERT(SUCCEEDED(result));
516ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com        d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
51732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
518c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView);
51932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        ASSERT(SUCCEEDED(result));
520ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com        d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target");
52132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
52232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
523211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    // If we are resizing the swap chain, we don't wish to recreate all the static resources
524211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    if (!mPassThroughResourcesInit)
525211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    {
526211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com        mPassThroughResourcesInit = true;
527211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com        initPassThroughResources();
528211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    }
529211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com
530ae81383606efb5f9b06e7e21e5ed4ac526b25248shannon.woods@transgaming.com    return resetOffscreenTexture(backbufferWidth, backbufferHeight);
531211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com}
532211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com
533211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.comvoid SwapChain11::initPassThroughResources()
534211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com{
535211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    ID3D11Device *device = mRenderer->getDevice();
536211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com
537211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    ASSERT(device != NULL);
538211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com
539211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    // Make sure our resources are all not allocated, when we create
540211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL);
541211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL);
542211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com
543e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    D3D11_BUFFER_DESC vbDesc;
544f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
545e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    vbDesc.Usage = D3D11_USAGE_DYNAMIC;
546e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
547e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
548e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    vbDesc.MiscFlags = 0;
549e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    vbDesc.StructureByteStride = 0;
550e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
551211fa2edc6290101d995dac7a21de54be3e0aab3shannon.woods@transgaming.com    HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB);
552e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
553ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer");
554e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
555e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    D3D11_SAMPLER_DESC samplerDesc;
556e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
557e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
558e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
559e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
560e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.MipLODBias = 0.0f;
561e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.MaxAnisotropy = 0;
562e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
563e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.BorderColor[0] = 0.0f;
564e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.BorderColor[1] = 0.0f;
565e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.BorderColor[2] = 0.0f;
566e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.BorderColor[3] = 0.0f;
567e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.MinLOD = 0;
568e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
569e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
570e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler);
571e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
572ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler");
573e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
574e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    D3D11_INPUT_ELEMENT_DESC quadLayout[] =
575e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
576e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
577e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
578e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    };
579e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
580e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mPassThroughIL);
581e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
582ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout");
583e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
584e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    result = device->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mPassThroughVS);
585e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
586ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader");
587e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
5882570c34d4ba9058b1544381d6b21a920b405e07ashannon.woods@transgaming.com    result = device->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mPassThroughPS);
589e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ASSERT(SUCCEEDED(result));
590ad3d8270521183b8a6858044fe37a09bf97e2e9edaniel@transgaming.com    d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader");
59132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
59232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
59332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// parameters should be validated/clamped by caller
59432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comEGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height)
59532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
59632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    if (!mSwapChain)
59732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
59832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com        return EGL_SUCCESS;
59932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
60032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
60132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    ID3D11Device *device = mRenderer->getDevice();
602e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
603e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
604e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Set vertices
605e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    D3D11_MAPPED_SUBRESOURCE mappedResource;
606e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    HRESULT result = deviceContext->Map(mQuadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
607e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    if (FAILED(result))
608e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    {
609e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com        return EGL_BAD_ACCESS;
610e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    }
611e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
612f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
61332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
614e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Create a quad in homogeneous coordinates
6152ac02d5e2e6bcaa080780fba11636ea3ab47d6e2shannon.woods@transgaming.com    float x1 = (x / float(mWidth)) * 2.0f - 1.0f;
6160bbb08d96ba8c7dba535909cb91594816df12c17shannon.woods@transgaming.com    float y1 = (y / float(mHeight)) * 2.0f - 1.0f;
6172ac02d5e2e6bcaa080780fba11636ea3ab47d6e2shannon.woods@transgaming.com    float x2 = ((x + width) / float(mWidth)) * 2.0f - 1.0f;
6180bbb08d96ba8c7dba535909cb91594816df12c17shannon.woods@transgaming.com    float y2 = ((y + height) / float(mHeight)) * 2.0f - 1.0f;
619e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
620e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    float u1 = x / float(mWidth);
621e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    float v1 = y / float(mHeight);
622e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    float u2 = (x + width) / float(mWidth);
623e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    float v2 = (y + height) / float(mHeight);
624e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
625f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v1);
626f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2);
627f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1);
628f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2);
629e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
630e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->Unmap(mQuadVB, 0);
631e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
632f3d820791a61fa7c7dbe35b1f5424e9587199e14shannon.woods@transgaming.com    static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
633e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    static UINT startIdx = 0;
634e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->IASetVertexBuffers(0, 1, &mQuadVB, &stride, &startIdx);
635e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
636e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Apply state
637e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
638e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
639e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
640e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
641e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
642e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->RSSetState(NULL);
643e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
644e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Apply shaders
645e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->IASetInputLayout(mPassThroughIL);
646e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
647e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->VSSetShader(mPassThroughVS, NULL, 0);
648e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->PSSetShader(mPassThroughPS, NULL, 0);
6493e773bb4f1a2202d02a546d18ca6e8d9d2440006shannon.woods@transgaming.com    deviceContext->GSSetShader(NULL, NULL, 0);
650e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
651e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Apply render targets
65252edb4a46c5303a84788d9fe25a36fef70d684e8shannon.woods%transgaming.com@gtempaccount.com    mRenderer->setOneTimeRenderTarget(mBackBufferRTView);
653e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
6549799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    // Set the viewport
6559799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    D3D11_VIEWPORT viewport;
6569799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.TopLeftX = 0;
6579799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.TopLeftY = 0;
6589799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.Width = mWidth;
6599799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.Height = mHeight;
6609799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.MinDepth = 0.0f;
6619799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    viewport.MaxDepth = 1.0f;
6629799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com    deviceContext->RSSetViewports(1, &viewport);
6639799a2f8595127019054bc7dd99354227fb893cedaniel@transgaming.com
664e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Apply textures
665e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->PSSetShaderResources(0, 1, &mOffscreenSRView);
666e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler);
667e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
668e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Draw
669e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->Draw(4, 0);
670ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    result = mSwapChain->Present(mSwapInterval, 0);
671ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com
672ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    if (result == DXGI_ERROR_DEVICE_REMOVED)
673ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    {
674ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        HRESULT removedReason = device->GetDeviceRemovedReason();
675ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        ERR("Present failed: the D3D11 device was removed: 0x%08X", removedReason);
676ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        return EGL_CONTEXT_LOST;
677ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    }
678ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    else if (result == DXGI_ERROR_DEVICE_RESET)
679ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    {
680ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        ERR("Present failed: the D3D11 device was reset from a bad command.");
681ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        return EGL_CONTEXT_LOST;
682ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    }
683ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    else if (FAILED(result))
684ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    {
685ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com        ERR("Present failed with error code 0x%08X", result);
686ddd6c80eaf97858d74d2e2effca558051fac5481shannon.woods@transgaming.com    }
687e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
688e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    // Unbind
689e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    static ID3D11ShaderResourceView *const nullSRV = NULL;
690e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    deviceContext->PSSetShaderResources(0, 1, &nullSRV);
691e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com
69252edb4a46c5303a84788d9fe25a36fef70d684e8shannon.woods%transgaming.com@gtempaccount.com    mRenderer->unapplyRenderTargets();
693e097047e2ae1400cb2e741aa50a51781fc28dce4daniel@transgaming.com    mRenderer->markAllStateDirty();
69432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
69532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    return EGL_SUCCESS;
69632fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
69732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
6985c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com// Increments refcount on texture.
6995c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com// caller must Release() the returned texture
7005c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.comID3D11Texture2D *SwapChain11::getOffscreenTexture()
7015c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com{
7025c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    if (mOffscreenTexture)
7035c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    {
7045c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com        mOffscreenTexture->AddRef();
7055c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    }
7065c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com
7075c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    return mOffscreenTexture;
7085c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com}
7095c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com
71032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// Increments refcount on view.
71132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// caller must Release() the returned view
71232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.comID3D11RenderTargetView *SwapChain11::getRenderTarget()
71332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
714c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    if (mOffscreenRTView)
71532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
716c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com        mOffscreenRTView->AddRef();
71732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
71832fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
719c8c70adcb87042b8fc509025f44ebae1c2291bd5daniel@transgaming.com    return mOffscreenRTView;
72032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
72132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
72232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// Increments refcount on view.
72332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com// caller must Release() the returned view
7245c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.comID3D11ShaderResourceView *SwapChain11::getRenderTargetShaderResource()
72532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
7265c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    if (mOffscreenSRView)
72732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
7285c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com        mOffscreenSRView->AddRef();
72932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
73032fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
7315c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    return mOffscreenSRView;
73232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
73332fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
7345c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com// Increments refcount on view.
7355c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com// caller must Release() the returned view
7365c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.comID3D11DepthStencilView *SwapChain11::getDepthStencil()
73732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com{
7385c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    if (mDepthStencilDSView)
73932fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    {
7405c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com        mDepthStencilDSView->AddRef();
74132fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com    }
74232fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
7435c25ed2f8aa6f1b2b906e9facbd6da21501cee70shannon.woods@transgaming.com    return mDepthStencilDSView;
74432fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
74532fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com
7467e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.comID3D11Texture2D *SwapChain11::getDepthStencilTexture()
7477e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com{
7487e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com    if (mDepthStencilTexture)
7497e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com    {
7507e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com        mDepthStencilTexture->AddRef();
7517e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com    }
7527e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com
7537e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com    return mDepthStencilTexture;
7547e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com}
7557e23285f51e48e8f3d75c99a1451023543fb4c66shannon.woods@transgaming.com
756d733bb88bc5544f6e55bc359e1ce26faa5d45565daniel@transgaming.comSwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain)
757d733bb88bc5544f6e55bc359e1ce26faa5d45565daniel@transgaming.com{
7588b400b1e8d84c5b93dd151807504a3e4b90d1b21apatrick@chromium.org    ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain11*, swapChain));
759d733bb88bc5544f6e55bc359e1ce26faa5d45565daniel@transgaming.com    return static_cast<rx::SwapChain11*>(swapChain);
760d733bb88bc5544f6e55bc359e1ce26faa5d45565daniel@transgaming.com}
761d733bb88bc5544f6e55bc359e1ce26faa5d45565daniel@transgaming.com
76288079a024f9655299b4f0c0aa62a01998135ff47shannon.woods%transgaming.com@gtempaccount.comvoid SwapChain11::recreate()
76388079a024f9655299b4f0c0aa62a01998135ff47shannon.woods%transgaming.com@gtempaccount.com{
76488079a024f9655299b4f0c0aa62a01998135ff47shannon.woods%transgaming.com@gtempaccount.com    // possibly should use this method instead of reset
76588079a024f9655299b4f0c0aa62a01998135ff47shannon.woods%transgaming.com@gtempaccount.com}
76688079a024f9655299b4f0c0aa62a01998135ff47shannon.woods%transgaming.com@gtempaccount.com
76732fdf822840418a1bf890d1f1f0fea68b43006b0daniel@transgaming.com}
768