WinRT: more work on moving rendering code from SDL_winrtrenderer.* to SDL_render_d3d11*
authorDavid Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 14:56:32 -0500
changeset 841259f1191a5cca
parent 8411 aca0591bc58f
child 8413 4ae81c5dc55e
WinRT: more work on moving rendering code from SDL_winrtrenderer.* to SDL_render_d3d11*
src/render/direct3d11/SDL_render_d3d11.cpp
src/render/direct3d11/SDL_render_d3d11_cpp.h
src/video/windowsrt/SDL_winrtrenderer.cpp
src/video/windowsrt/SDL_winrtrenderer.h
src/video/windowsrt/SDL_winrtvideo.cpp
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 09 14:35:06 2013 -0500
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 09 14:56:32 2013 -0500
     1.3 @@ -43,6 +43,11 @@
     1.4  using namespace Microsoft::WRL;
     1.5  using namespace std;
     1.6  
     1.7 +#ifdef __WINRT__
     1.8 +using namespace Windows::Graphics::Display;
     1.9 +using namespace Windows::UI::Core;
    1.10 +#endif
    1.11 +
    1.12  /* Direct3D 11.1 renderer implementation */
    1.13  
    1.14  static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.15 @@ -77,7 +82,7 @@
    1.16  //static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
    1.17  
    1.18  /* Direct3D 11.1 Internal Functions */
    1.19 -HRESULT WINRT_CreateDeviceResources(SDL_Renderer * renderer);
    1.20 +HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
    1.21  
    1.22  
    1.23  extern "C" {
    1.24 @@ -132,6 +137,8 @@
    1.25      data->featureLevel = (D3D_FEATURE_LEVEL) 0;
    1.26      data->vertexCount = 0;
    1.27      data->loadingComplete = false;
    1.28 +    data->windowSizeInDIPs = XMFLOAT2(0, 0);
    1.29 +    data->renderTargetSize = XMFLOAT2(0, 0);
    1.30  
    1.31      // TODO: Create Direct3D Object(s)
    1.32  
    1.33 @@ -327,7 +334,7 @@
    1.34  }
    1.35  
    1.36  HRESULT
    1.37 -WINRT_CreateDeviceResources(SDL_Renderer * renderer)
    1.38 +D3D11_CreateDeviceResources(SDL_Renderer * renderer)
    1.39  {
    1.40      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    1.41  
    1.42 @@ -512,6 +519,264 @@
    1.43      return S_OK;
    1.44  }
    1.45  
    1.46 +#ifdef __WINRT__
    1.47 +
    1.48 +CoreWindow ^
    1.49 +D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
    1.50 +{
    1.51 +    SDL_Window * sdlWindow = renderer->window;
    1.52 +    if ( ! renderer->window ) {
    1.53 +        return nullptr;
    1.54 +    }
    1.55 +
    1.56 +    SDL_SysWMinfo sdlWindowInfo;
    1.57 +    SDL_VERSION(&sdlWindowInfo.version);
    1.58 +    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
    1.59 +        return nullptr;
    1.60 +    }
    1.61 +
    1.62 +    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
    1.63 +        return nullptr;
    1.64 +    }
    1.65 +
    1.66 +    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
    1.67 +    if ( ! coreWindowPointer ) {
    1.68 +        return nullptr;
    1.69 +    }
    1.70 +
    1.71 +    return *coreWindowPointer;
    1.72 +}
    1.73 +
    1.74 +static float
    1.75 +D3D11_ConvertDipsToPixels(float dips)
    1.76 +{
    1.77 +    static const float dipsPerInch = 96.0f;
    1.78 +    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
    1.79 +}
    1.80 +#endif
    1.81 +
    1.82 +// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
    1.83 +HRESULT
    1.84 +D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
    1.85 +{
    1.86 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    1.87 +    HRESULT result = S_OK;
    1.88 +    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
    1.89 +
    1.90 +    // Store the window bounds so the next time we get a SizeChanged event we can
    1.91 +    // avoid rebuilding everything if the size is identical.
    1.92 +    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
    1.93 +    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
    1.94 +
    1.95 +    // Calculate the necessary swap chain and render target size in pixels.
    1.96 +    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
    1.97 +    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
    1.98 +
    1.99 +    // The width and height of the swap chain must be based on the window's
   1.100 +    // landscape-oriented width and height. If the window is in a portrait
   1.101 +    // orientation, the dimensions must be reversed.
   1.102 +    data->orientation = DisplayProperties::CurrentOrientation;
   1.103 +    bool swapDimensions =
   1.104 +        data->orientation == DisplayOrientations::Portrait ||
   1.105 +        data->orientation == DisplayOrientations::PortraitFlipped;
   1.106 +    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
   1.107 +    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
   1.108 +
   1.109 +    if(data->swapChain != nullptr)
   1.110 +    {
   1.111 +        // If the swap chain already exists, resize it.
   1.112 +        result = data->swapChain->ResizeBuffers(
   1.113 +            2, // Double-buffered swap chain.
   1.114 +            static_cast<UINT>(data->renderTargetSize.x),
   1.115 +            static_cast<UINT>(data->renderTargetSize.y),
   1.116 +            DXGI_FORMAT_B8G8R8A8_UNORM,
   1.117 +            0
   1.118 +            );
   1.119 +        if (FAILED(result)) {
   1.120 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.121 +            return result;
   1.122 +        }
   1.123 +    }
   1.124 +    else
   1.125 +    {
   1.126 +        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
   1.127 +        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
   1.128 +        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
   1.129 +        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
   1.130 +        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
   1.131 +        swapChainDesc.Stereo = false;
   1.132 +        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
   1.133 +        swapChainDesc.SampleDesc.Quality = 0;
   1.134 +        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
   1.135 +        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
   1.136 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   1.137 +        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
   1.138 +        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
   1.139 +#else
   1.140 +        swapChainDesc.Scaling = DXGI_SCALING_NONE;
   1.141 +        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
   1.142 +#endif
   1.143 +        swapChainDesc.Flags = 0;
   1.144 +
   1.145 +        ComPtr<IDXGIDevice1>  dxgiDevice;
   1.146 +        result = data->d3dDevice.As(&dxgiDevice);
   1.147 +        if (FAILED(result)) {
   1.148 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.149 +            return result;
   1.150 +        }
   1.151 +
   1.152 +        ComPtr<IDXGIAdapter> dxgiAdapter;
   1.153 +        result = dxgiDevice->GetAdapter(&dxgiAdapter);
   1.154 +        if (FAILED(result)) {
   1.155 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.156 +            return result;
   1.157 +        }
   1.158 +
   1.159 +        ComPtr<IDXGIFactory2> dxgiFactory;
   1.160 +        result = dxgiAdapter->GetParent(
   1.161 +            __uuidof(IDXGIFactory2), 
   1.162 +            &dxgiFactory
   1.163 +            );
   1.164 +        if (FAILED(result)) {
   1.165 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.166 +            return result;
   1.167 +        }
   1.168 +
   1.169 +        result = dxgiFactory->CreateSwapChainForCoreWindow(
   1.170 +            data->d3dDevice.Get(),
   1.171 +            reinterpret_cast<IUnknown*>(coreWindow),
   1.172 +            &swapChainDesc,
   1.173 +            nullptr, // Allow on all displays.
   1.174 +            &data->swapChain
   1.175 +            );
   1.176 +        if (FAILED(result)) {
   1.177 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.178 +            return result;
   1.179 +        }
   1.180 +            
   1.181 +        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
   1.182 +        // ensures that the application will only render after each VSync, minimizing power consumption.
   1.183 +        result = dxgiDevice->SetMaximumFrameLatency(1);
   1.184 +        if (FAILED(result)) {
   1.185 +            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.186 +            return result;
   1.187 +        }
   1.188 +    }
   1.189 +    
   1.190 +    // Set the proper orientation for the swap chain, and generate the
   1.191 +    // 3D matrix transformation for rendering to the rotated swap chain.
   1.192 +    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
   1.193 +    switch (data->orientation)
   1.194 +    {
   1.195 +        case DisplayOrientations::Landscape:
   1.196 +            rotation = DXGI_MODE_ROTATION_IDENTITY;
   1.197 +            data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
   1.198 +                1.0f, 0.0f, 0.0f, 0.0f,
   1.199 +                0.0f, 1.0f, 0.0f, 0.0f,
   1.200 +                0.0f, 0.0f, 1.0f, 0.0f,
   1.201 +                0.0f, 0.0f, 0.0f, 1.0f
   1.202 +                );
   1.203 +            break;
   1.204 +
   1.205 +        case DisplayOrientations::Portrait:
   1.206 +            rotation = DXGI_MODE_ROTATION_ROTATE270;
   1.207 +            data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
   1.208 +                0.0f, 1.0f, 0.0f, 0.0f,
   1.209 +                -1.0f, 0.0f, 0.0f, 0.0f,
   1.210 +                0.0f, 0.0f, 1.0f, 0.0f,
   1.211 +                0.0f, 0.0f, 0.0f, 1.0f
   1.212 +                );
   1.213 +            break;
   1.214 +
   1.215 +        case DisplayOrientations::LandscapeFlipped:
   1.216 +            rotation = DXGI_MODE_ROTATION_ROTATE180;
   1.217 +            data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
   1.218 +                -1.0f, 0.0f, 0.0f, 0.0f,
   1.219 +                0.0f, -1.0f, 0.0f, 0.0f,
   1.220 +                0.0f, 0.0f, 1.0f, 0.0f,
   1.221 +                0.0f, 0.0f, 0.0f, 1.0f
   1.222 +                );
   1.223 +            break;
   1.224 +
   1.225 +        case DisplayOrientations::PortraitFlipped:
   1.226 +            rotation = DXGI_MODE_ROTATION_ROTATE90;
   1.227 +            data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
   1.228 +                0.0f, -1.0f, 0.0f, 0.0f,
   1.229 +                1.0f, 0.0f, 0.0f, 0.0f,
   1.230 +                0.0f, 0.0f, 1.0f, 0.0f,
   1.231 +                0.0f, 0.0f, 0.0f, 1.0f
   1.232 +                );
   1.233 +            break;
   1.234 +
   1.235 +        default:
   1.236 +            throw ref new Platform::FailureException();
   1.237 +    }
   1.238 +
   1.239 +#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   1.240 +    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
   1.241 +    result = data->swapChain->SetRotation(rotation);
   1.242 +    if (FAILED(result)) {
   1.243 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.244 +        return result;
   1.245 +    }
   1.246 +#endif
   1.247 +
   1.248 +    // Create a render target view of the swap chain back buffer.
   1.249 +    ComPtr<ID3D11Texture2D> backBuffer;
   1.250 +    result = data->swapChain->GetBuffer(
   1.251 +        0,
   1.252 +        __uuidof(ID3D11Texture2D),
   1.253 +        &backBuffer
   1.254 +        );
   1.255 +    if (FAILED(result)) {
   1.256 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.257 +        return result;
   1.258 +    }
   1.259 +
   1.260 +    result = data->d3dDevice->CreateRenderTargetView(
   1.261 +        backBuffer.Get(),
   1.262 +        nullptr,
   1.263 +        &data->renderTargetView
   1.264 +        );
   1.265 +    if (FAILED(result)) {
   1.266 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.267 +        return result;
   1.268 +    }
   1.269 +
   1.270 +    // Create a depth stencil view.
   1.271 +    CD3D11_TEXTURE2D_DESC depthStencilDesc(
   1.272 +        DXGI_FORMAT_D24_UNORM_S8_UINT, 
   1.273 +        static_cast<UINT>(data->renderTargetSize.x),
   1.274 +        static_cast<UINT>(data->renderTargetSize.y),
   1.275 +        1,
   1.276 +        1,
   1.277 +        D3D11_BIND_DEPTH_STENCIL
   1.278 +        );
   1.279 +
   1.280 +    ComPtr<ID3D11Texture2D> depthStencil;
   1.281 +    result = data->d3dDevice->CreateTexture2D(
   1.282 +        &depthStencilDesc,
   1.283 +        nullptr,
   1.284 +        &depthStencil
   1.285 +        );
   1.286 +    if (FAILED(result)) {
   1.287 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
   1.288 +        return result;
   1.289 +    }
   1.290 +
   1.291 +    // Set the rendering viewport to target the entire window.
   1.292 +    CD3D11_VIEWPORT viewport(
   1.293 +        0.0f,
   1.294 +        0.0f,
   1.295 +        data->renderTargetSize.x,
   1.296 +        data->renderTargetSize.y
   1.297 +        );
   1.298 +
   1.299 +    data->d3dContext->RSSetViewports(1, &viewport);
   1.300 +
   1.301 +    return S_OK;
   1.302 +}
   1.303 +
   1.304  static int
   1.305  D3D11_UpdateViewport(SDL_Renderer * renderer)
   1.306  {
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 09 14:35:06 2013 -0500
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 09 14:56:32 2013 -0500
     2.3 @@ -40,6 +40,14 @@
     2.4      D3D_FEATURE_LEVEL featureLevel;
     2.5      unsigned int vertexCount;
     2.6      bool loadingComplete;
     2.7 +
     2.8 +    // Cached renderer properties.
     2.9 +    DirectX::XMFLOAT2 windowSizeInDIPs;
    2.10 +    DirectX::XMFLOAT2 renderTargetSize;
    2.11 +    Windows::Graphics::Display::DisplayOrientations orientation;
    2.12 +
    2.13 +    // Transform used for display orientation.
    2.14 +    DirectX::XMFLOAT4X4 orientationTransform3D;
    2.15  } D3D11_RenderData;
    2.16  
    2.17  typedef struct
     3.1 --- a/src/video/windowsrt/SDL_winrtrenderer.cpp	Sat Feb 09 14:35:06 2013 -0500
     3.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp	Sat Feb 09 14:56:32 2013 -0500
     3.3 @@ -5,6 +5,10 @@
     3.4  #include "SDLmain_WinRT_common.h"
     3.5  #include "SDL_winrtrenderer.h"
     3.6  
     3.7 +extern "C" {
     3.8 +#include "SDL_syswm.h"
     3.9 +}
    3.10 +
    3.11  using namespace DirectX;
    3.12  using namespace Microsoft::WRL;
    3.13  using namespace std;
    3.14 @@ -12,6 +16,10 @@
    3.15  using namespace Windows::Foundation;
    3.16  using namespace Windows::Graphics::Display;
    3.17  
    3.18 +extern HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
    3.19 +extern CoreWindow ^ D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer);
    3.20 +extern HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
    3.21 +
    3.22  // Constructor.
    3.23  SDL_winrtrenderer::SDL_winrtrenderer() :
    3.24      m_mainTextureHelperSurface(NULL),
    3.25 @@ -29,10 +37,8 @@
    3.26  }
    3.27  
    3.28  // Initialize the Direct3D resources required to run.
    3.29 -void SDL_winrtrenderer::Initialize(CoreWindow^ window)
    3.30 -{
    3.31 -    m_window = window;
    3.32 -    
    3.33 +void SDL_winrtrenderer::Initialize()
    3.34 +{ 
    3.35      CreateDeviceResources();
    3.36      CreateWindowSizeDependentResources();
    3.37  }
    3.38 @@ -41,8 +47,8 @@
    3.39  void SDL_winrtrenderer::HandleDeviceLost()
    3.40  {
    3.41      // Reset these member variables to ensure that UpdateForWindowSizeChange recreates all resources.
    3.42 -    m_windowBounds.Width = 0;
    3.43 -    m_windowBounds.Height = 0;
    3.44 +    m_sdlRendererData->windowSizeInDIPs.x = 0;
    3.45 +    m_sdlRendererData->windowSizeInDIPs.y = 0;
    3.46      m_sdlRendererData->swapChain = nullptr;
    3.47  
    3.48      // TODO, WinRT: reconnect HandleDeviceLost to SDL_Renderer
    3.49 @@ -50,207 +56,16 @@
    3.50      UpdateForWindowSizeChange();
    3.51  }
    3.52  
    3.53 -extern HRESULT WINRT_CreateDeviceResources(SDL_Renderer * renderer);
    3.54 -
    3.55  // These are the resources that depend on the device.
    3.56  void SDL_winrtrenderer::CreateDeviceResources()
    3.57  {
    3.58 -    DX::ThrowIfFailed(WINRT_CreateDeviceResources(m_sdlRenderer));
    3.59 +    DX::ThrowIfFailed(D3D11_CreateDeviceResources(m_sdlRenderer));
    3.60  }
    3.61  
    3.62  // Allocate all memory resources that change on a window SizeChanged event.
    3.63  void SDL_winrtrenderer::CreateWindowSizeDependentResources()
    3.64  {
    3.65 -    // Store the window bounds so the next time we get a SizeChanged event we can
    3.66 -    // avoid rebuilding everything if the size is identical.
    3.67 -    m_windowBounds = m_window->Bounds;
    3.68 -
    3.69 -    // Calculate the necessary swap chain and render target size in pixels.
    3.70 -    float windowWidth = ConvertDipsToPixels(m_windowBounds.Width);
    3.71 -    float windowHeight = ConvertDipsToPixels(m_windowBounds.Height);
    3.72 -
    3.73 -    // The width and height of the swap chain must be based on the window's
    3.74 -    // landscape-oriented width and height. If the window is in a portrait
    3.75 -    // orientation, the dimensions must be reversed.
    3.76 -    m_orientation = DisplayProperties::CurrentOrientation;
    3.77 -    bool swapDimensions =
    3.78 -        m_orientation == DisplayOrientations::Portrait ||
    3.79 -        m_orientation == DisplayOrientations::PortraitFlipped;
    3.80 -    m_renderTargetSize.Width = swapDimensions ? windowHeight : windowWidth;
    3.81 -    m_renderTargetSize.Height = swapDimensions ? windowWidth : windowHeight;
    3.82 -
    3.83 -    if(m_sdlRendererData->swapChain != nullptr)
    3.84 -    {
    3.85 -        // If the swap chain already exists, resize it.
    3.86 -        DX::ThrowIfFailed(
    3.87 -            m_sdlRendererData->swapChain->ResizeBuffers(
    3.88 -                2, // Double-buffered swap chain.
    3.89 -                static_cast<UINT>(m_renderTargetSize.Width),
    3.90 -                static_cast<UINT>(m_renderTargetSize.Height),
    3.91 -                DXGI_FORMAT_B8G8R8A8_UNORM,
    3.92 -                0
    3.93 -                )
    3.94 -            );
    3.95 -    }
    3.96 -    else
    3.97 -    {
    3.98 -        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
    3.99 -        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
   3.100 -        swapChainDesc.Width = static_cast<UINT>(m_renderTargetSize.Width); // Match the size of the window.
   3.101 -        swapChainDesc.Height = static_cast<UINT>(m_renderTargetSize.Height);
   3.102 -        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
   3.103 -        swapChainDesc.Stereo = false;
   3.104 -        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
   3.105 -        swapChainDesc.SampleDesc.Quality = 0;
   3.106 -        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
   3.107 -        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
   3.108 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   3.109 -        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
   3.110 -        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
   3.111 -#else
   3.112 -        swapChainDesc.Scaling = DXGI_SCALING_NONE;
   3.113 -        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
   3.114 -#endif
   3.115 -        swapChainDesc.Flags = 0;
   3.116 -
   3.117 -        ComPtr<IDXGIDevice1>  dxgiDevice;
   3.118 -        DX::ThrowIfFailed(
   3.119 -            m_sdlRendererData->d3dDevice.As(&dxgiDevice)
   3.120 -            );
   3.121 -
   3.122 -        ComPtr<IDXGIAdapter> dxgiAdapter;
   3.123 -        DX::ThrowIfFailed(
   3.124 -            dxgiDevice->GetAdapter(&dxgiAdapter)
   3.125 -            );
   3.126 -
   3.127 -        ComPtr<IDXGIFactory2> dxgiFactory;
   3.128 -        DX::ThrowIfFailed(
   3.129 -            dxgiAdapter->GetParent(
   3.130 -                __uuidof(IDXGIFactory2), 
   3.131 -                &dxgiFactory
   3.132 -                )
   3.133 -            );
   3.134 -
   3.135 -        Windows::UI::Core::CoreWindow^ window = m_window.Get();
   3.136 -        DX::ThrowIfFailed(
   3.137 -            dxgiFactory->CreateSwapChainForCoreWindow(
   3.138 -                m_sdlRendererData->d3dDevice.Get(),
   3.139 -                reinterpret_cast<IUnknown*>(window),
   3.140 -                &swapChainDesc,
   3.141 -                nullptr, // Allow on all displays.
   3.142 -                &m_sdlRendererData->swapChain
   3.143 -                )
   3.144 -            );
   3.145 -            
   3.146 -        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
   3.147 -        // ensures that the application will only render after each VSync, minimizing power consumption.
   3.148 -        DX::ThrowIfFailed(
   3.149 -            dxgiDevice->SetMaximumFrameLatency(1)
   3.150 -            );
   3.151 -    }
   3.152 -    
   3.153 -    // Set the proper orientation for the swap chain, and generate the
   3.154 -    // 3D matrix transformation for rendering to the rotated swap chain.
   3.155 -    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
   3.156 -    switch (m_orientation)
   3.157 -    {
   3.158 -        case DisplayOrientations::Landscape:
   3.159 -            rotation = DXGI_MODE_ROTATION_IDENTITY;
   3.160 -            m_orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
   3.161 -                1.0f, 0.0f, 0.0f, 0.0f,
   3.162 -                0.0f, 1.0f, 0.0f, 0.0f,
   3.163 -                0.0f, 0.0f, 1.0f, 0.0f,
   3.164 -                0.0f, 0.0f, 0.0f, 1.0f
   3.165 -                );
   3.166 -            break;
   3.167 -
   3.168 -        case DisplayOrientations::Portrait:
   3.169 -            rotation = DXGI_MODE_ROTATION_ROTATE270;
   3.170 -            m_orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
   3.171 -                0.0f, 1.0f, 0.0f, 0.0f,
   3.172 -                -1.0f, 0.0f, 0.0f, 0.0f,
   3.173 -                0.0f, 0.0f, 1.0f, 0.0f,
   3.174 -                0.0f, 0.0f, 0.0f, 1.0f
   3.175 -                );
   3.176 -            break;
   3.177 -
   3.178 -        case DisplayOrientations::LandscapeFlipped:
   3.179 -            rotation = DXGI_MODE_ROTATION_ROTATE180;
   3.180 -            m_orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
   3.181 -                -1.0f, 0.0f, 0.0f, 0.0f,
   3.182 -                0.0f, -1.0f, 0.0f, 0.0f,
   3.183 -                0.0f, 0.0f, 1.0f, 0.0f,
   3.184 -                0.0f, 0.0f, 0.0f, 1.0f
   3.185 -                );
   3.186 -            break;
   3.187 -
   3.188 -        case DisplayOrientations::PortraitFlipped:
   3.189 -            rotation = DXGI_MODE_ROTATION_ROTATE90;
   3.190 -            m_orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
   3.191 -                0.0f, -1.0f, 0.0f, 0.0f,
   3.192 -                1.0f, 0.0f, 0.0f, 0.0f,
   3.193 -                0.0f, 0.0f, 1.0f, 0.0f,
   3.194 -                0.0f, 0.0f, 0.0f, 1.0f
   3.195 -                );
   3.196 -            break;
   3.197 -
   3.198 -        default:
   3.199 -            throw ref new Platform::FailureException();
   3.200 -    }
   3.201 -
   3.202 -#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   3.203 -    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
   3.204 -    DX::ThrowIfFailed(
   3.205 -        m_sdlRendererData->swapChain->SetRotation(rotation)
   3.206 -        );
   3.207 -#endif
   3.208 -
   3.209 -    // Create a render target view of the swap chain back buffer.
   3.210 -    ComPtr<ID3D11Texture2D> backBuffer;
   3.211 -    DX::ThrowIfFailed(
   3.212 -        m_sdlRendererData->swapChain->GetBuffer(
   3.213 -            0,
   3.214 -            __uuidof(ID3D11Texture2D),
   3.215 -            &backBuffer
   3.216 -            )
   3.217 -        );
   3.218 -
   3.219 -    DX::ThrowIfFailed(
   3.220 -        m_sdlRendererData->d3dDevice->CreateRenderTargetView(
   3.221 -            backBuffer.Get(),
   3.222 -            nullptr,
   3.223 -            &m_sdlRendererData->renderTargetView
   3.224 -            )
   3.225 -        );
   3.226 -
   3.227 -    // Create a depth stencil view.
   3.228 -    CD3D11_TEXTURE2D_DESC depthStencilDesc(
   3.229 -        DXGI_FORMAT_D24_UNORM_S8_UINT, 
   3.230 -        static_cast<UINT>(m_renderTargetSize.Width),
   3.231 -        static_cast<UINT>(m_renderTargetSize.Height),
   3.232 -        1,
   3.233 -        1,
   3.234 -        D3D11_BIND_DEPTH_STENCIL
   3.235 -        );
   3.236 -
   3.237 -    ComPtr<ID3D11Texture2D> depthStencil;
   3.238 -    DX::ThrowIfFailed(
   3.239 -        m_sdlRendererData->d3dDevice->CreateTexture2D(
   3.240 -            &depthStencilDesc,
   3.241 -            nullptr,
   3.242 -            &depthStencil
   3.243 -            )
   3.244 -        );
   3.245 -
   3.246 -    // Set the rendering viewport to target the entire window.
   3.247 -    CD3D11_VIEWPORT viewport(
   3.248 -        0.0f,
   3.249 -        0.0f,
   3.250 -        m_renderTargetSize.Width,
   3.251 -        m_renderTargetSize.Height
   3.252 -        );
   3.253 -
   3.254 -    m_sdlRendererData->d3dContext->RSSetViewports(1, &viewport);
   3.255 +    DX::ThrowIfFailed(D3D11_CreateWindowSizeDependentResources(m_sdlRenderer));
   3.256  }
   3.257  
   3.258  void SDL_winrtrenderer::ResizeMainTexture(int w, int h)
   3.259 @@ -323,9 +138,10 @@
   3.260  // This method is called in the event handler for the SizeChanged event.
   3.261  void SDL_winrtrenderer::UpdateForWindowSizeChange()
   3.262  {
   3.263 -    if (m_window->Bounds.Width  != m_windowBounds.Width ||
   3.264 -        m_window->Bounds.Height != m_windowBounds.Height ||
   3.265 -        m_orientation != DisplayProperties::CurrentOrientation)
   3.266 +    CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(m_sdlRenderer);
   3.267 +    if (coreWindow->Bounds.Width  != m_sdlRendererData->windowSizeInDIPs.x ||
   3.268 +        coreWindow->Bounds.Height != m_sdlRendererData->windowSizeInDIPs.y ||
   3.269 +        m_sdlRendererData->orientation != DisplayProperties::CurrentOrientation)
   3.270      {
   3.271          ID3D11RenderTargetView* nullViews[] = {nullptr};
   3.272          m_sdlRendererData->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
     4.1 --- a/src/video/windowsrt/SDL_winrtrenderer.h	Sat Feb 09 14:35:06 2013 -0500
     4.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.h	Sat Feb 09 14:56:32 2013 -0500
     4.3 @@ -1,8 +1,12 @@
     4.4  #pragma once
     4.5  
     4.6  #include "DirectXHelper.h"
     4.7 +#include "../../render/direct3d11/SDL_render_d3d11_cpp.h"
     4.8 +
     4.9 +extern "C" {
    4.10  #include "SDL.h"
    4.11 -#include "../../render/direct3d11/SDL_render_d3d11_cpp.h"
    4.12 +#include "../../render/SDL_sysrender.h"
    4.13 +}
    4.14  
    4.15  // Helper class that initializes DirectX APIs for 3D rendering.
    4.16  ref class SDL_winrtrenderer
    4.17 @@ -12,7 +16,7 @@
    4.18  
    4.19  public:
    4.20      virtual ~SDL_winrtrenderer();
    4.21 -    virtual void Initialize(Windows::UI::Core::CoreWindow^ window);
    4.22 +    virtual void Initialize();
    4.23      virtual void HandleDeviceLost();
    4.24      virtual void CreateDeviceResources();
    4.25      virtual void CreateWindowSizeDependentResources();
    4.26 @@ -32,16 +36,4 @@
    4.27  protected private:
    4.28      // UpdateWindowSurface helper objects
    4.29      SDL_Surface * m_mainTextureHelperSurface;
    4.30 -
    4.31 -    // Cached renderer properties.
    4.32 -    Windows::Foundation::Size m_renderTargetSize;
    4.33 -    Windows::Foundation::Rect m_windowBounds;
    4.34 -    Platform::Agile<Windows::UI::Core::CoreWindow> m_window;
    4.35 -    Windows::Graphics::Display::DisplayOrientations m_orientation;
    4.36 -
    4.37 -    // Transform used for display orientation.
    4.38 -    DirectX::XMFLOAT4X4 m_orientationTransform3D;
    4.39 -
    4.40 -    // Has the renderer finished loading?
    4.41 -    bool m_loadingComplete;
    4.42  };
     5.1 --- a/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 14:35:06 2013 -0500
     5.2 +++ b/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 14:56:32 2013 -0500
     5.3 @@ -232,7 +232,7 @@
     5.4      SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
     5.5      SDL_WinRTGlobalApp->m_renderer->m_sdlRenderer = renderer;
     5.6      SDL_WinRTGlobalApp->m_renderer->m_sdlRendererData = (D3D11_RenderData *) renderer->driverdata;
     5.7 -    SDL_WinRTGlobalApp->m_renderer->Initialize(CoreWindow::GetForCurrentThread());
     5.8 +    SDL_WinRTGlobalApp->m_renderer->Initialize();
     5.9  
    5.10      /* All done! */
    5.11      return 0;