Converted David Ludwig's D3D11 renderer to C and optimized it.
authorSam Lantinga <slouken@libsdl.org>
Mon, 10 Mar 2014 01:51:03 -0700
changeset 85912228ae5a3ac6
parent 8590 6e6bd53feff0
child 8592 0d6dac81324f
Converted David Ludwig's D3D11 renderer to C and optimized it.
The D3D11 renderer is now slightly faster than D3D9 on my Windows 8 machine (testsprite2 runs at 3400 FPS vs 3100 FPS)
This will need tweaking to fix the Windows RT build.
CMakeLists.txt
VisualC/SDL/SDL_VS2008.vcproj
VisualC/SDL/SDL_VS2010.vcxproj
VisualC/SDL/SDL_VS2012.vcxproj
VisualC/SDL/SDL_VS2013.vcxproj
configure
configure.in
include/SDL_config.h.cmake
include/SDL_config.h.in
include/SDL_config_windows.h
src/render/SDL_render.c
src/render/direct3d11/SDL_render_d3d11.c
src/render/direct3d11/SDL_render_d3d11.cpp
     1.1 --- a/CMakeLists.txt	Sun Mar 09 22:48:38 2014 -0700
     1.2 +++ b/CMakeLists.txt	Mon Mar 10 01:51:03 2014 -0700
     1.3 @@ -799,11 +799,12 @@
     1.4      endif()
     1.5      set(CMAKE_REQUIRED_FLAGS "/I\"$ENV{DXSDK_DIR}\\Include\"")
     1.6      check_include_file(d3d9.h HAVE_D3D_H)
     1.7 +    check_include_file(d3d11_1.h HAVE_D3D11_H)
     1.8      check_include_file(ddraw.h HAVE_DDRAW_H)
     1.9      check_include_file(dsound.h HAVE_DSOUND_H)
    1.10      check_include_file(dinput.h HAVE_DINPUT_H)
    1.11      check_include_file(xaudio2.h HAVE_XAUDIO2_H)
    1.12 -    if(HAVE_D3D_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H OR HAVE_XAUDIO2_H)
    1.13 +    if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H OR HAVE_XAUDIO2_H)
    1.14        set(HAVE_DIRECTX TRUE)
    1.15        # TODO: change $ENV{DXSDL_DIR} to get the path from the include checks
    1.16        link_directories($ENV{DXSDK_DIR}\\lib\\${PROCESSOR_ARCH})
    1.17 @@ -844,6 +845,10 @@
    1.18        set(SDL_VIDEO_RENDER_D3D 1)
    1.19        set(HAVE_RENDER_D3D TRUE)
    1.20      endif()
    1.21 +    if(RENDER_D3D AND HAVE_D3D11_H)
    1.22 +      set(SDL_VIDEO_RENDER_D3D11 1)
    1.23 +      set(HAVE_RENDER_D3D TRUE)
    1.24 +    endif()
    1.25      set(HAVE_SDL_VIDEO TRUE)
    1.26    endif()
    1.27  
     2.1 --- a/VisualC/SDL/SDL_VS2008.vcproj	Sun Mar 09 22:48:38 2014 -0700
     2.2 +++ b/VisualC/SDL/SDL_VS2008.vcproj	Mon Mar 10 01:51:03 2014 -0700
     2.3 @@ -1021,6 +1021,10 @@
     2.4  			>
     2.5  		</File>
     2.6  		<File
     2.7 +			RelativePath="..\..\src\render\direct3d11\SDL_render_d3d11.c"
     2.8 +			>
     2.9 +		</File>
    2.10 +		<File
    2.11  			RelativePath="..\..\src\render\direct3d\SDL_render_d3d.c"
    2.12  			>
    2.13  		</File>
     3.1 --- a/VisualC/SDL/SDL_VS2010.vcxproj	Sun Mar 09 22:48:38 2014 -0700
     3.2 +++ b/VisualC/SDL/SDL_VS2010.vcxproj	Mon Mar 10 01:51:03 2014 -0700
     3.3 @@ -581,6 +581,7 @@
     3.4      <ClCompile Include="..\..\src\libm\s_floor.c" />
     3.5      <ClCompile Include="..\..\src\libm\s_scalbn.c" />
     3.6      <ClCompile Include="..\..\src\libm\s_sin.c" />
     3.7 +    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.c" />
     3.8      <ClCompile Include="..\..\src\render\direct3d\SDL_render_d3d.c" />
     3.9      <ClCompile Include="..\..\src\render\opengl\SDL_render_gl.c" />
    3.10      <ClCompile Include="..\..\src\render\opengl\SDL_shaders_gl.c" />
    3.11 @@ -687,4 +688,4 @@
    3.12    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    3.13    <ImportGroup Label="ExtensionTargets">
    3.14    </ImportGroup>
    3.15 -</Project>
    3.16 \ No newline at end of file
    3.17 +</Project>
     4.1 --- a/VisualC/SDL/SDL_VS2012.vcxproj	Sun Mar 09 22:48:38 2014 -0700
     4.2 +++ b/VisualC/SDL/SDL_VS2012.vcxproj	Mon Mar 10 01:51:03 2014 -0700
     4.3 @@ -372,6 +372,7 @@
     4.4      <ClCompile Include="..\..\src\libm\s_floor.c" />
     4.5      <ClCompile Include="..\..\src\libm\s_scalbn.c" />
     4.6      <ClCompile Include="..\..\src\libm\s_sin.c" />
     4.7 +    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.c" />
     4.8      <ClCompile Include="..\..\src\render\direct3d\SDL_render_d3d.c" />
     4.9      <ClCompile Include="..\..\src\render\opengl\SDL_render_gl.c" />
    4.10      <ClCompile Include="..\..\src\render\opengl\SDL_shaders_gl.c" />
     5.1 --- a/VisualC/SDL/SDL_VS2013.vcxproj	Sun Mar 09 22:48:38 2014 -0700
     5.2 +++ b/VisualC/SDL/SDL_VS2013.vcxproj	Mon Mar 10 01:51:03 2014 -0700
     5.3 @@ -374,6 +374,7 @@
     5.4      <ClCompile Include="..\..\src\libm\s_floor.c" />
     5.5      <ClCompile Include="..\..\src\libm\s_scalbn.c" />
     5.6      <ClCompile Include="..\..\src\libm\s_sin.c" />
     5.7 +    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.c" />
     5.8      <ClCompile Include="..\..\src\render\direct3d\SDL_render_d3d.c" />
     5.9      <ClCompile Include="..\..\src\render\opengles2\SDL_render_gles2.c" />
    5.10      <ClCompile Include="..\..\src\render\opengles2\SDL_shaders_gles2.c" />
     6.1 --- a/configure	Sun Mar 09 22:48:38 2014 -0700
     6.2 +++ b/configure	Mon Mar 10 01:51:03 2014 -0700
     6.3 @@ -21880,6 +21880,12 @@
     6.4  fi
     6.5  
     6.6  
     6.7 +        ac_fn_c_check_header_mongrel "$LINENO" "d3d11_1.h" "ac_cv_header_d3d11_1_h" "$ac_includes_default"
     6.8 +if test "x$ac_cv_header_d3d11_1_h" = xyes; then :
     6.9 +  have_d3d11=yes
    6.10 +fi
    6.11 +
    6.12 +
    6.13          ac_fn_c_check_header_mongrel "$LINENO" "ddraw.h" "ac_cv_header_ddraw_h" "$ac_includes_default"
    6.14  if test "x$ac_cv_header_ddraw_h" = xyes; then :
    6.15    have_ddraw=yes
    6.16 @@ -22717,6 +22723,11 @@
    6.17  $as_echo "#define SDL_VIDEO_RENDER_D3D 1" >>confdefs.h
    6.18  
    6.19              fi
    6.20 +            if test x$enable_render_d3d = xyes -a x$have_d3d11 = xyes; then
    6.21 +
    6.22 +$as_echo "#define SDL_VIDEO_RENDER_D3D11 1" >>confdefs.h
    6.23 +
    6.24 +            fi
    6.25          fi
    6.26          # Set up files for the audio library
    6.27          if test x$enable_audio = xyes; then
     7.1 --- a/configure.in	Sun Mar 09 22:48:38 2014 -0700
     7.2 +++ b/configure.in	Mon Mar 10 01:51:03 2014 -0700
     7.3 @@ -2394,6 +2394,7 @@
     7.4                    , enable_directx=yes)
     7.5      if test x$enable_directx = xyes; then
     7.6          AC_CHECK_HEADER(d3d9.h, have_d3d=yes)
     7.7 +        AC_CHECK_HEADER(d3d11_1.h, have_d3d11=yes)
     7.8          AC_CHECK_HEADER(ddraw.h, have_ddraw=yes)
     7.9          AC_CHECK_HEADER(dsound.h, have_dsound=yes)
    7.10          AC_CHECK_HEADER(dinput.h, have_dinput=yes)
    7.11 @@ -2791,6 +2792,9 @@
    7.12              if test x$enable_render_d3d = xyes -a x$have_d3d = xyes; then
    7.13                  AC_DEFINE(SDL_VIDEO_RENDER_D3D, 1, [ ])
    7.14              fi
    7.15 +            if test x$enable_render_d3d = xyes -a x$have_d3d11 = xyes; then
    7.16 +                AC_DEFINE(SDL_VIDEO_RENDER_D3D11, 1, [ ])
    7.17 +            fi
    7.18          fi
    7.19          # Set up files for the audio library
    7.20          if test x$enable_audio = xyes; then
     8.1 --- a/include/SDL_config.h.cmake	Sun Mar 09 22:48:38 2014 -0700
     8.2 +++ b/include/SDL_config.h.cmake	Mon Mar 10 01:51:03 2014 -0700
     8.3 @@ -295,6 +295,7 @@
     8.4  #cmakedefine SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM @SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM@
     8.5  
     8.6  #cmakedefine SDL_VIDEO_RENDER_D3D @SDL_VIDEO_RENDER_D3D@
     8.7 +#cmakedefine SDL_VIDEO_RENDER_D3D11 @SDL_VIDEO_RENDER_D3D11@
     8.8  #cmakedefine SDL_VIDEO_RENDER_OGL @SDL_VIDEO_RENDER_OGL@
     8.9  #cmakedefine SDL_VIDEO_RENDER_OGL_ES @SDL_VIDEO_RENDER_OGL_ES@
    8.10  #cmakedefine SDL_VIDEO_RENDER_OGL_ES2 @SDL_VIDEO_RENDER_OGL_ES2@
     9.1 --- a/include/SDL_config.h.in	Sun Mar 09 22:48:38 2014 -0700
     9.2 +++ b/include/SDL_config.h.in	Mon Mar 10 01:51:03 2014 -0700
     9.3 @@ -294,6 +294,7 @@
     9.4  #undef SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     9.5  
     9.6  #undef SDL_VIDEO_RENDER_D3D
     9.7 +#undef SDL_VIDEO_RENDER_D3D11
     9.8  #undef SDL_VIDEO_RENDER_OGL
     9.9  #undef SDL_VIDEO_RENDER_OGL_ES
    9.10  #undef SDL_VIDEO_RENDER_OGL_ES2
    10.1 --- a/include/SDL_config_windows.h	Sun Mar 09 22:48:38 2014 -0700
    10.2 +++ b/include/SDL_config_windows.h	Mon Mar 10 01:51:03 2014 -0700
    10.3 @@ -171,6 +171,9 @@
    10.4  #ifndef SDL_VIDEO_RENDER_D3D
    10.5  #define SDL_VIDEO_RENDER_D3D    1
    10.6  #endif
    10.7 +#ifndef SDL_VIDEO_RENDER_D3D11
    10.8 +#define SDL_VIDEO_RENDER_D3D11	1
    10.9 +#endif
   10.10  
   10.11  /* Enable OpenGL support */
   10.12  #ifndef SDL_VIDEO_OPENGL
    11.1 --- a/src/render/SDL_render.c	Sun Mar 09 22:48:38 2014 -0700
    11.2 +++ b/src/render/SDL_render.c	Mon Mar 10 01:51:03 2014 -0700
    11.3 @@ -47,12 +47,12 @@
    11.4  
    11.5  #if !SDL_RENDER_DISABLED
    11.6  static const SDL_RenderDriver *render_drivers[] = {
    11.7 +#if SDL_VIDEO_RENDER_D3D11
    11.8 +    &D3D11_RenderDriver,
    11.9 +#endif
   11.10  #if SDL_VIDEO_RENDER_D3D
   11.11      &D3D_RenderDriver,
   11.12  #endif
   11.13 -#if SDL_VIDEO_RENDER_D3D11
   11.14 -    &D3D11_RenderDriver,
   11.15 -#endif
   11.16  #if SDL_VIDEO_RENDER_OGL
   11.17      &GL_RenderDriver,
   11.18  #endif
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/render/direct3d11/SDL_render_d3d11.c	Mon Mar 10 01:51:03 2014 -0700
    12.3 @@ -0,0 +1,2545 @@
    12.4 +/*
    12.5 +  Simple DirectMedia Layer
    12.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    12.7 +
    12.8 +  This software is provided 'as-is', without any express or implied
    12.9 +  warranty.  In no event will the authors be held liable for any damages
   12.10 +  arising from the use of this software.
   12.11 +
   12.12 +  Permission is granted to anyone to use this software for any purpose,
   12.13 +  including commercial applications, and to alter it and redistribute it
   12.14 +  freely, subject to the following restrictions:
   12.15 +
   12.16 +  1. The origin of this software must not be misrepresented; you must not
   12.17 +     claim that you wrote the original software. If you use this software
   12.18 +     in a product, an acknowledgment in the product documentation would be
   12.19 +     appreciated but is not required.
   12.20 +  2. Altered source versions must be plainly marked as such, and must not be
   12.21 +     misrepresented as being the original software.
   12.22 +  3. This notice may not be removed or altered from any source distribution.
   12.23 +*/
   12.24 +#include "../../SDL_internal.h"
   12.25 +
   12.26 +#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
   12.27 +
   12.28 +#ifdef __WINRT__
   12.29 +#include <windows.ui.core.h>
   12.30 +#include <windows.foundation.h>
   12.31 +
   12.32 +#if WINAPI_FAMILY == WINAPI_FAMILY_APP
   12.33 +#include <windows.ui.xaml.media.dxinterop.h>
   12.34 +#endif
   12.35 +
   12.36 +using namespace Windows::UI::Core;
   12.37 +using namespace Windows::Graphics::Display;
   12.38 +#endif  /* __WINRT__ */
   12.39 +
   12.40 +#define COBJMACROS
   12.41 +#include "../../core/windows/SDL_windows.h"
   12.42 +#include "SDL_hints.h"
   12.43 +#include "SDL_loadso.h"
   12.44 +#include "SDL_syswm.h"
   12.45 +#include "../SDL_sysrender.h"
   12.46 +
   12.47 +#include <d3d11_1.h>
   12.48 +
   12.49 +
   12.50 +#define SAFE_RELEASE(X) if ( (X) ) { IUnknown_Release( SDL_static_cast(IUnknown*, X ) ); X = NULL; }
   12.51 +
   12.52 +typedef struct
   12.53 +{
   12.54 +    float x;
   12.55 +    float y;
   12.56 +} Float2;
   12.57 +
   12.58 +typedef struct
   12.59 +{
   12.60 +    float x;
   12.61 +    float y;
   12.62 +    float z;
   12.63 +} Float3;
   12.64 +
   12.65 +typedef struct
   12.66 +{
   12.67 +    float x;
   12.68 +    float y;
   12.69 +    float z;
   12.70 +    float w;
   12.71 +} Float4;
   12.72 +
   12.73 +typedef struct
   12.74 +{
   12.75 +    union {
   12.76 +        struct {
   12.77 +            float _11, _12, _13, _14;
   12.78 +            float _21, _22, _23, _24;
   12.79 +            float _31, _32, _33, _34;
   12.80 +            float _41, _42, _43, _44;
   12.81 +        };
   12.82 +        float m[4][4];
   12.83 +    };
   12.84 +} Float4X4;
   12.85 +
   12.86 +/* Vertex shader, common values */
   12.87 +typedef struct
   12.88 +{
   12.89 +    Float4X4 model;
   12.90 +    Float4X4 projectionAndView;
   12.91 +} VertexShaderConstants;
   12.92 +
   12.93 +/* Per-vertex data */
   12.94 +typedef struct
   12.95 +{
   12.96 +    Float3 pos;
   12.97 +    Float2 tex;
   12.98 +    Float4 color;
   12.99 +} VertexPositionColor;
  12.100 +
  12.101 +/* Per-texture data */
  12.102 +typedef struct
  12.103 +{
  12.104 +    ID3D11Texture2D *mainTexture;
  12.105 +    ID3D11ShaderResourceView *mainTextureResourceView;
  12.106 +    ID3D11RenderTargetView *mainTextureRenderTargetView;
  12.107 +    ID3D11Texture2D *stagingTexture;
  12.108 +    int lockedTexturePositionX;
  12.109 +    int lockedTexturePositionY;
  12.110 +    D3D11_FILTER scaleMode;
  12.111 +} D3D11_TextureData;
  12.112 +
  12.113 +/* Private renderer data */
  12.114 +typedef struct
  12.115 +{
  12.116 +    void *hD3D11Mod;
  12.117 +    ID3D11Device1 *d3dDevice;
  12.118 +    ID3D11DeviceContext1 *d3dContext;
  12.119 +    IDXGISwapChain1 *swapChain;
  12.120 +    DXGI_SWAP_EFFECT swapEffect;
  12.121 +    ID3D11RenderTargetView *mainRenderTargetView;
  12.122 +    ID3D11RenderTargetView *currentOffscreenRenderTargetView;
  12.123 +    ID3D11InputLayout *inputLayout;
  12.124 +    ID3D11Buffer *vertexBuffer;
  12.125 +    ID3D11VertexShader *vertexShader;
  12.126 +    ID3D11PixelShader *texturePixelShader;
  12.127 +    ID3D11PixelShader *colorPixelShader;
  12.128 +    ID3D11BlendState *blendModeBlend;
  12.129 +    ID3D11BlendState *blendModeAdd;
  12.130 +    ID3D11BlendState *blendModeMod;
  12.131 +    ID3D11SamplerState *nearestPixelSampler;
  12.132 +    ID3D11SamplerState *linearSampler;
  12.133 +    D3D_FEATURE_LEVEL featureLevel;
  12.134 +
  12.135 +    /* Rasterizers */
  12.136 +    ID3D11RasterizerState *mainRasterizer;
  12.137 +    ID3D11RasterizerState *clippedRasterizer;
  12.138 +
  12.139 +    /* Vertex buffer constants */
  12.140 +    VertexShaderConstants vertexShaderConstantsData;
  12.141 +    ID3D11Buffer *vertexShaderConstants;
  12.142 +
  12.143 +    /* Cached renderer properties */
  12.144 +    DXGI_MODE_ROTATION rotation;
  12.145 +    ID3D11RenderTargetView *currentRenderTargetView;
  12.146 +    ID3D11RasterizerState *currentRasterizerState;
  12.147 +    ID3D11BlendState *currentBlendState;
  12.148 +    ID3D11PixelShader *currentShader;
  12.149 +    ID3D11ShaderResourceView *currentShaderResource;
  12.150 +    ID3D11SamplerState *currentSampler;
  12.151 +} D3D11_RenderData;
  12.152 +
  12.153 +
  12.154 +/* Defined here so we don't have to include uuid.lib */
  12.155 +static const GUID IID_IDXGIFactory2 = { 0x50c83a1c, 0xe072, 0x4c48, { 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0 } };
  12.156 +static const GUID IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
  12.157 +static const GUID IID_ID3D11Texture2D = { 0x6f15aaf2, 0xd208, 0x4e89, { 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3, 0x4f, 0x9c } };
  12.158 +static const GUID IID_ID3D11Device1 = { 0xa04bfb29, 0x08ef, 0x43d6, { 0xa4, 0x9c, 0xa9, 0xbd, 0xbd, 0xcb, 0xe6, 0x86 } };
  12.159 +static const GUID IID_ID3D11DeviceContext1 = { 0xbb2c6faa, 0xb5fb, 0x4082, { 0x8e, 0x6b, 0x38, 0x8b, 0x8c, 0xfa, 0x90, 0xe1 } };
  12.160 +static const GUID IID_ID3D11Debug = { 0x79cf2233, 0x7536, 0x4948, { 0x9d, 0x36, 0x1e, 0x46, 0x92, 0xdc, 0x57, 0x60 } };
  12.161 +
  12.162 +/* Direct3D 11.x shaders
  12.163 +
  12.164 +   SDL's shaders are compiled into SDL itself, to simplify distribution.
  12.165 +
  12.166 +   All Direct3D 11.x shaders were compiled with the following:
  12.167 +
  12.168 +   fxc /E"main" /T "<TYPE>" /Fo"<OUTPUT FILE>" "<INPUT FILE>"
  12.169 +
  12.170 +     Variables:
  12.171 +     - <TYPE>: the type of shader.  A table of utilized shader types is
  12.172 +       listed below.
  12.173 +     - <OUTPUT FILE>: where to store compiled output
  12.174 +     - <INPUT FILE>: where to read shader source code from
  12.175 +
  12.176 +     Shader types:
  12.177 +     - ps_4_0_level_9_1: Pixel shader for Windows 8+, including Windows RT
  12.178 +     - vs_4_0_level_9_1: Vertex shader for Windows 8+, including Windows RT
  12.179 +     - ps_4_0_level_9_3: Pixel shader for Windows Phone 8
  12.180 +     - vs_4_0_level_9_3: Vertex shader for Windows Phone 8
  12.181 +   
  12.182 +
  12.183 +   Shader object code was converted to a list of DWORDs via the following
  12.184 +   *nix style command (available separately from Windows + MSVC):
  12.185 +
  12.186 +     hexdump -v -e '6/4 "0x%08.8x, " "\n"' <FILE>
  12.187 +  */
  12.188 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  12.189 +#define D3D11_USE_SHADER_MODEL_4_0_level_9_3
  12.190 +#else
  12.191 +#define D3D11_USE_SHADER_MODEL_4_0_level_9_1
  12.192 +#endif
  12.193 +
  12.194 +/* The texture-rendering pixel shader:
  12.195 +
  12.196 +    --- D3D11_PixelShader_Textures.hlsl ---
  12.197 +    Texture2D theTexture : register(t0);
  12.198 +    SamplerState theSampler : register(s0);
  12.199 +
  12.200 +    struct PixelShaderInput
  12.201 +    {
  12.202 +        float4 pos : SV_POSITION;
  12.203 +        float2 tex : TEXCOORD0;
  12.204 +        float4 color : COLOR0;
  12.205 +    };
  12.206 +
  12.207 +    float4 main(PixelShaderInput input) : SV_TARGET
  12.208 +    {
  12.209 +        return theTexture.Sample(theSampler, input.tex) * input.color;
  12.210 +    }
  12.211 +*/
  12.212 +#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  12.213 +static const DWORD D3D11_PixelShader_Textures[] = {
  12.214 +    0x43425844, 0x6299b59f, 0x155258f2, 0x873ab86a, 0xfcbb6dcd, 0x00000001,
  12.215 +    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
  12.216 +    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
  12.217 +    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
  12.218 +    0x00280000, 0x00000000, 0xffff0200, 0x0200001f, 0x80000000, 0xb0030000,
  12.219 +    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
  12.220 +    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
  12.221 +    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
  12.222 +    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
  12.223 +    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
  12.224 +    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
  12.225 +    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
  12.226 +    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
  12.227 +    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
  12.228 +    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
  12.229 +    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
  12.230 +    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.231 +    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.232 +    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.233 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
  12.234 +    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
  12.235 +    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
  12.236 +    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
  12.237 +    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
  12.238 +    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
  12.239 +    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
  12.240 +    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
  12.241 +    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  12.242 +    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  12.243 +    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
  12.244 +    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  12.245 +    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  12.246 +    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  12.247 +    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  12.248 +};
  12.249 +#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  12.250 +static const DWORD D3D11_PixelShader_Textures[] = {
  12.251 +    0x43425844, 0x5876569a, 0x01b6c87e, 0x8447454f, 0xc7f3ef10, 0x00000001,
  12.252 +    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
  12.253 +    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
  12.254 +    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
  12.255 +    0x00280000, 0x00000000, 0xffff0201, 0x0200001f, 0x80000000, 0xb0030000,
  12.256 +    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
  12.257 +    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
  12.258 +    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
  12.259 +    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
  12.260 +    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
  12.261 +    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
  12.262 +    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
  12.263 +    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
  12.264 +    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
  12.265 +    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
  12.266 +    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
  12.267 +    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.268 +    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.269 +    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.270 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
  12.271 +    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
  12.272 +    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
  12.273 +    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
  12.274 +    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
  12.275 +    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
  12.276 +    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
  12.277 +    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
  12.278 +    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  12.279 +    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  12.280 +    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
  12.281 +    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  12.282 +    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  12.283 +    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  12.284 +    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  12.285 +};
  12.286 +#else
  12.287 +#error "An appropriate 'textures' pixel shader is not defined"
  12.288 +#endif
  12.289 +
  12.290 +/* The color-only-rendering pixel shader:
  12.291 +
  12.292 +   --- D3D11_PixelShader_Colors.hlsl ---
  12.293 +   struct PixelShaderInput
  12.294 +   {
  12.295 +       float4 pos : SV_POSITION;
  12.296 +       float2 tex : TEXCOORD0;
  12.297 +       float4 color : COLOR0;
  12.298 +   };
  12.299 +
  12.300 +   float4 main(PixelShaderInput input) : SV_TARGET
  12.301 +   {
  12.302 +       return input.color;
  12.303 +   }
  12.304 +*/
  12.305 +#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  12.306 +static const DWORD D3D11_PixelShader_Colors[] = {
  12.307 +    0x43425844, 0xd74c28fe, 0xa1eb8804, 0x269d512a, 0x7699723d, 0x00000001,
  12.308 +    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
  12.309 +    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
  12.310 +    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
  12.311 +    0x00240000, 0xffff0200, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
  12.312 +    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
  12.313 +    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
  12.314 +    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
  12.315 +    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
  12.316 +    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  12.317 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.318 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
  12.319 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.320 +    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
  12.321 +    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
  12.322 +    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
  12.323 +    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
  12.324 +    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  12.325 +    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  12.326 +    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
  12.327 +    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  12.328 +    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  12.329 +    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  12.330 +    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  12.331 +};
  12.332 +#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  12.333 +static const DWORD D3D11_PixelShader_Colors[] = {
  12.334 +    0x43425844, 0x93f6ccfc, 0x5f919270, 0x7a11aa4f, 0x9148e931, 0x00000001,
  12.335 +    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
  12.336 +    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
  12.337 +    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
  12.338 +    0x00240000, 0xffff0201, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
  12.339 +    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
  12.340 +    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
  12.341 +    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
  12.342 +    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
  12.343 +    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  12.344 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.345 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
  12.346 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.347 +    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
  12.348 +    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
  12.349 +    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
  12.350 +    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
  12.351 +    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  12.352 +    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  12.353 +    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
  12.354 +    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  12.355 +    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  12.356 +    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  12.357 +    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  12.358 +};
  12.359 +#else
  12.360 +#error "An appropriate 'colors' pixel shader is not defined."
  12.361 +#endif
  12.362 +
  12.363 +/* The sole vertex shader:
  12.364 +
  12.365 +   --- D3D11_VertexShader.hlsl ---
  12.366 +   #pragma pack_matrix( row_major )
  12.367 +
  12.368 +   cbuffer VertexShaderConstants : register(b0)
  12.369 +   {
  12.370 +       matrix model;
  12.371 +       matrix projectionAndView;
  12.372 +   };
  12.373 +
  12.374 +   struct VertexShaderInput
  12.375 +   {
  12.376 +       float3 pos : POSITION;
  12.377 +       float2 tex : TEXCOORD0;
  12.378 +       float4 color : COLOR0;
  12.379 +   };
  12.380 +
  12.381 +   struct VertexShaderOutput
  12.382 +   {
  12.383 +       float4 pos : SV_POSITION;
  12.384 +       float2 tex : TEXCOORD0;
  12.385 +       float4 color : COLOR0;
  12.386 +   };
  12.387 +
  12.388 +   VertexShaderOutput main(VertexShaderInput input)
  12.389 +   {
  12.390 +       VertexShaderOutput output;
  12.391 +       float4 pos = float4(input.pos, 1.0f);
  12.392 +
  12.393 +       // Transform the vertex position into projected space.
  12.394 +       pos = mul(pos, model);
  12.395 +       pos = mul(pos, projectionAndView);
  12.396 +       output.pos = pos;
  12.397 +
  12.398 +       // Pass through texture coordinates and color values without transformation
  12.399 +       output.tex = input.tex;
  12.400 +       output.color = input.color;
  12.401 +
  12.402 +       return output;
  12.403 +   }
  12.404 +*/
  12.405 +#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  12.406 +static const DWORD D3D11_VertexShader[] = {
  12.407 +    0x43425844, 0x62dfae5f, 0x3e8bd8df, 0x9ec97127, 0x5044eefb, 0x00000001,
  12.408 +    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
  12.409 +    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
  12.410 +    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
  12.411 +    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0200,
  12.412 +    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
  12.413 +    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
  12.414 +    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
  12.415 +    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
  12.416 +    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
  12.417 +    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
  12.418 +    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
  12.419 +    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
  12.420 +    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
  12.421 +    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
  12.422 +    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
  12.423 +    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
  12.424 +    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
  12.425 +    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
  12.426 +    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
  12.427 +    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
  12.428 +    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
  12.429 +    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
  12.430 +    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
  12.431 +    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
  12.432 +    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
  12.433 +    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
  12.434 +    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
  12.435 +    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
  12.436 +    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
  12.437 +    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
  12.438 +    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
  12.439 +    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
  12.440 +    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
  12.441 +    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
  12.442 +    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  12.443 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.444 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
  12.445 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.446 +    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
  12.447 +    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
  12.448 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
  12.449 +    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
  12.450 +    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
  12.451 +    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
  12.452 +    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
  12.453 +    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
  12.454 +    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
  12.455 +    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
  12.456 +    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
  12.457 +    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
  12.458 +    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
  12.459 +    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
  12.460 +    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
  12.461 +    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
  12.462 +    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
  12.463 +    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
  12.464 +    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
  12.465 +    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
  12.466 +    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
  12.467 +};
  12.468 +#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  12.469 +static const DWORD D3D11_VertexShader[] = {
  12.470 +    0x43425844, 0x01a24e41, 0x696af551, 0x4b2a87d1, 0x82ea03f6, 0x00000001,
  12.471 +    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
  12.472 +    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
  12.473 +    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
  12.474 +    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0201,
  12.475 +    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
  12.476 +    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
  12.477 +    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
  12.478 +    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
  12.479 +    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
  12.480 +    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
  12.481 +    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
  12.482 +    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
  12.483 +    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
  12.484 +    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
  12.485 +    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
  12.486 +    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
  12.487 +    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
  12.488 +    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
  12.489 +    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
  12.490 +    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
  12.491 +    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
  12.492 +    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
  12.493 +    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
  12.494 +    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
  12.495 +    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
  12.496 +    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
  12.497 +    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
  12.498 +    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
  12.499 +    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
  12.500 +    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
  12.501 +    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
  12.502 +    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
  12.503 +    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
  12.504 +    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
  12.505 +    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  12.506 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.507 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
  12.508 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  12.509 +    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
  12.510 +    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
  12.511 +    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
  12.512 +    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
  12.513 +    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
  12.514 +    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
  12.515 +    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
  12.516 +    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
  12.517 +    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
  12.518 +    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
  12.519 +    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
  12.520 +    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
  12.521 +    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
  12.522 +    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
  12.523 +    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
  12.524 +    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
  12.525 +    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
  12.526 +    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
  12.527 +    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
  12.528 +    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
  12.529 +    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
  12.530 +};
  12.531 +#else
  12.532 +#error "An appropriate vertex shader is not defined."
  12.533 +#endif
  12.534 +
  12.535 +/* Direct3D matrix math functions */
  12.536 +
  12.537 +static Float4X4 MatrixIdentity()
  12.538 +{
  12.539 +    Float4X4 m;
  12.540 +    SDL_zero(m);
  12.541 +    m._11 = 1.0f;
  12.542 +    m._22 = 1.0f;
  12.543 +    m._33 = 1.0f;
  12.544 +    m._44 = 1.0f;
  12.545 +    return m;
  12.546 +}
  12.547 +
  12.548 +static Float4X4 MatrixMultiply(Float4X4 M1, Float4X4 M2)
  12.549 +{
  12.550 +    Float4X4 m;
  12.551 +    SDL_zero(m);
  12.552 +    m._11 = M1._11 * M2._11 + M1._12 * M2._21 + M1._13 * M2._31 + M1._14 * M2._41;
  12.553 +    m._12 = M1._11 * M2._12 + M1._12 * M2._22 + M1._13 * M2._32 + M1._14 * M2._42;
  12.554 +    m._13 = M1._11 * M2._13 + M1._12 * M2._23 + M1._13 * M2._33 + M1._14 * M2._43;
  12.555 +    m._14 = M1._11 * M2._14 + M1._12 * M2._24 + M1._13 * M2._34 + M1._14 * M2._44;
  12.556 +    m._21 = M1._21 * M2._11 + M1._22 * M2._21 + M1._23 * M2._31 + M1._24 * M2._41;
  12.557 +    m._22 = M1._21 * M2._12 + M1._22 * M2._22 + M1._23 * M2._32 + M1._24 * M2._42;
  12.558 +    m._23 = M1._21 * M2._13 + M1._22 * M2._23 + M1._23 * M2._33 + M1._24 * M2._43;
  12.559 +    m._24 = M1._21 * M2._14 + M1._22 * M2._24 + M1._23 * M2._34 + M1._24 * M2._44;
  12.560 +    m._31 = M1._31 * M2._11 + M1._32 * M2._21 + M1._33 * M2._31 + M1._34 * M2._41;
  12.561 +    m._32 = M1._31 * M2._12 + M1._32 * M2._22 + M1._33 * M2._32 + M1._34 * M2._42;
  12.562 +    m._33 = M1._31 * M2._13 + M1._32 * M2._23 + M1._33 * M2._33 + M1._34 * M2._43;
  12.563 +    m._34 = M1._31 * M2._14 + M1._32 * M2._24 + M1._33 * M2._34 + M1._34 * M2._44;
  12.564 +    m._41 = M1._41 * M2._11 + M1._42 * M2._21 + M1._43 * M2._31 + M1._44 * M2._41;
  12.565 +    m._42 = M1._41 * M2._12 + M1._42 * M2._22 + M1._43 * M2._32 + M1._44 * M2._42;
  12.566 +    m._43 = M1._41 * M2._13 + M1._42 * M2._23 + M1._43 * M2._33 + M1._44 * M2._43;
  12.567 +    m._44 = M1._41 * M2._14 + M1._42 * M2._24 + M1._43 * M2._34 + M1._44 * M2._44;
  12.568 +    return m;
  12.569 +}
  12.570 +
  12.571 +static Float4X4 MatrixScaling(float x, float y, float z)
  12.572 +{
  12.573 +    Float4X4 m;
  12.574 +    SDL_zero(m);
  12.575 +    m._11 = x;
  12.576 +    m._22 = y;
  12.577 +    m._33 = z;
  12.578 +    m._44 = 1.0f;
  12.579 +    return m;
  12.580 +}
  12.581 +
  12.582 +static Float4X4 MatrixTranslation(float x, float y, float z)
  12.583 +{
  12.584 +    Float4X4 m;
  12.585 +    SDL_zero(m);
  12.586 +    m._11 = 1.0f;
  12.587 +    m._22 = 1.0f;
  12.588 +    m._33 = 1.0f;
  12.589 +    m._44 = 1.0f;
  12.590 +    m._41 = x;
  12.591 +    m._42 = y;
  12.592 +    m._43 = z;
  12.593 +    return m;
  12.594 +}
  12.595 +
  12.596 +static Float4X4 MatrixRotationX(float r)
  12.597 +{
  12.598 +    float sinR = SDL_sinf(r);
  12.599 +    float cosR = SDL_cosf(r);
  12.600 +    Float4X4 m;
  12.601 +    SDL_zero(m);
  12.602 +    m._11 = 1.0f;
  12.603 +    m._22 = cosR;
  12.604 +    m._23 = sinR;
  12.605 +    m._32 = -sinR;
  12.606 +    m._33 = cosR;
  12.607 +    m._44 = 1.0f;
  12.608 +    return m;
  12.609 +}
  12.610 +
  12.611 +static Float4X4 MatrixRotationY(float r)
  12.612 +{
  12.613 +    float sinR = SDL_sinf(r);
  12.614 +    float cosR = SDL_cosf(r);
  12.615 +    Float4X4 m;
  12.616 +    SDL_zero(m);
  12.617 +    m._11 = cosR;
  12.618 +    m._13 = -sinR;
  12.619 +    m._22 = 1.0f;
  12.620 +    m._31 = sinR;
  12.621 +    m._33 = cosR;
  12.622 +    m._44 = 1.0f;
  12.623 +    return m;
  12.624 +}
  12.625 +
  12.626 +static Float4X4 MatrixRotationZ(float r)
  12.627 +{
  12.628 +    float sinR = SDL_sinf(r);
  12.629 +    float cosR = SDL_cosf(r);
  12.630 +    Float4X4 m;
  12.631 +    SDL_zero(m);
  12.632 +    m._11 = cosR;
  12.633 +    m._12 = sinR;
  12.634 +    m._21 = -sinR;
  12.635 +    m._22 = cosR;
  12.636 +    m._33 = 1.0f;
  12.637 +    m._44 = 1.0f;
  12.638 +    return m;
  12.639 +}
  12.640 +
  12.641 +
  12.642 +/* Direct3D 11.1 renderer implementation */
  12.643 +static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
  12.644 +static void D3D11_WindowEvent(SDL_Renderer * renderer,
  12.645 +                            const SDL_WindowEvent *event);
  12.646 +static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  12.647 +static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  12.648 +                             const SDL_Rect * rect, const void *srcPixels,
  12.649 +                             int srcPitch);
  12.650 +static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  12.651 +                             const SDL_Rect * rect, void **pixels, int *pitch);
  12.652 +static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  12.653 +static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
  12.654 +static int D3D11_UpdateViewport(SDL_Renderer * renderer);
  12.655 +static int D3D11_UpdateClipRect(SDL_Renderer * renderer);
  12.656 +static int D3D11_RenderClear(SDL_Renderer * renderer);
  12.657 +static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
  12.658 +                                  const SDL_FPoint * points, int count);
  12.659 +static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
  12.660 +                                 const SDL_FPoint * points, int count);
  12.661 +static int D3D11_RenderFillRects(SDL_Renderer * renderer,
  12.662 +                                 const SDL_FRect * rects, int count);
  12.663 +static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  12.664 +                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
  12.665 +static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  12.666 +                              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  12.667 +                              const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
  12.668 +static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  12.669 +                                  Uint32 format, void * pixels, int pitch);
  12.670 +static void D3D11_RenderPresent(SDL_Renderer * renderer);
  12.671 +static void D3D11_DestroyTexture(SDL_Renderer * renderer,
  12.672 +                                 SDL_Texture * texture);
  12.673 +static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
  12.674 +
  12.675 +/* Direct3D 11.1 Internal Functions */
  12.676 +static HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
  12.677 +static HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
  12.678 +static HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
  12.679 +static HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
  12.680 +static void D3D11_ReleaseMainRenderTargetView(SDL_Renderer * renderer);
  12.681 +
  12.682 +SDL_RenderDriver D3D11_RenderDriver = {
  12.683 +    D3D11_CreateRenderer,
  12.684 +    {
  12.685 +        "direct3d11",
  12.686 +        (
  12.687 +            SDL_RENDERER_ACCELERATED |
  12.688 +            SDL_RENDERER_PRESENTVSYNC |
  12.689 +            SDL_RENDERER_TARGETTEXTURE
  12.690 +        ),                          /* flags.  see SDL_RendererFlags */
  12.691 +        2,                          /* num_texture_formats */
  12.692 +        {                           /* texture_formats */
  12.693 +            SDL_PIXELFORMAT_RGB888,
  12.694 +            SDL_PIXELFORMAT_ARGB8888
  12.695 +        },
  12.696 +        0,                          /* max_texture_width: will be filled in later */
  12.697 +        0                           /* max_texture_height: will be filled in later */
  12.698 +    }
  12.699 +};
  12.700 +
  12.701 +
  12.702 +static Uint32
  12.703 +DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat) {
  12.704 +    switch (dxgiFormat) {
  12.705 +        case DXGI_FORMAT_B8G8R8A8_UNORM:
  12.706 +            return SDL_PIXELFORMAT_ARGB8888;
  12.707 +        case DXGI_FORMAT_B8G8R8X8_UNORM:
  12.708 +            return SDL_PIXELFORMAT_RGB888;
  12.709 +        default:
  12.710 +            return SDL_PIXELFORMAT_UNKNOWN;
  12.711 +    }
  12.712 +}
  12.713 +
  12.714 +static DXGI_FORMAT
  12.715 +SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
  12.716 +{
  12.717 +    switch (sdlFormat) {
  12.718 +        case SDL_PIXELFORMAT_ARGB8888:
  12.719 +            return DXGI_FORMAT_B8G8R8A8_UNORM;
  12.720 +        case SDL_PIXELFORMAT_RGB888:
  12.721 +            return DXGI_FORMAT_B8G8R8X8_UNORM;
  12.722 +        default:
  12.723 +            return DXGI_FORMAT_UNKNOWN;
  12.724 +    }
  12.725 +}
  12.726 +
  12.727 +SDL_Renderer *
  12.728 +D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
  12.729 +{
  12.730 +    SDL_Renderer *renderer;
  12.731 +    D3D11_RenderData *data;
  12.732 +
  12.733 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  12.734 +    if (!renderer) {
  12.735 +        SDL_OutOfMemory();
  12.736 +        return NULL;
  12.737 +    }
  12.738 +
  12.739 +    data = (D3D11_RenderData *) SDL_calloc(1, sizeof(*data));
  12.740 +    if (!data) {
  12.741 +        SDL_OutOfMemory();
  12.742 +        return NULL;
  12.743 +    }
  12.744 +
  12.745 +    renderer->WindowEvent = D3D11_WindowEvent;
  12.746 +    renderer->CreateTexture = D3D11_CreateTexture;
  12.747 +    renderer->UpdateTexture = D3D11_UpdateTexture;
  12.748 +    renderer->LockTexture = D3D11_LockTexture;
  12.749 +    renderer->UnlockTexture = D3D11_UnlockTexture;
  12.750 +    renderer->SetRenderTarget = D3D11_SetRenderTarget;
  12.751 +    renderer->UpdateViewport = D3D11_UpdateViewport;
  12.752 +    renderer->UpdateClipRect = D3D11_UpdateClipRect;
  12.753 +    renderer->RenderClear = D3D11_RenderClear;
  12.754 +    renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
  12.755 +    renderer->RenderDrawLines = D3D11_RenderDrawLines;
  12.756 +    renderer->RenderFillRects = D3D11_RenderFillRects;
  12.757 +    renderer->RenderCopy = D3D11_RenderCopy;
  12.758 +    renderer->RenderCopyEx = D3D11_RenderCopyEx;
  12.759 +    renderer->RenderReadPixels = D3D11_RenderReadPixels;
  12.760 +    renderer->RenderPresent = D3D11_RenderPresent;
  12.761 +    renderer->DestroyTexture = D3D11_DestroyTexture;
  12.762 +    renderer->DestroyRenderer = D3D11_DestroyRenderer;
  12.763 +    renderer->info = D3D11_RenderDriver.info;
  12.764 +    renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  12.765 +    renderer->driverdata = data;
  12.766 +
  12.767 +    if ((flags & SDL_RENDERER_PRESENTVSYNC)) {
  12.768 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  12.769 +    }
  12.770 +
  12.771 +    /* HACK: make sure the SDL_Renderer references the SDL_Window data now, in
  12.772 +     * order to give init functions access to the underlying window handle:
  12.773 +     */
  12.774 +    renderer->window = window;
  12.775 +
  12.776 +    /* Initialize Direct3D resources */
  12.777 +    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
  12.778 +        D3D11_DestroyRenderer(renderer);
  12.779 +        return NULL;
  12.780 +    }
  12.781 +    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
  12.782 +        D3D11_DestroyRenderer(renderer);
  12.783 +        return NULL;
  12.784 +    }
  12.785 +
  12.786 +    return renderer;
  12.787 +}
  12.788 +
  12.789 +static void
  12.790 +D3D11_DestroyRenderer(SDL_Renderer * renderer)
  12.791 +{
  12.792 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  12.793 +
  12.794 +    if (data) {
  12.795 +        SAFE_RELEASE(data->d3dDevice);
  12.796 +        SAFE_RELEASE(data->d3dContext);
  12.797 +        SAFE_RELEASE(data->swapChain);
  12.798 +        SAFE_RELEASE(data->mainRenderTargetView);
  12.799 +        SAFE_RELEASE(data->currentOffscreenRenderTargetView);
  12.800 +        SAFE_RELEASE(data->inputLayout);
  12.801 +        SAFE_RELEASE(data->vertexBuffer);
  12.802 +        SAFE_RELEASE(data->vertexShader);
  12.803 +        SAFE_RELEASE(data->texturePixelShader);
  12.804 +        SAFE_RELEASE(data->colorPixelShader);
  12.805 +        SAFE_RELEASE(data->blendModeBlend);
  12.806 +        SAFE_RELEASE(data->blendModeAdd);
  12.807 +        SAFE_RELEASE(data->blendModeMod);
  12.808 +        SAFE_RELEASE(data->nearestPixelSampler);
  12.809 +        SAFE_RELEASE(data->linearSampler);
  12.810 +        SAFE_RELEASE(data->mainRasterizer);
  12.811 +        SAFE_RELEASE(data->clippedRasterizer);
  12.812 +        SAFE_RELEASE(data->vertexShaderConstants);
  12.813 +
  12.814 +        if (data->hD3D11Mod) {
  12.815 +            SDL_UnloadObject(data->hD3D11Mod);
  12.816 +        }
  12.817 +        SDL_free(data);
  12.818 +    }
  12.819 +    SDL_free(renderer);
  12.820 +}
  12.821 +
  12.822 +static HRESULT
  12.823 +D3D11_CreateBlendMode(SDL_Renderer * renderer,
  12.824 +                      BOOL enableBlending,
  12.825 +                      D3D11_BLEND srcBlend,
  12.826 +                      D3D11_BLEND destBlend,
  12.827 +                      D3D11_BLEND srcBlendAlpha,
  12.828 +                      D3D11_BLEND destBlendAlpha,
  12.829 +                      ID3D11BlendState ** blendStateOutput)
  12.830 +{
  12.831 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  12.832 +    HRESULT result = S_OK;
  12.833 +
  12.834 +    D3D11_BLEND_DESC blendDesc;
  12.835 +    SDL_zero(blendDesc);
  12.836 +    blendDesc.AlphaToCoverageEnable = FALSE;
  12.837 +    blendDesc.IndependentBlendEnable = FALSE;
  12.838 +    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
  12.839 +    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
  12.840 +    blendDesc.RenderTarget[0].DestBlend = destBlend;
  12.841 +    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  12.842 +    blendDesc.RenderTarget[0].SrcBlendAlpha = srcBlendAlpha;
  12.843 +    blendDesc.RenderTarget[0].DestBlendAlpha = destBlendAlpha;
  12.844 +    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  12.845 +    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  12.846 +    result = ID3D11Device_CreateBlendState(data->d3dDevice, &blendDesc, blendStateOutput);
  12.847 +    if (FAILED(result)) {
  12.848 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBlendState", result);
  12.849 +        return result;
  12.850 +    }
  12.851 +
  12.852 +    return S_OK;
  12.853 +}
  12.854 +
  12.855 +/* Create resources that depend on the device. */
  12.856 +static HRESULT
  12.857 +D3D11_CreateDeviceResources(SDL_Renderer * renderer)
  12.858 +{
  12.859 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  12.860 +    PFN_D3D11_CREATE_DEVICE D3D11CreateDeviceFunc;
  12.861 +    ID3D11Device *d3dDevice = NULL;
  12.862 +    ID3D11DeviceContext *d3dContext = NULL;
  12.863 +    HRESULT result = S_OK;
  12.864 +
  12.865 +#ifdef __WINRT__
  12.866 +    D3D11CreateDeviceFunc = D3D11CreateDevice;
  12.867 +#else
  12.868 +    data->hD3D11Mod = SDL_LoadObject("d3d11.dll");
  12.869 +    if (!data->hD3D11Mod) {
  12.870 +        result = E_FAIL;
  12.871 +        goto done;
  12.872 +    }
  12.873 +
  12.874 +    D3D11CreateDeviceFunc = (PFN_D3D11_CREATE_DEVICE)SDL_LoadFunction(data->hD3D11Mod, "D3D11CreateDevice");
  12.875 +    if (!D3D11CreateDeviceFunc) {
  12.876 +        result = E_FAIL;
  12.877 +        goto done;
  12.878 +    }
  12.879 +#endif /* __WINRT__ */
  12.880 +
  12.881 +    /* This flag adds support for surfaces with a different color channel ordering
  12.882 +     * than the API default. It is required for compatibility with Direct2D.
  12.883 +     */
  12.884 +    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  12.885 +
  12.886 +    /* Make sure Direct3D's debugging feature gets used, if the app requests it. */
  12.887 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
  12.888 +    if (hint && SDL_atoi(hint) > 0) {
  12.889 +        creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  12.890 +    }
  12.891 +
  12.892 +    /* This array defines the set of DirectX hardware feature levels this app will support.
  12.893 +     * Note the ordering should be preserved.
  12.894 +     * Don't forget to declare your application's minimum required feature level in its
  12.895 +     * description.  All applications are assumed to support 9.1 unless otherwise stated.
  12.896 +     */
  12.897 +    D3D_FEATURE_LEVEL featureLevels[] = 
  12.898 +    {
  12.899 +        D3D_FEATURE_LEVEL_11_1,
  12.900 +        D3D_FEATURE_LEVEL_11_0,
  12.901 +        D3D_FEATURE_LEVEL_10_1,
  12.902 +        D3D_FEATURE_LEVEL_10_0,
  12.903 +        D3D_FEATURE_LEVEL_9_3,
  12.904 +        D3D_FEATURE_LEVEL_9_2,
  12.905 +        D3D_FEATURE_LEVEL_9_1
  12.906 +    };
  12.907 +
  12.908 +    /* Create the Direct3D 11 API device object and a corresponding context. */
  12.909 +    result = D3D11CreateDeviceFunc(
  12.910 +        NULL, /* Specify NULL to use the default adapter */
  12.911 +        D3D_DRIVER_TYPE_HARDWARE,
  12.912 +        NULL,
  12.913 +        creationFlags, /* Set set debug and Direct2D compatibility flags. */
  12.914 +        featureLevels, /* List of feature levels this app can support. */
  12.915 +        SDL_arraysize(featureLevels),
  12.916 +        D3D11_SDK_VERSION, /* Always set this to D3D11_SDK_VERSION for Windows Store apps. */
  12.917 +        &d3dDevice, /* Returns the Direct3D device created. */
  12.918 +        &data->featureLevel, /* Returns feature level of device created. */
  12.919 +        &d3dContext /* Returns the device immediate context. */
  12.920 +        );
  12.921 +    if (FAILED(result)) {
  12.922 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", D3D11CreateDevice", result);
  12.923 +        goto done;
  12.924 +    }
  12.925 +
  12.926 +    result = ID3D11Device_QueryInterface(d3dDevice, &IID_ID3D11Device1, &data->d3dDevice);
  12.927 +    if (FAILED(result)) {
  12.928 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to ID3D11Device1", result);
  12.929 +        goto done;
  12.930 +    }
  12.931 +
  12.932 +    result = ID3D11DeviceContext_QueryInterface(d3dContext, &IID_ID3D11DeviceContext1, &data->d3dContext);
  12.933 +    if (FAILED(result)) {
  12.934 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext to ID3D11DeviceContext1", result);
  12.935 +        goto done;
  12.936 +    }
  12.937 +
  12.938 +    /* Make note of the maximum texture size
  12.939 +     * Max texture sizes are documented on MSDN, at:
  12.940 +     * http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
  12.941 +     */
  12.942 +    switch (data->featureLevel) {
  12.943 +        case D3D_FEATURE_LEVEL_11_1:
  12.944 +        case D3D_FEATURE_LEVEL_11_0:
  12.945 +            renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
  12.946 +            break;
  12.947 +
  12.948 +        case D3D_FEATURE_LEVEL_10_1:
  12.949 +        case D3D_FEATURE_LEVEL_10_0:
  12.950 +            renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
  12.951 +            break;
  12.952 +
  12.953 +        case D3D_FEATURE_LEVEL_9_3:
  12.954 +            renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
  12.955 +            break;
  12.956 +
  12.957 +        case D3D_FEATURE_LEVEL_9_2:
  12.958 +        case D3D_FEATURE_LEVEL_9_1:
  12.959 +            renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
  12.960 +            break;
  12.961 +
  12.962 +        default:
  12.963 +            SDL_SetError(__FUNCTION__ ", Unexpected feature level: %d", data->featureLevel);
  12.964 +            result = E_FAIL;
  12.965 +            goto done;
  12.966 +    }
  12.967 +
  12.968 +    /* Load in SDL's one and only vertex shader: */
  12.969 +    result = ID3D11Device_CreateVertexShader(data->d3dDevice,
  12.970 +        D3D11_VertexShader,
  12.971 +        sizeof(D3D11_VertexShader),
  12.972 +        NULL,
  12.973 +        &data->vertexShader
  12.974 +        );
  12.975 +    if (FAILED(result)) {
  12.976 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateVertexShader", result);
  12.977 +        goto done;
  12.978 +    }
  12.979 +
  12.980 +    /* Create an input layout for SDL's vertex shader: */
  12.981 +    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
  12.982 +    {
  12.983 +        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  12.984 +        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  12.985 +        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  12.986 +    };
  12.987 +
  12.988 +    result = ID3D11Device_CreateInputLayout(data->d3dDevice,
  12.989 +        vertexDesc,
  12.990 +        ARRAYSIZE(vertexDesc),
  12.991 +        D3D11_VertexShader,
  12.992 +        sizeof(D3D11_VertexShader),
  12.993 +        &data->inputLayout
  12.994 +        );
  12.995 +    if (FAILED(result)) {
  12.996 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateInputLayout", result);
  12.997 +        goto done;
  12.998 +    }
  12.999 +
 12.1000 +    /* Load in SDL's pixel shaders */
 12.1001 +    result = ID3D11Device_CreatePixelShader(data->d3dDevice,
 12.1002 +        D3D11_PixelShader_Textures,
 12.1003 +        sizeof(D3D11_PixelShader_Textures),
 12.1004 +        NULL,
 12.1005 +        &data->texturePixelShader
 12.1006 +        );
 12.1007 +    if (FAILED(result)) {
 12.1008 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['textures' shader]", result);
 12.1009 +        goto done;
 12.1010 +    }
 12.1011 +
 12.1012 +    result = ID3D11Device_CreatePixelShader(data->d3dDevice,
 12.1013 +        D3D11_PixelShader_Colors,
 12.1014 +        sizeof(D3D11_PixelShader_Colors),
 12.1015 +        NULL,
 12.1016 +        &data->colorPixelShader
 12.1017 +        );
 12.1018 +    if (FAILED(result)) {
 12.1019 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['color' shader]", result);
 12.1020 +        goto done;
 12.1021 +    }
 12.1022 +
 12.1023 +    /* Setup space to hold vertex shader constants: */
 12.1024 +    D3D11_BUFFER_DESC constantBufferDesc;
 12.1025 +    SDL_zero(constantBufferDesc);
 12.1026 +    constantBufferDesc.ByteWidth = sizeof(VertexShaderConstants);
 12.1027 +    constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
 12.1028 +    constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
 12.1029 +    result = ID3D11Device_CreateBuffer(data->d3dDevice,
 12.1030 +		&constantBufferDesc,
 12.1031 +		NULL,
 12.1032 +        &data->vertexShaderConstants
 12.1033 +		);
 12.1034 +    if (FAILED(result)) {
 12.1035 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex shader constants]", result);
 12.1036 +        goto done;
 12.1037 +    }
 12.1038 +
 12.1039 +    /* Create samplers to use when drawing textures: */
 12.1040 +    D3D11_SAMPLER_DESC samplerDesc;
 12.1041 +    SDL_zero(samplerDesc);
 12.1042 +    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
 12.1043 +    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
 12.1044 +    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
 12.1045 +    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
 12.1046 +    samplerDesc.MipLODBias = 0.0f;
 12.1047 +    samplerDesc.MaxAnisotropy = 1;
 12.1048 +    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
 12.1049 +    samplerDesc.MinLOD = 0.0f;
 12.1050 +    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
 12.1051 +    result = ID3D11Device_CreateSamplerState(data->d3dDevice,
 12.1052 +        &samplerDesc,
 12.1053 +        &data->nearestPixelSampler
 12.1054 +        );
 12.1055 +    if (FAILED(result)) {
 12.1056 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [nearest-pixel filter]", result);
 12.1057 +        goto done;
 12.1058 +    }
 12.1059 +
 12.1060 +    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
 12.1061 +    result = ID3D11Device_CreateSamplerState(data->d3dDevice,
 12.1062 +        &samplerDesc,
 12.1063 +        &data->linearSampler
 12.1064 +        );
 12.1065 +    if (FAILED(result)) {
 12.1066 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [linear filter]", result);
 12.1067 +        goto done;
 12.1068 +    }
 12.1069 +
 12.1070 +    /* Setup Direct3D rasterizer states */
 12.1071 +    D3D11_RASTERIZER_DESC rasterDesc;
 12.1072 +    SDL_zero(rasterDesc);
 12.1073 +	rasterDesc.AntialiasedLineEnable = FALSE;
 12.1074 +	rasterDesc.CullMode = D3D11_CULL_NONE;
 12.1075 +	rasterDesc.DepthBias = 0;
 12.1076 +	rasterDesc.DepthBiasClamp = 0.0f;
 12.1077 +	rasterDesc.DepthClipEnable = TRUE;
 12.1078 +	rasterDesc.FillMode = D3D11_FILL_SOLID;
 12.1079 +	rasterDesc.FrontCounterClockwise = FALSE;
 12.1080 +    rasterDesc.MultisampleEnable = FALSE;
 12.1081 +    rasterDesc.ScissorEnable = FALSE;
 12.1082 +	rasterDesc.SlopeScaledDepthBias = 0.0f;
 12.1083 +    result = ID3D11Device_CreateRasterizerState(data->d3dDevice, &rasterDesc, &data->mainRasterizer);
 12.1084 +	if (FAILED(result)) {
 12.1085 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [main rasterizer]", result);
 12.1086 +        goto done;
 12.1087 +    }
 12.1088 +
 12.1089 +    rasterDesc.ScissorEnable = TRUE;
 12.1090 +    result = ID3D11Device_CreateRasterizerState(data->d3dDevice, &rasterDesc, &data->clippedRasterizer);
 12.1091 +	if (FAILED(result)) {
 12.1092 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [clipped rasterizer]", result);
 12.1093 +        goto done;
 12.1094 +    }
 12.1095 +
 12.1096 +    /* Create blending states: */
 12.1097 +    result = D3D11_CreateBlendMode(
 12.1098 +        renderer,
 12.1099 +        TRUE,
 12.1100 +        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
 12.1101 +        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlend */
 12.1102 +        D3D11_BLEND_ONE,                /* srcBlendAlpha */
 12.1103 +        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlendAlpha */
 12.1104 +        &data->blendModeBlend);
 12.1105 +    if (FAILED(result)) {
 12.1106 +        /* D3D11_CreateBlendMode will set the SDL error, if it fails */
 12.1107 +        goto done;
 12.1108 +    }
 12.1109 +
 12.1110 +    result = D3D11_CreateBlendMode(
 12.1111 +        renderer,
 12.1112 +        TRUE,
 12.1113 +        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
 12.1114 +        D3D11_BLEND_ONE,                /* destBlend */
 12.1115 +        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
 12.1116 +        D3D11_BLEND_ONE,                /* destBlendAlpha */
 12.1117 +        &data->blendModeAdd);
 12.1118 +    if (FAILED(result)) {
 12.1119 +        /* D3D11_CreateBlendMode will set the SDL error, if it fails */
 12.1120 +        goto done;
 12.1121 +    }
 12.1122 +
 12.1123 +    result = D3D11_CreateBlendMode(
 12.1124 +        renderer,
 12.1125 +        TRUE,
 12.1126 +        D3D11_BLEND_ZERO,               /* srcBlend */
 12.1127 +        D3D11_BLEND_SRC_COLOR,          /* destBlend */
 12.1128 +        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
 12.1129 +        D3D11_BLEND_ONE,                /* destBlendAlpha */
 12.1130 +        &data->blendModeMod);
 12.1131 +    if (FAILED(result)) {
 12.1132 +        /* D3D11_CreateBlendMode will set the SDL error, if it fails */
 12.1133 +        goto done;
 12.1134 +    }
 12.1135 +
 12.1136 +    /* Setup render state that doesn't change through the program */
 12.1137 +    ID3D11DeviceContext_IASetInputLayout(data->d3dContext, data->inputLayout);
 12.1138 +    ID3D11DeviceContext_VSSetShader(data->d3dContext, data->vertexShader, NULL, 0);
 12.1139 +    ID3D11DeviceContext_VSSetConstantBuffers(data->d3dContext, 0, 1, &data->vertexShaderConstants);
 12.1140 +
 12.1141 +done:
 12.1142 +    SAFE_RELEASE(d3dDevice);
 12.1143 +    SAFE_RELEASE(d3dContext);
 12.1144 +    return result;
 12.1145 +}
 12.1146 +
 12.1147 +#ifdef __WINRT__
 12.1148 +
 12.1149 +#if WINAPI_FAMILY == WINAPI_FAMILY_APP
 12.1150 +/* TODO, WinRT, XAML: get the ISwapChainBackgroundPanelNative from something other than a global var */
 12.1151 +extern ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative;
 12.1152 +#endif
 12.1153 +
 12.1154 +static IUnknown *
 12.1155 +D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
 12.1156 +{
 12.1157 +    SDL_Window * sdlWindow = renderer->window;
 12.1158 +    if ( ! renderer->window ) {
 12.1159 +        return NULL;
 12.1160 +    }
 12.1161 +
 12.1162 +    SDL_SysWMinfo sdlWindowInfo;
 12.1163 +    SDL_VERSION(&sdlWindowInfo.version);
 12.1164 +    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
 12.1165 +        return NULL;
 12.1166 +    }
 12.1167 +
 12.1168 +    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINRT) {
 12.1169 +        return NULL;
 12.1170 +    }
 12.1171 +
 12.1172 +    if (!sdlWindowInfo.info.winrt.window) {
 12.1173 +        return NULL;
 12.1174 +    }
 12.1175 +
 12.1176 +    ABI::Windows::UI::Core::ICoreWindow *coreWindow = NULL;
 12.1177 +    if (FAILED(sdlWindowInfo.info.winrt.window->QueryInterface(&coreWindow))) {
 12.1178 +        return NULL;
 12.1179 +    }
 12.1180 +
 12.1181 +    IUnknown *coreWindowAsIUnknown = NULL;
 12.1182 +    coreWindow->QueryInterface(&coreWindowAsIUnknown);
 12.1183 +    coreWindow->Release();
 12.1184 +
 12.1185 +    return coreWindowAsIUnknown;
 12.1186 +}
 12.1187 +
 12.1188 +static DXGI_MODE_ROTATION
 12.1189 +D3D11_GetCurrentRotation()
 12.1190 +{
 12.1191 +    switch (DisplayProperties::CurrentOrientation)
 12.1192 +    {
 12.1193 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 12.1194 +        /* Windows Phone rotations */
 12.1195 +    case DisplayOrientations::Landscape:
 12.1196 +        return DXGI_MODE_ROTATION_ROTATE90;
 12.1197 +    case DisplayOrientations::Portrait:
 12.1198 +        return DXGI_MODE_ROTATION_IDENTITY;
 12.1199 +    case DisplayOrientations::LandscapeFlipped:
 12.1200 +        return DXGI_MODE_ROTATION_ROTATE270;
 12.1201 +    case DisplayOrientations::PortraitFlipped:
 12.1202 +        return DXGI_MODE_ROTATION_ROTATE180;
 12.1203 +#else
 12.1204 +        /* Non-Windows-Phone rotations (ex: Windows 8, Windows RT) */
 12.1205 +    case DisplayOrientations::Landscape:
 12.1206 +        return DXGI_MODE_ROTATION_IDENTITY;
 12.1207 +    case DisplayOrientations::Portrait:
 12.1208 +        return DXGI_MODE_ROTATION_ROTATE270;
 12.1209 +    case DisplayOrientations::LandscapeFlipped:
 12.1210 +        return DXGI_MODE_ROTATION_ROTATE180;
 12.1211 +    case DisplayOrientations::PortraitFlipped:
 12.1212 +        return DXGI_MODE_ROTATION_ROTATE90;
 12.1213 +#endif /* WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP */
 12.1214 +
 12.1215 +    default:
 12.1216 +        return DXGI_MODE_ROTATION_UNSPECIFIED;
 12.1217 +    }
 12.1218 +}
 12.1219 +
 12.1220 +#else
 12.1221 +
 12.1222 +static DXGI_MODE_ROTATION
 12.1223 +D3D11_GetCurrentRotation()
 12.1224 +{
 12.1225 +    /* FIXME */
 12.1226 +    return DXGI_MODE_ROTATION_IDENTITY;
 12.1227 +}
 12.1228 +
 12.1229 +#endif /* __WINRT__ */
 12.1230 +
 12.1231 +static BOOL
 12.1232 +D3D11_IsDisplayRotated90Degrees(DXGI_MODE_ROTATION rotation)
 12.1233 +{
 12.1234 +    switch (rotation) {
 12.1235 +        case DXGI_MODE_ROTATION_ROTATE90:
 12.1236 +        case DXGI_MODE_ROTATION_ROTATE270:
 12.1237 +            return TRUE;
 12.1238 +        default:
 12.1239 +            return FALSE;
 12.1240 +    }
 12.1241 +}
 12.1242 +
 12.1243 +static int
 12.1244 +D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect)
 12.1245 +{
 12.1246 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1247 +    switch (data->rotation) {
 12.1248 +        case DXGI_MODE_ROTATION_IDENTITY:
 12.1249 +            outRect->left = sdlRect->x;
 12.1250 +            outRect->right = sdlRect->x + sdlRect->w;
 12.1251 +            outRect->top = sdlRect->y;
 12.1252 +            outRect->bottom = sdlRect->y + sdlRect->h;
 12.1253 +            break;
 12.1254 +        case DXGI_MODE_ROTATION_ROTATE270:
 12.1255 +            outRect->left = sdlRect->y;
 12.1256 +            outRect->right = sdlRect->y + sdlRect->h;
 12.1257 +            outRect->top = renderer->viewport.w - sdlRect->x - sdlRect->w;
 12.1258 +            outRect->bottom = renderer->viewport.w - sdlRect->x;
 12.1259 +            break;
 12.1260 +        case DXGI_MODE_ROTATION_ROTATE180:
 12.1261 +            outRect->left = renderer->viewport.w - sdlRect->x - sdlRect->w;
 12.1262 +            outRect->right = renderer->viewport.w - sdlRect->x;
 12.1263 +            outRect->top = renderer->viewport.h - sdlRect->y - sdlRect->h;
 12.1264 +            outRect->bottom = renderer->viewport.h - sdlRect->y;
 12.1265 +            break;
 12.1266 +        case DXGI_MODE_ROTATION_ROTATE90:
 12.1267 +            outRect->left = renderer->viewport.h - sdlRect->y - sdlRect->h;
 12.1268 +            outRect->right = renderer->viewport.h - sdlRect->y;
 12.1269 +            outRect->top = sdlRect->x;
 12.1270 +            outRect->bottom = sdlRect->x + sdlRect->h;
 12.1271 +            break;
 12.1272 +        default:
 12.1273 +            return SDL_SetError("The physical display is in an unknown or unsupported rotation");
 12.1274 +    }
 12.1275 +    return 0;
 12.1276 +}
 12.1277 +
 12.1278 +static HRESULT
 12.1279 +D3D11_CreateSwapChain(SDL_Renderer * renderer, int w, int h)
 12.1280 +{
 12.1281 +    D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
 12.1282 +#ifdef __WINRT__
 12.1283 +    IUnknown *coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
 12.1284 +    const BOOL usingXAML = (coreWindow == NULL);
 12.1285 +#else
 12.1286 +    IUnknown *coreWindow = NULL;
 12.1287 +    const BOOL usingXAML = FALSE;
 12.1288 +#endif
 12.1289 +    IDXGIDevice1 *dxgiDevice = NULL;
 12.1290 +    IDXGIAdapter *dxgiAdapter = NULL;
 12.1291 +    IDXGIFactory2 *dxgiFactory = NULL;
 12.1292 +    HRESULT result = S_OK;
 12.1293 +
 12.1294 +    /* Create a swap chain using the same adapter as the existing Direct3D device. */
 12.1295 +    DXGI_SWAP_CHAIN_DESC1 swapChainDesc;
 12.1296 +    SDL_zero(swapChainDesc);
 12.1297 +    swapChainDesc.Width = w;
 12.1298 +    swapChainDesc.Height = h;
 12.1299 +    swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; /* This is the most common swap chain format. */
 12.1300 +    swapChainDesc.Stereo = FALSE;
 12.1301 +    swapChainDesc.SampleDesc.Count = 1; /* Don't use multi-sampling. */
 12.1302 +    swapChainDesc.SampleDesc.Quality = 0;
 12.1303 +    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 12.1304 +    swapChainDesc.BufferCount = 2; /* Use double-buffering to minimize latency. */
 12.1305 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 12.1306 +    swapChainDesc.Scaling = DXGI_SCALING_STRETCH; /* On phone, only stretch and aspect-ratio stretch scaling are allowed. */
 12.1307 +    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; /* On phone, no swap effects are supported. */
 12.1308 +#else
 12.1309 +    if (usingXAML) {
 12.1310 +        swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
 12.1311 +    } else {
 12.1312 +        swapChainDesc.Scaling = DXGI_SCALING_NONE;
 12.1313 +    }
 12.1314 +    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; /* All Windows Store apps must use this SwapEffect. */
 12.1315 +#endif
 12.1316 +    swapChainDesc.Flags = 0;
 12.1317 +
 12.1318 +    result = ID3D11Device_QueryInterface(data->d3dDevice, &IID_IDXGIDevice1, &dxgiDevice);
 12.1319 +    if (FAILED(result)) {
 12.1320 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to IDXGIDevice1", result);
 12.1321 +        goto done;
 12.1322 +    }
 12.1323 +
 12.1324 +    result = IDXGIDevice1_GetAdapter(dxgiDevice, &dxgiAdapter);
 12.1325 +    if (FAILED(result)) {
 12.1326 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::GetAdapter", result);
 12.1327 +        goto done;
 12.1328 +    }
 12.1329 +
 12.1330 +    result = IDXGIAdapter_GetParent(dxgiAdapter, &IID_IDXGIFactory2, &dxgiFactory);
 12.1331 +    if (FAILED(result)) {
 12.1332 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIAdapter::GetParent", result);
 12.1333 +        goto done;
 12.1334 +    }
 12.1335 +
 12.1336 +    if (coreWindow) {
 12.1337 +        result = IDXGIFactory2_CreateSwapChainForCoreWindow(dxgiFactory,
 12.1338 +            (IUnknown *)data->d3dDevice,
 12.1339 +            coreWindow,
 12.1340 +            &swapChainDesc,
 12.1341 +            NULL, /* Allow on all displays. */
 12.1342 +            &data->swapChain
 12.1343 +            );
 12.1344 +        if (FAILED(result)) {
 12.1345 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForCoreWindow", result);
 12.1346 +            goto done;
 12.1347 +        }
 12.1348 +    } else if (usingXAML) {
 12.1349 +        result = IDXGIFactory2_CreateSwapChainForComposition(dxgiFactory,
 12.1350 +            (IUnknown *)data->d3dDevice,
 12.1351 +            &swapChainDesc,
 12.1352 +            NULL,
 12.1353 +            &data->swapChain);
 12.1354 +        if (FAILED(result)) {
 12.1355 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForComposition", result);
 12.1356 +            goto done;
 12.1357 +        }
 12.1358 +
 12.1359 +#if WINAPI_FAMILY == WINAPI_FAMILY_APP
 12.1360 +        result = WINRT_GlobalSwapChainBackgroundPanelNative->SetSwapChain(data->swapChain);
 12.1361 +        if (FAILED(result)) {
 12.1362 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ISwapChainBackgroundPanelNative::SetSwapChain", result);
 12.1363 +            return result;
 12.1364 +        }
 12.1365 +#else
 12.1366 +        SDL_SetError(__FUNCTION__ ", XAML support is not yet available for Windows Phone");
 12.1367 +        result = E_FAIL;
 12.1368 +        goto done;
 12.1369 +#endif
 12.1370 +    } else {
 12.1371 +        SDL_SysWMinfo windowinfo;
 12.1372 +        SDL_VERSION(&windowinfo.version);
 12.1373 +        SDL_GetWindowWMInfo(renderer->window, &windowinfo);
 12.1374 +
 12.1375 +        result = IDXGIFactory2_CreateSwapChainForHwnd(dxgiFactory,
 12.1376 +            (IUnknown *)data->d3dDevice,
 12.1377 +            windowinfo.info.win.window,
 12.1378 +            &swapChainDesc,
 12.1379 +            NULL,
 12.1380 +            NULL, /* Allow on all displays. */
 12.1381 +            &data->swapChain
 12.1382 +            );
 12.1383 +        if (FAILED(result)) {
 12.1384 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForHwnd", result);
 12.1385 +            goto done;
 12.1386 +        }
 12.1387 +    }
 12.1388 +    data->swapEffect = swapChainDesc.SwapEffect;
 12.1389 +
 12.1390 +    /* Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
 12.1391 +     * ensures that the application will only render after each VSync, minimizing power consumption.
 12.1392 +     */
 12.1393 +    result = IDXGIDevice1_SetMaximumFrameLatency(dxgiDevice, 1);
 12.1394 +    if (FAILED(result)) {
 12.1395 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::SetMaximumFrameLatency", result);
 12.1396 +        goto done;
 12.1397 +    }
 12.1398 +
 12.1399 +done:
 12.1400 +    SAFE_RELEASE(coreWindow);
 12.1401 +    SAFE_RELEASE(dxgiDevice);
 12.1402 +    SAFE_RELEASE(dxgiAdapter);
 12.1403 +    SAFE_RELEASE(dxgiFactory);
 12.1404 +    return result;
 12.1405 +}
 12.1406 +
 12.1407 +
 12.1408 +/* Initialize all resources that change when the window's size changes. */
 12.1409 +static HRESULT
 12.1410 +D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
 12.1411 +{
 12.1412 +    D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
 12.1413 +    ID3D11Texture2D *backBuffer = NULL;
 12.1414 +    HRESULT result = S_OK;
 12.1415 +    int w, h;
 12.1416 +
 12.1417 +    /* Release the previous render target view */
 12.1418 +    D3D11_ReleaseMainRenderTargetView(renderer);
 12.1419 +
 12.1420 +    /* The width and height of the swap chain must be based on the window's
 12.1421 +     * landscape-oriented width and height. If the window is in a portrait
 12.1422 +     * rotation, the dimensions must be reversed.
 12.1423 +     */
 12.1424 +    SDL_GetWindowSize(renderer->window, &w, &h);
 12.1425 +    data->rotation = D3D11_GetCurrentRotation();
 12.1426 +
 12.1427 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 12.1428 +    const BOOL swapDimensions = FALSE;
 12.1429 +#else
 12.1430 +    const BOOL swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
 12.1431 +#endif
 12.1432 +    if (swapDimensions) {
 12.1433 +        int tmp = w;
 12.1434 +        w = h;
 12.1435 +        h = tmp;
 12.1436 +    }
 12.1437 +
 12.1438 +    if (data->swapChain) {
 12.1439 +        /* If the swap chain already exists, resize it. */
 12.1440 +        result = IDXGISwapChain_ResizeBuffers(data->swapChain,
 12.1441 +            0,
 12.1442 +            w, h,
 12.1443 +            DXGI_FORMAT_UNKNOWN,
 12.1444 +            0
 12.1445 +            );
 12.1446 +        if (FAILED(result)) {
 12.1447 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain::ResizeBuffers", result);
 12.1448 +            goto done;
 12.1449 +        }
 12.1450 +    } else {
 12.1451 +        result = D3D11_CreateSwapChain(renderer, w, h);
 12.1452 +        if (FAILED(result)) {
 12.1453 +            goto done;
 12.1454 +        }
 12.1455 +    }
 12.1456 +    
 12.1457 +#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
 12.1458 +    /* Set the proper rotation for the swap chain, and generate the
 12.1459 +     * 3D matrix transformation for rendering to the rotated swap chain.
 12.1460 +     *
 12.1461 +     * To note, the call for this, IDXGISwapChain1::SetRotation, is not necessary
 12.1462 +     * on Windows Phone, nor is it supported there.  It's only needed in Windows 8/RT.
 12.1463 +     */
 12.1464 +    if (data->swapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL)
 12.1465 +    {
 12.1466 +        result = IDXGISwapChain1_SetRotation(data->swapChain, data->rotation);
 12.1467 +        if (FAILED(result)) {
 12.1468 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::SetRotation", result);
 12.1469 +            goto done;
 12.1470 +        }
 12.1471 +    }
 12.1472 +#endif
 12.1473 +
 12.1474 +    result = IDXGISwapChain_GetBuffer(data->swapChain,
 12.1475 +        0,
 12.1476 +        &IID_ID3D11Texture2D,
 12.1477 +        &backBuffer
 12.1478 +        );
 12.1479 +    if (FAILED(result)) {
 12.1480 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain::GetBuffer [back-buffer]", result);
 12.1481 +        goto done;
 12.1482 +    }
 12.1483 +
 12.1484 +    /* Create a render target view of the swap chain back buffer. */
 12.1485 +    result = ID3D11Device_CreateRenderTargetView(data->d3dDevice,
 12.1486 +        (ID3D11Resource *)backBuffer,
 12.1487 +        NULL,
 12.1488 +        &data->mainRenderTargetView
 12.1489 +        );
 12.1490 +    if (FAILED(result)) {
 12.1491 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device::CreateRenderTargetView", result);
 12.1492 +        goto done;
 12.1493 +    }
 12.1494 +
 12.1495 +    if (D3D11_UpdateViewport(renderer) != 0) {
 12.1496 +        /* D3D11_UpdateViewport will set the SDL error if it fails. */
 12.1497 +        result = E_FAIL;
 12.1498 +        goto done;
 12.1499 +    }
 12.1500 +
 12.1501 +done:
 12.1502 +    SAFE_RELEASE(backBuffer);
 12.1503 +    return result;
 12.1504 +}
 12.1505 +
 12.1506 +/* This method is called when the window's size changes. */
 12.1507 +static HRESULT
 12.1508 +D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
 12.1509 +{
 12.1510 +    D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
 12.1511 +    /* FIXME: Do we need to release render targets like we do in D3D9? */
 12.1512 +    return D3D11_CreateWindowSizeDependentResources(renderer);
 12.1513 +}
 12.1514 +
 12.1515 +HRESULT
 12.1516 +D3D11_HandleDeviceLost(SDL_Renderer * renderer)
 12.1517 +{
 12.1518 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1519 +    HRESULT result = S_OK;
 12.1520 +
 12.1521 +    /* FIXME: Need to release all resources - all texures are invalid! */
 12.1522 +
 12.1523 +    result = D3D11_CreateDeviceResources(renderer);
 12.1524 +    if (FAILED(result)) {
 12.1525 +        /* D3D11_CreateDeviceResources will set the SDL error */
 12.1526 +        return result;
 12.1527 +    }
 12.1528 +
 12.1529 +    result = D3D11_UpdateForWindowSizeChange(renderer);
 12.1530 +    if (FAILED(result)) {
 12.1531 +        /* D3D11_UpdateForWindowSizeChange will set the SDL error */
 12.1532 +        return result;
 12.1533 +    }
 12.1534 +
 12.1535 +    return S_OK;
 12.1536 +}
 12.1537 +
 12.1538 +static void
 12.1539 +D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
 12.1540 +{
 12.1541 +    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
 12.1542 +        D3D11_UpdateForWindowSizeChange(renderer);
 12.1543 +    }
 12.1544 +}
 12.1545 +
 12.1546 +static D3D11_FILTER
 12.1547 +GetScaleQuality(void)
 12.1548 +{
 12.1549 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
 12.1550 +    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
 12.1551 +        return D3D11_FILTER_MIN_MAG_MIP_POINT;
 12.1552 +    } else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
 12.1553 +        return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
 12.1554 +    }
 12.1555 +}
 12.1556 +
 12.1557 +static int
 12.1558 +D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 12.1559 +{
 12.1560 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.1561 +    D3D11_TextureData *textureData;
 12.1562 +    HRESULT result;
 12.1563 +    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
 12.1564 +    if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
 12.1565 +        return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
 12.1566 +            __FUNCTION__, texture->format);
 12.1567 +    }
 12.1568 +
 12.1569 +    textureData = (D3D11_TextureData*) SDL_calloc(1, sizeof(*textureData));
 12.1570 +    if (!textureData) {
 12.1571 +        SDL_OutOfMemory();
 12.1572 +        return -1;
 12.1573 +    }
 12.1574 +    textureData->scaleMode = GetScaleQuality();
 12.1575 +
 12.1576 +    texture->driverdata = textureData;
 12.1577 +
 12.1578 +    D3D11_TEXTURE2D_DESC textureDesc;
 12.1579 +    SDL_zero(textureDesc);
 12.1580 +    textureDesc.Width = texture->w;
 12.1581 +    textureDesc.Height = texture->h;
 12.1582 +    textureDesc.MipLevels = 1;
 12.1583 +    textureDesc.ArraySize = 1;
 12.1584 +    textureDesc.Format = textureFormat;
 12.1585 +    textureDesc.SampleDesc.Count = 1;
 12.1586 +    textureDesc.SampleDesc.Quality = 0;
 12.1587 +    textureDesc.MiscFlags = 0;
 12.1588 +
 12.1589 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
 12.1590 +        textureDesc.Usage = D3D11_USAGE_DYNAMIC;
 12.1591 +        textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 12.1592 +    } else {
 12.1593 +        textureDesc.Usage = D3D11_USAGE_DEFAULT;
 12.1594 +        textureDesc.CPUAccessFlags = 0;
 12.1595 +    }
 12.1596 +
 12.1597 +    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
 12.1598 +        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
 12.1599 +    } else {
 12.1600 +        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
 12.1601 +    }
 12.1602 +
 12.1603 +    result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
 12.1604 +        &textureDesc,
 12.1605 +        NULL,
 12.1606 +        &textureData->mainTexture
 12.1607 +        );
 12.1608 +    if (FAILED(result)) {
 12.1609 +        D3D11_DestroyTexture(renderer, texture);
 12.1610 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D", result);
 12.1611 +        return -1;
 12.1612 +    }
 12.1613 +
 12.1614 +    if (texture->access & SDL_TEXTUREACCESS_TARGET) {
 12.1615 +        D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
 12.1616 +        renderTargetViewDesc.Format = textureDesc.Format;
 12.1617 +        renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
 12.1618 +        renderTargetViewDesc.Texture2D.MipSlice = 0;
 12.1619 +
 12.1620 +        result = ID3D11Device_CreateRenderTargetView(rendererData->d3dDevice,
 12.1621 +            (ID3D11Resource *)textureData->mainTexture,
 12.1622 +            &renderTargetViewDesc,
 12.1623 +            &textureData->mainTextureRenderTargetView);
 12.1624 +        if (FAILED(result)) {
 12.1625 +            D3D11_DestroyTexture(renderer, texture);
 12.1626 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRenderTargetView", result);
 12.1627 +            return -1;
 12.1628 +        }
 12.1629 +    }
 12.1630 +
 12.1631 +    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
 12.1632 +    resourceViewDesc.Format = textureDesc.Format;
 12.1633 +    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
 12.1634 +    resourceViewDesc.Texture2D.MostDetailedMip = 0;
 12.1635 +    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
 12.1636 +    result = ID3D11Device_CreateShaderResourceView(rendererData->d3dDevice,
 12.1637 +        (ID3D11Resource *)textureData->mainTexture,
 12.1638 +        &resourceViewDesc,
 12.1639 +        &textureData->mainTextureResourceView
 12.1640 +        );
 12.1641 +    if (FAILED(result)) {
 12.1642 +        D3D11_DestroyTexture(renderer, texture);
 12.1643 +        WIN_SetErrorFromHRESULT(__FUNCTION__ "ID3D11Device1::CreateShaderResourceView", result);
 12.1644 +        return -1;
 12.1645 +    }
 12.1646 +
 12.1647 +    return 0;
 12.1648 +}
 12.1649 +
 12.1650 +static void
 12.1651 +D3D11_DestroyTexture(SDL_Renderer * renderer,
 12.1652 +                     SDL_Texture * texture)
 12.1653 +{
 12.1654 +    D3D11_TextureData *data = (D3D11_TextureData *)texture->driverdata;
 12.1655 +
 12.1656 +    if (!data) {
 12.1657 +        return;
 12.1658 +    }
 12.1659 +
 12.1660 +    SAFE_RELEASE(data->mainTexture);
 12.1661 +    SAFE_RELEASE(data->mainTextureResourceView);
 12.1662 +    SAFE_RELEASE(data->mainTextureRenderTargetView);
 12.1663 +    SAFE_RELEASE(data->stagingTexture);
 12.1664 +    SDL_free(data);
 12.1665 +    texture->driverdata = NULL;
 12.1666 +}
 12.1667 +
 12.1668 +static int
 12.1669 +D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 12.1670 +                    const SDL_Rect * rect, const void * srcPixels,
 12.1671 +                    int srcPitch)
 12.1672 +{
 12.1673 +    /* Lock the texture, retrieving a buffer to write pixel data to: */
 12.1674 +    void * destPixels = NULL;
 12.1675 +    int destPitch = 0;
 12.1676 +    if (D3D11_LockTexture(renderer, texture, rect, &destPixels, &destPitch) != 0) {
 12.1677 +        /* An error is already set.  Attach some info to it, then return to the caller. */
 12.1678 +        char errorMessage[1024];
 12.1679 +        SDL_snprintf(errorMessage, sizeof(errorMessage), __FUNCTION__ ", Lock Texture Failed: %s", SDL_GetError());
 12.1680 +        return SDL_SetError(errorMessage);
 12.1681 +    }
 12.1682 +
 12.1683 +    /* Copy pixel data to the locked texture's memory: */
 12.1684 +    for (int y = 0; y < rect->h; ++y) {
 12.1685 +        SDL_memcpy(
 12.1686 +            ((Uint8 *)destPixels) + (destPitch * y),
 12.1687 +            ((Uint8 *)srcPixels) + (srcPitch * y),
 12.1688 +            srcPitch
 12.1689 +            );
 12.1690 +    }
 12.1691 +
 12.1692 +    /* Commit the texture's memory back to Direct3D: */
 12.1693 +    D3D11_UnlockTexture(renderer, texture);
 12.1694 +
 12.1695 +    return 0;
 12.1696 +}
 12.1697 +
 12.1698 +static int
 12.1699 +D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 12.1700 +                  const SDL_Rect * rect, void **pixels, int *pitch)
 12.1701 +{
 12.1702 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.1703 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.1704 +    HRESULT result = S_OK;
 12.1705 +
 12.1706 +    if (textureData->stagingTexture) {
 12.1707 +        return SDL_SetError("texture is already locked");
 12.1708 +    }
 12.1709 +    
 12.1710 +    /* Create a 'staging' texture, which will be used to write to a portion
 12.1711 +     * of the main texture.  This is necessary, as Direct3D 11.1 does not
 12.1712 +     * have the ability to write a CPU-bound pixel buffer to a rectangular
 12.1713 +     * subrect of a texture.  Direct3D 11.1 can, however, write a pixel
 12.1714 +     * buffer to an entire texture, hence the use of a staging texture.
 12.1715 +     *
 12.1716 +     * TODO, WinRT: consider avoiding the use of a staging texture in D3D11_LockTexture if/when the entire texture is being updated
 12.1717 +     */
 12.1718 +    D3D11_TEXTURE2D_DESC stagingTextureDesc;
 12.1719 +    ID3D11Texture2D_GetDesc(textureData->mainTexture, &stagingTextureDesc);
 12.1720 +    stagingTextureDesc.Width = rect->w;
 12.1721 +    stagingTextureDesc.Height = rect->h;
 12.1722 +    stagingTextureDesc.BindFlags = 0;
 12.1723 +    stagingTextureDesc.MiscFlags = 0;
 12.1724 +    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 12.1725 +    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
 12.1726 +    result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
 12.1727 +        &stagingTextureDesc,
 12.1728 +        NULL,
 12.1729 +        &textureData->stagingTexture);
 12.1730 +    if (FAILED(result)) {
 12.1731 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
 12.1732 +        return -1;
 12.1733 +    }
 12.1734 +
 12.1735 +    /* Get a write-only pointer to data in the staging texture: */
 12.1736 +    D3D11_MAPPED_SUBRESOURCE textureMemory;
 12.1737 +    result = ID3D11DeviceContext_Map(rendererData->d3dContext,
 12.1738 +        (ID3D11Resource *)textureData->stagingTexture,
 12.1739 +        0,
 12.1740 +        D3D11_MAP_WRITE,
 12.1741 +        0,
 12.1742 +        &textureMemory
 12.1743 +        );
 12.1744 +    if (FAILED(result)) {
 12.1745 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
 12.1746 +        SAFE_RELEASE(textureData->stagingTexture);
 12.1747 +        return -1;
 12.1748 +    }
 12.1749 +
 12.1750 +    /* Make note of where the staging texture will be written to 
 12.1751 +     * (on a call to SDL_UnlockTexture):
 12.1752 +     */
 12.1753 +    textureData->lockedTexturePositionX = rect->x;
 12.1754 +    textureData->lockedTexturePositionY = rect->y;
 12.1755 +
 12.1756 +    /* Make sure the caller has information on the texture's pixel buffer,
 12.1757 +     * then return:
 12.1758 +     */
 12.1759 +    *pixels = textureMemory.pData;
 12.1760 +    *pitch = textureMemory.RowPitch;
 12.1761 +    return 0;
 12.1762 +}
 12.1763 +
 12.1764 +static void
 12.1765 +D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 12.1766 +{
 12.1767 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.1768 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.1769 +
 12.1770 +    /* Commit the pixel buffer's changes back to the staging texture: */
 12.1771 +    ID3D11DeviceContext_Unmap(rendererData->d3dContext,
 12.1772 +        (ID3D11Resource *)textureData->stagingTexture,
 12.1773 +        0);
 12.1774 +
 12.1775 +    /* Copy the staging texture's contents back to the main texture: */
 12.1776 +    ID3D11DeviceContext_CopySubresourceRegion(rendererData->d3dContext,
 12.1777 +        (ID3D11Resource *)textureData->mainTexture,
 12.1778 +        0,
 12.1779 +        textureData->lockedTexturePositionX,
 12.1780 +        textureData->lockedTexturePositionY,
 12.1781 +        0,
 12.1782 +        (ID3D11Resource *)textureData->stagingTexture,
 12.1783 +        0,
 12.1784 +        NULL);
 12.1785 +
 12.1786 +    SAFE_RELEASE(textureData->stagingTexture);
 12.1787 +}
 12.1788 +
 12.1789 +static int
 12.1790 +D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
 12.1791 +{
 12.1792 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.1793 +
 12.1794 +    if (texture == NULL) {
 12.1795 +        rendererData->currentOffscreenRenderTargetView = NULL;
 12.1796 +        return 0;
 12.1797 +    }
 12.1798 +
 12.1799 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.1800 +
 12.1801 +    if (!textureData->mainTextureRenderTargetView) {
 12.1802 +        return SDL_SetError("specified texture is not a render target");
 12.1803 +    }
 12.1804 +
 12.1805 +    rendererData->currentOffscreenRenderTargetView = textureData->mainTextureRenderTargetView;
 12.1806 +
 12.1807 +    return 0;
 12.1808 +}
 12.1809 +
 12.1810 +static void
 12.1811 +D3D11_SetModelMatrix(SDL_Renderer *renderer, const Float4X4 *matrix)
 12.1812 +{
 12.1813 +    D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
 12.1814 +
 12.1815 +    if (matrix) {
 12.1816 +        data->vertexShaderConstantsData.model = *matrix;
 12.1817 +    } else {
 12.1818 +        data->vertexShaderConstantsData.model = MatrixIdentity();
 12.1819 +    }
 12.1820 +
 12.1821 +    ID3D11DeviceContext_UpdateSubresource(data->d3dContext,
 12.1822 +        (ID3D11Resource *)data->vertexShaderConstants,
 12.1823 +        0,
 12.1824 +        NULL,
 12.1825 +        &data->vertexShaderConstantsData,
 12.1826 +        0,
 12.1827 +        0
 12.1828 +        );
 12.1829 +}
 12.1830 +
 12.1831 +static int
 12.1832 +D3D11_UpdateViewport(SDL_Renderer * renderer)
 12.1833 +{
 12.1834 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1835 +
 12.1836 +    if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
 12.1837 +        /* If the viewport is empty, assume that it is because
 12.1838 +         * SDL_CreateRenderer is calling it, and will call it again later
 12.1839 +         * with a non-empty viewport.
 12.1840 +         */
 12.1841 +        return 0;
 12.1842 +    }
 12.1843 +
 12.1844 +    /* Make sure the SDL viewport gets rotated to that of the physical display's rotation.
 12.1845 +     * Keep in mind here that the Y-axis will be been inverted (from Direct3D's
 12.1846 +     * default coordinate system) so rotations will be done in the opposite
 12.1847 +     * direction of the DXGI_MODE_ROTATION enumeration.
 12.1848 +     */
 12.1849 +    Float4X4 projection;
 12.1850 +    switch (data->rotation)
 12.1851 +    {
 12.1852 +        case DXGI_MODE_ROTATION_IDENTITY:
 12.1853 +            projection = MatrixIdentity();
 12.1854 +            break;
 12.1855 +        case DXGI_MODE_ROTATION_ROTATE270:
 12.1856 +            projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
 12.1857 +            break;
 12.1858 +        case DXGI_MODE_ROTATION_ROTATE180:
 12.1859 +            projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
 12.1860 +            break;
 12.1861 +        case DXGI_MODE_ROTATION_ROTATE90:
 12.1862 +            projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
 12.1863 +            break;
 12.1864 +        default:
 12.1865 +            return SDL_SetError("An unknown DisplayOrientation is being used");
 12.1866 +    }
 12.1867 +
 12.1868 +    /* Update the view matrix */
 12.1869 +    Float4X4 view;
 12.1870 +    view.m[0][0] = 2.0f / renderer->viewport.w;
 12.1871 +    view.m[0][1] = 0.0f;
 12.1872 +    view.m[0][2] = 0.0f;
 12.1873 +    view.m[0][3] = 0.0f;
 12.1874 +    view.m[1][0] = 0.0f;
 12.1875 +    view.m[1][1] = -2.0f / renderer->viewport.h;
 12.1876 +    view.m[1][2] = 0.0f;
 12.1877 +    view.m[1][3] = 0.0f;
 12.1878 +    view.m[2][0] = 0.0f;
 12.1879 +    view.m[2][1] = 0.0f;
 12.1880 +    view.m[2][2] = 1.0f;
 12.1881 +    view.m[2][3] = 0.0f;
 12.1882 +    view.m[3][0] = -1.0f;
 12.1883 +    view.m[3][1] = 1.0f;
 12.1884 +    view.m[3][2] = 0.0f;
 12.1885 +    view.m[3][3] = 1.0f;
 12.1886 +
 12.1887 +    /* Combine the projection + view matrix together now, as both only get
 12.1888 +     * set here (as of this writing, on Dec 26, 2013).  When done, store it
 12.1889 +     * for eventual transfer to the GPU.
 12.1890 +     */
 12.1891 +    data->vertexShaderConstantsData.projectionAndView = MatrixMultiply(
 12.1892 +            view,
 12.1893 +            projection);
 12.1894 +
 12.1895 +    /* Reset the model matrix */
 12.1896 +    D3D11_SetModelMatrix(renderer, NULL);
 12.1897 +
 12.1898 +    /* Update the Direct3D viewport, which seems to be aligned to the
 12.1899 +     * swap buffer's coordinate space, which is always in either
 12.1900 +     * a landscape mode, for all Windows 8/RT devices, or a portrait mode,
 12.1901 +     * for Windows Phone devices.
 12.1902 +     */
 12.1903 +    SDL_FRect orientationAlignedViewport;
 12.1904 +    const BOOL swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
 12.1905 +    if (swapDimensions) {
 12.1906 +        orientationAlignedViewport.x = (float) renderer->viewport.y;
 12.1907 +        orientationAlignedViewport.y = (float) renderer->viewport.x;
 12.1908 +        orientationAlignedViewport.w = (float) renderer->viewport.h;
 12.1909 +        orientationAlignedViewport.h = (float) renderer->viewport.w;
 12.1910 +    } else {
 12.1911 +        orientationAlignedViewport.x = (float) renderer->viewport.x;
 12.1912 +        orientationAlignedViewport.y = (float) renderer->viewport.y;
 12.1913 +        orientationAlignedViewport.w = (float) renderer->viewport.w;
 12.1914 +        orientationAlignedViewport.h = (float) renderer->viewport.h;
 12.1915 +    }
 12.1916 +    /* TODO, WinRT: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped) */
 12.1917 +
 12.1918 +    D3D11_VIEWPORT viewport;
 12.1919 +    viewport.TopLeftX = orientationAlignedViewport.x;
 12.1920 +    viewport.TopLeftY = orientationAlignedViewport.y;
 12.1921 +    viewport.Width = orientationAlignedViewport.w;
 12.1922 +    viewport.Height = orientationAlignedViewport.h;
 12.1923 +    viewport.MinDepth = 0.0f;
 12.1924 +    viewport.MaxDepth = 1.0f;
 12.1925 +    ID3D11DeviceContext_RSSetViewports(data->d3dContext, 1, &viewport);
 12.1926 +
 12.1927 +    return 0;
 12.1928 +}
 12.1929 +
 12.1930 +static int
 12.1931 +D3D11_UpdateClipRect(SDL_Renderer * renderer)
 12.1932 +{
 12.1933 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1934 +    const SDL_Rect *rect = &renderer->clip_rect;
 12.1935 +
 12.1936 +    if (SDL_RectEmpty(rect)) {
 12.1937 +        ID3D11DeviceContext_RSSetScissorRects(data->d3dContext, 0, NULL);
 12.1938 +    } else {
 12.1939 +        D3D11_RECT scissorRect;
 12.1940 +        if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &scissorRect) != 0) {
 12.1941 +            /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
 12.1942 +            return -1;
 12.1943 +        }
 12.1944 +        ID3D11DeviceContext_RSSetScissorRects(data->d3dContext, 1, &scissorRect);
 12.1945 +    }
 12.1946 +
 12.1947 +    return 0;
 12.1948 +}
 12.1949 +
 12.1950 +static void
 12.1951 +D3D11_ReleaseMainRenderTargetView(SDL_Renderer * renderer)
 12.1952 +{
 12.1953 +    D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
 12.1954 +    ID3D11DeviceContext_OMSetRenderTargets(data->d3dContext, 0, NULL, NULL);
 12.1955 +    SAFE_RELEASE(data->mainRenderTargetView);
 12.1956 +}
 12.1957 +
 12.1958 +static ID3D11RenderTargetView *
 12.1959 +D3D11_GetCurrentRenderTargetView(SDL_Renderer * renderer)
 12.1960 +{
 12.1961 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1962 +    if (data->currentOffscreenRenderTargetView) {
 12.1963 +        return data->currentOffscreenRenderTargetView;
 12.1964 +    } else {
 12.1965 +        return data->mainRenderTargetView;
 12.1966 +    }
 12.1967 +}
 12.1968 +
 12.1969 +static int
 12.1970 +D3D11_RenderClear(SDL_Renderer * renderer)
 12.1971 +{
 12.1972 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.1973 +    const float colorRGBA[] = {
 12.1974 +        (renderer->r / 255.0f),
 12.1975 +        (renderer->g / 255.0f),
 12.1976 +        (renderer->b / 255.0f),
 12.1977 +        (renderer->a / 255.0f)
 12.1978 +    };
 12.1979 +    ID3D11DeviceContext_ClearRenderTargetView(data->d3dContext,
 12.1980 +        D3D11_GetCurrentRenderTargetView(renderer),
 12.1981 +        colorRGBA
 12.1982 +        );
 12.1983 +    return 0;
 12.1984 +}
 12.1985 +
 12.1986 +static int
 12.1987 +D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
 12.1988 +                         const void * vertexData, size_t dataSizeInBytes)
 12.1989 +{
 12.1990 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.1991 +    D3D11_BUFFER_DESC vertexBufferDesc;
 12.1992 +    HRESULT result = S_OK;
 12.1993 +
 12.1994 +    if (rendererData->vertexBuffer) {
 12.1995 +        ID3D11Buffer_GetDesc(rendererData->vertexBuffer, &vertexBufferDesc);
 12.1996 +    } else {
 12.1997 +        SDL_zero(vertexBufferDesc);
 12.1998 +    }
 12.1999 +
 12.2000 +    if (rendererData->vertexBuffer && vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
 12.2001 +        D3D11_MAPPED_SUBRESOURCE mappedResource;
 12.2002 +        result = ID3D11DeviceContext_Map(rendererData->d3dContext,
 12.2003 +            (ID3D11Resource *)rendererData->vertexBuffer,
 12.2004 +            0,
 12.2005 +            D3D11_MAP_WRITE_DISCARD,
 12.2006 +            0,
 12.2007 +            &mappedResource
 12.2008 +            );
 12.2009 +        if (FAILED(result)) {
 12.2010 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [vertex buffer]", result);
 12.2011 +            return -1;
 12.2012 +        }
 12.2013 +        SDL_memcpy(mappedResource.pData, vertexData, dataSizeInBytes);
 12.2014 +        ID3D11DeviceContext_Unmap(rendererData->d3dContext, (ID3D11Resource *)rendererData->vertexBuffer, 0);
 12.2015 +    } else {
 12.2016 +        SAFE_RELEASE(rendererData->vertexBuffer);
 12.2017 +
 12.2018 +        vertexBufferDesc.ByteWidth = dataSizeInBytes;
 12.2019 +        vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
 12.2020 +        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
 12.2021 +        vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 12.2022 +
 12.2023 +        D3D11_SUBRESOURCE_DATA vertexBufferData;
 12.2024 +        SDL_zero(vertexBufferData);
 12.2025 +        vertexBufferData.pSysMem = vertexData;
 12.2026 +        vertexBufferData.SysMemPitch = 0;
 12.2027 +        vertexBufferData.SysMemSlicePitch = 0;
 12.2028 +
 12.2029 +        result = ID3D11Device_CreateBuffer(rendererData->d3dDevice,
 12.2030 +            &vertexBufferDesc,
 12.2031 +            &vertexBufferData,
 12.2032 +            &rendererData->vertexBuffer
 12.2033 +            );
 12.2034 +        if (FAILED(result)) {
 12.2035 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex buffer]", result);
 12.2036 +            return -1;
 12.2037 +        }
 12.2038 +
 12.2039 +        UINT stride = sizeof(VertexPositionColor);
 12.2040 +        UINT offset = 0;
 12.2041 +        ID3D11DeviceContext_IASetVertexBuffers(rendererData->d3dContext,
 12.2042 +            0,
 12.2043 +            1,
 12.2044 +            &rendererData->vertexBuffer,
 12.2045 +            &stride,
 12.2046 +            &offset
 12.2047 +            );
 12.2048 +    }
 12.2049 +
 12.2050 +    return 0;
 12.2051 +}
 12.2052 +
 12.2053 +static void
 12.2054 +D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
 12.2055 +{
 12.2056 +    D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
 12.2057 +    ID3D11RenderTargetView *renderTargetView = D3D11_GetCurrentRenderTargetView(renderer);
 12.2058 +    if (renderTargetView != rendererData->currentRenderTargetView) {
 12.2059 +        ID3D11DeviceContext_OMSetRenderTargets(rendererData->d3dContext,
 12.2060 +            1,
 12.2061 +            &renderTargetView,
 12.2062 +            NULL
 12.2063 +            );
 12.2064 +        rendererData->currentRenderTargetView = renderTargetView;
 12.2065 +    }
 12.2066 +
 12.2067 +    ID3D11RasterizerState *rasterizerState;
 12.2068 +    if (SDL_RectEmpty(&renderer->clip_rect)) {
 12.2069 +        rasterizerState = rendererData->mainRasterizer;
 12.2070 +    } else {
 12.2071 +        rasterizerState = rendererData->clippedRasterizer;
 12.2072 +    }
 12.2073 +    if (rasterizerState != rendererData->currentRasterizerState) {
 12.2074 +        ID3D11DeviceContext_RSSetState(rendererData->d3dContext, rasterizerState);
 12.2075 +        rendererData->currentRasterizerState = rasterizerState;
 12.2076 +    }
 12.2077 +}
 12.2078 +
 12.2079 +static void
 12.2080 +D3D11_RenderSetBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
 12.2081 +{
 12.2082 +    D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
 12.2083 +    ID3D11BlendState *blendState;
 12.2084 +    switch (blendMode) {
 12.2085 +    case SDL_BLENDMODE_BLEND:
 12.2086 +        blendState = rendererData->blendModeBlend;
 12.2087 +        break;
 12.2088 +    case SDL_BLENDMODE_ADD:
 12.2089 +        blendState = rendererData->blendModeAdd;
 12.2090 +        break;
 12.2091 +    case SDL_BLENDMODE_MOD:
 12.2092 +        blendState = rendererData->blendModeMod;
 12.2093 +        break;
 12.2094 +    case SDL_BLENDMODE_NONE:
 12.2095 +        blendState = NULL;
 12.2096 +        break;
 12.2097 +    }
 12.2098 +    if (blendState != rendererData->currentBlendState) {
 12.2099 +        ID3D11DeviceContext_OMSetBlendState(rendererData->d3dContext, blendState, 0, 0xFFFFFFFF);
 12.2100 +        rendererData->currentBlendState = blendState;
 12.2101 +    }
 12.2102 +}
 12.2103 +
 12.2104 +static void
 12.2105 +D3D11_SetPixelShader(SDL_Renderer * renderer,
 12.2106 +                     ID3D11PixelShader * shader,
 12.2107 +                     ID3D11ShaderResourceView * shaderResource,
 12.2108 +                     ID3D11SamplerState * sampler)
 12.2109 +{
 12.2110 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2111 +    if (shader != rendererData->currentShader) {
 12.2112 +        ID3D11DeviceContext_PSSetShader(rendererData->d3dContext, shader, NULL, 0);
 12.2113 +        rendererData->currentShader = shader;
 12.2114 +    }
 12.2115 +    if (shaderResource != rendererData->currentShaderResource) {
 12.2116 +        ID3D11DeviceContext_PSSetShaderResources(rendererData->d3dContext, 0, 1, &shaderResource);
 12.2117 +        rendererData->currentShaderResource = shaderResource;
 12.2118 +    }
 12.2119 +    if (sampler != rendererData->currentSampler) {
 12.2120 +        ID3D11DeviceContext_PSSetSamplers(rendererData->d3dContext, 0, 1, &sampler);
 12.2121 +        rendererData->currentSampler = sampler;
 12.2122 +    }
 12.2123 +}
 12.2124 +
 12.2125 +static void
 12.2126 +D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
 12.2127 +                         D3D11_PRIMITIVE_TOPOLOGY primitiveTopology,
 12.2128 +                         UINT vertexCount)
 12.2129 +{
 12.2130 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2131 +
 12.2132 +    ID3D11DeviceContext_IASetPrimitiveTopology(rendererData->d3dContext, primitiveTopology);
 12.2133 +    ID3D11DeviceContext_Draw(rendererData->d3dContext, vertexCount, 0);
 12.2134 +}
 12.2135 +
 12.2136 +static int
 12.2137 +D3D11_RenderDrawPoints(SDL_Renderer * renderer,
 12.2138 +                       const SDL_FPoint * points, int count)
 12.2139 +{
 12.2140 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2141 +    float r, g, b, a;
 12.2142 +
 12.2143 +    r = (float)(renderer->r / 255.0f);
 12.2144 +    g = (float)(renderer->g / 255.0f);
 12.2145 +    b = (float)(renderer->b / 255.0f);
 12.2146 +    a = (float)(renderer->a / 255.0f);
 12.2147 +
 12.2148 +    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
 12.2149 +    for (int i = 0; i < min(count, 128); ++i) {
 12.2150 +        const VertexPositionColor v = { { points[i].x, points[i].y, 0.0f }, { 0.0f, 0.0f }, { r, g, b, a } };
 12.2151 +        vertices[i] = v;
 12.2152 +    }
 12.2153 +
 12.2154 +    D3D11_RenderStartDrawOp(renderer);
 12.2155 +    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 12.2156 +    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
 12.2157 +        SDL_stack_free(vertices);
 12.2158 +        return -1;
 12.2159 +    }
 12.2160 +
 12.2161 +    D3D11_SetPixelShader(
 12.2162 +        renderer,
 12.2163 +        rendererData->colorPixelShader,
 12.2164 +        NULL,
 12.2165 +        NULL);
 12.2166 +
 12.2167 +    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, count);
 12.2168 +    SDL_stack_free(vertices);
 12.2169 +    return 0;
 12.2170 +}
 12.2171 +
 12.2172 +static int
 12.2173 +D3D11_RenderDrawLines(SDL_Renderer * renderer,
 12.2174 +                      const SDL_FPoint * points, int count)
 12.2175 +{
 12.2176 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2177 +    float r, g, b, a;
 12.2178 +
 12.2179 +    r = (float)(renderer->r / 255.0f);
 12.2180 +    g = (float)(renderer->g / 255.0f);
 12.2181 +    b = (float)(renderer->b / 255.0f);
 12.2182 +    a = (float)(renderer->a / 255.0f);
 12.2183 +
 12.2184 +    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
 12.2185 +    for (int i = 0; i < count; ++i) {
 12.2186 +        const VertexPositionColor v = { { points[i].x, points[i].y, 0.0f }, { 0.0f, 0.0f }, { r, g, b, a } };
 12.2187 +        vertices[i] = v;
 12.2188 +    }
 12.2189 +
 12.2190 +    D3D11_RenderStartDrawOp(renderer);
 12.2191 +    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 12.2192 +    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
 12.2193 +        SDL_stack_free(vertices);
 12.2194 +        return -1;
 12.2195 +    }
 12.2196 +
 12.2197 +    D3D11_SetPixelShader(
 12.2198 +        renderer,
 12.2199 +        rendererData->colorPixelShader,
 12.2200 +        NULL,
 12.2201 +        NULL);
 12.2202 +
 12.2203 +    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, count);
 12.2204 +    SDL_stack_free(vertices);
 12.2205 +    return 0;
 12.2206 +}
 12.2207 +
 12.2208 +static int
 12.2209 +D3D11_RenderFillRects(SDL_Renderer * renderer,
 12.2210 +                      const SDL_FRect * rects, int count)
 12.2211 +{
 12.2212 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2213 +    float r, g, b, a;
 12.2214 +
 12.2215 +    r = (float)(renderer->r / 255.0f);
 12.2216 +    g = (float)(renderer->g / 255.0f);
 12.2217 +    b = (float)(renderer->b / 255.0f);
 12.2218 +    a = (float)(renderer->a / 255.0f);
 12.2219 +
 12.2220 +    for (int i = 0; i < count; ++i) {
 12.2221 +        D3D11_RenderStartDrawOp(renderer);
 12.2222 +        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 12.2223 +
 12.2224 +        VertexPositionColor vertices[] = {
 12.2225 +            { { rects[i].x, rects[i].y, 0.0f },                             { 0.0f, 0.0f}, {r, g, b, a} },
 12.2226 +            { { rects[i].x, rects[i].y + rects[i].h, 0.0f },                { 0.0f, 0.0f }, { r, g, b, a } },
 12.2227 +            { { rects[i].x + rects[i].w, rects[i].y, 0.0f },                { 0.0f, 0.0f }, { r, g, b, a } },
 12.2228 +            { { rects[i].x + rects[i].w, rects[i].y + rects[i].h, 0.0f },   { 0.0f, 0.0f }, { r, g, b, a } },
 12.2229 +        };
 12.2230 +        if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 12.2231 +            return -1;
 12.2232 +        }
 12.2233 +
 12.2234 +        D3D11_SetPixelShader(
 12.2235 +            renderer,
 12.2236 +            rendererData->colorPixelShader,
 12.2237 +            NULL,
 12.2238 +            NULL);
 12.2239 +
 12.2240 +        D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, SDL_arraysize(vertices));
 12.2241 +    }
 12.2242 +
 12.2243 +    return 0;
 12.2244 +}
 12.2245 +
 12.2246 +static ID3D11SamplerState *
 12.2247 +D3D11_RenderGetSampler(SDL_Renderer * renderer, SDL_Texture * texture)
 12.2248 +{
 12.2249 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2250 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.2251 +
 12.2252 +    switch (textureData->scaleMode) {
 12.2253 +    case D3D11_FILTER_MIN_MAG_MIP_POINT:
 12.2254 +        return rendererData->nearestPixelSampler;
 12.2255 +    case D3D11_FILTER_MIN_MAG_MIP_LINEAR:
 12.2256 +        return rendererData->linearSampler;
 12.2257 +    default:
 12.2258 +        return NULL;
 12.2259 +    }
 12.2260 +}
 12.2261 +
 12.2262 +static int
 12.2263 +D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
 12.2264 +                 const SDL_Rect * srcrect, const SDL_FRect * dstrect)
 12.2265 +{
 12.2266 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2267 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.2268 +
 12.2269 +    D3D11_RenderStartDrawOp(renderer);
 12.2270 +    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
 12.2271 +
 12.2272 +    float minu = (float) srcrect->x / texture->w;
 12.2273 +    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
 12.2274 +    float minv = (float) srcrect->y / texture->h;
 12.2275 +    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
 12.2276 +
 12.2277 +    float r = 1.0f;
 12.2278 +    float g = 1.0f;
 12.2279 +    float b = 1.0f;
 12.2280 +    float a = 1.0f;
 12.2281 +    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
 12.2282 +        r = (float)(texture->r / 255.0f);
 12.2283 +        g = (float)(texture->g / 255.0f);
 12.2284 +        b = (float)(texture->b / 255.0f);
 12.2285 +    }
 12.2286 +    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
 12.2287 +        a = (float)(texture->a / 255.0f);
 12.2288 +    }
 12.2289 +
 12.2290 +    VertexPositionColor vertices[] = {
 12.2291 +        { { dstrect->x, dstrect->y, 0.0f },                             { minu, minv }, { r, g, b, a } },
 12.2292 +        { { dstrect->x, dstrect->y + dstrect->h, 0.0f },                { minu, maxv }, { r, g, b, a } },
 12.2293 +        { { dstrect->x + dstrect->w, dstrect->y, 0.0f },                { maxu, minv }, { r, g, b, a } },
 12.2294 +        { { dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f },   { maxu, maxv }, { r, g, b, a } },
 12.2295 +    };
 12.2296 +    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 12.2297 +        return -1;
 12.2298 +    }
 12.2299 +
 12.2300 +    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
 12.2301 +    D3D11_SetPixelShader(
 12.2302 +        renderer,
 12.2303 +        rendererData->texturePixelShader,
 12.2304 +        textureData->mainTextureResourceView,
 12.2305 +        textureSampler);
 12.2306 +
 12.2307 +    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
 12.2308 +
 12.2309 +    return 0;
 12.2310 +}
 12.2311 +
 12.2312 +static int
 12.2313 +D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
 12.2314 +                   const SDL_Rect * srcrect, const SDL_FRect * dstrect,
 12.2315 +                   const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
 12.2316 +{
 12.2317 +    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 12.2318 +    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 12.2319 +
 12.2320 +    D3D11_RenderStartDrawOp(renderer);
 12.2321 +    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
 12.2322 +
 12.2323 +    float minu = (float) srcrect->x / texture->w;
 12.2324 +    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
 12.2325 +    float minv = (float) srcrect->y / texture->h;
 12.2326 +    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
 12.2327 +
 12.2328 +    float r = 1.0f;
 12.2329 +    float g = 1.0f;
 12.2330 +    float b = 1.0f;
 12.2331 +    float a = 1.0f;
 12.2332 +    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
 12.2333 +        r = (float)(texture->r / 255.0f);
 12.2334 +        g = (float)(texture->g / 255.0f);
 12.2335 +        b = (float)(texture->b / 255.0f);
 12.2336 +    }
 12.2337 +    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
 12.2338 +        a = (float)(texture->a / 255.0f);
 12.2339 +    }
 12.2340 +
 12.2341 +    if (flip & SDL_FLIP_HORIZONTAL) {
 12.2342 +        float tmp = maxu;
 12.2343 +        maxu = minu;
 12.2344 +        minu = tmp;
 12.2345 +    }
 12.2346 +    if (flip & SDL_FLIP_VERTICAL) {
 12.2347 +        float tmp = maxv;
 12.2348 +        maxv = minv;
 12.2349 +        minv = tmp;
 12.2350 +    }
 12.2351 +
 12.2352 +    Float4X4 oldModelMatrix = rendererData->vertexShaderConstantsData.model;
 12.2353 +    Float4X4 newModelMatrix = MatrixMultiply(
 12.2354 +            MatrixRotationZ((float)(M_PI * (float) angle / 180.0f)),
 12.2355 +            MatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
 12.2356 +            );
 12.2357 +    D3D11_SetModelMatrix(renderer, &newModelMatrix);
 12.2358 +
 12.2359 +    const float minx = -center->x;
 12.2360 +    const float maxx = dstrect->w - center->x;
 12.2361 +    const float miny = -center->y;
 12.2362 +    const float maxy = dstrect->h - center->y;
 12.2363 +
 12.2364 +    VertexPositionColor vertices[] = {
 12.2365 +        {{minx, miny, 0.0f}, {minu, minv}, {r, g, b, a}},
 12.2366 +        {{minx, maxy, 0.0f}, {minu, maxv}, {r, g, b, a}},
 12.2367 +        {{maxx, miny, 0.0f}, {maxu, minv}, {r, g, b, a}},
 12.2368 +        {{maxx, maxy, 0.0f}, {maxu, maxv}, {r, g, b, a}},
 12.2369 +    };
 12.2370 +    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 12.2371 +        return -1;
 12.2372 +    }
 12.2373 +
 12.2374 +    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
 12.2375 +    D3D11_SetPixelShader(
 12.2376 +        renderer,
 12.2377 +        rendererData->texturePixelShader,
 12.2378 +        textureData->mainTextureResourceView,
 12.2379 +        textureSampler);
 12.2380 +
 12.2381 +    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
 12.2382 +
 12.2383 +    D3D11_SetModelMatrix(renderer, &oldModelMatrix);
 12.2384 +
 12.2385 +    return 0;
 12.2386 +}
 12.2387 +
 12.2388 +static int
 12.2389 +D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 12.2390 +                       Uint32 format, void * pixels, int pitch)
 12.2391 +{
 12.2392 +    D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
 12.2393 +    ID3D11Texture2D *backBuffer = NULL;
 12.2394 +    ID3D11Texture2D *stagingTexture = NULL;
 12.2395 +    HRESULT result;
 12.2396 +    int status = -1;
 12.2397 +
 12.2398 +    /* Retrieve a pointer to the back buffer: */
 12.2399 +    result = IDXGISwapChain_GetBuffer(data->swapChain,
 12.2400 +        0,
 12.2401 +        &IID_ID3D11Texture2D,
 12.2402 +        &backBuffer
 12.2403 +        );
 12.2404 +    if (FAILED(result)) {
 12.2405 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::GetBuffer [get back buffer]", result);
 12.2406 +        goto done;
 12.2407 +    }
 12.2408 +
 12.2409 +    /* Create a staging texture to copy the screen's data to: */
 12.2410 +    D3D11_TEXTURE2D_DESC stagingTextureDesc;
 12.2411 +    ID3D11Texture2D_GetDesc(backBuffer, &stagingTextureDesc);
 12.2412 +    stagingTextureDesc.Width = rect->w;
 12.2413 +    stagingTextureDesc.Height = rect->h;
 12.2414 +    stagingTextureDesc.BindFlags = 0;
 12.2415 +    stagingTextureDesc.MiscFlags = 0;
 12.2416 +    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
 12.2417 +    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
 12.2418 +    result = ID3D11Device_CreateTexture2D(data->d3dDevice,
 12.2419 +        &stagingTextureDesc,
 12.2420 +        NULL,
 12.2421 +        &stagingTexture);
 12.2422 +    if (FAILED(result)) {
 12.2423 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
 12.2424 +        goto done;
 12.2425 +    }
 12.2426 +
 12.2427 +    /* Copy the desired portion of the back buffer to the staging texture: */
 12.2428 +    D3D11_RECT srcRect;
 12.2429 +    if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
 12.2430 +        /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
 12.2431 +        goto done;
 12.2432 +    }
 12.2433 +
 12.2434 +    D3D11_BOX srcBox;
 12.2435 +    srcBox.left = srcRect.left;
 12.2436 +    srcBox.right = srcRect.right;
 12.2437 +    srcBox.top = srcRect.top;
 12.2438 +    srcBox.bottom = srcRect.bottom;
 12.2439 +    srcBox.front = 0;
 12.2440 +    srcBox.back = 1;
 12.2441 +    ID3D11DeviceContext_CopySubresourceRegion(data->d3dContext,
 12.2442 +        (ID3D11Resource *)stagingTexture,
 12.2443 +        0,
 12.2444 +        0, 0, 0,
 12.2445 +        (ID3D11Resource *)backBuffer,
 12.2446 +        0,
 12.2447 +        &srcBox);
 12.2448 +
 12.2449 +    /* Map the staging texture's data to CPU-accessible memory: */
 12.2450 +    D3D11_MAPPED_SUBRESOURCE textureMemory;
 12.2451 +    result = ID3D11DeviceContext_Map(data->d3dContext,
 12.2452 +        (ID3D11Resource *)stagingTexture,
 12.2453 +        0,
 12.2454 +        D3D11_MAP_READ,
 12.2455 +        0,
 12.2456 +        &textureMemory);
 12.2457 +    if (FAILED(result)) {
 12.2458 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
 12.2459 +        goto done;
 12.2460 +    }
 12.2461 +
 12.2462 +    /* Copy the data into the desired buffer, converting pixels to the
 12.2463 +     * desired format at the same time:
 12.2464 +     */
 12.2465 +    if (SDL_ConvertPixels(
 12.2466 +        rect->w, rect->h,
 12.2467 +        DXGIFormatToSDLPixelFormat(stagingTextureDesc.Format),
 12.2468 +        textureMemory.pData,
 12.2469 +        textureMemory.RowPitch,
 12.2470 +        format,
 12.2471 +        pixels,
 12.2472 +        pitch) != 0)
 12.2473 +    {
 12.2474 +        /* When SDL_ConvertPixels fails, it'll have already set the format.
 12.2475 +         * Get the error message, and attach some extra data to it.
 12.2476 +         */
 12.2477 +        char errorMessage[1024];
 12.2478 +        SDL_snprintf(errorMessage, sizeof(errorMessage), __FUNCTION__ ", Convert Pixels failed: %s", SDL_GetError());
 12.2479 +        SDL_SetError(errorMessage);
 12.2480 +        goto done;
 12.2481 +    }
 12.2482 +
 12.2483 +    /* Unmap the texture: */
 12.2484 +    ID3D11DeviceContext_Unmap(data->d3dContext,
 12.2485 +        (ID3D11Resource *)stagingTexture,
 12.2486 +        0);
 12.2487 +
 12.2488 +    status = 0;
 12.2489 +
 12.2490 +done:
 12.2491 +    SAFE_RELEASE(backBuffer);
 12.2492 +    SAFE_RELEASE(stagingTexture);
 12.2493 +    return status;
 12.2494 +}
 12.2495 +
 12.2496 +static void
 12.2497 +D3D11_RenderPresent(SDL_Renderer * renderer)
 12.2498 +{
 12.2499 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 12.2500 +    UINT syncInterval;
 12.2501 +    UINT presentFlags;
 12.2502 +
 12.2503 +    if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
 12.2504 +        syncInterval = 1;
 12.2505 +        presentFlags = 0;
 12.2506 +    } else {
 12.2507 +        syncInterval = 0;
 12.2508 +        presentFlags = DXGI_PRESENT_DO_NOT_WAIT;
 12.2509 +    }
 12.2510 +
 12.2511 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 12.2512 +    HRESULT result = IDXGISwapChain_Present(data->swapChain, syncInterval, presentFlags);
 12.2513 +#else
 12.2514 +    /* The application may optionally specify "dirty" or "scroll"
 12.2515 +     * rects to improve efficiency in certain scenarios.
 12.2516 +     * This option is not available on Windows Phone 8, to note.
 12.2517 +     */
 12.2518 +    DXGI_PRESENT_PARAMETERS parameters;
 12.2519 +    SDL_zero(parameters);
 12.2520 +    HRESULT result = IDXGISwapChain1_Present1(data->swapChain, syncInterval, presentFlags, &parameters);
 12.2521 +#endif
 12.2522 +
 12.2523 +    /* Discard the contents of the render target.
 12.2524 +     * This is a valid operation only when the existing contents will be entirely
 12.2525 +     * overwritten. If dirty or scroll rects are used, this call should be removed.
 12.2526 +     */
 12.2527 +    ID3D11DeviceContext1_DiscardView(data->d3dContext, (ID3D11View*)data->mainRenderTargetView);
 12.2528 +
 12.2529 +    /* When the present flips, it unbinds the current view, so bind it again on the next draw call */
 12.2530 +    data->currentRenderTargetView = NULL;
 12.2531 +
 12.2532 +    if (FAILED(result) && result != DXGI_ERROR_WAS_STILL_DRAWING) {
 12.2533 +        /* If the device was removed either by a disconnect or a driver upgrade, we 
 12.2534 +         * must recreate all device resources.
 12.2535 +         *
 12.2536 +         * TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvage debug info from users' machines
 12.2537 +         */
 12.2538 +        if (result == DXGI_ERROR_DEVICE_REMOVED) {
 12.2539 +            D3D11_HandleDeviceLost(renderer);
 12.2540 +        } else {
 12.2541 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain::Present", result);
 12.2542 +        }
 12.2543 +    }
 12.2544 +}
 12.2545 +
 12.2546 +#endif /* SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED */
 12.2547 +
 12.2548 +/* vi: set ts=4 sw=4 expandtab: */
    13.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Sun Mar 09 22:48:38 2014 -0700
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,2386 +0,0 @@
    13.4 -/*
    13.5 -  Simple DirectMedia Layer
    13.6 -  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    13.7 -
    13.8 -  This software is provided 'as-is', without any express or implied
    13.9 -  warranty.  In no event will the authors be held liable for any damages
   13.10 -  arising from the use of this software.
   13.11 -
   13.12 -  Permission is granted to anyone to use this software for any purpose,
   13.13 -  including commercial applications, and to alter it and redistribute it
   13.14 -  freely, subject to the following restrictions:
   13.15 -
   13.16 -  1. The origin of this software must not be misrepresented; you must not
   13.17 -     claim that you wrote the original software. If you use this software
   13.18 -     in a product, an acknowledgment in the product documentation would be
   13.19 -     appreciated but is not required.
   13.20 -  2. Altered source versions must be plainly marked as such, and must not be
   13.21 -     misrepresented as being the original software.
   13.22 -  3. This notice may not be removed or altered from any source distribution.
   13.23 -*/
   13.24 -
   13.25 -#include "SDL_config.h"
   13.26 -
   13.27 -#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
   13.28 -
   13.29 -#ifdef __WINRT__
   13.30 -#include <windows.ui.core.h>
   13.31 -#include <windows.foundation.h>
   13.32 -
   13.33 -#if WINAPI_FAMILY == WINAPI_FAMILY_APP
   13.34 -#include <windows.ui.xaml.media.dxinterop.h>
   13.35 -#endif
   13.36 -
   13.37 -#endif
   13.38 -
   13.39 -extern "C" {
   13.40 -#include "../../core/windows/SDL_windows.h"
   13.41 -#include "SDL_hints.h"
   13.42 -#include "SDL_system.h"
   13.43 -#include "SDL_syswm.h"
   13.44 -#include "../SDL_sysrender.h"
   13.45 -#include "../../video/SDL_sysvideo.h"
   13.46 -}
   13.47 -
   13.48 -#include <string>
   13.49 -#include <vector>
   13.50 -
   13.51 -#include <D3D11_1.h>
   13.52 -#include <DirectXMath.h>
   13.53 -#include <wrl/client.h>
   13.54 -
   13.55 -
   13.56 -using namespace DirectX;
   13.57 -using namespace Microsoft::WRL;
   13.58 -using namespace std;
   13.59 -
   13.60 -#ifdef __WINRT__
   13.61 -using namespace Windows::Graphics::Display;
   13.62 -using namespace Windows::UI::Core;
   13.63 -#endif
   13.64 -
   13.65 -/* Texture sampling types */
   13.66 -static const D3D11_FILTER SDL_D3D11_NEAREST_PIXEL_FILTER = D3D11_FILTER_MIN_MAG_MIP_POINT;
   13.67 -static const D3D11_FILTER SDL_D3D11_LINEAR_FILTER = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
   13.68 -
   13.69 -/* Vertex shader, common values */
   13.70 -struct VertexShaderConstants
   13.71 -{
   13.72 -    DirectX::XMFLOAT4X4 model;
   13.73 -    DirectX::XMFLOAT4X4 projectionAndView;
   13.74 -};
   13.75 -
   13.76 -/* Per-vertex data */
   13.77 -struct VertexPositionColor
   13.78 -{
   13.79 -    DirectX::XMFLOAT3 pos;
   13.80 -    DirectX::XMFLOAT2 tex;
   13.81 -    DirectX::XMFLOAT4 color;
   13.82 -};
   13.83 -
   13.84 -/* Per-texture data */
   13.85 -typedef struct
   13.86 -{
   13.87 -    Microsoft::WRL::ComPtr<ID3D11Texture2D> mainTexture;
   13.88 -    Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> mainTextureResourceView;
   13.89 -    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainTextureRenderTargetView;
   13.90 -    SDL_PixelFormat * pixelFormat;
   13.91 -    Microsoft::WRL::ComPtr<ID3D11Texture2D> stagingTexture;
   13.92 -    DirectX::XMINT2 lockedTexturePosition;
   13.93 -    D3D11_FILTER scaleMode;
   13.94 -} D3D11_TextureData;
   13.95 -
   13.96 -/* Private renderer data */
   13.97 -typedef struct
   13.98 -{
   13.99 -    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice;
  13.100 -    Microsoft::WRL::ComPtr<ID3D11DeviceContext1> d3dContext;
  13.101 -    Microsoft::WRL::ComPtr<IDXGISwapChain1> swapChain;
  13.102 -    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainRenderTargetView;
  13.103 -    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> currentOffscreenRenderTargetView;
  13.104 -    Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout;
  13.105 -    Microsoft::WRL::ComPtr<ID3D11Buffer> vertexBuffer;
  13.106 -    Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
  13.107 -    Microsoft::WRL::ComPtr<ID3D11PixelShader> texturePixelShader;
  13.108 -    Microsoft::WRL::ComPtr<ID3D11PixelShader> colorPixelShader;
  13.109 -    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend;
  13.110 -    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd;
  13.111 -    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod;
  13.112 -    Microsoft::WRL::ComPtr<ID3D11SamplerState> nearestPixelSampler;
  13.113 -    Microsoft::WRL::ComPtr<ID3D11SamplerState> linearSampler;
  13.114 -    D3D_FEATURE_LEVEL featureLevel;
  13.115 -
  13.116 -    // Rasterizers:
  13.117 -    // If this list starts to get unwieldy, then consider using a map<> of them.
  13.118 -    Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
  13.119 -    Microsoft::WRL::ComPtr<ID3D11RasterizerState> clippedRasterizer;
  13.120 -
  13.121 -    // Vertex buffer constants:
  13.122 -    VertexShaderConstants vertexShaderConstantsData;
  13.123 -    Microsoft::WRL::ComPtr<ID3D11Buffer> vertexShaderConstants;
  13.124 -
  13.125 -    // Cached renderer properties.
  13.126 -    DirectX::XMFLOAT2 windowSizeInDIPs;
  13.127 -    DirectX::XMFLOAT2 renderTargetSize;
  13.128 -    Windows::Graphics::Display::DisplayOrientations orientation;
  13.129 -
  13.130 -    // Transform used for display orientation.
  13.131 -    DirectX::XMFLOAT4X4 orientationTransform3D;
  13.132 -} D3D11_RenderData;
  13.133 -
  13.134 -
  13.135 -/* Direct3D 11.x shaders
  13.136 -
  13.137 -   SDL's shaders are compiled into SDL itself, to simplify distribution.
  13.138 -
  13.139 -   All Direct3D 11.x shaders were compiled with the following:
  13.140 -
  13.141 -   fxc /E"main" /T "<TYPE>" /Fo"<OUTPUT FILE>" "<INPUT FILE>"
  13.142 -
  13.143 -     Variables:
  13.144 -     - <TYPE>: the type of shader.  A table of utilized shader types is
  13.145 -       listed below.
  13.146 -     - <OUTPUT FILE>: where to store compiled output
  13.147 -     - <INPUT FILE>: where to read shader source code from
  13.148 -
  13.149 -     Shader types:
  13.150 -     - ps_4_0_level_9_1: Pixel shader for Windows 8+, including Windows RT
  13.151 -     - vs_4_0_level_9_1: Vertex shader for Windows 8+, including Windows RT
  13.152 -     - ps_4_0_level_9_3: Pixel shader for Windows Phone 8
  13.153 -     - vs_4_0_level_9_3: Vertex shader for Windows Phone 8
  13.154 -   
  13.155 -
  13.156 -   Shader object code was converted to a list of DWORDs via the following
  13.157 -   *nix style command (available separately from Windows + MSVC):
  13.158 -
  13.159 -     hexdump -v -e '6/4 "0x%08.8x, " "\n"' <FILE>
  13.160 -  */
  13.161 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  13.162 -#define D3D11_USE_SHADER_MODEL_4_0_level_9_3
  13.163 -#else
  13.164 -#define D3D11_USE_SHADER_MODEL_4_0_level_9_1
  13.165 -#endif
  13.166 -
  13.167 -/* The texture-rendering pixel shader:
  13.168 -
  13.169 -    --- D3D11_PixelShader_Textures.hlsl ---
  13.170 -    Texture2D theTexture : register(t0);
  13.171 -    SamplerState theSampler : register(s0);
  13.172 -
  13.173 -    struct PixelShaderInput
  13.174 -    {
  13.175 -        float4 pos : SV_POSITION;
  13.176 -        float2 tex : TEXCOORD0;
  13.177 -        float4 color : COLOR0;
  13.178 -    };
  13.179 -
  13.180 -    float4 main(PixelShaderInput input) : SV_TARGET
  13.181 -    {
  13.182 -        return theTexture.Sample(theSampler, input.tex) * input.color;
  13.183 -    }
  13.184 -*/
  13.185 -#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  13.186 -static const DWORD D3D11_PixelShader_Textures[] = {
  13.187 -    0x43425844, 0x6299b59f, 0x155258f2, 0x873ab86a, 0xfcbb6dcd, 0x00000001,
  13.188 -    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
  13.189 -    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
  13.190 -    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
  13.191 -    0x00280000, 0x00000000, 0xffff0200, 0x0200001f, 0x80000000, 0xb0030000,
  13.192 -    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
  13.193 -    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
  13.194 -    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
  13.195 -    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
  13.196 -    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
  13.197 -    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
  13.198 -    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
  13.199 -    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
  13.200 -    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
  13.201 -    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
  13.202 -    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
  13.203 -    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.204 -    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.205 -    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.206 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
  13.207 -    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
  13.208 -    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
  13.209 -    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
  13.210 -    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
  13.211 -    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
  13.212 -    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
  13.213 -    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
  13.214 -    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  13.215 -    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  13.216 -    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
  13.217 -    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  13.218 -    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  13.219 -    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  13.220 -    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  13.221 -};
  13.222 -#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  13.223 -static const DWORD D3D11_PixelShader_Textures[] = {
  13.224 -    0x43425844, 0x5876569a, 0x01b6c87e, 0x8447454f, 0xc7f3ef10, 0x00000001,
  13.225 -    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
  13.226 -    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
  13.227 -    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
  13.228 -    0x00280000, 0x00000000, 0xffff0201, 0x0200001f, 0x80000000, 0xb0030000,
  13.229 -    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
  13.230 -    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
  13.231 -    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
  13.232 -    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
  13.233 -    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
  13.234 -    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
  13.235 -    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
  13.236 -    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
  13.237 -    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
  13.238 -    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
  13.239 -    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
  13.240 -    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.241 -    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.242 -    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.243 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
  13.244 -    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
  13.245 -    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
  13.246 -    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
  13.247 -    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
  13.248 -    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
  13.249 -    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
  13.250 -    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
  13.251 -    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  13.252 -    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  13.253 -    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
  13.254 -    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  13.255 -    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  13.256 -    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  13.257 -    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  13.258 -};
  13.259 -#else
  13.260 -#error "An appropriate 'textures' pixel shader is not defined"
  13.261 -#endif
  13.262 -
  13.263 -/* The color-only-rendering pixel shader:
  13.264 -
  13.265 -   --- D3D11_PixelShader_Colors.hlsl ---
  13.266 -   struct PixelShaderInput
  13.267 -   {
  13.268 -       float4 pos : SV_POSITION;
  13.269 -       float2 tex : TEXCOORD0;
  13.270 -       float4 color : COLOR0;
  13.271 -   };
  13.272 -
  13.273 -   float4 main(PixelShaderInput input) : SV_TARGET
  13.274 -   {
  13.275 -       return input.color;
  13.276 -   }
  13.277 -*/
  13.278 -#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  13.279 -static const DWORD D3D11_PixelShader_Colors[] = {
  13.280 -    0x43425844, 0xd74c28fe, 0xa1eb8804, 0x269d512a, 0x7699723d, 0x00000001,
  13.281 -    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
  13.282 -    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
  13.283 -    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
  13.284 -    0x00240000, 0xffff0200, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
  13.285 -    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
  13.286 -    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
  13.287 -    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
  13.288 -    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
  13.289 -    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  13.290 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.291 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
  13.292 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.293 -    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
  13.294 -    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
  13.295 -    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
  13.296 -    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
  13.297 -    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  13.298 -    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  13.299 -    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
  13.300 -    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  13.301 -    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  13.302 -    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  13.303 -    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  13.304 -};
  13.305 -#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  13.306 -static const DWORD D3D11_PixelShader_Colors[] = {
  13.307 -    0x43425844, 0x93f6ccfc, 0x5f919270, 0x7a11aa4f, 0x9148e931, 0x00000001,
  13.308 -    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
  13.309 -    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
  13.310 -    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
  13.311 -    0x00240000, 0xffff0201, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
  13.312 -    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
  13.313 -    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
  13.314 -    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
  13.315 -    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
  13.316 -    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  13.317 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.318 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
  13.319 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.320 -    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
  13.321 -    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
  13.322 -    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
  13.323 -    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
  13.324 -    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
  13.325 -    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
  13.326 -    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
  13.327 -    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
  13.328 -    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
  13.329 -    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
  13.330 -    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
  13.331 -};
  13.332 -#else
  13.333 -#error "An appropriate 'colors' pixel shader is not defined."
  13.334 -#endif
  13.335 -
  13.336 -/* The sole vertex shader:
  13.337 -
  13.338 -   --- D3D11_VertexShader.hlsl ---
  13.339 -   #pragma pack_matrix( row_major )
  13.340 -
  13.341 -   cbuffer VertexShaderConstants : register(b0)
  13.342 -   {
  13.343 -       matrix model;
  13.344 -       matrix projectionAndView;
  13.345 -   };
  13.346 -
  13.347 -   struct VertexShaderInput
  13.348 -   {
  13.349 -       float3 pos : POSITION;
  13.350 -       float2 tex : TEXCOORD0;
  13.351 -       float4 color : COLOR0;
  13.352 -   };
  13.353 -
  13.354 -   struct VertexShaderOutput
  13.355 -   {
  13.356 -       float4 pos : SV_POSITION;
  13.357 -       float2 tex : TEXCOORD0;
  13.358 -       float4 color : COLOR0;
  13.359 -   };
  13.360 -
  13.361 -   VertexShaderOutput main(VertexShaderInput input)
  13.362 -   {
  13.363 -       VertexShaderOutput output;
  13.364 -       float4 pos = float4(input.pos, 1.0f);
  13.365 -
  13.366 -       // Transform the vertex position into projected space.
  13.367 -       pos = mul(pos, model);
  13.368 -       pos = mul(pos, projectionAndView);
  13.369 -       output.pos = pos;
  13.370 -
  13.371 -       // Pass through texture coordinates and color values without transformation
  13.372 -       output.tex = input.tex;
  13.373 -       output.color = input.color;
  13.374 -
  13.375 -       return output;
  13.376 -   }
  13.377 -*/
  13.378 -#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
  13.379 -static const DWORD D3D11_VertexShader[] = {
  13.380 -    0x43425844, 0x62dfae5f, 0x3e8bd8df, 0x9ec97127, 0x5044eefb, 0x00000001,
  13.381 -    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
  13.382 -    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
  13.383 -    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
  13.384 -    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0200,
  13.385 -    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
  13.386 -    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
  13.387 -    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
  13.388 -    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
  13.389 -    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
  13.390 -    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
  13.391 -    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
  13.392 -    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
  13.393 -    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
  13.394 -    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
  13.395 -    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
  13.396 -    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
  13.397 -    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
  13.398 -    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
  13.399 -    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
  13.400 -    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
  13.401 -    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
  13.402 -    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
  13.403 -    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
  13.404 -    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
  13.405 -    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
  13.406 -    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
  13.407 -    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
  13.408 -    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
  13.409 -    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
  13.410 -    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
  13.411 -    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
  13.412 -    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
  13.413 -    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
  13.414 -    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
  13.415 -    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  13.416 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.417 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
  13.418 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.419 -    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
  13.420 -    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
  13.421 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
  13.422 -    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
  13.423 -    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
  13.424 -    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
  13.425 -    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
  13.426 -    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
  13.427 -    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
  13.428 -    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
  13.429 -    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
  13.430 -    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
  13.431 -    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
  13.432 -    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
  13.433 -    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
  13.434 -    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
  13.435 -    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
  13.436 -    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
  13.437 -    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
  13.438 -    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
  13.439 -    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
  13.440 -};
  13.441 -#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
  13.442 -static const DWORD D3D11_VertexShader[] = {
  13.443 -    0x43425844, 0x01a24e41, 0x696af551, 0x4b2a87d1, 0x82ea03f6, 0x00000001,
  13.444 -    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
  13.445 -    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
  13.446 -    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
  13.447 -    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0201,
  13.448 -    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
  13.449 -    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
  13.450 -    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
  13.451 -    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
  13.452 -    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
  13.453 -    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
  13.454 -    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
  13.455 -    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
  13.456 -    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
  13.457 -    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
  13.458 -    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
  13.459 -    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
  13.460 -    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
  13.461 -    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
  13.462 -    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
  13.463 -    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
  13.464 -    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
  13.465 -    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
  13.466 -    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
  13.467 -    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
  13.468 -    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
  13.469 -    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
  13.470 -    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
  13.471 -    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
  13.472 -    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
  13.473 -    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
  13.474 -    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
  13.475 -    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
  13.476 -    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
  13.477 -    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
  13.478 -    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
  13.479 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.480 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
  13.481 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  13.482 -    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
  13.483 -    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
  13.484 -    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
  13.485 -    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
  13.486 -    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
  13.487 -    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
  13.488 -    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
  13.489 -    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
  13.490 -    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
  13.491 -    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
  13.492 -    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
  13.493 -    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
  13.494 -    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
  13.495 -    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
  13.496 -    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
  13.497 -    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
  13.498 -    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
  13.499 -    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
  13.500 -    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
  13.501 -    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
  13.502 -    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
  13.503 -};
  13.504 -#else
  13.505 -#error "An appropriate vertex shader is not defined."
  13.506 -#endif
  13.507 -
  13.508 -/* Direct3D 11.1 renderer implementation */
  13.509 -static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
  13.510 -static void D3D11_WindowEvent(SDL_Renderer * renderer,
  13.511 -                            const SDL_WindowEvent *event);
  13.512 -static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  13.513 -static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  13.514 -                             const SDL_Rect * rect, const void *srcPixels,
  13.515 -                             int srcPitch);
  13.516 -static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  13.517 -                             const SDL_Rect * rect, void **pixels, int *pitch);
  13.518 -static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  13.519 -static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
  13.520 -static int D3D11_UpdateViewport(SDL_Renderer * renderer);
  13.521 -static int D3D11_UpdateClipRect(SDL_Renderer * renderer);
  13.522 -static int D3D11_RenderClear(SDL_Renderer * renderer);
  13.523 -static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
  13.524 -                                  const SDL_FPoint * points, int count);
  13.525 -static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
  13.526 -                                 const SDL_FPoint * points, int count);
  13.527 -static int D3D11_RenderFillRects(SDL_Renderer * renderer,
  13.528 -                                 const SDL_FRect * rects, int count);
  13.529 -static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  13.530 -                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
  13.531 -static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  13.532 -                              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  13.533 -                              const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
  13.534 -static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  13.535 -                                  Uint32 format, void * pixels, int pitch);
  13.536 -static void D3D11_RenderPresent(SDL_Renderer * renderer);
  13.537 -static void D3D11_DestroyTexture(SDL_Renderer * renderer,
  13.538 -                                 SDL_Texture * texture);
  13.539 -static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
  13.540 -
  13.541 -/* Direct3D 11.1 Internal Functions */
  13.542 -HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
  13.543 -HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
  13.544 -HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
  13.545 -HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
  13.546 -
  13.547 -extern "C" SDL_RenderDriver D3D11_RenderDriver = {
  13.548 -    D3D11_CreateRenderer,
  13.549 -    {
  13.550 -        "direct3d 11.1",
  13.551 -        (
  13.552 -            SDL_RENDERER_ACCELERATED |
  13.553 -            SDL_RENDERER_PRESENTVSYNC |
  13.554 -            SDL_RENDERER_TARGETTEXTURE
  13.555 -        ),                          // flags.  see SDL_RendererFlags
  13.556 -        2,                          // num_texture_formats
  13.557 -        {                           // texture_formats
  13.558 -            SDL_PIXELFORMAT_RGB888,
  13.559 -            SDL_PIXELFORMAT_ARGB8888
  13.560 -        },
  13.561 -        0,                          // max_texture_width: will be filled in later
  13.562 -        0                           // max_texture_height: will be filled in later
  13.563 -    }
  13.564 -};
  13.565 -
  13.566 -
  13.567 -static Uint32
  13.568 -DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat) {
  13.569 -    switch (dxgiFormat) {
  13.570 -        case DXGI_FORMAT_B8G8R8A8_UNORM:
  13.571 -            return SDL_PIXELFORMAT_ARGB8888;
  13.572 -        case DXGI_FORMAT_B8G8R8X8_UNORM:
  13.573 -            return SDL_PIXELFORMAT_RGB888;
  13.574 -        default:
  13.575 -            return SDL_PIXELFORMAT_UNKNOWN;
  13.576 -    }
  13.577 -}
  13.578 -
  13.579 -static DXGI_FORMAT
  13.580 -SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
  13.581 -{
  13.582 -    switch (sdlFormat) {
  13.583 -        case SDL_PIXELFORMAT_ARGB8888:
  13.584 -            return DXGI_FORMAT_B8G8R8A8_UNORM;
  13.585 -        case SDL_PIXELFORMAT_RGB888:
  13.586 -            return DXGI_FORMAT_B8G8R8X8_UNORM;
  13.587 -        default:
  13.588 -            return DXGI_FORMAT_UNKNOWN;
  13.589 -    }
  13.590 -}
  13.591 -
  13.592 -SDL_Renderer *
  13.593 -D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
  13.594 -{
  13.595 -    SDL_Renderer *renderer;
  13.596 -    D3D11_RenderData *data;
  13.597 -
  13.598 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  13.599 -    if (!renderer) {
  13.600 -        SDL_OutOfMemory();
  13.601 -        return NULL;
  13.602 -    }
  13.603 -    SDL_zerop(renderer);
  13.604 -
  13.605 -    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
  13.606 -    if (!data) {
  13.607 -        SDL_OutOfMemory();
  13.608 -        return NULL;
  13.609 -    }
  13.610 -    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
  13.611 -    data->windowSizeInDIPs = XMFLOAT2(0, 0);
  13.612 -    data->renderTargetSize = XMFLOAT2(0, 0);
  13.613 -
  13.614 -    renderer->WindowEvent = D3D11_WindowEvent;
  13.615 -    renderer->CreateTexture = D3D11_CreateTexture;
  13.616 -    renderer->UpdateTexture = D3D11_UpdateTexture;
  13.617 -    renderer->LockTexture = D3D11_LockTexture;
  13.618 -    renderer->UnlockTexture = D3D11_UnlockTexture;
  13.619 -    renderer->SetRenderTarget = D3D11_SetRenderTarget;
  13.620 -    renderer->UpdateViewport = D3D11_UpdateViewport;
  13.621 -    renderer->UpdateClipRect = D3D11_UpdateClipRect;
  13.622 -    renderer->RenderClear = D3D11_RenderClear;
  13.623 -    renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
  13.624 -    renderer->RenderDrawLines = D3D11_RenderDrawLines;
  13.625 -    renderer->RenderFillRects = D3D11_RenderFillRects;
  13.626 -    renderer->RenderCopy = D3D11_RenderCopy;
  13.627 -    renderer->RenderCopyEx = D3D11_RenderCopyEx;
  13.628 -    renderer->RenderReadPixels = D3D11_RenderReadPixels;
  13.629 -    renderer->RenderPresent = D3D11_RenderPresent;
  13.630 -    renderer->DestroyTexture = D3D11_DestroyTexture;
  13.631 -    renderer->DestroyRenderer = D3D11_DestroyRenderer;
  13.632 -    renderer->info = D3D11_RenderDriver.info;
  13.633 -    renderer->driverdata = data;
  13.634 -
  13.635 -    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
  13.636 -    // order to give init functions access to the underlying window handle:
  13.637 -    renderer->window = window;
  13.638 -
  13.639 -    /* Initialize Direct3D resources */
  13.640 -    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
  13.641 -        D3D11_DestroyRenderer(renderer);
  13.642 -        return NULL;
  13.643 -    }
  13.644 -    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
  13.645 -        D3D11_DestroyRenderer(renderer);
  13.646 -        return NULL;
  13.647 -    }
  13.648 -
  13.649 -    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
  13.650 -
  13.651 -    return renderer;
  13.652 -}
  13.653 -
  13.654 -static void
  13.655 -D3D11_DestroyRenderer(SDL_Renderer * renderer)
  13.656 -{
  13.657 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  13.658 -    if (data) {
  13.659 -        delete data;
  13.660 -        data = NULL;
  13.661 -    }
  13.662 -}
  13.663 -
  13.664 -static HRESULT
  13.665 -D3D11_CreateBlendMode(SDL_Renderer * renderer,
  13.666 -                      BOOL enableBlending,
  13.667 -                      D3D11_BLEND srcBlend,
  13.668 -                      D3D11_BLEND destBlend,
  13.669 -                      D3D11_BLEND srcBlendAlpha,
  13.670 -                      D3D11_BLEND destBlendAlpha,
  13.671 -                      ID3D11BlendState ** blendStateOutput)
  13.672 -{
  13.673 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  13.674 -    HRESULT result = S_OK;
  13.675 -
  13.676 -    D3D11_BLEND_DESC blendDesc;
  13.677 -    memset(&blendDesc, 0, sizeof(blendDesc));
  13.678 -    blendDesc.AlphaToCoverageEnable = FALSE;
  13.679 -    blendDesc.IndependentBlendEnable = FALSE;
  13.680 -    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
  13.681 -    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
  13.682 -    blendDesc.RenderTarget[0].DestBlend = destBlend;
  13.683 -    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  13.684 -    blendDesc.RenderTarget[0].SrcBlendAlpha = srcBlendAlpha;
  13.685 -    blendDesc.RenderTarget[0].DestBlendAlpha = destBlendAlpha;
  13.686 -    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  13.687 -    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  13.688 -    result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
  13.689 -    if (FAILED(result)) {
  13.690 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBlendState", result);
  13.691 -        return result;
  13.692 -    }
  13.693 -
  13.694 -    return S_OK;
  13.695 -}
  13.696 -
  13.697 -// Create resources that depend on the device.
  13.698 -HRESULT
  13.699 -D3D11_CreateDeviceResources(SDL_Renderer * renderer)
  13.700 -{
  13.701 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  13.702 -
  13.703 -    // This flag adds support for surfaces with a different color channel ordering
  13.704 -    // than the API default. It is required for compatibility with Direct2D.
  13.705 -    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  13.706 -
  13.707 -    // Make sure Direct3D's debugging feature gets used, if the app requests it.
  13.708 -    const char *hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
  13.709 -    if (hint) {
  13.710 -        if (*hint == '1') {
  13.711 -            creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  13.712 -        }
  13.713 -    }
  13.714 -
  13.715 -    // This array defines the set of DirectX hardware feature levels this app will support.
  13.716 -    // Note the ordering should be preserved.
  13.717 -    // Don't forget to declare your application's minimum required feature level in its
  13.718 -    // description.  All applications are assumed to support 9.1 unless otherwise stated.
  13.719 -    D3D_FEATURE_LEVEL featureLevels[] = 
  13.720 -    {
  13.721 -        D3D_FEATURE_LEVEL_11_1,
  13.722 -        D3D_FEATURE_LEVEL_11_0,
  13.723 -        D3D_FEATURE_LEVEL_10_1,
  13.724 -        D3D_FEATURE_LEVEL_10_0,
  13.725 -        D3D_FEATURE_LEVEL_9_3,
  13.726 -        D3D_FEATURE_LEVEL_9_2,
  13.727 -        D3D_FEATURE_LEVEL_9_1
  13.728 -    };
  13.729 -
  13.730 -    // Create the Direct3D 11 API device object and a corresponding context.
  13.731 -    ComPtr<ID3D11Device> device;
  13.732 -    ComPtr<ID3D11DeviceContext> context;
  13.733 -    HRESULT result = S_OK;
  13.734 -    result = D3D11CreateDevice(
  13.735 -        nullptr, // Specify nullptr to use the default adapter.
  13.736 -        D3D_DRIVER_TYPE_HARDWARE,
  13.737 -        nullptr,
  13.738 -        creationFlags, // Set set debug and Direct2D compatibility flags.
  13.739 -        featureLevels, // List of feature levels this app can support.
  13.740 -        ARRAYSIZE(featureLevels),
  13.741 -        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
  13.742 -        &device, // Returns the Direct3D device created.
  13.743 -        &data->featureLevel, // Returns feature level of device created.
  13.744 -        &context // Returns the device immediate context.
  13.745 -        );
  13.746 -    if (FAILED(result)) {
  13.747 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", D3D11CreateDevice", result);
  13.748 -        return result;
  13.749 -    }
  13.750 -
  13.751 -    // Get the Direct3D 11.1 API device and context interfaces.
  13.752 -    result = device.As(&(data->d3dDevice));
  13.753 -    if (FAILED(result)) {
  13.754 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to ID3D11Device1", result);
  13.755 -        return result;
  13.756 -    }
  13.757 -
  13.758 -    result = context.As(&data->d3dContext);
  13.759 -    if (FAILED(result)) {
  13.760 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext to ID3D11DeviceContext1", result);
  13.761 -        return result;
  13.762 -    }
  13.763 -
  13.764 -    //
  13.765 -    // Make note of the maximum texture size
  13.766 -    // Max texture sizes are documented on MSDN, at:
  13.767 -    // http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
  13.768 -    //
  13.769 -    switch (data->d3dDevice->GetFeatureLevel()) {
  13.770 -        case D3D_FEATURE_LEVEL_11_1:
  13.771 -        case D3D_FEATURE_LEVEL_11_0:
  13.772 -            renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
  13.773 -            break;
  13.774 -
  13.775 -        case D3D_FEATURE_LEVEL_10_1:
  13.776 -        case D3D_FEATURE_LEVEL_10_0:
  13.777 -            renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
  13.778 -            break;
  13.779 -
  13.780 -        case D3D_FEATURE_LEVEL_9_3:
  13.781 -            renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
  13.782 -            break;
  13.783 -
  13.784 -        case D3D_FEATURE_LEVEL_9_2:
  13.785 -        case D3D_FEATURE_LEVEL_9_1:
  13.786 -            renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
  13.787 -            break;
  13.788 -    }
  13.789 -
  13.790 -    //
  13.791 -    // Load in SDL's one and only vertex shader:
  13.792 -    //
  13.793 -    result = data->d3dDevice->CreateVertexShader(
  13.794 -        D3D11_VertexShader,
  13.795 -        sizeof(D3D11_VertexShader),
  13.796 -        nullptr,
  13.797 -        &data->vertexShader
  13.798 -        );
  13.799 -    if (FAILED(result)) {
  13.800 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateVertexShader", result);
  13.801 -        return result;
  13.802 -    }
  13.803 -
  13.804 -    //
  13.805 -    // Create an input layout for SDL's vertex shader:
  13.806 -    //
  13.807 -    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
  13.808 -    {
  13.809 -        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  13.810 -        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  13.811 -        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  13.812 -    };
  13.813 -
  13.814 -    result = data->d3dDevice->CreateInputLayout(
  13.815 -        vertexDesc,
  13.816 -        ARRAYSIZE(vertexDesc),
  13.817 -        D3D11_VertexShader,
  13.818 -        sizeof(D3D11_VertexShader),
  13.819 -        &data->inputLayout
  13.820 -        );
  13.821 -    if (FAILED(result)) {
  13.822 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateInputLayout", result);
  13.823 -        return result;
  13.824 -    }
  13.825 -
  13.826 -    //
  13.827 -    // Load in SDL's pixel shaders
  13.828 -    //
  13.829 -
  13.830 -    result = data->d3dDevice->CreatePixelShader(
  13.831 -        D3D11_PixelShader_Textures,
  13.832 -        sizeof(D3D11_PixelShader_Textures),
  13.833 -        nullptr,
  13.834 -        &data->texturePixelShader
  13.835 -        );
  13.836 -    if (FAILED(result)) {
  13.837 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['textures' shader]", result);
  13.838 -        return result;
  13.839 -    }
  13.840 -
  13.841 -    result = data->d3dDevice->CreatePixelShader(
  13.842 -        D3D11_PixelShader_Colors,
  13.843 -        sizeof(D3D11_PixelShader_Colors),
  13.844 -        nullptr,
  13.845 -        &data->colorPixelShader
  13.846 -        );
  13.847 -    if (FAILED(result)) {
  13.848 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['color' shader]", result);
  13.849 -        return result;
  13.850 -    }
  13.851 -
  13.852 -    //
  13.853 -    // Setup space to hold vertex shader constants:
  13.854 -    //
  13.855 -    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
  13.856 -    result = data->d3dDevice->CreateBuffer(
  13.857 -		&constantBufferDesc,
  13.858 -		nullptr,
  13.859 -        &data->vertexShaderConstants
  13.860 -		);
  13.861 -    if (FAILED(result)) {
  13.862 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex shader constants]", result);
  13.863 -        return result;
  13.864 -    }
  13.865 -
  13.866 -    //
  13.867 -    // Make sure that the vertex buffer, if already created, gets freed.
  13.868 -    // It will be recreated later.
  13.869 -    //
  13.870 -    data->vertexBuffer = nullptr;
  13.871 -
  13.872 -    //
  13.873 -    // Create samplers to use when drawing textures:
  13.874 -    //
  13.875 -    D3D11_SAMPLER_DESC samplerDesc;
  13.876 -    samplerDesc.Filter = SDL_D3D11_NEAREST_PIXEL_FILTER;
  13.877 -    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  13.878 -    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  13.879 -    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  13.880 -    samplerDesc.MipLODBias = 0.0f;
  13.881 -    samplerDesc.MaxAnisotropy = 1;
  13.882 -    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  13.883 -    samplerDesc.BorderColor[0] = 0.0f;
  13.884 -    samplerDesc.BorderColor[1] = 0.0f;
  13.885 -    samplerDesc.BorderColor[2] = 0.0f;
  13.886 -    samplerDesc.BorderColor[3] = 0.0f;
  13.887 -    samplerDesc.MinLOD = 0.0f;
  13.888 -    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  13.889 -    result = data->d3dDevice->CreateSamplerState(
  13.890 -        &samplerDesc,
  13.891 -        &data->nearestPixelSampler
  13.892 -        );
  13.893 -    if (FAILED(result)) {
  13.894 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [nearest-pixel filter]", result);
  13.895 -        return result;
  13.896 -    }
  13.897 -
  13.898 -    samplerDesc.Filter = SDL_D3D11_LINEAR_FILTER;
  13.899 -    result = data->d3dDevice->CreateSamplerState(
  13.900 -        &samplerDesc,
  13.901 -        &data->linearSampler
  13.902 -        );
  13.903 -    if (FAILED(result)) {
  13.904 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [linear filter]", result);
  13.905 -        return result;
  13.906 -    }
  13.907 -
  13.908 -    //
  13.909 -    // Setup Direct3D rasterizer states
  13.910 -    //
  13.911 -    D3D11_RASTERIZER_DESC rasterDesc;
  13.912 -    memset(&rasterDesc, 0, sizeof(rasterDesc));
  13.913 -	rasterDesc.AntialiasedLineEnable = false;
  13.914 -	rasterDesc.CullMode = D3D11_CULL_NONE;
  13.915 -	rasterDesc.DepthBias = 0;
  13.916 -	rasterDesc.DepthBiasClamp = 0.0f;
  13.917 -	rasterDesc.DepthClipEnable = true;
  13.918 -	rasterDesc.FillMode = D3D11_FILL_SOLID;
  13.919 -	rasterDesc.FrontCounterClockwise = false;
  13.920 -	rasterDesc.MultisampleEnable = false;
  13.921 -	rasterDesc.ScissorEnable = false;
  13.922 -	rasterDesc.SlopeScaledDepthBias = 0.0f;
  13.923 -	result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
  13.924 -	if (FAILED(result)) {
  13.925 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [main rasterizer]", result);
  13.926 -        return result;
  13.927 -    }
  13.928 -
  13.929 -    rasterDesc.ScissorEnable = true;
  13.930 -    result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->clippedRasterizer);
  13.931 -	if (FAILED(result)) {
  13.932 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [clipped rasterizer]", result);
  13.933 -        return result;
  13.934 -    }
  13.935 -
  13.936 -    //
  13.937 -    // Create blending states:
  13.938 -    //
  13.939 -    result = D3D11_CreateBlendMode(
  13.940 -        renderer,
  13.941 -        TRUE,
  13.942 -        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
  13.943 -        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlend */
  13.944 -        D3D11_BLEND_ONE,                /* srcBlendAlpha */
  13.945 -        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlendAlpha */
  13.946 -        &data->blendModeBlend);
  13.947 -    if (FAILED(result)) {
  13.948 -        // D3D11_CreateBlendMode will set the SDL error, if it fails
  13.949 -        return result;
  13.950 -    }
  13.951 -
  13.952 -    result = D3D11_CreateBlendMode(
  13.953 -        renderer,
  13.954 -        TRUE,
  13.955 -        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
  13.956 -        D3D11_BLEND_ONE,                /* destBlend */
  13.957 -        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
  13.958 -        D3D11_BLEND_ONE,                /* destBlendAlpha */
  13.959 -        &data->blendModeAdd);
  13.960 -    if (FAILED(result)) {
  13.961 -        // D3D11_CreateBlendMode will set the SDL error, if it fails
  13.962 -        return result;
  13.963 -    }
  13.964 -
  13.965 -    result = D3D11_CreateBlendMode(
  13.966 -        renderer,
  13.967 -        TRUE,
  13.968 -        D3D11_BLEND_ZERO,               /* srcBlend */
  13.969 -        D3D11_BLEND_SRC_COLOR,          /* destBlend */
  13.970 -        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
  13.971 -        D3D11_BLEND_ONE,                /* destBlendAlpha */
  13.972 -        &data->blendModeMod);
  13.973 -    if (FAILED(result)) {
  13.974 -        // D3D11_CreateBlendMode will set the SDL error, if it fails
  13.975 -        return result;
  13.976 -    }
  13.977 -
  13.978 -    //
  13.979 -    // All done!
  13.980 -    //
  13.981 -    return S_OK;
  13.982 -}
  13.983 -
  13.984 -#ifdef __WINRT__
  13.985 -
  13.986 -static ABI::Windows::UI::Core::ICoreWindow *
  13.987 -D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
  13.988 -{
  13.989 -    SDL_Window * sdlWindow = renderer->window;
  13.990 -    if ( ! renderer->window ) {
  13.991 -        return nullptr;
  13.992 -    }
  13.993 -
  13.994 -    SDL_SysWMinfo sdlWindowInfo;
  13.995 -    SDL_VERSION(&sdlWindowInfo.version);
  13.996 -    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
  13.997 -        return nullptr;
  13.998 -    }
  13.999 -
 13.1000 -    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINRT) {
 13.1001 -        return nullptr;
 13.1002 -    }
 13.1003 -
 13.1004 -    if ( ! sdlWindowInfo.info.winrt.window ) {
 13.1005 -        return nullptr;
 13.1006 -    }
 13.1007 -
 13.1008 -    ABI::Windows::UI::Core::ICoreWindow * coreWindow = nullptr;
 13.1009 -    if (FAILED(sdlWindowInfo.info.winrt.window->QueryInterface(&coreWindow))) {
 13.1010 -        return nullptr;
 13.1011 -    }
 13.1012 -
 13.1013 -    return coreWindow;
 13.1014 -}
 13.1015 -
 13.1016 -// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
 13.1017 -static float
 13.1018 -D3D11_ConvertDipsToPixels(float dips)
 13.1019 -{
 13.1020 -    static const float dipsPerInch = 96.0f;
 13.1021 -    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
 13.1022 -}
 13.1023 -#endif
 13.1024 -
 13.1025 -#if WINAPI_FAMILY == WINAPI_FAMILY_APP
 13.1026 -// TODO, WinRT, XAML: get the ISwapChainBackgroundPanelNative from something other than a global var
 13.1027 -extern ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative;
 13.1028 -#endif
 13.1029 -
 13.1030 -static DXGI_MODE_ROTATION
 13.1031 -D3D11_GetRotationForOrientation(Windows::Graphics::Display::DisplayOrientations orientation)
 13.1032 -{
 13.1033 -    switch (orientation)
 13.1034 -    {
 13.1035 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 13.1036 -        //
 13.1037 -        // Windows Phone rotations
 13.1038 -        //
 13.1039 -        case DisplayOrientations::Landscape:
 13.1040 -            return DXGI_MODE_ROTATION_ROTATE90;
 13.1041 -        case DisplayOrientations::Portrait:
 13.1042 -            return DXGI_MODE_ROTATION_IDENTITY;
 13.1043 -        case DisplayOrientations::LandscapeFlipped:
 13.1044 -            return DXGI_MODE_ROTATION_ROTATE270;
 13.1045 -        case DisplayOrientations::PortraitFlipped:
 13.1046 -            return DXGI_MODE_ROTATION_ROTATE180;
 13.1047 -#else
 13.1048 -        //
 13.1049 -        // Non-Windows-Phone rotations (ex: Windows 8, Windows RT)
 13.1050 -        //
 13.1051 -        case DisplayOrientations::Landscape:
 13.1052 -            return DXGI_MODE_ROTATION_IDENTITY;
 13.1053 -        case DisplayOrientations::Portrait:
 13.1054 -            return DXGI_MODE_ROTATION_ROTATE270;
 13.1055 -        case DisplayOrientations::LandscapeFlipped:
 13.1056 -            return DXGI_MODE_ROTATION_ROTATE180;
 13.1057 -        case DisplayOrientations::PortraitFlipped:
 13.1058 -            return DXGI_MODE_ROTATION_ROTATE90;
 13.1059 -#endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 13.1060 -
 13.1061 -        default:
 13.1062 -            return DXGI_MODE_ROTATION_UNSPECIFIED;
 13.1063 -    }
 13.1064 -}
 13.1065 -
 13.1066 -static bool
 13.1067 -D3D11_IsDisplayRotated90Degrees(Windows::Graphics::Display::DisplayOrientations orientation)
 13.1068 -{
 13.1069 -    switch (D3D11_GetRotationForOrientation(orientation)) {
 13.1070 -        case DXGI_MODE_ROTATION_ROTATE90:
 13.1071 -        case DXGI_MODE_ROTATION_ROTATE270:
 13.1072 -            return true;
 13.1073 -        default:
 13.1074 -            return false;
 13.1075 -    }
 13.1076 -}
 13.1077 -
 13.1078 -static int
 13.1079 -D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect)
 13.1080 -{
 13.1081 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1082 -    switch (D3D11_GetRotationForOrientation(data-> orientation)) {
 13.1083 -        case DXGI_MODE_ROTATION_IDENTITY:
 13.1084 -            outRect->left = sdlRect->x;
 13.1085 -            outRect->right = sdlRect->x + sdlRect->w;
 13.1086 -            outRect->top = sdlRect->y;
 13.1087 -            outRect->bottom = sdlRect->y + sdlRect->h;
 13.1088 -            break;
 13.1089 -        case DXGI_MODE_ROTATION_ROTATE270:
 13.1090 -            outRect->left = sdlRect->y;
 13.1091 -            outRect->right = sdlRect->y + sdlRect->h;
 13.1092 -            outRect->top = renderer->viewport.w - sdlRect->x - sdlRect->w;
 13.1093 -            outRect->bottom = renderer->viewport.w - sdlRect->x;
 13.1094 -            break;
 13.1095 -        case DXGI_MODE_ROTATION_ROTATE180:
 13.1096 -            outRect->left = renderer->viewport.w - sdlRect->x - sdlRect->w;
 13.1097 -            outRect->right = renderer->viewport.w - sdlRect->x;
 13.1098 -            outRect->top = renderer->viewport.h - sdlRect->y - sdlRect->h;
 13.1099 -            outRect->bottom = renderer->viewport.h - sdlRect->y;
 13.1100 -            break;
 13.1101 -        case DXGI_MODE_ROTATION_ROTATE90:
 13.1102 -            outRect->left = renderer->viewport.h - sdlRect->y - sdlRect->h;
 13.1103 -            outRect->right = renderer->viewport.h - sdlRect->y;
 13.1104 -            outRect->top = sdlRect->x;
 13.1105 -            outRect->bottom = sdlRect->x + sdlRect->h;
 13.1106 -            break;
 13.1107 -        default:
 13.1108 -            return SDL_SetError("The physical display is in an unknown or unsupported orientation");
 13.1109 -    }
 13.1110 -    return 0;
 13.1111 -}
 13.1112 -
 13.1113 -
 13.1114 -// Initialize all resources that change when the window's size changes.
 13.1115 -// TODO, WinRT: get D3D11_CreateWindowSizeDependentResources working on Win32
 13.1116 -HRESULT
 13.1117 -D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
 13.1118 -{
 13.1119 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1120 -    HRESULT result = S_OK;
 13.1121 -    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
 13.1122 -
 13.1123 -    // Store the window bounds so the next time we get a SizeChanged event we can
 13.1124 -    // avoid rebuilding everything if the size is identical.
 13.1125 -    ABI::Windows::Foundation::Rect nativeWindowBounds;
 13.1126 -    if (coreWindow) {
 13.1127 -        result = coreWindow->get_Bounds(&nativeWindowBounds);
 13.1128 -        if (FAILED(result)) {
 13.1129 -            WIN_SetErrorFromHRESULT(__FUNCTION__", ICoreWindow::get_Bounds [get native-window bounds]", result);
 13.1130 -            return result;
 13.1131 -        }
 13.1132 -    } else {
 13.1133 -        // TODO, WinRT, XAML: clean up window-bounds code in D3D11_CreateWindowSizeDependentResources
 13.1134 -        SDL_DisplayMode displayMode;
 13.1135 -        if (SDL_GetDesktopDisplayMode(0, &displayMode) < 0) {
 13.1136 -            SDL_SetError(__FUNCTION__", Get Window Bounds (XAML): Unable to retrieve the native window's size");
 13.1137 -            return E_FAIL;
 13.1138 -        }
 13.1139 -
 13.1140 -        nativeWindowBounds.Width = (FLOAT) displayMode.w;
 13.1141 -        nativeWindowBounds.Height = (FLOAT) displayMode.h;
 13.1142 -    }
 13.1143 -
 13.1144 -    // TODO, WinRT, XAML: see if window/control sizes are in DIPs, or something else.  If something else, then adjust renderer size tracking accordingly.
 13.1145 -    data->windowSizeInDIPs.x = nativeWindowBounds.Width;
 13.1146 -    data->windowSizeInDIPs.y = nativeWindowBounds.Height;
 13.1147 -
 13.1148 -    // Calculate the necessary swap chain and render target size in pixels.
 13.1149 -    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
 13.1150 -    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
 13.1151 -
 13.1152 -    // The width and height of the swap chain must be based on the window's
 13.1153 -    // landscape-oriented width and height. If the window is in a portrait
 13.1154 -    // orientation, the dimensions must be reversed.
 13.1155 -    data->orientation = DisplayProperties::CurrentOrientation;
 13.1156 -
 13.1157 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 13.1158 -    const bool swapDimensions = false;
 13.1159 -#else
 13.1160 -    const bool swapDimensions = D3D11_IsDisplayRotated90Degrees(data->orientation);
 13.1161 -#endif
 13.1162 -    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
 13.1163 -    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
 13.1164 -
 13.1165 -    if(data->swapChain != nullptr)
 13.1166 -    {
 13.1167 -        // If the swap chain already exists, resize it.
 13.1168 -        result = data->swapChain->ResizeBuffers(
 13.1169 -            2, // Double-buffered swap chain.
 13.1170 -            static_cast<UINT>(data->renderTargetSize.x),
 13.1171 -            static_cast<UINT>(data->renderTargetSize.y),
 13.1172 -            DXGI_FORMAT_B8G8R8A8_UNORM,
 13.1173 -            0
 13.1174 -            );
 13.1175 -        if (FAILED(result)) {
 13.1176 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::ResizeBuffers", result);
 13.1177 -            return result;
 13.1178 -        }
 13.1179 -    }
 13.1180 -    else
 13.1181 -    {
 13.1182 -        const bool usingXAML = (coreWindow == nullptr);
 13.1183 -
 13.1184 -        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
 13.1185 -        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
 13.1186 -        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
 13.1187 -        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
 13.1188 -        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
 13.1189 -        swapChainDesc.Stereo = false;
 13.1190 -        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
 13.1191 -        swapChainDesc.SampleDesc.Quality = 0;
 13.1192 -        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 13.1193 -        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
 13.1194 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 13.1195 -        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
 13.1196 -        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
 13.1197 -#else
 13.1198 -        if (usingXAML) {
 13.1199 -            swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
 13.1200 -        } else {
 13.1201 -            swapChainDesc.Scaling = DXGI_SCALING_NONE;
 13.1202 -        }
 13.1203 -        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
 13.1204 -#endif
 13.1205 -        swapChainDesc.Flags = 0;
 13.1206 -
 13.1207 -        ComPtr<IDXGIDevice1> dxgiDevice;
 13.1208 -        result = data->d3dDevice.As(&dxgiDevice);
 13.1209 -        if (FAILED(result)) {
 13.1210 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1 to IDXGIDevice1", result);
 13.1211 -            return result;
 13.1212 -        }
 13.1213 -
 13.1214 -        ComPtr<IDXGIAdapter> dxgiAdapter;
 13.1215 -        result = dxgiDevice->GetAdapter(&dxgiAdapter);
 13.1216 -        if (FAILED(result)) {
 13.1217 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::GetAdapter", result);
 13.1218 -            return result;
 13.1219 -        }
 13.1220 -
 13.1221 -        ComPtr<IDXGIFactory2> dxgiFactory;
 13.1222 -        result = dxgiAdapter->GetParent(
 13.1223 -            __uuidof(IDXGIFactory2), 
 13.1224 -            &dxgiFactory
 13.1225 -            );
 13.1226 -        if (FAILED(result)) {
 13.1227 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIAdapter::GetParent", result);
 13.1228 -            return result;
 13.1229 -        }
 13.1230 -
 13.1231 -        if (usingXAML) {
 13.1232 -            result = dxgiFactory->CreateSwapChainForComposition(
 13.1233 -                data->d3dDevice.Get(),
 13.1234 -                &swapChainDesc,
 13.1235 -                nullptr,
 13.1236 -                &data->swapChain);
 13.1237 -            if (FAILED(result)) {
 13.1238 -                WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForComposition", result);
 13.1239 -                return result;
 13.1240 -            }
 13.1241 -
 13.1242 -#if WINAPI_FAMILY == WINAPI_FAMILY_APP
 13.1243 -            result = WINRT_GlobalSwapChainBackgroundPanelNative->SetSwapChain(data->swapChain.Get());
 13.1244 -            if (FAILED(result)) {
 13.1245 -                WIN_SetErrorFromHRESULT(__FUNCTION__ ", ISwapChainBackgroundPanelNative::SetSwapChain", result);
 13.1246 -                return result;
 13.1247 -            }
 13.1248 -#else
 13.1249 -            SDL_SetError(__FUNCTION__ ", XAML support is not yet available for Windows Phone");
 13.1250 -            return E_FAIL;
 13.1251 -#endif
 13.1252 -        } else {
 13.1253 -            IUnknown * coreWindowAsIUnknown = nullptr;
 13.1254 -            result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
 13.1255 -            if (FAILED(result)) {
 13.1256 -                WIN_SetErrorFromHRESULT(__FUNCTION__ ", ICoreWindow to IUnknown", result);
 13.1257 -                return result;
 13.1258 -            }
 13.1259 -
 13.1260 -            result = dxgiFactory->CreateSwapChainForCoreWindow(
 13.1261 -                data->d3dDevice.Get(),
 13.1262 -                coreWindowAsIUnknown,
 13.1263 -                &swapChainDesc,
 13.1264 -                nullptr, // Allow on all displays.
 13.1265 -                &data->swapChain
 13.1266 -                );
 13.1267 -            if (FAILED(result)) {
 13.1268 -                WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForCoreWindow", result);
 13.1269 -                return result;
 13.1270 -            }
 13.1271 -        }
 13.1272 -        
 13.1273 -        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
 13.1274 -        // ensures that the application will only render after each VSync, minimizing power consumption.
 13.1275 -        result = dxgiDevice->SetMaximumFrameLatency(1);
 13.1276 -        if (FAILED(result)) {
 13.1277 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::SetMaximumFrameLatency", result);
 13.1278 -            return result;
 13.1279 -        }
 13.1280 -    }
 13.1281 -    
 13.1282 -#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
 13.1283 -    // Set the proper orientation for the swap chain, and generate the
 13.1284 -    // 3D matrix transformation for rendering to the rotated swap chain.
 13.1285 -    //
 13.1286 -    // To note, the call for this, IDXGISwapChain1::SetRotation, is not necessary
 13.1287 -    // on Windows Phone, nor is it supported there.  It's only needed in Windows 8/RT.
 13.1288 -    DXGI_MODE_ROTATION rotation = D3D11_GetRotationForOrientation(data->orientation);
 13.1289 -    result = data->swapChain->SetRotation(rotation);
 13.1290 -    if (FAILED(result)) {
 13.1291 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::SetRotation" , result);
 13.1292 -        return result;
 13.1293 -    }
 13.1294 -#endif
 13.1295 -
 13.1296 -    // Create a render target view of the swap chain back buffer.
 13.1297 -    ComPtr<ID3D11Texture2D> backBuffer;
 13.1298 -    result = data->swapChain->GetBuffer(
 13.1299 -        0,
 13.1300 -        __uuidof(ID3D11Texture2D),
 13.1301 -        &backBuffer
 13.1302 -        );
 13.1303 -    if (FAILED(result)) {
 13.1304 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::GetBuffer [back-buffer]", result);
 13.1305 -        return result;
 13.1306 -    }
 13.1307 -
 13.1308 -    result = data->d3dDevice->CreateRenderTargetView(
 13.1309 -        backBuffer.Get(),
 13.1310 -        nullptr,
 13.1311 -        &data->mainRenderTargetView
 13.1312 -        );
 13.1313 -    if (FAILED(result)) {
 13.1314 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRenderTargetView", result);
 13.1315 -        return result;
 13.1316 -    }
 13.1317 -
 13.1318 -    if (D3D11_UpdateViewport(renderer) != 0) {
 13.1319 -        // D3D11_UpdateViewport will set the SDL error if it fails.
 13.1320 -        return E_FAIL;
 13.1321 -    }
 13.1322 -
 13.1323 -    return S_OK;
 13.1324 -}
 13.1325 -
 13.1326 -// This method is called when the window's size changes.
 13.1327 -HRESULT
 13.1328 -D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
 13.1329 -{
 13.1330 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1331 -    HRESULT result = S_OK;
 13.1332 -    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
 13.1333 -    ABI::Windows::Foundation::Rect coreWindowBounds;
 13.1334 -
 13.1335 -    result = coreWindow->get_Bounds(&coreWindowBounds);
 13.1336 -    if (FAILED(result)) {
 13.1337 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ICoreWindow::get_Bounds [get window bounds]", result);
 13.1338 -        return result;
 13.1339 -    }
 13.1340 -
 13.1341 -    if (coreWindowBounds.Width  != data->windowSizeInDIPs.x ||
 13.1342 -        coreWindowBounds.Height != data->windowSizeInDIPs.y ||
 13.1343 -        data->orientation != DisplayProperties::CurrentOrientation)
 13.1344 -    {
 13.1345 -        ID3D11RenderTargetView* nullViews[] = {nullptr};
 13.1346 -        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
 13.1347 -        data->mainRenderTargetView = nullptr;
 13.1348 -        data->d3dContext->Flush();
 13.1349 -        result = D3D11_CreateWindowSizeDependentResources(renderer);
 13.1350 -        if (FAILED(result)) {
 13.1351 -            /* D3D11_CreateWindowSizeDependentResources will set the SDL error */
 13.1352 -            return result;
 13.1353 -        }
 13.1354 -    }
 13.1355 -
 13.1356 -    return S_OK;
 13.1357 -}
 13.1358 -
 13.1359 -HRESULT
 13.1360 -D3D11_HandleDeviceLost(SDL_Renderer * renderer)
 13.1361 -{
 13.1362 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1363 -    HRESULT result = S_OK;
 13.1364 -
 13.1365 -    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
 13.1366 -    data->windowSizeInDIPs.x = 0;
 13.1367 -    data->windowSizeInDIPs.y = 0;
 13.1368 -    data->swapChain = nullptr;
 13.1369 -
 13.1370 -    result = D3D11_CreateDeviceResources(renderer);
 13.1371 -    if (FAILED(result)) {
 13.1372 -        /* D3D11_CreateDeviceResources will set the SDL error */
 13.1373 -        return result;
 13.1374 -    }
 13.1375 -
 13.1376 -    result = D3D11_UpdateForWindowSizeChange(renderer);
 13.1377 -    if (FAILED(result)) {
 13.1378 -        /* D3D11_UpdateForWindowSizeChange will set the SDL error */
 13.1379 -        return result;
 13.1380 -    }
 13.1381 -
 13.1382 -    return S_OK;
 13.1383 -}
 13.1384 -
 13.1385 -static void
 13.1386 -D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
 13.1387 -{
 13.1388 -    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1389 -
 13.1390 -    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
 13.1391 -        D3D11_UpdateForWindowSizeChange(renderer);
 13.1392 -    }
 13.1393 -}
 13.1394 -
 13.1395 -static D3D11_FILTER
 13.1396 -GetScaleQuality(void)
 13.1397 -{
 13.1398 -    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
 13.1399 -    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
 13.1400 -        return SDL_D3D11_NEAREST_PIXEL_FILTER;
 13.1401 -    } else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
 13.1402 -        return SDL_D3D11_LINEAR_FILTER;
 13.1403 -    }
 13.1404 -}
 13.1405 -
 13.1406 -static int
 13.1407 -D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 13.1408 -{
 13.1409 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1410 -    D3D11_TextureData *textureData;
 13.1411 -    HRESULT result;
 13.1412 -    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
 13.1413 -    if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
 13.1414 -        return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
 13.1415 -            __FUNCTION__, texture->format);
 13.1416 -    }
 13.1417 -
 13.1418 -    textureData = new D3D11_TextureData;
 13.1419 -    if (!textureData) {
 13.1420 -        SDL_OutOfMemory();
 13.1421 -        return -1;
 13.1422 -    }
 13.1423 -    textureData->pixelFormat = SDL_AllocFormat(texture->format);
 13.1424 -    textureData->lockedTexturePosition = XMINT2(0, 0);
 13.1425 -    textureData->scaleMode = GetScaleQuality();
 13.1426 -
 13.1427 -    texture->driverdata = textureData;
 13.1428 -
 13.1429 -    D3D11_TEXTURE2D_DESC textureDesc = {0};
 13.1430 -    textureDesc.Width = texture->w;
 13.1431 -    textureDesc.Height = texture->h;
 13.1432 -    textureDesc.MipLevels = 1;
 13.1433 -    textureDesc.ArraySize = 1;
 13.1434 -    textureDesc.Format = textureFormat;
 13.1435 -    textureDesc.SampleDesc.Count = 1;
 13.1436 -    textureDesc.SampleDesc.Quality = 0;
 13.1437 -    textureDesc.MiscFlags = 0;
 13.1438 -
 13.1439 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
 13.1440 -        textureDesc.Usage = D3D11_USAGE_DYNAMIC;
 13.1441 -        textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 13.1442 -    } else {
 13.1443 -        textureDesc.Usage = D3D11_USAGE_DEFAULT;
 13.1444 -        textureDesc.CPUAccessFlags = 0;
 13.1445 -    }
 13.1446 -
 13.1447 -    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
 13.1448 -        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
 13.1449 -    } else {
 13.1450 -        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
 13.1451 -    }
 13.1452 -
 13.1453 -#if 0
 13.1454 -    // Fill the texture with a non-black color, for debugging purposes:
 13.1455 -    const int numPixels = textureDesc.Width * textureDesc.Height;
 13.1456 -    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
 13.1457 -    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
 13.1458 -    for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
 13.1459 -        initialTexturePixels[i+0] = 0xff;
 13.1460 -        initialTexturePixels[i+1] = 0xff;
 13.1461 -        initialTexturePixels[i+2] = 0x00;
 13.1462 -        initialTexturePixels[i+3] = 0xff;
 13.1463 -    }
 13.1464 -    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
 13.1465 -    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
 13.1466 -    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
 13.1467 -    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
 13.1468 -#endif
 13.1469 -
 13.1470 -    result = rendererData->d3dDevice->CreateTexture2D(
 13.1471 -        &textureDesc,
 13.1472 -        NULL,   // &initialTextureData,
 13.1473 -        &textureData->mainTexture
 13.1474 -        );
 13.1475 -    if (FAILED(result)) {
 13.1476 -        D3D11_DestroyTexture(renderer, texture);
 13.1477 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D", result);
 13.1478 -        return -1;
 13.1479 -    }
 13.1480 -
 13.1481 -    if (texture->access & SDL_TEXTUREACCESS_TARGET) {
 13.1482 -        D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
 13.1483 -        renderTargetViewDesc.Format = textureDesc.Format;
 13.1484 -        renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
 13.1485 -        renderTargetViewDesc.Texture2D.MipSlice = 0;
 13.1486 -
 13.1487 -        result = rendererData->d3dDevice->CreateRenderTargetView(
 13.1488 -            textureData->mainTexture.Get(),
 13.1489 -            &renderTargetViewDesc,
 13.1490 -            &textureData->mainTextureRenderTargetView);
 13.1491 -        if (FAILED(result)) {
 13.1492 -            D3D11_DestroyTexture(renderer, texture);
 13.1493 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRenderTargetView", result);
 13.1494 -            return -1;
 13.1495 -        }
 13.1496 -    }
 13.1497 -
 13.1498 -    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
 13.1499 -    resourceViewDesc.Format = textureDesc.Format;
 13.1500 -    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
 13.1501 -    resourceViewDesc.Texture2D.MostDetailedMip = 0;
 13.1502 -    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
 13.1503 -    result = rendererData->d3dDevice->CreateShaderResourceView(
 13.1504 -        textureData->mainTexture.Get(),
 13.1505 -        &resourceViewDesc,
 13.1506 -        &textureData->mainTextureResourceView
 13.1507 -        );
 13.1508 -    if (FAILED(result)) {
 13.1509 -        D3D11_DestroyTexture(renderer, texture);
 13.1510 -        WIN_SetErrorFromHRESULT(__FUNCTION__ "ID3D11Device1::CreateShaderResourceView", result);
 13.1511 -        return -1;
 13.1512 -    }
 13.1513 -
 13.1514 -    return 0;
 13.1515 -}
 13.1516 -
 13.1517 -static void
 13.1518 -D3D11_DestroyTexture(SDL_Renderer * renderer,
 13.1519 -                     SDL_Texture * texture)
 13.1520 -{
 13.1521 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.1522 -
 13.1523 -    if (textureData) {
 13.1524 -        if (textureData->pixelFormat) {
 13.1525 -            SDL_FreeFormat(textureData->pixelFormat);
 13.1526 -            textureData->pixelFormat = NULL;
 13.1527 -        }
 13.1528 -
 13.1529 -        delete textureData;
 13.1530 -        texture->driverdata = NULL;
 13.1531 -    }
 13.1532 -}
 13.1533 -
 13.1534 -static int
 13.1535 -D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 13.1536 -                    const SDL_Rect * rect, const void * srcPixels,
 13.1537 -                    int srcPitch)
 13.1538 -{
 13.1539 -    // Lock the texture, retrieving a buffer to write pixel data to:
 13.1540 -    void * destPixels = NULL;
 13.1541 -    int destPitch = 0;
 13.1542 -    if (D3D11_LockTexture(renderer, texture, rect, &destPixels, &destPitch) != 0) {
 13.1543 -        // An error is already set.  Attach some info to it, then return to
 13.1544 -        // the caller.
 13.1545 -        std::string errorMessage = string(__FUNCTION__ ", Lock Texture Failed: ") + SDL_GetError();
 13.1546 -        return SDL_SetError(errorMessage.c_str());
 13.1547 -    }
 13.1548 -
 13.1549 -    // Copy pixel data to the locked texture's memory:
 13.1550 -    for (int y = 0; y < rect->h; ++y) {
 13.1551 -        memcpy(
 13.1552 -            ((Uint8 *)destPixels) + (destPitch * y),
 13.1553 -            ((Uint8 *)srcPixels) + (srcPitch * y),
 13.1554 -            srcPitch
 13.1555 -            );
 13.1556 -    }
 13.1557 -
 13.1558 -    // Commit the texture's memory back to Direct3D:
 13.1559 -    D3D11_UnlockTexture(renderer, texture);
 13.1560 -
 13.1561 -    // Return to the caller:
 13.1562 -    return 0;
 13.1563 -}
 13.1564 -
 13.1565 -static int
 13.1566 -D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 13.1567 -                  const SDL_Rect * rect, void **pixels, int *pitch)
 13.1568 -{
 13.1569 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1570 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.1571 -    HRESULT result = S_OK;
 13.1572 -
 13.1573 -    if (textureData->stagingTexture) {
 13.1574 -        return SDL_SetError("texture is already locked");
 13.1575 -    }
 13.1576 -    
 13.1577 -    // Create a 'staging' texture, which will be used to write to a portion
 13.1578 -    // of the main texture.  This is necessary, as Direct3D 11.1 does not
 13.1579 -    // have the ability to write a CPU-bound pixel buffer to a rectangular
 13.1580 -    // subrect of a texture.  Direct3D 11.1 can, however, write a pixel
 13.1581 -    // buffer to an entire texture, hence the use of a staging texture.
 13.1582 -    //
 13.1583 -    // TODO, WinRT: consider avoiding the use of a staging texture in D3D11_LockTexture if/when the entire texture is being updated
 13.1584 -    D3D11_TEXTURE2D_DESC stagingTextureDesc;
 13.1585 -    textureData->mainTexture->GetDesc(&stagingTextureDesc);
 13.1586 -    stagingTextureDesc.Width = rect->w;
 13.1587 -    stagingTextureDesc.Height = rect->h;
 13.1588 -    stagingTextureDesc.BindFlags = 0;
 13.1589 -    stagingTextureDesc.MiscFlags = 0;
 13.1590 -    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 13.1591 -    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
 13.1592 -    result = rendererData->d3dDevice->CreateTexture2D(
 13.1593 -        &stagingTextureDesc,
 13.1594 -        NULL,
 13.1595 -        &textureData->stagingTexture);
 13.1596 -    if (FAILED(result)) {
 13.1597 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
 13.1598 -        return -1;
 13.1599 -    }
 13.1600 -
 13.1601 -    // Get a write-only pointer to data in the staging texture:
 13.1602 -    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
 13.1603 -    result = rendererData->d3dContext->Map(
 13.1604 -        textureData->stagingTexture.Get(),
 13.1605 -        D3D11CalcSubresource(0, 0, 0),
 13.1606 -        D3D11_MAP_WRITE,
 13.1607 -        0,
 13.1608 -        &textureMemory
 13.1609 -        );
 13.1610 -    if (FAILED(result)) {
 13.1611 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
 13.1612 -        textureData->stagingTexture = nullptr;
 13.1613 -        return -1;
 13.1614 -    }
 13.1615 -
 13.1616 -    // Make note of where the staging texture will be written to (on a
 13.1617 -    // call to SDL_UnlockTexture):
 13.1618 -    textureData->lockedTexturePosition = XMINT2(rect->x, rect->y);
 13.1619 -
 13.1620 -    // Make sure the caller has information on the texture's pixel buffer,
 13.1621 -    // then return:
 13.1622 -    *pixels = textureMemory.pData;
 13.1623 -    *pitch = textureMemory.RowPitch;
 13.1624 -    return 0;
 13.1625 -}
 13.1626 -
 13.1627 -static void
 13.1628 -D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 13.1629 -{
 13.1630 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1631 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.1632 -
 13.1633 -    // Commit the pixel buffer's changes back to the staging texture:
 13.1634 -    rendererData->d3dContext->Unmap(
 13.1635 -        textureData->stagingTexture.Get(),
 13.1636 -        0);
 13.1637 -
 13.1638 -    // Copy the staging texture's contents back to the main texture:
 13.1639 -    rendererData->d3dContext->CopySubresourceRegion(
 13.1640 -        textureData->mainTexture.Get(),
 13.1641 -        D3D11CalcSubresource(0, 0, 0),
 13.1642 -        textureData->lockedTexturePosition.x,
 13.1643 -        textureData->lockedTexturePosition.y,
 13.1644 -        0,
 13.1645 -        textureData->stagingTexture.Get(),
 13.1646 -        D3D11CalcSubresource(0, 0, 0),
 13.1647 -        NULL);
 13.1648 -
 13.1649 -    // Clean up and return:
 13.1650 -    textureData->stagingTexture = nullptr;
 13.1651 -    textureData->lockedTexturePosition = XMINT2(0, 0);
 13.1652 -}
 13.1653 -
 13.1654 -static int
 13.1655 -D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
 13.1656 -{
 13.1657 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1658 -
 13.1659 -    if (texture == NULL) {
 13.1660 -        rendererData->currentOffscreenRenderTargetView = nullptr;
 13.1661 -        return 0;
 13.1662 -    }
 13.1663 -
 13.1664 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.1665 -
 13.1666 -    if (!textureData->mainTextureRenderTargetView) {
 13.1667 -        return SDL_SetError("specified texture is not a render target");
 13.1668 -    }
 13.1669 -
 13.1670 -    rendererData->currentOffscreenRenderTargetView = textureData->mainTextureRenderTargetView;
 13.1671 -
 13.1672 -    return 0;
 13.1673 -}
 13.1674 -
 13.1675 -static int
 13.1676 -D3D11_UpdateViewport(SDL_Renderer * renderer)
 13.1677 -{
 13.1678 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1679 -
 13.1680 -    if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
 13.1681 -        // If the viewport is empty, assume that it is because
 13.1682 -        // SDL_CreateRenderer is calling it, and will call it again later
 13.1683 -        // with a non-empty viewport.
 13.1684 -        return 0;
 13.1685 -    }
 13.1686 -
 13.1687 -    // Make sure the SDL viewport gets rotated to that of the physical display's orientation.
 13.1688 -    // Keep in mind here that the Y-axis will be been inverted (from Direct3D's
 13.1689 -    // default coordinate system) so rotations will be done in the opposite
 13.1690 -    // direction of the DXGI_MODE_ROTATION enumeration.
 13.1691 -    DirectX::XMMATRIX projection;
 13.1692 -    switch (D3D11_GetRotationForOrientation(data->orientation))
 13.1693 -    {
 13.1694 -        case DXGI_MODE_ROTATION_IDENTITY:
 13.1695 -            projection = XMMatrixIdentity();
 13.1696 -            break;
 13.1697 -        case DXGI_MODE_ROTATION_ROTATE270:
 13.1698 -            projection = XMMatrixRotationZ(XM_PIDIV2);
 13.1699 -            break;
 13.1700 -        case DXGI_MODE_ROTATION_ROTATE180:
 13.1701 -            projection = XMMatrixRotationZ(XM_PI);
 13.1702 -            break;
 13.1703 -        case DXGI_MODE_ROTATION_ROTATE90:
 13.1704 -            projection = XMMatrixRotationZ(-XM_PIDIV2);
 13.1705 -            break;
 13.1706 -        default:
 13.1707 -            return SDL_SetError("An unknown DisplayOrientation is being used");
 13.1708 -    }
 13.1709 -
 13.1710 -    //
 13.1711 -    // Update the view matrix
 13.1712 -    //
 13.1713 -    float viewportWidth = (float) renderer->viewport.w;
 13.1714 -    float viewportHeight = (float) renderer->viewport.h;
 13.1715 -    DirectX::XMMATRIX view = XMMatrixMultiply(
 13.1716 -        XMMatrixScaling(2.0f / viewportWidth, 2.0f / viewportHeight, 1.0f),
 13.1717 -        XMMatrixMultiply(
 13.1718 -            XMMatrixTranslation(-1, -1, 0),
 13.1719 -            XMMatrixRotationX(XM_PI)
 13.1720 -            ));
 13.1721 -
 13.1722 -    //
 13.1723 -    // Combine the projection + view matrix together now, as both only get
 13.1724 -    // set here (as of this writing, on Dec 26, 2013).  When done, store it
 13.1725 -    // for eventual transfer to the GPU.
 13.1726 -    //
 13.1727 -    XMStoreFloat4x4(&data->vertexShaderConstantsData.projectionAndView,
 13.1728 -        XMMatrixMultiply(
 13.1729 -            view,
 13.1730 -            projection));
 13.1731 -
 13.1732 -    //
 13.1733 -    // Reset the model matrix
 13.1734 -    //
 13.1735 -    XMStoreFloat4x4(&data->vertexShaderConstantsData.model, XMMatrixIdentity());
 13.1736 -
 13.1737 -    //
 13.1738 -    // Update the Direct3D viewport, which seems to be aligned to the
 13.1739 -    // swap buffer's coordinate space, which is always in either
 13.1740 -    // a landscape mode, for all Windows 8/RT devices, or a portrait mode,
 13.1741 -    // for Windows Phone devices.
 13.1742 -    //
 13.1743 -    SDL_FRect orientationAlignedViewport;
 13.1744 -    const bool swapDimensions = D3D11_IsDisplayRotated90Degrees(data->orientation);
 13.1745 -    if (swapDimensions) {
 13.1746 -        orientationAlignedViewport.x = (float) renderer->viewport.y;
 13.1747 -        orientationAlignedViewport.y = (float) renderer->viewport.x;
 13.1748 -        orientationAlignedViewport.w = (float) renderer->viewport.h;
 13.1749 -        orientationAlignedViewport.h = (float) renderer->viewport.w;
 13.1750 -    } else {
 13.1751 -        orientationAlignedViewport.x = (float) renderer->viewport.x;
 13.1752 -        orientationAlignedViewport.y = (float) renderer->viewport.y;
 13.1753 -        orientationAlignedViewport.w = (float) renderer->viewport.w;
 13.1754 -        orientationAlignedViewport.h = (float) renderer->viewport.h;
 13.1755 -    }
 13.1756 -    // TODO, WinRT: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped)
 13.1757 -
 13.1758 -    D3D11_VIEWPORT viewport;
 13.1759 -    memset(&viewport, 0, sizeof(viewport));
 13.1760 -    viewport.TopLeftX = orientationAlignedViewport.x;
 13.1761 -    viewport.TopLeftY = orientationAlignedViewport.y;
 13.1762 -    viewport.Width = orientationAlignedViewport.w;
 13.1763 -    viewport.Height = orientationAlignedViewport.h;
 13.1764 -    viewport.MinDepth = 0.0f;
 13.1765 -    viewport.MaxDepth = 1.0f;
 13.1766 -    data->d3dContext->RSSetViewports(1, &viewport);
 13.1767 -
 13.1768 -#if 0
 13.1769 -    SDL_Log("%s, oav={%.0f,%.0f,%.0f,%.0f}, rend={%.0f,%.0f}\n",
 13.1770 -        __FUNCTION__,
 13.1771 -        orientationAlignedViewport.x,
 13.1772 -        orientationAlignedViewport.y,
 13.1773 -        orientationAlignedViewport.w,
 13.1774 -        orientationAlignedViewport.h,
 13.1775 -        data->renderTargetSize.x,
 13.1776 -        data->renderTargetSize.y);
 13.1777 -#endif
 13.1778 -
 13.1779 -    return 0;
 13.1780 -}
 13.1781 -
 13.1782 -static int
 13.1783 -D3D11_UpdateClipRect(SDL_Renderer * renderer)
 13.1784 -{
 13.1785 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1786 -    const SDL_Rect *rect = &renderer->clip_rect;
 13.1787 -
 13.1788 -    if (SDL_RectEmpty(rect)) {
 13.1789 -        data->d3dContext->RSSetScissorRects(0, 0);
 13.1790 -    } else {
 13.1791 -        D3D11_RECT scissorRect;
 13.1792 -        if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &scissorRect) != 0) {
 13.1793 -            /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
 13.1794 -            return -1;
 13.1795 -        }
 13.1796 -        data->d3dContext->RSSetScissorRects(1, &scissorRect);
 13.1797 -    }
 13.1798 -
 13.1799 -    return 0;
 13.1800 -}
 13.1801 -
 13.1802 -static ComPtr<ID3D11RenderTargetView> &
 13.1803 -D3D11_GetCurrentRenderTargetView(SDL_Renderer * renderer)
 13.1804 -{
 13.1805 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1806 -    if (data->currentOffscreenRenderTargetView) {
 13.1807 -        return data->currentOffscreenRenderTargetView;
 13.1808 -    } else {
 13.1809 -        return data->mainRenderTargetView;
 13.1810 -    }
 13.1811 -}
 13.1812 -
 13.1813 -static int
 13.1814 -D3D11_RenderClear(SDL_Renderer * renderer)
 13.1815 -{
 13.1816 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.1817 -    const float colorRGBA[] = {
 13.1818 -        (renderer->r / 255.0f),
 13.1819 -        (renderer->g / 255.0f),
 13.1820 -        (renderer->b / 255.0f),
 13.1821 -        (renderer->a / 255.0f)
 13.1822 -    };
 13.1823 -    data->d3dContext->ClearRenderTargetView(
 13.1824 -        D3D11_GetCurrentRenderTargetView(renderer).Get(),
 13.1825 -        colorRGBA
 13.1826 -        );
 13.1827 -    return 0;
 13.1828 -}
 13.1829 -
 13.1830 -static int
 13.1831 -D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
 13.1832 -                         const void * vertexData, size_t dataSizeInBytes)
 13.1833 -{
 13.1834 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1835 -    HRESULT result = S_OK;
 13.1836 -    D3D11_BUFFER_DESC vertexBufferDesc;
 13.1837 -
 13.1838 -    if (rendererData->vertexBuffer) {
 13.1839 -        rendererData->vertexBuffer->GetDesc(&vertexBufferDesc);
 13.1840 -    } else {
 13.1841 -        memset(&vertexBufferDesc, 0, sizeof(vertexBufferDesc));
 13.1842 -    }
 13.1843 -
 13.1844 -    if (vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
 13.1845 -        D3D11_MAPPED_SUBRESOURCE mappedResource;
 13.1846 -        ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
 13.1847 -        result = rendererData->d3dContext->Map(rendererData->vertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
 13.1848 -        if (FAILED(result)) {
 13.1849 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [vertex buffer]", result);
 13.1850 -            return -1;
 13.1851 -        }
 13.1852 -        memcpy(mappedResource.pData, vertexData, dataSizeInBytes);
 13.1853 -        rendererData->d3dContext->Unmap(rendererData->vertexBuffer.Get(), 0);
 13.1854 -    } else {
 13.1855 -        vertexBufferDesc.ByteWidth = dataSizeInBytes;
 13.1856 -        vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
 13.1857 -        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
 13.1858 -        vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
 13.1859 -
 13.1860 -        D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
 13.1861 -        vertexBufferData.pSysMem = vertexData;
 13.1862 -        vertexBufferData.SysMemPitch = 0;
 13.1863 -        vertexBufferData.SysMemSlicePitch = 0;
 13.1864 -
 13.1865 -        result = rendererData->d3dDevice->CreateBuffer(
 13.1866 -            &vertexBufferDesc,
 13.1867 -            &vertexBufferData,
 13.1868 -            &rendererData->vertexBuffer
 13.1869 -            );
 13.1870 -        if (FAILED(result)) {
 13.1871 -            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex buffer]", result);
 13.1872 -            return -1;
 13.1873 -        }
 13.1874 -    }
 13.1875 -
 13.1876 -    UINT stride = sizeof(VertexPositionColor);
 13.1877 -    UINT offset = 0;
 13.1878 -    rendererData->d3dContext->IASetVertexBuffers(
 13.1879 -        0,
 13.1880 -        1,
 13.1881 -        rendererData->vertexBuffer.GetAddressOf(),
 13.1882 -        &stride,
 13.1883 -        &offset
 13.1884 -        );
 13.1885 -
 13.1886 -    return 0;
 13.1887 -}
 13.1888 -
 13.1889 -static void
 13.1890 -D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
 13.1891 -{
 13.1892 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1893 -
 13.1894 -    rendererData->d3dContext->OMSetRenderTargets(
 13.1895 -        1,
 13.1896 -        D3D11_GetCurrentRenderTargetView(renderer).GetAddressOf(),
 13.1897 -        nullptr
 13.1898 -        );
 13.1899 -}
 13.1900 -
 13.1901 -static void
 13.1902 -D3D11_RenderSetBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
 13.1903 -{
 13.1904 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1905 -    switch (blendMode) {
 13.1906 -        case SDL_BLENDMODE_BLEND:
 13.1907 -            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeBlend.Get(), 0, 0xFFFFFFFF);
 13.1908 -            break;
 13.1909 -        case SDL_BLENDMODE_ADD:
 13.1910 -            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeAdd.Get(), 0, 0xFFFFFFFF);
 13.1911 -            break;
 13.1912 -        case SDL_BLENDMODE_MOD:
 13.1913 -            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeMod.Get(), 0, 0xFFFFFFFF);
 13.1914 -            break;
 13.1915 -        case SDL_BLENDMODE_NONE:
 13.1916 -            rendererData->d3dContext->OMSetBlendState(NULL, 0, 0xFFFFFFFF);
 13.1917 -            break;
 13.1918 -    }
 13.1919 -}
 13.1920 -
 13.1921 -static void
 13.1922 -D3D11_SetPixelShader(SDL_Renderer * renderer,
 13.1923 -                     ID3D11PixelShader * shader,
 13.1924 -                     ID3D11ShaderResourceView * shaderResource,
 13.1925 -                     ID3D11SamplerState * sampler)
 13.1926 -{
 13.1927 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1928 -    rendererData->d3dContext->PSSetShader(shader, nullptr, 0);
 13.1929 -    rendererData->d3dContext->PSSetShaderResources(0, 1, &shaderResource);
 13.1930 -    rendererData->d3dContext->PSSetSamplers(0, 1, &sampler);
 13.1931 -}
 13.1932 -
 13.1933 -static void
 13.1934 -D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
 13.1935 -                         D3D11_PRIMITIVE_TOPOLOGY primitiveTopology,
 13.1936 -                         UINT vertexCount)
 13.1937 -{
 13.1938 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1939 -
 13.1940 -    rendererData->d3dContext->UpdateSubresource(
 13.1941 -        rendererData->vertexShaderConstants.Get(),
 13.1942 -        0,
 13.1943 -        NULL,
 13.1944 -        &rendererData->vertexShaderConstantsData,
 13.1945 -        0,
 13.1946 -        0
 13.1947 -        );
 13.1948 -
 13.1949 -    rendererData->d3dContext->IASetPrimitiveTopology(primitiveTopology);
 13.1950 -    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
 13.1951 -    rendererData->d3dContext->VSSetShader(rendererData->vertexShader.Get(), nullptr, 0);
 13.1952 -    rendererData->d3dContext->VSSetConstantBuffers(0, 1, rendererData->vertexShaderConstants.GetAddressOf());
 13.1953 -    if (SDL_RectEmpty(&(renderer->clip_rect))) {
 13.1954 -        rendererData->d3dContext->RSSetState(rendererData->mainRasterizer.Get());
 13.1955 -    } else {
 13.1956 -        rendererData->d3dContext->RSSetState(rendererData->clippedRasterizer.Get());
 13.1957 -    }
 13.1958 -    rendererData->d3dContext->Draw(vertexCount, 0);
 13.1959 -}
 13.1960 -
 13.1961 -static int
 13.1962 -D3D11_RenderDrawPoints(SDL_Renderer * renderer,
 13.1963 -                       const SDL_FPoint * points, int count)
 13.1964 -{
 13.1965 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.1966 -    float r, g, b, a;
 13.1967 -
 13.1968 -    r = (float)(renderer->r / 255.0f);
 13.1969 -    g = (float)(renderer->g / 255.0f);
 13.1970 -    b = (float)(renderer->b / 255.0f);
 13.1971 -    a = (float)(renderer->a / 255.0f);
 13.1972 -
 13.1973 -    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
 13.1974 -    for (int i = 0; i < min(count, 128); ++i) {
 13.1975 -        const VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
 13.1976 -        vertices[i] = v;
 13.1977 -    }
 13.1978 -
 13.1979 -    D3D11_RenderStartDrawOp(renderer);
 13.1980 -    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 13.1981 -    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
 13.1982 -        SDL_stack_free(vertices);
 13.1983 -        return -1;
 13.1984 -    }
 13.1985 -
 13.1986 -    D3D11_SetPixelShader(
 13.1987 -        renderer,
 13.1988 -        rendererData->colorPixelShader.Get(),
 13.1989 -        nullptr,
 13.1990 -        nullptr);
 13.1991 -
 13.1992 -    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, count);
 13.1993 -    SDL_stack_free(vertices);
 13.1994 -    return 0;
 13.1995 -}
 13.1996 -
 13.1997 -static int
 13.1998 -D3D11_RenderDrawLines(SDL_Renderer * renderer,
 13.1999 -                      const SDL_FPoint * points, int count)
 13.2000 -{
 13.2001 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.2002 -    float r, g, b, a;
 13.2003 -
 13.2004 -    r = (float)(renderer->r / 255.0f);
 13.2005 -    g = (float)(renderer->g / 255.0f);
 13.2006 -    b = (float)(renderer->b / 255.0f);
 13.2007 -    a = (float)(renderer->a / 255.0f);
 13.2008 -
 13.2009 -    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
 13.2010 -    for (int i = 0; i < count; ++i) {
 13.2011 -        const VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
 13.2012 -        vertices[i] = v;
 13.2013 -    }
 13.2014 -
 13.2015 -    D3D11_RenderStartDrawOp(renderer);
 13.2016 -    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 13.2017 -    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
 13.2018 -        SDL_stack_free(vertices);
 13.2019 -        return -1;
 13.2020 -    }
 13.2021 -
 13.2022 -    D3D11_SetPixelShader(
 13.2023 -        renderer,
 13.2024 -        rendererData->colorPixelShader.Get(),
 13.2025 -        nullptr,
 13.2026 -        nullptr);
 13.2027 -
 13.2028 -    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, count);
 13.2029 -    SDL_stack_free(vertices);
 13.2030 -    return 0;
 13.2031 -}
 13.2032 -
 13.2033 -static int
 13.2034 -D3D11_RenderFillRects(SDL_Renderer * renderer,
 13.2035 -                      const SDL_FRect * rects, int count)
 13.2036 -{
 13.2037 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.2038 -    float r, g, b, a;
 13.2039 -
 13.2040 -    r = (float)(renderer->r / 255.0f);
 13.2041 -    g = (float)(renderer->g / 255.0f);
 13.2042 -    b = (float)(renderer->b / 255.0f);
 13.2043 -    a = (float)(renderer->a / 255.0f);
 13.2044 -
 13.2045 -#if 0
 13.2046 -    // Set up a test pattern:
 13.2047 -    SDL_FRect _rects[] = {
 13.2048 -        {-1.1f, 1.1f, 1.1f, -1.1f},
 13.2049 -        {-1.0f, 1.0f, 1.0f, -1.0f},     // red
 13.2050 -        {0.0f, 1.0f, 1.0f, -1.0f},      // green
 13.2051 -        {-1.0f, 0.0f, 1.0f, -1.0f},     // blue
 13.2052 -        {0.0f, 0.0f, 1.0f, -1.0f}       // white
 13.2053 -    };
 13.2054 -    count = sizeof(_rects) / sizeof(SDL_FRect);
 13.2055 -#define rects _rects
 13.2056 -#endif
 13.2057 -
 13.2058 -    for (int i = 0; i < count; ++i) {
 13.2059 -        D3D11_RenderStartDrawOp(renderer);
 13.2060 -        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
 13.2061 -
 13.2062 -#if 0
 13.2063 -        // Set colors for the test pattern:
 13.2064 -        a = 1.0f;
 13.2065 -        switch (i) {
 13.2066 -            case 0: r = 1.0f; g = 1.0f; b = 0.0f; break;
 13.2067 -            case 1: r = 1.0f; g = 0.0f; b = 0.0f; break;
 13.2068 -            case 2: r = 0.0f; g = 1.0f; b = 0.0f; break;
 13.2069 -            case 3: r = 0.0f; g = 0.0f; b = 1.0f; break;
 13.2070 -            case 4: r = 1.0f; g = 1.0f; b = 1.0f; break;
 13.2071 -        }
 13.2072 -#endif
 13.2073 -
 13.2074 -        VertexPositionColor vertices[] = {
 13.2075 -            {XMFLOAT3(rects[i].x, rects[i].y, 0.0f),                           XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
 13.2076 -            {XMFLOAT3(rects[i].x, rects[i].y + rects[i].h, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
 13.2077 -            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
 13.2078 -            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y + rects[i].h, 0.0f), XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
 13.2079 -        };
 13.2080 -        if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 13.2081 -            return -1;
 13.2082 -        }
 13.2083 -
 13.2084 -        D3D11_SetPixelShader(
 13.2085 -            renderer,
 13.2086 -            rendererData->colorPixelShader.Get(),
 13.2087 -            nullptr,
 13.2088 -            nullptr);
 13.2089 -
 13.2090 -        D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
 13.2091 -    }
 13.2092 -
 13.2093 -    return 0;
 13.2094 -}
 13.2095 -
 13.2096 -static ID3D11SamplerState *
 13.2097 -D3D11_RenderGetSampler(SDL_Renderer * renderer, SDL_Texture * texture)
 13.2098 -{
 13.2099 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.2100 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.2101 -
 13.2102 -    switch (textureData->scaleMode) {
 13.2103 -        case SDL_D3D11_NEAREST_PIXEL_FILTER:
 13.2104 -            return rendererData->nearestPixelSampler.Get();
 13.2105 -        case SDL_D3D11_LINEAR_FILTER:
 13.2106 -            return rendererData->linearSampler.Get();
 13.2107 -        default:
 13.2108 -            return NULL;
 13.2109 -    }
 13.2110 -}
 13.2111 -
 13.2112 -static int
 13.2113 -D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
 13.2114 -                 const SDL_Rect * srcrect, const SDL_FRect * dstrect)
 13.2115 -{
 13.2116 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.2117 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.2118 -
 13.2119 -    D3D11_RenderStartDrawOp(renderer);
 13.2120 -    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
 13.2121 -
 13.2122 -    float minu = (float) srcrect->x / texture->w;
 13.2123 -    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
 13.2124 -    float minv = (float) srcrect->y / texture->h;
 13.2125 -    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
 13.2126 -
 13.2127 -    float r = 1.0f;
 13.2128 -    float g = 1.0f;
 13.2129 -    float b = 1.0f;
 13.2130 -    float a = 1.0f;
 13.2131 -    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
 13.2132 -        r = (float)(texture->r / 255.0f);
 13.2133 -        g = (float)(texture->g / 255.0f);
 13.2134 -        b = (float)(texture->b / 255.0f);
 13.2135 -    }
 13.2136 -    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
 13.2137 -        a = (float)(texture->a / 255.0f);
 13.2138 -    }
 13.2139 -
 13.2140 -    VertexPositionColor vertices[] = {
 13.2141 -        {XMFLOAT3(dstrect->x, dstrect->y, 0.0f),                           XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
 13.2142 -        {XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f),              XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
 13.2143 -        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y, 0.0f),              XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
 13.2144 -        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
 13.2145 -    };
 13.2146 -    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 13.2147 -        return -1;
 13.2148 -    }
 13.2149 -
 13.2150 -    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
 13.2151 -    D3D11_SetPixelShader(
 13.2152 -        renderer,
 13.2153 -        rendererData->texturePixelShader.Get(),
 13.2154 -        textureData->mainTextureResourceView.Get(),
 13.2155 -        textureSampler);
 13.2156 -
 13.2157 -    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
 13.2158 -
 13.2159 -    return 0;
 13.2160 -}
 13.2161 -
 13.2162 -static int
 13.2163 -D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
 13.2164 -                   const SDL_Rect * srcrect, const SDL_FRect * dstrect,
 13.2165 -                   const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
 13.2166 -{
 13.2167 -    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
 13.2168 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
 13.2169 -
 13.2170 -    D3D11_RenderStartDrawOp(renderer);
 13.2171 -    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
 13.2172 -
 13.2173 -    float minu = (float) srcrect->x / texture->w;
 13.2174 -    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
 13.2175 -    float minv = (float) srcrect->y / texture->h;
 13.2176 -    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
 13.2177 -
 13.2178 -    float r = 1.0f;
 13.2179 -    float g = 1.0f;
 13.2180 -    float b = 1.0f;
 13.2181 -    float a = 1.0f;
 13.2182 -    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
 13.2183 -        r = (float)(texture->r / 255.0f);
 13.2184 -        g = (float)(texture->g / 255.0f);
 13.2185 -        b = (float)(texture->b / 255.0f);
 13.2186 -    }
 13.2187 -    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
 13.2188 -        a = (float)(texture->a / 255.0f);
 13.2189 -    }
 13.2190 -
 13.2191 -    if (flip & SDL_FLIP_HORIZONTAL) {
 13.2192 -        float tmp = maxu;
 13.2193 -        maxu = minu;
 13.2194 -        minu = tmp;
 13.2195 -    }
 13.2196 -    if (flip & SDL_FLIP_VERTICAL) {
 13.2197 -        float tmp = maxv;
 13.2198 -        maxv = minv;
 13.2199 -        minv = tmp;
 13.2200 -    }
 13.2201 -
 13.2202 -    XMFLOAT4X4 oldModelMatrix = rendererData->vertexShaderConstantsData.model;
 13.2203 -    XMStoreFloat4x4(
 13.2204 -        &rendererData->vertexShaderConstantsData.model,
 13.2205 -        XMMatrixMultiply(
 13.2206 -            XMMatrixRotationZ((float)(XM_PI * (float) angle / 180.0f)),
 13.2207 -            XMMatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
 13.2208 -            ));
 13.2209 -
 13.2210 -    const float minx = -center->x;
 13.2211 -    const float maxx = dstrect->w - center->x;
 13.2212 -    const float miny = -center->y;
 13.2213 -    const float maxy = dstrect->h - center->y;
 13.2214 -
 13.2215 -    VertexPositionColor vertices[] = {
 13.2216 -        {XMFLOAT3(minx, miny, 0.0f), XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
 13.2217 -        {XMFLOAT3(minx, maxy, 0.0f), XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
 13.2218 -        {XMFLOAT3(maxx, miny, 0.0f), XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
 13.2219 -        {XMFLOAT3(maxx, maxy, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
 13.2220 -    };
 13.2221 -    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
 13.2222 -        return -1;
 13.2223 -    }
 13.2224 -
 13.2225 -    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
 13.2226 -    D3D11_SetPixelShader(
 13.2227 -        renderer,
 13.2228 -        rendererData->texturePixelShader.Get(),
 13.2229 -        textureData->mainTextureResourceView.Get(),
 13.2230 -        textureSampler);
 13.2231 -
 13.2232 -    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
 13.2233 -
 13.2234 -    rendererData->vertexShaderConstantsData.model = oldModelMatrix;
 13.2235 -
 13.2236 -    return 0;
 13.2237 -}
 13.2238 -
 13.2239 -static int
 13.2240 -D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 13.2241 -                       Uint32 format, void * pixels, int pitch)
 13.2242 -{
 13.2243 -    D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
 13.2244 -    HRESULT result = S_OK;
 13.2245 -
 13.2246 -    // Retrieve a pointer to the back buffer:
 13.2247 -    ComPtr<ID3D11Texture2D> backBuffer;
 13.2248 -    result = data->swapChain->GetBuffer(
 13.2249 -        0,
 13.2250 -        __uuidof(ID3D11Texture2D),
 13.2251 -        &backBuffer
 13.2252 -        );
 13.2253 -    if (FAILED(result)) {
 13.2254 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::GetBuffer [get back buffer]", result);
 13.2255 -        return -1;
 13.2256 -    }
 13.2257 -
 13.2258 -    // Create a staging texture to copy the screen's data to:
 13.2259 -    ComPtr<ID3D11Texture2D> stagingTexture;
 13.2260 -    D3D11_TEXTURE2D_DESC stagingTextureDesc;
 13.2261 -    backBuffer->GetDesc(&stagingTextureDesc);
 13.2262 -    stagingTextureDesc.Width = rect->w;
 13.2263 -    stagingTextureDesc.Height = rect->h;
 13.2264 -    stagingTextureDesc.BindFlags = 0;
 13.2265 -    stagingTextureDesc.MiscFlags = 0;
 13.2266 -    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
 13.2267 -    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
 13.2268 -    result = data->d3dDevice->CreateTexture2D(
 13.2269 -        &stagingTextureDesc,
 13.2270 -        NULL,
 13.2271 -        &stagingTexture);
 13.2272 -    if (FAILED(result)) {
 13.2273 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
 13.2274 -        return -1;
 13.2275 -    }
 13.2276 -
 13.2277 -    // Copy the desired portion of the back buffer to the staging texture:
 13.2278 -    D3D11_RECT srcRect;
 13.2279 -    if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
 13.2280 -        /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
 13.2281 -        return -1;
 13.2282 -    }
 13.2283 -
 13.2284 -    D3D11_BOX srcBox;
 13.2285 -    srcBox.left = srcRect.left;
 13.2286 -    srcBox.right = srcRect.right;
 13.2287 -    srcBox.top = srcRect.top;
 13.2288 -    srcBox.bottom = srcRect.bottom;
 13.2289 -    srcBox.front = 0;
 13.2290 -    srcBox.back = 1;
 13.2291 -    data->d3dContext->CopySubresourceRegion(
 13.2292 -        stagingTexture.Get(),
 13.2293 -        D3D11CalcSubresource(0, 0, 0),
 13.2294 -        0, 0, 0,
 13.2295 -        backBuffer.Get(),
 13.2296 -        D3D11CalcSubresource(0, 0, 0),
 13.2297 -        &srcBox);
 13.2298 -
 13.2299 -    // Map the staging texture's data to CPU-accessible memory:
 13.2300 -    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
 13.2301 -    result = data->d3dContext->Map(
 13.2302 -        stagingTexture.Get(),
 13.2303 -        D3D11CalcSubresource(0, 0, 0),
 13.2304 -        D3D11_MAP_READ,
 13.2305 -        0,
 13.2306 -        &textureMemory);
 13.2307 -    if (FAILED(result)) {
 13.2308 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
 13.2309 -        return -1;
 13.2310 -    }
 13.2311 -
 13.2312 -    // Copy the data into the desired buffer, converting pixels to the
 13.2313 -    // desired format at the same time:
 13.2314 -    if (SDL_ConvertPixels(
 13.2315 -        rect->w, rect->h,
 13.2316 -        DXGIFormatToSDLPixelFormat(stagingTextureDesc.Format),
 13.2317 -        textureMemory.pData,
 13.2318 -        textureMemory.RowPitch,
 13.2319 -        format,
 13.2320 -        pixels,
 13.2321 -        pitch) != 0)
 13.2322 -    {
 13.2323 -        // When SDL_ConvertPixels fails, it'll have already set the format.
 13.2324 -        // Get the error message, and attach some extra data to it.
 13.2325 -        std::string errorMessage = string(__FUNCTION__ ", Convert Pixels failed: ") + SDL_GetError();
 13.2326 -        return SDL_SetError(errorMessage.c_str());
 13.2327 -    }
 13.2328 -
 13.2329 -    // Unmap the texture:
 13.2330 -    data->d3dContext->Unmap(
 13.2331 -        stagingTexture.Get(),
 13.2332 -        D3D11CalcSubresource(0, 0, 0));
 13.2333 -
 13.2334 -    // All done.  The staging texture will be cleaned up in it's container
 13.2335 -    // ComPtr<>'s destructor.
 13.2336 -    return 0;
 13.2337 -}
 13.2338 -
 13.2339 -static void
 13.2340 -D3D11_RenderPresent(SDL_Renderer * renderer)
 13.2341 -{
 13.2342 -    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
 13.2343 -
 13.2344 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 13.2345 -    // The first argument instructs DXGI to block until VSync, putting the application
 13.2346 -    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
 13.2347 -    // frames that will never be displayed to the screen.
 13.2348 -    HRESULT hr = data->swapChain->Present(1, 0);
 13.2349 -#else
 13.2350 -    // The application may optionally specify "dirty" or "scroll"
 13.2351 -    // rects to improve efficiency in certain scenarios.
 13.2352 -    // This option is not available on Windows Phone 8, to note.
 13.2353 -    DXGI_PRESENT_PARAMETERS parameters = {0};
 13.2354 -    parameters.DirtyRectsCount = 0;
 13.2355 -    parameters.pDirtyRects = nullptr;
 13.2356 -    parameters.pScrollRect = nullptr;
 13.2357 -    parameters.pScrollOffset = nullptr;
 13.2358 -    
 13.2359 -    // The first argument instructs DXGI to block until VSync, putting the application
 13.2360 -    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
 13.2361 -    // frames that will never be displayed to the screen.
 13.2362 -    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
 13.2363 -#endif
 13.2364 -
 13.2365 -    // Discard the contents of the render target.
 13.2366 -    // This is a valid operation only when the existing contents will be entirely
 13.2367 -    // overwritten. If dirty or scroll rects are used, this call should be removed.
 13.2368 -    data->d3dContext->DiscardView(data->mainRenderTargetView.Get());
 13.2369 -
 13.2370 -    // If the device was removed either by a disconnect or a driver upgrade, we 
 13.2371 -    // must recreate all device resources.
 13.2372 -    //
 13.2373 -    // TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvedge debug info from users' machines
 13.2374 -    if (hr == DXGI_ERROR_DEVICE_REMOVED)
 13.2375 -    {
 13.2376 -        hr = D3D11_HandleDeviceLost(renderer);
 13.2377 -        if (FAILED(hr)) {
 13.2378 -            /* D3D11_HandleDeviceLost will set the SDL error */
 13.2379 -        }
 13.2380 -    }
 13.2381 -    else
 13.2382 -    {
 13.2383 -        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::DiscardView", hr);
 13.2384 -    }
 13.2385 -}
 13.2386 -
 13.2387 -#endif /* SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED */
 13.2388 -
 13.2389 -/* vi: set ts=4 sw=4 expandtab: */