michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // SwapChain11.cpp: Implements a back-end specific class for the D3D11 swap chain. michael@0: michael@0: #include "libGLESv2/renderer/SwapChain11.h" michael@0: michael@0: #include "libGLESv2/renderer/renderer11_utils.h" michael@0: #include "libGLESv2/renderer/Renderer11.h" michael@0: #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" michael@0: #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: SwapChain11::SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle, michael@0: GLenum backBufferFormat, GLenum depthBufferFormat) michael@0: : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) michael@0: { michael@0: mSwapChain = NULL; michael@0: mBackBufferTexture = NULL; michael@0: mBackBufferRTView = NULL; michael@0: mOffscreenTexture = NULL; michael@0: mOffscreenRTView = NULL; michael@0: mOffscreenSRView = NULL; michael@0: mDepthStencilTexture = NULL; michael@0: mDepthStencilDSView = NULL; michael@0: mQuadVB = NULL; michael@0: mPassThroughSampler = NULL; michael@0: mPassThroughIL = NULL; michael@0: mPassThroughVS = NULL; michael@0: mPassThroughPS = NULL; michael@0: mWidth = -1; michael@0: mHeight = -1; michael@0: mSwapInterval = 0; michael@0: mAppCreatedShareHandle = mShareHandle != NULL; michael@0: mPassThroughResourcesInit = false; michael@0: } michael@0: michael@0: SwapChain11::~SwapChain11() michael@0: { michael@0: release(); michael@0: } michael@0: michael@0: void SwapChain11::release() michael@0: { michael@0: if (mSwapChain) michael@0: { michael@0: mSwapChain->Release(); michael@0: mSwapChain = NULL; michael@0: } michael@0: michael@0: if (mBackBufferTexture) michael@0: { michael@0: mBackBufferTexture->Release(); michael@0: mBackBufferTexture = NULL; michael@0: } michael@0: michael@0: if (mBackBufferRTView) michael@0: { michael@0: mBackBufferRTView->Release(); michael@0: mBackBufferRTView = NULL; michael@0: } michael@0: michael@0: if (mOffscreenTexture) michael@0: { michael@0: mOffscreenTexture->Release(); michael@0: mOffscreenTexture = NULL; michael@0: } michael@0: michael@0: if (mOffscreenRTView) michael@0: { michael@0: mOffscreenRTView->Release(); michael@0: mOffscreenRTView = NULL; michael@0: } michael@0: michael@0: if (mOffscreenSRView) michael@0: { michael@0: mOffscreenSRView->Release(); michael@0: mOffscreenSRView = NULL; michael@0: } michael@0: michael@0: if (mDepthStencilTexture) michael@0: { michael@0: mDepthStencilTexture->Release(); michael@0: mDepthStencilTexture = NULL; michael@0: } michael@0: michael@0: if (mDepthStencilDSView) michael@0: { michael@0: mDepthStencilDSView->Release(); michael@0: mDepthStencilDSView = NULL; michael@0: } michael@0: michael@0: if (mQuadVB) michael@0: { michael@0: mQuadVB->Release(); michael@0: mQuadVB = NULL; michael@0: } michael@0: michael@0: if (mPassThroughSampler) michael@0: { michael@0: mPassThroughSampler->Release(); michael@0: mPassThroughSampler = NULL; michael@0: } michael@0: michael@0: if (mPassThroughIL) michael@0: { michael@0: mPassThroughIL->Release(); michael@0: mPassThroughIL = NULL; michael@0: } michael@0: michael@0: if (mPassThroughVS) michael@0: { michael@0: mPassThroughVS->Release(); michael@0: mPassThroughVS = NULL; michael@0: } michael@0: michael@0: if (mPassThroughPS) michael@0: { michael@0: mPassThroughPS->Release(); michael@0: mPassThroughPS = NULL; michael@0: } michael@0: michael@0: if (!mAppCreatedShareHandle) michael@0: { michael@0: mShareHandle = NULL; michael@0: } michael@0: } michael@0: michael@0: void SwapChain11::releaseOffscreenTexture() michael@0: { michael@0: if (mOffscreenTexture) michael@0: { michael@0: mOffscreenTexture->Release(); michael@0: mOffscreenTexture = NULL; michael@0: } michael@0: michael@0: if (mOffscreenRTView) michael@0: { michael@0: mOffscreenRTView->Release(); michael@0: mOffscreenRTView = NULL; michael@0: } michael@0: michael@0: if (mOffscreenSRView) michael@0: { michael@0: mOffscreenSRView->Release(); michael@0: mOffscreenSRView = NULL; michael@0: } michael@0: michael@0: if (mDepthStencilTexture) michael@0: { michael@0: mDepthStencilTexture->Release(); michael@0: mDepthStencilTexture = NULL; michael@0: } michael@0: michael@0: if (mDepthStencilDSView) michael@0: { michael@0: mDepthStencilDSView->Release(); michael@0: mDepthStencilDSView = NULL; michael@0: } michael@0: } michael@0: michael@0: EGLint SwapChain11::resetOffscreenTexture(int backbufferWidth, int backbufferHeight) michael@0: { michael@0: ID3D11Device *device = mRenderer->getDevice(); michael@0: michael@0: ASSERT(device != NULL); michael@0: michael@0: // D3D11 does not allow zero size textures michael@0: ASSERT(backbufferWidth >= 1); michael@0: ASSERT(backbufferHeight >= 1); michael@0: michael@0: // Preserve the render target content michael@0: ID3D11Texture2D *previousOffscreenTexture = mOffscreenTexture; michael@0: if (previousOffscreenTexture) michael@0: { michael@0: previousOffscreenTexture->AddRef(); michael@0: } michael@0: const int previousWidth = mWidth; michael@0: const int previousHeight = mHeight; michael@0: michael@0: releaseOffscreenTexture(); michael@0: michael@0: // If the app passed in a share handle, open the resource michael@0: // See EGL_ANGLE_d3d_share_handle_client_buffer michael@0: if (mAppCreatedShareHandle) michael@0: { michael@0: ID3D11Resource *tempResource11; michael@0: HRESULT result = device->OpenSharedResource(mShareHandle, __uuidof(ID3D11Resource), (void**)&tempResource11); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to open the swap chain pbuffer share handle: %08lX", result); michael@0: release(); michael@0: return EGL_BAD_PARAMETER; michael@0: } michael@0: michael@0: result = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&mOffscreenTexture); michael@0: tempResource11->Release(); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to query texture2d interface in pbuffer share handle: %08lX", result); michael@0: release(); michael@0: return EGL_BAD_PARAMETER; michael@0: } michael@0: michael@0: // Validate offscreen texture parameters michael@0: D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; michael@0: mOffscreenTexture->GetDesc(&offscreenTextureDesc); michael@0: michael@0: if (offscreenTextureDesc.Width != (UINT)backbufferWidth michael@0: || offscreenTextureDesc.Height != (UINT)backbufferHeight michael@0: || offscreenTextureDesc.Format != gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat) michael@0: || offscreenTextureDesc.MipLevels != 1 michael@0: || offscreenTextureDesc.ArraySize != 1) michael@0: { michael@0: ERR("Invalid texture parameters in the shared offscreen texture pbuffer"); michael@0: release(); michael@0: return EGL_BAD_PARAMETER; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: const bool useSharedResource = !mWindow && mRenderer->getShareHandleSupport(); michael@0: michael@0: D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; michael@0: offscreenTextureDesc.Width = backbufferWidth; michael@0: offscreenTextureDesc.Height = backbufferHeight; michael@0: offscreenTextureDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); michael@0: offscreenTextureDesc.MipLevels = 1; michael@0: offscreenTextureDesc.ArraySize = 1; michael@0: offscreenTextureDesc.SampleDesc.Count = 1; michael@0: offscreenTextureDesc.SampleDesc.Quality = 0; michael@0: offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT; michael@0: offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; michael@0: offscreenTextureDesc.CPUAccessFlags = 0; michael@0: offscreenTextureDesc.MiscFlags = useSharedResource ? D3D11_RESOURCE_MISC_SHARED : 0; michael@0: michael@0: HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Could not create offscreen texture: %08lX", result); michael@0: release(); michael@0: michael@0: if (d3d11::isDeviceLostError(result)) michael@0: { michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else michael@0: { michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: } michael@0: michael@0: d3d11::SetDebugName(mOffscreenTexture, "Offscreen texture"); michael@0: michael@0: // EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client michael@0: if (useSharedResource) michael@0: { michael@0: IDXGIResource *offscreenTextureResource = NULL; michael@0: result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource); michael@0: michael@0: // Fall back to no share handle on failure michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Could not query offscreen texture resource: %08lX", result); michael@0: } michael@0: else michael@0: { michael@0: result = offscreenTextureResource->GetSharedHandle(&mShareHandle); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: mShareHandle = NULL; michael@0: ERR("Could not get offscreen texture shared handle: %08lX", result); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: HRESULT result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mOffscreenRTView); michael@0: michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mOffscreenRTView, "Offscreen render target"); michael@0: michael@0: result = device->CreateShaderResourceView(mOffscreenTexture, NULL, &mOffscreenSRView); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mOffscreenSRView, "Offscreen shader resource"); michael@0: michael@0: if (mDepthBufferFormat != GL_NONE) michael@0: { michael@0: D3D11_TEXTURE2D_DESC depthStencilDesc = {0}; michael@0: depthStencilDesc.Width = backbufferWidth; michael@0: depthStencilDesc.Height = backbufferHeight; michael@0: depthStencilDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mDepthBufferFormat); michael@0: depthStencilDesc.MipLevels = 1; michael@0: depthStencilDesc.ArraySize = 1; michael@0: depthStencilDesc.SampleDesc.Count = 1; michael@0: depthStencilDesc.SampleDesc.Quality = 0; michael@0: depthStencilDesc.Usage = D3D11_USAGE_DEFAULT; michael@0: depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; michael@0: depthStencilDesc.CPUAccessFlags = 0; michael@0: depthStencilDesc.MiscFlags = 0; michael@0: michael@0: result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencilTexture); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); michael@0: release(); michael@0: michael@0: if (d3d11::isDeviceLostError(result)) michael@0: { michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else michael@0: { michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: } michael@0: d3d11::SetDebugName(mDepthStencilTexture, "Depth stencil texture"); michael@0: michael@0: result = device->CreateDepthStencilView(mDepthStencilTexture, NULL, &mDepthStencilDSView); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mDepthStencilDSView, "Depth stencil view"); michael@0: } michael@0: michael@0: mWidth = backbufferWidth; michael@0: mHeight = backbufferHeight; michael@0: michael@0: if (previousOffscreenTexture != NULL) michael@0: { michael@0: D3D11_BOX sourceBox = {0}; michael@0: sourceBox.left = 0; michael@0: sourceBox.right = std::min(previousWidth, mWidth); michael@0: sourceBox.top = std::max(previousHeight - mHeight, 0); michael@0: sourceBox.bottom = previousHeight; michael@0: sourceBox.front = 0; michael@0: sourceBox.back = 1; michael@0: michael@0: ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); michael@0: const int yoffset = std::max(mHeight - previousHeight, 0); michael@0: deviceContext->CopySubresourceRegion(mOffscreenTexture, 0, 0, yoffset, 0, previousOffscreenTexture, 0, &sourceBox); michael@0: michael@0: previousOffscreenTexture->Release(); michael@0: michael@0: if (mSwapChain) michael@0: { michael@0: swapRect(0, 0, mWidth, mHeight); michael@0: } michael@0: } michael@0: michael@0: return EGL_SUCCESS; michael@0: } michael@0: michael@0: EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight) michael@0: { michael@0: ID3D11Device *device = mRenderer->getDevice(); michael@0: michael@0: if (device == NULL) michael@0: { michael@0: return EGL_BAD_ACCESS; michael@0: } michael@0: michael@0: // Can only call resize if we have already created our swap buffer and resources michael@0: ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView); michael@0: michael@0: if (mBackBufferTexture) michael@0: { michael@0: mBackBufferTexture->Release(); michael@0: mBackBufferTexture = NULL; michael@0: } michael@0: michael@0: if (mBackBufferRTView) michael@0: { michael@0: mBackBufferRTView->Release(); michael@0: mBackBufferRTView = NULL; michael@0: } michael@0: michael@0: // Resize swap chain michael@0: DXGI_FORMAT backbufferDXGIFormat = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); michael@0: HRESULT result = mSwapChain->ResizeBuffers(2, backbufferWidth, backbufferHeight, backbufferDXGIFormat, 0); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Error resizing swap chain buffers: 0x%08X", result); michael@0: release(); michael@0: michael@0: if (d3d11::isDeviceLostError(result)) michael@0: { michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else michael@0: { michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: } michael@0: michael@0: result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); michael@0: ASSERT(SUCCEEDED(result)); michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture"); michael@0: } michael@0: michael@0: result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); michael@0: ASSERT(SUCCEEDED(result)); michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target"); michael@0: } michael@0: michael@0: return resetOffscreenTexture(backbufferWidth, backbufferHeight); michael@0: } michael@0: michael@0: EGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) michael@0: { michael@0: ID3D11Device *device = mRenderer->getDevice(); michael@0: michael@0: if (device == NULL) michael@0: { michael@0: return EGL_BAD_ACCESS; michael@0: } michael@0: michael@0: // Release specific resources to free up memory for the new render target, while the michael@0: // old render target still exists for the purpose of preserving its contents. michael@0: if (mSwapChain) michael@0: { michael@0: mSwapChain->Release(); michael@0: mSwapChain = NULL; michael@0: } michael@0: michael@0: if (mBackBufferTexture) michael@0: { michael@0: mBackBufferTexture->Release(); michael@0: mBackBufferTexture = NULL; michael@0: } michael@0: michael@0: if (mBackBufferRTView) michael@0: { michael@0: mBackBufferRTView->Release(); michael@0: mBackBufferRTView = NULL; michael@0: } michael@0: michael@0: mSwapInterval = static_cast(swapInterval); michael@0: if (mSwapInterval > 4) michael@0: { michael@0: // IDXGISwapChain::Present documentation states that valid sync intervals are in the [0,4] range michael@0: return EGL_BAD_PARAMETER; michael@0: } michael@0: michael@0: // EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains michael@0: if (backbufferWidth < 1 || backbufferHeight < 1) michael@0: { michael@0: releaseOffscreenTexture(); michael@0: return EGL_SUCCESS; michael@0: } michael@0: michael@0: if (mWindow) michael@0: { michael@0: // We cannot create a swap chain for an HWND that is owned by a different process michael@0: DWORD currentProcessId = GetCurrentProcessId(); michael@0: DWORD wndProcessId; michael@0: GetWindowThreadProcessId(mWindow, &wndProcessId); michael@0: michael@0: if (currentProcessId != wndProcessId) michael@0: { michael@0: ERR("Could not create swap chain, window owned by different process"); michael@0: release(); michael@0: return EGL_BAD_NATIVE_WINDOW; michael@0: } michael@0: michael@0: IDXGIFactory *factory = mRenderer->getDxgiFactory(); michael@0: michael@0: DXGI_SWAP_CHAIN_DESC swapChainDesc = {0}; michael@0: swapChainDesc.BufferCount = 2; michael@0: swapChainDesc.BufferDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); michael@0: swapChainDesc.BufferDesc.Width = backbufferWidth; michael@0: swapChainDesc.BufferDesc.Height = backbufferHeight; michael@0: swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; michael@0: swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; michael@0: swapChainDesc.BufferDesc.RefreshRate.Numerator = 0; michael@0: swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; michael@0: swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; michael@0: swapChainDesc.Flags = 0; michael@0: swapChainDesc.OutputWindow = mWindow; michael@0: swapChainDesc.SampleDesc.Count = 1; michael@0: swapChainDesc.SampleDesc.Quality = 0; michael@0: swapChainDesc.Windowed = TRUE; michael@0: michael@0: HRESULT result = factory->CreateSwapChain(device, &swapChainDesc, &mSwapChain); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); michael@0: release(); michael@0: michael@0: if (d3d11::isDeviceLostError(result)) michael@0: { michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else michael@0: { michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: } michael@0: michael@0: result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture"); michael@0: michael@0: result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target"); michael@0: } michael@0: michael@0: // If we are resizing the swap chain, we don't wish to recreate all the static resources michael@0: if (!mPassThroughResourcesInit) michael@0: { michael@0: mPassThroughResourcesInit = true; michael@0: initPassThroughResources(); michael@0: } michael@0: michael@0: return resetOffscreenTexture(backbufferWidth, backbufferHeight); michael@0: } michael@0: michael@0: void SwapChain11::initPassThroughResources() michael@0: { michael@0: ID3D11Device *device = mRenderer->getDevice(); michael@0: michael@0: ASSERT(device != NULL); michael@0: michael@0: // Make sure our resources are all not allocated, when we create michael@0: ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL); michael@0: ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL); michael@0: michael@0: D3D11_BUFFER_DESC vbDesc; michael@0: vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; michael@0: vbDesc.Usage = D3D11_USAGE_DYNAMIC; michael@0: vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; michael@0: vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; michael@0: vbDesc.MiscFlags = 0; michael@0: vbDesc.StructureByteStride = 0; michael@0: michael@0: HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer"); michael@0: michael@0: D3D11_SAMPLER_DESC samplerDesc; michael@0: samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; michael@0: samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; michael@0: samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; michael@0: samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; michael@0: samplerDesc.MipLODBias = 0.0f; michael@0: samplerDesc.MaxAnisotropy = 0; michael@0: samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; michael@0: samplerDesc.BorderColor[0] = 0.0f; michael@0: samplerDesc.BorderColor[1] = 0.0f; michael@0: samplerDesc.BorderColor[2] = 0.0f; michael@0: samplerDesc.BorderColor[3] = 0.0f; michael@0: samplerDesc.MinLOD = 0; michael@0: samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; michael@0: michael@0: result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); michael@0: michael@0: D3D11_INPUT_ELEMENT_DESC quadLayout[] = michael@0: { michael@0: { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, michael@0: { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, michael@0: }; michael@0: michael@0: result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mPassThroughIL); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); michael@0: michael@0: result = device->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mPassThroughVS); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader"); michael@0: michael@0: result = device->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mPassThroughPS); michael@0: ASSERT(SUCCEEDED(result)); michael@0: d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader"); michael@0: } michael@0: michael@0: // parameters should be validated/clamped by caller michael@0: EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) michael@0: { michael@0: if (!mSwapChain) michael@0: { michael@0: return EGL_SUCCESS; michael@0: } michael@0: michael@0: ID3D11Device *device = mRenderer->getDevice(); michael@0: ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); michael@0: michael@0: // Set vertices michael@0: D3D11_MAPPED_SUBRESOURCE mappedResource; michael@0: HRESULT result = deviceContext->Map(mQuadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); michael@0: if (FAILED(result)) michael@0: { michael@0: return EGL_BAD_ACCESS; michael@0: } michael@0: michael@0: d3d11::PositionTexCoordVertex *vertices = static_cast(mappedResource.pData); michael@0: michael@0: // Create a quad in homogeneous coordinates michael@0: float x1 = (x / float(mWidth)) * 2.0f - 1.0f; michael@0: float y1 = (y / float(mHeight)) * 2.0f - 1.0f; michael@0: float x2 = ((x + width) / float(mWidth)) * 2.0f - 1.0f; michael@0: float y2 = ((y + height) / float(mHeight)) * 2.0f - 1.0f; michael@0: michael@0: float u1 = x / float(mWidth); michael@0: float v1 = y / float(mHeight); michael@0: float u2 = (x + width) / float(mWidth); michael@0: float v2 = (y + height) / float(mHeight); michael@0: michael@0: d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v1); michael@0: d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2); michael@0: d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1); michael@0: d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2); michael@0: michael@0: deviceContext->Unmap(mQuadVB, 0); michael@0: michael@0: static UINT stride = sizeof(d3d11::PositionTexCoordVertex); michael@0: static UINT startIdx = 0; michael@0: deviceContext->IASetVertexBuffers(0, 1, &mQuadVB, &stride, &startIdx); michael@0: michael@0: // Apply state michael@0: deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); michael@0: michael@0: static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; michael@0: deviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF); michael@0: michael@0: deviceContext->RSSetState(NULL); michael@0: michael@0: // Apply shaders michael@0: deviceContext->IASetInputLayout(mPassThroughIL); michael@0: deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); michael@0: deviceContext->VSSetShader(mPassThroughVS, NULL, 0); michael@0: deviceContext->PSSetShader(mPassThroughPS, NULL, 0); michael@0: deviceContext->GSSetShader(NULL, NULL, 0); michael@0: michael@0: // Apply render targets michael@0: mRenderer->setOneTimeRenderTarget(mBackBufferRTView); michael@0: michael@0: // Set the viewport michael@0: D3D11_VIEWPORT viewport; michael@0: viewport.TopLeftX = 0; michael@0: viewport.TopLeftY = 0; michael@0: viewport.Width = mWidth; michael@0: viewport.Height = mHeight; michael@0: viewport.MinDepth = 0.0f; michael@0: viewport.MaxDepth = 1.0f; michael@0: deviceContext->RSSetViewports(1, &viewport); michael@0: michael@0: // Apply textures michael@0: deviceContext->PSSetShaderResources(0, 1, &mOffscreenSRView); michael@0: deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler); michael@0: michael@0: // Draw michael@0: deviceContext->Draw(4, 0); michael@0: result = mSwapChain->Present(mSwapInterval, 0); michael@0: michael@0: if (result == DXGI_ERROR_DEVICE_REMOVED) michael@0: { michael@0: HRESULT removedReason = device->GetDeviceRemovedReason(); michael@0: ERR("Present failed: the D3D11 device was removed: 0x%08X", removedReason); michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else if (result == DXGI_ERROR_DEVICE_RESET) michael@0: { michael@0: ERR("Present failed: the D3D11 device was reset from a bad command."); michael@0: return EGL_CONTEXT_LOST; michael@0: } michael@0: else if (FAILED(result)) michael@0: { michael@0: ERR("Present failed with error code 0x%08X", result); michael@0: } michael@0: michael@0: // Unbind michael@0: static ID3D11ShaderResourceView *const nullSRV = NULL; michael@0: deviceContext->PSSetShaderResources(0, 1, &nullSRV); michael@0: michael@0: mRenderer->unapplyRenderTargets(); michael@0: mRenderer->markAllStateDirty(); michael@0: michael@0: return EGL_SUCCESS; michael@0: } michael@0: michael@0: // Increments refcount on texture. michael@0: // caller must Release() the returned texture michael@0: ID3D11Texture2D *SwapChain11::getOffscreenTexture() michael@0: { michael@0: if (mOffscreenTexture) michael@0: { michael@0: mOffscreenTexture->AddRef(); michael@0: } michael@0: michael@0: return mOffscreenTexture; michael@0: } michael@0: michael@0: // Increments refcount on view. michael@0: // caller must Release() the returned view michael@0: ID3D11RenderTargetView *SwapChain11::getRenderTarget() michael@0: { michael@0: if (mOffscreenRTView) michael@0: { michael@0: mOffscreenRTView->AddRef(); michael@0: } michael@0: michael@0: return mOffscreenRTView; michael@0: } michael@0: michael@0: // Increments refcount on view. michael@0: // caller must Release() the returned view michael@0: ID3D11ShaderResourceView *SwapChain11::getRenderTargetShaderResource() michael@0: { michael@0: if (mOffscreenSRView) michael@0: { michael@0: mOffscreenSRView->AddRef(); michael@0: } michael@0: michael@0: return mOffscreenSRView; michael@0: } michael@0: michael@0: // Increments refcount on view. michael@0: // caller must Release() the returned view michael@0: ID3D11DepthStencilView *SwapChain11::getDepthStencil() michael@0: { michael@0: if (mDepthStencilDSView) michael@0: { michael@0: mDepthStencilDSView->AddRef(); michael@0: } michael@0: michael@0: return mDepthStencilDSView; michael@0: } michael@0: michael@0: ID3D11Texture2D *SwapChain11::getDepthStencilTexture() michael@0: { michael@0: if (mDepthStencilTexture) michael@0: { michael@0: mDepthStencilTexture->AddRef(); michael@0: } michael@0: michael@0: return mDepthStencilTexture; michael@0: } michael@0: michael@0: SwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain11*, swapChain)); michael@0: return static_cast(swapChain); michael@0: } michael@0: michael@0: void SwapChain11::recreate() michael@0: { michael@0: // possibly should use this method instead of reset michael@0: } michael@0: michael@0: }