src/render/direct3d11/SDL_render_d3d11.cpp
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 Mar 2014 11:06:11 -0700
changeset 8582 c3e9a2b93517
parent 8575 97a2e1b1a019
permissions -rw-r--r--
Fixed line endings on WinRT source code
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8463
    26
#ifdef __WINRT__
dludwig@8463
    27
#include <windows.ui.core.h>
dludwig@8463
    28
#include <windows.foundation.h>
dludwig@8505
    29
dludwig@8505
    30
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    31
#include <windows.ui.xaml.media.dxinterop.h>
dludwig@8505
    32
#endif
dludwig@8505
    33
dludwig@8463
    34
#endif
dludwig@8463
    35
dludwig@8400
    36
extern "C" {
dludwig@8400
    37
#include "../../core/windows/SDL_windows.h"
dludwig@8533
    38
#include "SDL_hints.h"
dludwig@8410
    39
#include "SDL_system.h"
dludwig@8400
    40
#include "SDL_syswm.h"
dludwig@8400
    41
#include "../SDL_sysrender.h"
dludwig@8433
    42
#include "../../video/SDL_sysvideo.h"
dludwig@8400
    43
}
dludwig@8400
    44
dludwig@8410
    45
#include <string>
dludwig@8410
    46
#include <vector>
dludwig@8410
    47
dludwig@8559
    48
#include <D3D11_1.h>
dludwig@8559
    49
#include <DirectXMath.h>
dludwig@8559
    50
#include <wrl/client.h>
dludwig@8559
    51
dludwig@8400
    52
dludwig@8410
    53
using namespace DirectX;
dludwig@8410
    54
using namespace Microsoft::WRL;
dludwig@8410
    55
using namespace std;
dludwig@8410
    56
dludwig@8412
    57
#ifdef __WINRT__
dludwig@8412
    58
using namespace Windows::Graphics::Display;
dludwig@8412
    59
using namespace Windows::UI::Core;
dludwig@8412
    60
#endif
dludwig@8412
    61
dludwig@8540
    62
/* Texture sampling types */
dludwig@8540
    63
static const D3D11_FILTER SDL_D3D11_NEAREST_PIXEL_FILTER = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8540
    64
static const D3D11_FILTER SDL_D3D11_LINEAR_FILTER = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
dludwig@8540
    65
dludwig@8559
    66
/* Vertex shader, common values */
slouken@8582
    67
struct VertexShaderConstants
slouken@8582
    68
{
slouken@8582
    69
    DirectX::XMFLOAT4X4 model;
slouken@8582
    70
    DirectX::XMFLOAT4X4 projectionAndView;
dludwig@8559
    71
};
dludwig@8559
    72
dludwig@8559
    73
/* Per-vertex data */
slouken@8582
    74
struct VertexPositionColor
slouken@8582
    75
{
slouken@8582
    76
    DirectX::XMFLOAT3 pos;
slouken@8582
    77
    DirectX::XMFLOAT2 tex;
slouken@8582
    78
    DirectX::XMFLOAT4 color;
dludwig@8559
    79
};
dludwig@8559
    80
dludwig@8559
    81
/* Per-texture data */
dludwig@8559
    82
typedef struct
dludwig@8559
    83
{
dludwig@8559
    84
    Microsoft::WRL::ComPtr<ID3D11Texture2D> mainTexture;
dludwig@8559
    85
    Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> mainTextureResourceView;
slouken@8582
    86
    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainTextureRenderTargetView;
dludwig@8559
    87
    SDL_PixelFormat * pixelFormat;
dludwig@8559
    88
    Microsoft::WRL::ComPtr<ID3D11Texture2D> stagingTexture;
dludwig@8559
    89
    DirectX::XMINT2 lockedTexturePosition;
dludwig@8559
    90
    D3D11_FILTER scaleMode;
dludwig@8559
    91
} D3D11_TextureData;
dludwig@8559
    92
dludwig@8559
    93
/* Private renderer data */
dludwig@8559
    94
typedef struct
dludwig@8559
    95
{
dludwig@8559
    96
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice;
slouken@8582
    97
    Microsoft::WRL::ComPtr<ID3D11DeviceContext1> d3dContext;
slouken@8582
    98
    Microsoft::WRL::ComPtr<IDXGISwapChain1> swapChain;
slouken@8582
    99
    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainRenderTargetView;
slouken@8582
   100
    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> currentOffscreenRenderTargetView;
slouken@8582
   101
    Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout;
slouken@8582
   102
    Microsoft::WRL::ComPtr<ID3D11Buffer> vertexBuffer;
slouken@8582
   103
    Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
slouken@8582
   104
    Microsoft::WRL::ComPtr<ID3D11PixelShader> texturePixelShader;
slouken@8582
   105
    Microsoft::WRL::ComPtr<ID3D11PixelShader> colorPixelShader;
slouken@8582
   106
    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend;
slouken@8582
   107
    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd;
slouken@8582
   108
    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod;
dludwig@8559
   109
    Microsoft::WRL::ComPtr<ID3D11SamplerState> nearestPixelSampler;
dludwig@8559
   110
    Microsoft::WRL::ComPtr<ID3D11SamplerState> linearSampler;
dludwig@8569
   111
    D3D_FEATURE_LEVEL featureLevel;
dludwig@8569
   112
dludwig@8569
   113
    // Rasterizers:
dludwig@8569
   114
    // If this list starts to get unwieldy, then consider using a map<> of them.
dludwig@8559
   115
    Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
dludwig@8569
   116
    Microsoft::WRL::ComPtr<ID3D11RasterizerState> clippedRasterizer;
dludwig@8559
   117
dludwig@8559
   118
    // Vertex buffer constants:
dludwig@8560
   119
    VertexShaderConstants vertexShaderConstantsData;
dludwig@8559
   120
    Microsoft::WRL::ComPtr<ID3D11Buffer> vertexShaderConstants;
dludwig@8559
   121
dludwig@8559
   122
    // Cached renderer properties.
slouken@8582
   123
    DirectX::XMFLOAT2 windowSizeInDIPs;
dludwig@8559
   124
    DirectX::XMFLOAT2 renderTargetSize;
dludwig@8559
   125
    Windows::Graphics::Display::DisplayOrientations orientation;
dludwig@8559
   126
slouken@8582
   127
    // Transform used for display orientation.
dludwig@8559
   128
    DirectX::XMFLOAT4X4 orientationTransform3D;
dludwig@8559
   129
} D3D11_RenderData;
dludwig@8559
   130
dludwig@8559
   131
dludwig@8563
   132
/* Direct3D 11.x shaders
dludwig@8563
   133
dludwig@8563
   134
   SDL's shaders are compiled into SDL itself, to simplify distribution.
dludwig@8563
   135
dludwig@8563
   136
   All Direct3D 11.x shaders were compiled with the following:
slouken@8582
   137
slouken@8582
   138
   fxc /E"main" /T "<TYPE>" /Fo"<OUTPUT FILE>" "<INPUT FILE>"
slouken@8582
   139
slouken@8582
   140
     Variables:
slouken@8582
   141
     - <TYPE>: the type of shader.  A table of utilized shader types is
slouken@8582
   142
       listed below.
slouken@8582
   143
     - <OUTPUT FILE>: where to store compiled output
slouken@8582
   144
     - <INPUT FILE>: where to read shader source code from
slouken@8582
   145
slouken@8582
   146
     Shader types:
slouken@8582
   147
     - ps_4_0_level_9_1: Pixel shader for Windows 8+, including Windows RT
slouken@8582
   148
     - vs_4_0_level_9_1: Vertex shader for Windows 8+, including Windows RT
slouken@8582
   149
     - ps_4_0_level_9_3: Pixel shader for Windows Phone 8
slouken@8582
   150
     - vs_4_0_level_9_3: Vertex shader for Windows Phone 8
slouken@8582
   151
   
slouken@8582
   152
slouken@8582
   153
   Shader object code was converted to a list of DWORDs via the following
slouken@8582
   154
   *nix style command (available separately from Windows + MSVC):
slouken@8582
   155
slouken@8582
   156
     hexdump -v -e '6/4 "0x%08.8x, " "\n"' <FILE>
slouken@8582
   157
  */
slouken@8582
   158
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
   159
#define D3D11_USE_SHADER_MODEL_4_0_level_9_3
slouken@8582
   160
#else
slouken@8582
   161
#define D3D11_USE_SHADER_MODEL_4_0_level_9_1
slouken@8582
   162
#endif
slouken@8582
   163
slouken@8582
   164
/* The texture-rendering pixel shader:
slouken@8582
   165
slouken@8582
   166
    --- D3D11_PixelShader_Textures.hlsl ---
slouken@8582
   167
    Texture2D theTexture : register(t0);
slouken@8582
   168
    SamplerState theSampler : register(s0);
slouken@8582
   169
slouken@8582
   170
    struct PixelShaderInput
slouken@8582
   171
    {
slouken@8582
   172
        float4 pos : SV_POSITION;
slouken@8582
   173
        float2 tex : TEXCOORD0;
slouken@8582
   174
        float4 color : COLOR0;
slouken@8582
   175
    };
slouken@8582
   176
slouken@8582
   177
    float4 main(PixelShaderInput input) : SV_TARGET
slouken@8582
   178
    {
slouken@8582
   179
        return theTexture.Sample(theSampler, input.tex) * input.color;
slouken@8582
   180
    }
slouken@8582
   181
*/
slouken@8582
   182
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
slouken@8582
   183
static const DWORD D3D11_PixelShader_Textures[] = {
slouken@8582
   184
    0x43425844, 0x6299b59f, 0x155258f2, 0x873ab86a, 0xfcbb6dcd, 0x00000001,
slouken@8582
   185
    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
slouken@8582
   186
    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
slouken@8582
   187
    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
slouken@8582
   188
    0x00280000, 0x00000000, 0xffff0200, 0x0200001f, 0x80000000, 0xb0030000,
slouken@8582
   189
    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
slouken@8582
   190
    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
slouken@8582
   191
    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
slouken@8582
   192
    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
slouken@8582
   193
    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
slouken@8582
   194
    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
slouken@8582
   195
    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
slouken@8582
   196
    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
slouken@8582
   197
    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
slouken@8582
   198
    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
slouken@8582
   199
    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
slouken@8582
   200
    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   201
    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   202
    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   203
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
slouken@8582
   204
    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
slouken@8582
   205
    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   206
    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
slouken@8582
   207
    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
slouken@8582
   208
    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
slouken@8582
   209
    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
slouken@8582
   210
    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
slouken@8582
   211
    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
slouken@8582
   212
    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
slouken@8582
   213
    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
slouken@8582
   214
    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
slouken@8582
   215
    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
slouken@8582
   216
    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
slouken@8582
   217
    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
dludwig@8563
   218
};
dludwig@8563
   219
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
dludwig@8563
   220
static const DWORD D3D11_PixelShader_Textures[] = {
slouken@8582
   221
    0x43425844, 0x5876569a, 0x01b6c87e, 0x8447454f, 0xc7f3ef10, 0x00000001,
slouken@8582
   222
    0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
slouken@8582
   223
    0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
slouken@8582
   224
    0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
slouken@8582
   225
    0x00280000, 0x00000000, 0xffff0201, 0x0200001f, 0x80000000, 0xb0030000,
slouken@8582
   226
    0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
slouken@8582
   227
    0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
slouken@8582
   228
    0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
slouken@8582
   229
    0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
slouken@8582
   230
    0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
slouken@8582
   231
    0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
slouken@8582
   232
    0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
slouken@8582
   233
    0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
slouken@8582
   234
    0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
slouken@8582
   235
    0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
slouken@8582
   236
    0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
slouken@8582
   237
    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   238
    0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   239
    0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   240
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
slouken@8582
   241
    0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
slouken@8582
   242
    0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   243
    0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
slouken@8582
   244
    0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
slouken@8582
   245
    0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
slouken@8582
   246
    0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
slouken@8582
   247
    0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
slouken@8582
   248
    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
slouken@8582
   249
    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
slouken@8582
   250
    0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
slouken@8582
   251
    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
slouken@8582
   252
    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
slouken@8582
   253
    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
dludwig@8563
   254
    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
dludwig@8563
   255
};
dludwig@8563
   256
#else
dludwig@8563
   257
#error "An appropriate 'textures' pixel shader is not defined"
dludwig@8563
   258
#endif
dludwig@8563
   259
dludwig@8563
   260
/* The color-only-rendering pixel shader:
dludwig@8563
   261
dludwig@8563
   262
   --- D3D11_PixelShader_Colors.hlsl ---
slouken@8582
   263
   struct PixelShaderInput
slouken@8582
   264
   {
slouken@8582
   265
       float4 pos : SV_POSITION;
slouken@8582
   266
       float2 tex : TEXCOORD0;
slouken@8582
   267
       float4 color : COLOR0;
slouken@8582
   268
   };
slouken@8582
   269
slouken@8582
   270
   float4 main(PixelShaderInput input) : SV_TARGET
slouken@8582
   271
   {
slouken@8582
   272
       return input.color;
slouken@8582
   273
   }
slouken@8582
   274
*/
dludwig@8563
   275
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
slouken@8582
   276
static const DWORD D3D11_PixelShader_Colors[] = {
slouken@8582
   277
    0x43425844, 0xd74c28fe, 0xa1eb8804, 0x269d512a, 0x7699723d, 0x00000001,
slouken@8582
   278
    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
slouken@8582
   279
    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
slouken@8582
   280
    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
slouken@8582
   281
    0x00240000, 0xffff0200, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
slouken@8582
   282
    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
slouken@8582
   283
    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
slouken@8582
   284
    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
slouken@8582
   285
    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
slouken@8582
   286
    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
slouken@8582
   287
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   288
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
slouken@8582
   289
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   290
    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
slouken@8582
   291
    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
slouken@8582
   292
    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
slouken@8582
   293
    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
slouken@8582
   294
    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
slouken@8582
   295
    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
slouken@8582
   296
    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
slouken@8582
   297
    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
slouken@8582
   298
    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
slouken@8582
   299
    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
slouken@8582
   300
    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
slouken@8582
   301
};
slouken@8582
   302
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
slouken@8582
   303
static const DWORD D3D11_PixelShader_Colors[] = {
slouken@8582
   304
    0x43425844, 0x93f6ccfc, 0x5f919270, 0x7a11aa4f, 0x9148e931, 0x00000001,
slouken@8582
   305
    0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
slouken@8582
   306
    0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
slouken@8582
   307
    0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
slouken@8582
   308
    0x00240000, 0xffff0201, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
slouken@8582
   309
    0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
slouken@8582
   310
    0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
slouken@8582
   311
    0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
slouken@8582
   312
    0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
slouken@8582
   313
    0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
slouken@8582
   314
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   315
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
slouken@8582
   316
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   317
    0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
slouken@8582
   318
    0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
slouken@8582
   319
    0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
slouken@8582
   320
    0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
slouken@8582
   321
    0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
slouken@8582
   322
    0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
slouken@8582
   323
    0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
slouken@8582
   324
    0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
slouken@8582
   325
    0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
slouken@8582
   326
    0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
slouken@8582
   327
    0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
slouken@8582
   328
};
dludwig@8563
   329
#else
dludwig@8563
   330
#error "An appropriate 'colors' pixel shader is not defined."
dludwig@8563
   331
#endif
dludwig@8563
   332
dludwig@8563
   333
/* The sole vertex shader:
dludwig@8563
   334
dludwig@8563
   335
   --- D3D11_VertexShader.hlsl ---
slouken@8582
   336
   #pragma pack_matrix( row_major )
slouken@8582
   337
slouken@8582
   338
   cbuffer VertexShaderConstants : register(b0)
slouken@8582
   339
   {
slouken@8582
   340
       matrix model;
slouken@8582
   341
       matrix projectionAndView;
slouken@8582
   342
   };
slouken@8582
   343
slouken@8582
   344
   struct VertexShaderInput
slouken@8582
   345
   {
slouken@8582
   346
       float3 pos : POSITION;
slouken@8582
   347
       float2 tex : TEXCOORD0;
slouken@8582
   348
       float4 color : COLOR0;
slouken@8582
   349
   };
slouken@8582
   350
slouken@8582
   351
   struct VertexShaderOutput
slouken@8582
   352
   {
slouken@8582
   353
       float4 pos : SV_POSITION;
slouken@8582
   354
       float2 tex : TEXCOORD0;
slouken@8582
   355
       float4 color : COLOR0;
slouken@8582
   356
   };
slouken@8582
   357
slouken@8582
   358
   VertexShaderOutput main(VertexShaderInput input)
slouken@8582
   359
   {
slouken@8582
   360
       VertexShaderOutput output;
slouken@8582
   361
       float4 pos = float4(input.pos, 1.0f);
slouken@8582
   362
slouken@8582
   363
       // Transform the vertex position into projected space.
slouken@8582
   364
       pos = mul(pos, model);
slouken@8582
   365
       pos = mul(pos, projectionAndView);
slouken@8582
   366
       output.pos = pos;
slouken@8582
   367
slouken@8582
   368
       // Pass through texture coordinates and color values without transformation
slouken@8582
   369
       output.tex = input.tex;
slouken@8582
   370
       output.color = input.color;
slouken@8582
   371
slouken@8582
   372
       return output;
slouken@8582
   373
   }
dludwig@8563
   374
*/
dludwig@8563
   375
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
dludwig@8563
   376
static const DWORD D3D11_VertexShader[] = {
slouken@8582
   377
    0x43425844, 0x62dfae5f, 0x3e8bd8df, 0x9ec97127, 0x5044eefb, 0x00000001,
slouken@8582
   378
    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
slouken@8582
   379
    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
slouken@8582
   380
    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
slouken@8582
   381
    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0200,
slouken@8582
   382
    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
slouken@8582
   383
    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
slouken@8582
   384
    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
slouken@8582
   385
    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
slouken@8582
   386
    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
slouken@8582
   387
    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
slouken@8582
   388
    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
slouken@8582
   389
    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
slouken@8582
   390
    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
slouken@8582
   391
    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
slouken@8582
   392
    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
slouken@8582
   393
    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
slouken@8582
   394
    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
slouken@8582
   395
    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
slouken@8582
   396
    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
slouken@8582
   397
    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
slouken@8582
   398
    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
slouken@8582
   399
    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
slouken@8582
   400
    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
slouken@8582
   401
    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
slouken@8582
   402
    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
slouken@8582
   403
    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
slouken@8582
   404
    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
slouken@8582
   405
    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
slouken@8582
   406
    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
slouken@8582
   407
    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
slouken@8582
   408
    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
slouken@8582
   409
    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
slouken@8582
   410
    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
slouken@8582
   411
    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
slouken@8582
   412
    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
slouken@8582
   413
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   414
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
slouken@8582
   415
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   416
    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
slouken@8582
   417
    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
slouken@8582
   418
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
slouken@8582
   419
    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
slouken@8582
   420
    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
slouken@8582
   421
    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
slouken@8582
   422
    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
slouken@8582
   423
    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
slouken@8582
   424
    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
slouken@8582
   425
    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
slouken@8582
   426
    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
slouken@8582
   427
    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
slouken@8582
   428
    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
slouken@8582
   429
    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
slouken@8582
   430
    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
slouken@8582
   431
    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
slouken@8582
   432
    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
slouken@8582
   433
    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
slouken@8582
   434
    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
slouken@8582
   435
    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
dludwig@8570
   436
    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
dludwig@8563
   437
};
dludwig@8563
   438
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
dludwig@8563
   439
static const DWORD D3D11_VertexShader[] = {
slouken@8582
   440
    0x43425844, 0x01a24e41, 0x696af551, 0x4b2a87d1, 0x82ea03f6, 0x00000001,
slouken@8582
   441
    0x00000598, 0x00000006, 0x00000038, 0x0000016c, 0x00000334, 0x000003b0,
slouken@8582
   442
    0x000004b4, 0x00000524, 0x396e6f41, 0x0000012c, 0x0000012c, 0xfffe0200,
slouken@8582
   443
    0x000000f8, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
slouken@8582
   444
    0x00300001, 0x00000000, 0x00010008, 0x00000000, 0x00000000, 0xfffe0201,
slouken@8582
   445
    0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
slouken@8582
   446
    0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
slouken@8582
   447
    0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
slouken@8582
   448
    0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
slouken@8582
   449
    0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
slouken@8582
   450
    0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
slouken@8582
   451
    0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
slouken@8582
   452
    0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x04000004, 0xc0030000,
slouken@8582
   453
    0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000, 0x80e40000,
slouken@8582
   454
    0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001, 0x90e40002,
slouken@8582
   455
    0x0000ffff, 0x52444853, 0x000001c0, 0x00010040, 0x00000070, 0x04000059,
slouken@8582
   456
    0x00208e46, 0x00000000, 0x00000008, 0x0300005f, 0x00101072, 0x00000000,
slouken@8582
   457
    0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2, 0x00000002,
slouken@8582
   458
    0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032,
slouken@8582
   459
    0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068, 0x00000002,
slouken@8582
   460
    0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000, 0x00208e46,
slouken@8582
   461
    0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000, 0x00101006,
slouken@8582
   462
    0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46, 0x00000000,
slouken@8582
   463
    0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000, 0x00208e46,
slouken@8582
   464
    0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000, 0x001000f2,
slouken@8582
   465
    0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000, 0x00000003,
slouken@8582
   466
    0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000, 0x00208e46,
slouken@8582
   467
    0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001, 0x00100006,
slouken@8582
   468
    0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46, 0x00000001,
slouken@8582
   469
    0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000, 0x00208e46,
slouken@8582
   470
    0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032, 0x001020f2,
slouken@8582
   471
    0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000, 0x00000007,
slouken@8582
   472
    0x00100e46, 0x00000001, 0x05000036, 0x00102032, 0x00000001, 0x00101046,
slouken@8582
   473
    0x00000001, 0x05000036, 0x001020f2, 0x00000002, 0x00101e46, 0x00000002,
slouken@8582
   474
    0x0100003e, 0x54415453, 0x00000074, 0x0000000b, 0x00000002, 0x00000000,
slouken@8582
   475
    0x00000006, 0x00000003, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
slouken@8582
   476
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   477
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
slouken@8582
   478
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
slouken@8582
   479
    0x00000000, 0x00000000, 0x46454452, 0x000000fc, 0x00000001, 0x00000054,
slouken@8582
   480
    0x00000001, 0x0000001c, 0xfffe0400, 0x00000100, 0x000000c6, 0x0000003c,
slouken@8582
   481
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
slouken@8582
   482
    0x00000001, 0x74726556, 0x68537865, 0x72656461, 0x736e6f43, 0x746e6174,
slouken@8582
   483
    0xabab0073, 0x0000003c, 0x00000002, 0x0000006c, 0x00000080, 0x00000000,
slouken@8582
   484
    0x00000000, 0x0000009c, 0x00000000, 0x00000040, 0x00000002, 0x000000a4,
slouken@8582
   485
    0x00000000, 0x000000b4, 0x00000040, 0x00000040, 0x00000002, 0x000000a4,
slouken@8582
   486
    0x00000000, 0x65646f6d, 0xabab006c, 0x00030002, 0x00040004, 0x00000000,
slouken@8582
   487
    0x00000000, 0x6a6f7270, 0x69746365, 0x6e416e6f, 0x65695664, 0x694d0077,
slouken@8582
   488
    0x736f7263, 0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564,
slouken@8582
   489
    0x706d6f43, 0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336,
slouken@8582
   490
    0xababab00, 0x4e475349, 0x00000068, 0x00000003, 0x00000008, 0x00000050,
slouken@8582
   491
    0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000707, 0x00000059,
slouken@8582
   492
    0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000062,
slouken@8582
   493
    0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x49534f50,
slouken@8582
   494
    0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044, 0x00524f4c, 0x4e47534f,
slouken@8582
   495
    0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001,
slouken@8582
   496
    0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000,
slouken@8582
   497
    0x00000003, 0x00000001, 0x00000c03, 0x00000065, 0x00000000, 0x00000000,
slouken@8582
   498
    0x00000003, 0x00000002, 0x0000000f, 0x505f5653, 0x5449534f, 0x004e4f49,
slouken@8582
   499
    0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
dludwig@8563
   500
};
dludwig@8563
   501
#else
dludwig@8563
   502
#error "An appropriate vertex shader is not defined."
dludwig@8563
   503
#endif
dludwig@8563
   504
dludwig@8410
   505
/* Direct3D 11.1 renderer implementation */
dludwig@8400
   506
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8415
   507
static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8415
   508
                            const SDL_WindowEvent *event);
dludwig@8416
   509
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8416
   510
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8459
   511
                             const SDL_Rect * rect, const void *srcPixels,
dludwig@8459
   512
                             int srcPitch);
dludwig@8451
   513
static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8451
   514
                             const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8451
   515
static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8459
   516
static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
   517
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8482
   518
static int D3D11_UpdateClipRect(SDL_Renderer * renderer);
dludwig@8416
   519
static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8450
   520
static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
   521
                                  const SDL_FPoint * points, int count);
dludwig@8449
   522
static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
   523
                                 const SDL_FPoint * points, int count);
dludwig@8429
   524
static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
   525
                                 const SDL_FRect * rects, int count);
dludwig@8416
   526
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   527
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8455
   528
static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8455
   529
                              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8455
   530
                              const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8454
   531
static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8454
   532
                                  Uint32 format, void * pixels, int pitch);
dludwig@8401
   533
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8416
   534
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
   535
                                 SDL_Texture * texture);
dludwig@8413
   536
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
   537
dludwig@8410
   538
/* Direct3D 11.1 Internal Functions */
dludwig@8412
   539
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
   540
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8414
   541
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
dludwig@8414
   542
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
dludwig@8400
   543
dludwig@8442
   544
extern "C" SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
   545
    D3D11_CreateRenderer,
dludwig@8400
   546
    {
dludwig@8442
   547
        "direct3d 11.1",
dludwig@8459
   548
        (
dludwig@8459
   549
            SDL_RENDERER_ACCELERATED |
dludwig@8459
   550
            SDL_RENDERER_PRESENTVSYNC |
dludwig@8459
   551
            SDL_RENDERER_TARGETTEXTURE
dludwig@8459
   552
        ),                          // flags.  see SDL_RendererFlags
dludwig@8442
   553
        2,                          // num_texture_formats
dludwig@8442
   554
        {                           // texture_formats
dludwig@8442
   555
            SDL_PIXELFORMAT_RGB888,
dludwig@8442
   556
            SDL_PIXELFORMAT_ARGB8888
dludwig@8442
   557
        },
dludwig@8446
   558
        0,                          // max_texture_width: will be filled in later
dludwig@8446
   559
        0                           // max_texture_height: will be filled in later
dludwig@8442
   560
    }
dludwig@8442
   561
};
dludwig@8442
   562
dludwig@8400
   563
dludwig@8454
   564
static Uint32
dludwig@8454
   565
DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat) {
dludwig@8454
   566
    switch (dxgiFormat) {
dludwig@8454
   567
        case DXGI_FORMAT_B8G8R8A8_UNORM:
dludwig@8454
   568
            return SDL_PIXELFORMAT_ARGB8888;
dludwig@8454
   569
        case DXGI_FORMAT_B8G8R8X8_UNORM:
dludwig@8454
   570
            return SDL_PIXELFORMAT_RGB888;
dludwig@8454
   571
        default:
dludwig@8454
   572
            return SDL_PIXELFORMAT_UNKNOWN;
dludwig@8454
   573
    }
dludwig@8454
   574
}
dludwig@8454
   575
dludwig@8454
   576
static DXGI_FORMAT
dludwig@8454
   577
SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
dludwig@8454
   578
{
slouken@8582
   579
    switch (sdlFormat) {
slouken@8582
   580
        case SDL_PIXELFORMAT_ARGB8888:
slouken@8582
   581
            return DXGI_FORMAT_B8G8R8A8_UNORM;
slouken@8582
   582
        case SDL_PIXELFORMAT_RGB888:
slouken@8582
   583
            return DXGI_FORMAT_B8G8R8X8_UNORM;
slouken@8582
   584
        default:
slouken@8582
   585
            return DXGI_FORMAT_UNKNOWN;
slouken@8582
   586
    }
dludwig@8566
   587
}
dludwig@8400
   588
slouken@8582
   589
SDL_Renderer *
slouken@8582
   590
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@8582
   591
{
slouken@8582
   592
    SDL_Renderer *renderer;
slouken@8582
   593
    D3D11_RenderData *data;
slouken@8582
   594
slouken@8582
   595
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@8582
   596
    if (!renderer) {
slouken@8582
   597
        SDL_OutOfMemory();
slouken@8582
   598
        return NULL;
slouken@8582
   599
    }
slouken@8582
   600
    SDL_zerop(renderer);
slouken@8582
   601
slouken@8582
   602
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
slouken@8582
   603
    if (!data) {
slouken@8582
   604
        SDL_OutOfMemory();
slouken@8582
   605
        return NULL;
slouken@8582
   606
    }
slouken@8582
   607
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
slouken@8582
   608
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
slouken@8582
   609
    data->renderTargetSize = XMFLOAT2(0, 0);
slouken@8582
   610
slouken@8582
   611
    renderer->WindowEvent = D3D11_WindowEvent;
slouken@8582
   612
    renderer->CreateTexture = D3D11_CreateTexture;
slouken@8582
   613
    renderer->UpdateTexture = D3D11_UpdateTexture;
slouken@8582
   614
    renderer->LockTexture = D3D11_LockTexture;
slouken@8582
   615
    renderer->UnlockTexture = D3D11_UnlockTexture;
slouken@8582
   616
    renderer->SetRenderTarget = D3D11_SetRenderTarget;
slouken@8582
   617
    renderer->UpdateViewport = D3D11_UpdateViewport;
slouken@8582
   618
    renderer->UpdateClipRect = D3D11_UpdateClipRect;
slouken@8582
   619
    renderer->RenderClear = D3D11_RenderClear;
slouken@8582
   620
    renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
slouken@8582
   621
    renderer->RenderDrawLines = D3D11_RenderDrawLines;
slouken@8582
   622
    renderer->RenderFillRects = D3D11_RenderFillRects;
slouken@8582
   623
    renderer->RenderCopy = D3D11_RenderCopy;
slouken@8582
   624
    renderer->RenderCopyEx = D3D11_RenderCopyEx;
slouken@8582
   625
    renderer->RenderReadPixels = D3D11_RenderReadPixels;
slouken@8582
   626
    renderer->RenderPresent = D3D11_RenderPresent;
slouken@8582
   627
    renderer->DestroyTexture = D3D11_DestroyTexture;
slouken@8582
   628
    renderer->DestroyRenderer = D3D11_DestroyRenderer;
slouken@8582
   629
    renderer->info = D3D11_RenderDriver.info;
slouken@8582
   630
    renderer->driverdata = data;
slouken@8582
   631
slouken@8582
   632
    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
slouken@8582
   633
    // order to give init functions access to the underlying window handle:
slouken@8582
   634
    renderer->window = window;
slouken@8582
   635
slouken@8582
   636
    /* Initialize Direct3D resources */
slouken@8582
   637
    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
slouken@8582
   638
        D3D11_DestroyRenderer(renderer);
slouken@8582
   639
        return NULL;
slouken@8582
   640
    }
slouken@8582
   641
    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
slouken@8582
   642
        D3D11_DestroyRenderer(renderer);
slouken@8582
   643
        return NULL;
slouken@8582
   644
    }
slouken@8582
   645
slouken@8582
   646
    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
slouken@8582
   647
slouken@8582
   648
    return renderer;
dludwig@8400
   649
}
dludwig@8400
   650
dludwig@8413
   651
static void
dludwig@8413
   652
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   653
{
dludwig@8413
   654
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   655
    if (data) {
dludwig@8413
   656
        delete data;
dludwig@8413
   657
        data = NULL;
dludwig@8413
   658
    }
dludwig@8413
   659
}
dludwig@8413
   660
dludwig@8431
   661
static HRESULT
dludwig@8431
   662
D3D11_CreateBlendMode(SDL_Renderer * renderer,
dludwig@8431
   663
                      BOOL enableBlending,
dludwig@8431
   664
                      D3D11_BLEND srcBlend,
dludwig@8431
   665
                      D3D11_BLEND destBlend,
dludwig@8575
   666
                      D3D11_BLEND srcBlendAlpha,
dludwig@8575
   667
                      D3D11_BLEND destBlendAlpha,
dludwig@8431
   668
                      ID3D11BlendState ** blendStateOutput)
dludwig@8431
   669
{
dludwig@8431
   670
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8431
   671
    HRESULT result = S_OK;
slouken@8582
   672
slouken@8582
   673
    D3D11_BLEND_DESC blendDesc;
slouken@8582
   674
    memset(&blendDesc, 0, sizeof(blendDesc));
slouken@8582
   675
    blendDesc.AlphaToCoverageEnable = FALSE;
slouken@8582
   676
    blendDesc.IndependentBlendEnable = FALSE;
slouken@8582
   677
    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
slouken@8582
   678
    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
slouken@8582
   679
    blendDesc.RenderTarget[0].DestBlend = destBlend;
slouken@8582
   680
    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
slouken@8582
   681
    blendDesc.RenderTarget[0].SrcBlendAlpha = srcBlendAlpha;
slouken@8582
   682
    blendDesc.RenderTarget[0].DestBlendAlpha = destBlendAlpha;
slouken@8582
   683
    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
slouken@8582
   684
    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
slouken@8582
   685
    result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
slouken@8582
   686
    if (FAILED(result)) {
slouken@8582
   687
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBlendState", result);
slouken@8582
   688
        return result;
slouken@8582
   689
    }
slouken@8582
   690
slouken@8582
   691
    return S_OK;
dludwig@8431
   692
}
dludwig@8431
   693
dludwig@8414
   694
// Create resources that depend on the device.
dludwig@8410
   695
HRESULT
slouken@8582
   696
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
slouken@8582
   697
{
slouken@8582
   698
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
   699
slouken@8582
   700
    // This flag adds support for surfaces with a different color channel ordering
slouken@8582
   701
    // than the API default. It is required for compatibility with Direct2D.
slouken@8582
   702
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
slouken@8582
   703
slouken@8582
   704
    // Make sure Direct3D's debugging feature gets used, if the app requests it.
dludwig@8564
   705
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
dludwig@8564
   706
    if (hint) {
dludwig@8564
   707
        if (*hint == '1') {
dludwig@8533
   708
            creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8564
   709
        }
slouken@8582
   710
    }
slouken@8582
   711
slouken@8582
   712
    // This array defines the set of DirectX hardware feature levels this app will support.
slouken@8582
   713
    // Note the ordering should be preserved.
slouken@8582
   714
    // Don't forget to declare your application's minimum required feature level in its
slouken@8582
   715
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
slouken@8582
   716
    D3D_FEATURE_LEVEL featureLevels[] = 
slouken@8582
   717
    {
slouken@8582
   718
        D3D_FEATURE_LEVEL_11_1,
slouken@8582
   719
        D3D_FEATURE_LEVEL_11_0,
slouken@8582
   720
        D3D_FEATURE_LEVEL_10_1,
slouken@8582
   721
        D3D_FEATURE_LEVEL_10_0,
slouken@8582
   722
        D3D_FEATURE_LEVEL_9_3,
slouken@8582
   723
        D3D_FEATURE_LEVEL_9_2,
slouken@8582
   724
        D3D_FEATURE_LEVEL_9_1
slouken@8582
   725
    };
slouken@8582
   726
slouken@8582
   727
    // Create the Direct3D 11 API device object and a corresponding context.
slouken@8582
   728
    ComPtr<ID3D11Device> device;
slouken@8582
   729
    ComPtr<ID3D11DeviceContext> context;
slouken@8582
   730
    HRESULT result = S_OK;
slouken@8582
   731
    result = D3D11CreateDevice(
slouken@8582
   732
        nullptr, // Specify nullptr to use the default adapter.
slouken@8582
   733
        D3D_DRIVER_TYPE_HARDWARE,
slouken@8582
   734
        nullptr,
slouken@8582
   735
        creationFlags, // Set set debug and Direct2D compatibility flags.
slouken@8582
   736
        featureLevels, // List of feature levels this app can support.
slouken@8582
   737
        ARRAYSIZE(featureLevels),
slouken@8582
   738
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
slouken@8582
   739
        &device, // Returns the Direct3D device created.
slouken@8582
   740
        &data->featureLevel, // Returns feature level of device created.
slouken@8582
   741
        &context // Returns the device immediate context.
slouken@8582
   742
        );
slouken@8582
   743
    if (FAILED(result)) {
slouken@8582
   744
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", D3D11CreateDevice", result);
slouken@8582
   745
        return result;
slouken@8582
   746
    }
slouken@8582
   747
slouken@8582
   748
    // Get the Direct3D 11.1 API device and context interfaces.
slouken@8582
   749
    result = device.As(&(data->d3dDevice));
slouken@8582
   750
    if (FAILED(result)) {
slouken@8582
   751
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to ID3D11Device1", result);
slouken@8582
   752
        return result;
slouken@8582
   753
    }
slouken@8582
   754
slouken@8582
   755
    result = context.As(&data->d3dContext);
slouken@8582
   756
    if (FAILED(result)) {
slouken@8582
   757
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext to ID3D11DeviceContext1", result);
slouken@8582
   758
        return result;
slouken@8582
   759
    }
slouken@8582
   760
slouken@8582
   761
    //
slouken@8582
   762
    // Make note of the maximum texture size
slouken@8582
   763
    // Max texture sizes are documented on MSDN, at:
slouken@8582
   764
    // http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
slouken@8582
   765
    //
slouken@8582
   766
    switch (data->d3dDevice->GetFeatureLevel()) {
slouken@8582
   767
        case D3D_FEATURE_LEVEL_11_1:
slouken@8582
   768
        case D3D_FEATURE_LEVEL_11_0:
slouken@8582
   769
            renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
slouken@8582
   770
            break;
slouken@8582
   771
slouken@8582
   772
        case D3D_FEATURE_LEVEL_10_1:
slouken@8582
   773
        case D3D_FEATURE_LEVEL_10_0:
slouken@8582
   774
            renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
slouken@8582
   775
            break;
slouken@8582
   776
slouken@8582
   777
        case D3D_FEATURE_LEVEL_9_3:
slouken@8582
   778
            renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
slouken@8582
   779
            break;
slouken@8582
   780
slouken@8582
   781
        case D3D_FEATURE_LEVEL_9_2:
slouken@8582
   782
        case D3D_FEATURE_LEVEL_9_1:
slouken@8582
   783
            renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
slouken@8582
   784
            break;
slouken@8582
   785
    }
slouken@8582
   786
slouken@8582
   787
    //
slouken@8582
   788
    // Load in SDL's one and only vertex shader:
slouken@8582
   789
    //
slouken@8582
   790
    result = data->d3dDevice->CreateVertexShader(
slouken@8582
   791
        D3D11_VertexShader,
slouken@8582
   792
        sizeof(D3D11_VertexShader),
slouken@8582
   793
        nullptr,
slouken@8582
   794
        &data->vertexShader
slouken@8582
   795
        );
slouken@8582
   796
    if (FAILED(result)) {
slouken@8582
   797
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateVertexShader", result);
slouken@8582
   798
        return result;
slouken@8582
   799
    }
slouken@8582
   800
slouken@8582
   801
    //
slouken@8582
   802
    // Create an input layout for SDL's vertex shader:
slouken@8582
   803
    //
slouken@8582
   804
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
slouken@8582
   805
    {
slouken@8582
   806
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
slouken@8582
   807
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
slouken@8582
   808
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
slouken@8582
   809
    };
slouken@8582
   810
slouken@8582
   811
    result = data->d3dDevice->CreateInputLayout(
slouken@8582
   812
        vertexDesc,
slouken@8582
   813
        ARRAYSIZE(vertexDesc),
slouken@8582
   814
        D3D11_VertexShader,
slouken@8582
   815
        sizeof(D3D11_VertexShader),
slouken@8582
   816
        &data->inputLayout
slouken@8582
   817
        );
slouken@8582
   818
    if (FAILED(result)) {
slouken@8582
   819
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateInputLayout", result);
slouken@8582
   820
        return result;
slouken@8582
   821
    }
slouken@8582
   822
slouken@8582
   823
    //
slouken@8582
   824
    // Load in SDL's pixel shaders
slouken@8582
   825
    //
slouken@8582
   826
slouken@8582
   827
    result = data->d3dDevice->CreatePixelShader(
slouken@8582
   828
        D3D11_PixelShader_Textures,
slouken@8582
   829
        sizeof(D3D11_PixelShader_Textures),
slouken@8582
   830
        nullptr,
slouken@8582
   831
        &data->texturePixelShader
slouken@8582
   832
        );
slouken@8582
   833
    if (FAILED(result)) {
slouken@8582
   834
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['textures' shader]", result);
slouken@8582
   835
        return result;
slouken@8582
   836
    }
slouken@8582
   837
slouken@8582
   838
    result = data->d3dDevice->CreatePixelShader(
slouken@8582
   839
        D3D11_PixelShader_Colors,
slouken@8582
   840
        sizeof(D3D11_PixelShader_Colors),
slouken@8582
   841
        nullptr,
slouken@8582
   842
        &data->colorPixelShader
slouken@8582
   843
        );
slouken@8582
   844
    if (FAILED(result)) {
slouken@8582
   845
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['color' shader]", result);
slouken@8582
   846
        return result;
slouken@8582
   847
    }
slouken@8582
   848
slouken@8582
   849
    //
slouken@8582
   850
    // Setup space to hold vertex shader constants:
slouken@8582
   851
    //
slouken@8582
   852
    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
slouken@8582
   853
    result = data->d3dDevice->CreateBuffer(
slouken@8582
   854
		&constantBufferDesc,
slouken@8582
   855
		nullptr,
slouken@8582
   856
        &data->vertexShaderConstants
slouken@8582
   857
		);
slouken@8582
   858
    if (FAILED(result)) {
slouken@8582
   859
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex shader constants]", result);
slouken@8582
   860
        return result;
slouken@8582
   861
    }
slouken@8582
   862
slouken@8582
   863
    //
slouken@8582
   864
    // Make sure that the vertex buffer, if already created, gets freed.
slouken@8582
   865
    // It will be recreated later.
slouken@8582
   866
    //
slouken@8582
   867
    data->vertexBuffer = nullptr;
slouken@8582
   868
slouken@8582
   869
    //
slouken@8582
   870
    // Create samplers to use when drawing textures:
slouken@8582
   871
    //
slouken@8582
   872
    D3D11_SAMPLER_DESC samplerDesc;
slouken@8582
   873
    samplerDesc.Filter = SDL_D3D11_NEAREST_PIXEL_FILTER;
slouken@8582
   874
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
slouken@8582
   875
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
slouken@8582
   876
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
slouken@8582
   877
    samplerDesc.MipLODBias = 0.0f;
slouken@8582
   878
    samplerDesc.MaxAnisotropy = 1;
slouken@8582
   879
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
slouken@8582
   880
    samplerDesc.BorderColor[0] = 0.0f;
slouken@8582
   881
    samplerDesc.BorderColor[1] = 0.0f;
slouken@8582
   882
    samplerDesc.BorderColor[2] = 0.0f;
slouken@8582
   883
    samplerDesc.BorderColor[3] = 0.0f;
slouken@8582
   884
    samplerDesc.MinLOD = 0.0f;
slouken@8582
   885
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
slouken@8582
   886
    result = data->d3dDevice->CreateSamplerState(
slouken@8582
   887
        &samplerDesc,
slouken@8582
   888
        &data->nearestPixelSampler
slouken@8582
   889
        );
slouken@8582
   890
    if (FAILED(result)) {
slouken@8582
   891
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [nearest-pixel filter]", result);
slouken@8582
   892
        return result;
slouken@8582
   893
    }
slouken@8582
   894
slouken@8582
   895
    samplerDesc.Filter = SDL_D3D11_LINEAR_FILTER;
slouken@8582
   896
    result = data->d3dDevice->CreateSamplerState(
slouken@8582
   897
        &samplerDesc,
slouken@8582
   898
        &data->linearSampler
slouken@8582
   899
        );
slouken@8582
   900
    if (FAILED(result)) {
slouken@8582
   901
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [linear filter]", result);
slouken@8582
   902
        return result;
slouken@8582
   903
    }
slouken@8582
   904
slouken@8582
   905
    //
slouken@8582
   906
    // Setup Direct3D rasterizer states
slouken@8582
   907
    //
slouken@8582
   908
    D3D11_RASTERIZER_DESC rasterDesc;
slouken@8582
   909
    memset(&rasterDesc, 0, sizeof(rasterDesc));
slouken@8582
   910
	rasterDesc.AntialiasedLineEnable = false;
slouken@8582
   911
	rasterDesc.CullMode = D3D11_CULL_NONE;
slouken@8582
   912
	rasterDesc.DepthBias = 0;
slouken@8582
   913
	rasterDesc.DepthBiasClamp = 0.0f;
slouken@8582
   914
	rasterDesc.DepthClipEnable = true;
slouken@8582
   915
	rasterDesc.FillMode = D3D11_FILL_SOLID;
slouken@8582
   916
	rasterDesc.FrontCounterClockwise = false;
slouken@8582
   917
	rasterDesc.MultisampleEnable = false;
slouken@8582
   918
	rasterDesc.ScissorEnable = false;
slouken@8582
   919
	rasterDesc.SlopeScaledDepthBias = 0.0f;
slouken@8582
   920
	result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
slouken@8582
   921
	if (FAILED(result)) {
slouken@8582
   922
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [main rasterizer]", result);
slouken@8582
   923
        return result;
slouken@8582
   924
    }
slouken@8582
   925
slouken@8582
   926
    rasterDesc.ScissorEnable = true;
slouken@8582
   927
    result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->clippedRasterizer);
slouken@8582
   928
	if (FAILED(result)) {
slouken@8582
   929
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState [clipped rasterizer]", result);
slouken@8582
   930
        return result;
slouken@8582
   931
    }
slouken@8582
   932
slouken@8582
   933
    //
slouken@8582
   934
    // Create blending states:
slouken@8582
   935
    //
slouken@8582
   936
    result = D3D11_CreateBlendMode(
slouken@8582
   937
        renderer,
slouken@8582
   938
        TRUE,
slouken@8582
   939
        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
slouken@8582
   940
        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlend */
slouken@8582
   941
        D3D11_BLEND_ONE,                /* srcBlendAlpha */
slouken@8582
   942
        D3D11_BLEND_INV_SRC_ALPHA,      /* destBlendAlpha */
slouken@8582
   943
        &data->blendModeBlend);
slouken@8582
   944
    if (FAILED(result)) {
slouken@8582
   945
        // D3D11_CreateBlendMode will set the SDL error, if it fails
slouken@8582
   946
        return result;
slouken@8582
   947
    }
slouken@8582
   948
slouken@8582
   949
    result = D3D11_CreateBlendMode(
slouken@8582
   950
        renderer,
slouken@8582
   951
        TRUE,
slouken@8582
   952
        D3D11_BLEND_SRC_ALPHA,          /* srcBlend */
slouken@8582
   953
        D3D11_BLEND_ONE,                /* destBlend */
slouken@8582
   954
        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
slouken@8582
   955
        D3D11_BLEND_ONE,                /* destBlendAlpha */
slouken@8582
   956
        &data->blendModeAdd);
slouken@8582
   957
    if (FAILED(result)) {
slouken@8582
   958
        // D3D11_CreateBlendMode will set the SDL error, if it fails
slouken@8582
   959
        return result;
slouken@8582
   960
    }
slouken@8582
   961
slouken@8582
   962
    result = D3D11_CreateBlendMode(
slouken@8582
   963
        renderer,
slouken@8582
   964
        TRUE,
slouken@8582
   965
        D3D11_BLEND_ZERO,               /* srcBlend */
slouken@8582
   966
        D3D11_BLEND_SRC_COLOR,          /* destBlend */
slouken@8582
   967
        D3D11_BLEND_ZERO,               /* srcBlendAlpha */
slouken@8582
   968
        D3D11_BLEND_ONE,                /* destBlendAlpha */
slouken@8582
   969
        &data->blendModeMod);
slouken@8582
   970
    if (FAILED(result)) {
slouken@8582
   971
        // D3D11_CreateBlendMode will set the SDL error, if it fails
slouken@8582
   972
        return result;
slouken@8582
   973
    }
slouken@8582
   974
slouken@8582
   975
    //
slouken@8582
   976
    // All done!
slouken@8582
   977
    //
slouken@8582
   978
    return S_OK;
dludwig@8410
   979
}
dludwig@8410
   980
dludwig@8412
   981
#ifdef __WINRT__
slouken@8582
   982
slouken@8582
   983
static ABI::Windows::UI::Core::ICoreWindow *
slouken@8582
   984
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
slouken@8582
   985
{
slouken@8582
   986
    SDL_Window * sdlWindow = renderer->window;
slouken@8582
   987
    if ( ! renderer->window ) {
slouken@8582
   988
        return nullptr;
slouken@8582
   989
    }
slouken@8582
   990
slouken@8582
   991
    SDL_SysWMinfo sdlWindowInfo;
slouken@8582
   992
    SDL_VERSION(&sdlWindowInfo.version);
slouken@8582
   993
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
slouken@8582
   994
        return nullptr;
slouken@8582
   995
    }
slouken@8582
   996
slouken@8582
   997
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINRT) {
slouken@8582
   998
        return nullptr;
slouken@8582
   999
    }
slouken@8582
  1000
slouken@8582
  1001
    if ( ! sdlWindowInfo.info.winrt.window ) {
slouken@8582
  1002
        return nullptr;
slouken@8582
  1003
    }
slouken@8582
  1004
slouken@8582
  1005
    ABI::Windows::UI::Core::ICoreWindow * coreWindow = nullptr;
slouken@8582
  1006
    if (FAILED(sdlWindowInfo.info.winrt.window->QueryInterface(&coreWindow))) {
slouken@8582
  1007
        return nullptr;
slouken@8582
  1008
    }
slouken@8582
  1009
slouken@8582
  1010
    return coreWindow;
dludwig@8412
  1011
}
dludwig@8412
  1012
dludwig@8414
  1013
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
slouken@8582
  1014
static float
slouken@8582
  1015
D3D11_ConvertDipsToPixels(float dips)
slouken@8582
  1016
{
slouken@8582
  1017
    static const float dipsPerInch = 96.0f;
slouken@8582
  1018
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
  1019
}
dludwig@8412
  1020
#endif
dludwig@8412
  1021
dludwig@8505
  1022
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
  1023
// TODO, WinRT, XAML: get the ISwapChainBackgroundPanelNative from something other than a global var
dludwig@8505
  1024
extern ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative;
dludwig@8505
  1025
#endif
dludwig@8505
  1026
dludwig@8548
  1027
static DXGI_MODE_ROTATION
dludwig@8548
  1028
D3D11_GetRotationForOrientation(Windows::Graphics::Display::DisplayOrientations orientation)
dludwig@8548
  1029
{
dludwig@8548
  1030
    switch (orientation)
dludwig@8548
  1031
    {
dludwig@8548
  1032
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8548
  1033
        //
dludwig@8548
  1034
        // Windows Phone rotations
dludwig@8548
  1035
        //
slouken@8582
  1036
        case DisplayOrientations::Landscape:
slouken@8582
  1037
            return DXGI_MODE_ROTATION_ROTATE90;
slouken@8582
  1038
        case DisplayOrientations::Portrait:
slouken@8582
  1039
            return DXGI_MODE_ROTATION_IDENTITY;
slouken@8582
  1040
        case DisplayOrientations::LandscapeFlipped:
slouken@8582
  1041
            return DXGI_MODE_ROTATION_ROTATE270;
slouken@8582
  1042
        case DisplayOrientations::PortraitFlipped:
slouken@8582
  1043
            return DXGI_MODE_ROTATION_ROTATE180;
dludwig@8548
  1044
#else
dludwig@8548
  1045
        //
dludwig@8548
  1046
        // Non-Windows-Phone rotations (ex: Windows 8, Windows RT)
slouken@8582
  1047
        //
slouken@8582
  1048
        case DisplayOrientations::Landscape:
slouken@8582
  1049
            return DXGI_MODE_ROTATION_IDENTITY;
slouken@8582
  1050
        case DisplayOrientations::Portrait:
slouken@8582
  1051
            return DXGI_MODE_ROTATION_ROTATE270;
slouken@8582
  1052
        case DisplayOrientations::LandscapeFlipped:
slouken@8582
  1053
            return DXGI_MODE_ROTATION_ROTATE180;
slouken@8582
  1054
        case DisplayOrientations::PortraitFlipped:
slouken@8582
  1055
            return DXGI_MODE_ROTATION_ROTATE90;
slouken@8582
  1056
#endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8548
  1057
dludwig@8548
  1058
        default:
dludwig@8548
  1059
            return DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8548
  1060
    }
dludwig@8548
  1061
}
dludwig@8505
  1062
dludwig@8567
  1063
static bool
dludwig@8567
  1064
D3D11_IsDisplayRotated90Degrees(Windows::Graphics::Display::DisplayOrientations orientation)
dludwig@8567
  1065
{
dludwig@8567
  1066
    switch (D3D11_GetRotationForOrientation(orientation)) {
dludwig@8567
  1067
        case DXGI_MODE_ROTATION_ROTATE90:
dludwig@8567
  1068
        case DXGI_MODE_ROTATION_ROTATE270:
dludwig@8567
  1069
            return true;
dludwig@8567
  1070
        default:
dludwig@8567
  1071
            return false;
dludwig@8567
  1072
    }
dludwig@8567
  1073
}
dludwig@8567
  1074
dludwig@8569
  1075
static int
dludwig@8569
  1076
D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect)
dludwig@8569
  1077
{
dludwig@8569
  1078
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8569
  1079
    switch (D3D11_GetRotationForOrientation(data-> orientation)) {
dludwig@8569
  1080
        case DXGI_MODE_ROTATION_IDENTITY:
dludwig@8569
  1081
            outRect->left = sdlRect->x;
dludwig@8569
  1082
            outRect->right = sdlRect->x + sdlRect->w;
dludwig@8569
  1083
            outRect->top = sdlRect->y;
dludwig@8569
  1084
            outRect->bottom = sdlRect->y + sdlRect->h;
dludwig@8569
  1085
            break;
dludwig@8569
  1086
        case DXGI_MODE_ROTATION_ROTATE270:
slouken@8582
  1087
            outRect->left = sdlRect->y;
slouken@8582
  1088
            outRect->right = sdlRect->y + sdlRect->h;
slouken@8582
  1089
            outRect->top = renderer->viewport.w - sdlRect->x - sdlRect->w;
dludwig@8569
  1090
            outRect->bottom = renderer->viewport.w - sdlRect->x;
dludwig@8569
  1091
            break;
dludwig@8569
  1092
        case DXGI_MODE_ROTATION_ROTATE180:
dludwig@8569
  1093
            outRect->left = renderer->viewport.w - sdlRect->x - sdlRect->w;
dludwig@8569
  1094
            outRect->right = renderer->viewport.w - sdlRect->x;
dludwig@8569
  1095
            outRect->top = renderer->viewport.h - sdlRect->y - sdlRect->h;
dludwig@8569
  1096
            outRect->bottom = renderer->viewport.h - sdlRect->y;
dludwig@8569
  1097
            break;
dludwig@8569
  1098
        case DXGI_MODE_ROTATION_ROTATE90:
dludwig@8569
  1099
            outRect->left = renderer->viewport.h - sdlRect->y - sdlRect->h;
dludwig@8569
  1100
            outRect->right = renderer->viewport.h - sdlRect->y;
slouken@8582
  1101
            outRect->top = sdlRect->x;
dludwig@8569
  1102
            outRect->bottom = sdlRect->x + sdlRect->h;
dludwig@8569
  1103
            break;
dludwig@8569
  1104
        default:
dludwig@8569
  1105
            return SDL_SetError("The physical display is in an unknown or unsupported orientation");
dludwig@8569
  1106
    }
dludwig@8569
  1107
    return 0;
dludwig@8569
  1108
}
dludwig@8569
  1109
dludwig@8569
  1110
dludwig@8414
  1111
// Initialize all resources that change when the window's size changes.
dludwig@8504
  1112
// TODO, WinRT: get D3D11_CreateWindowSizeDependentResources working on Win32
slouken@8582
  1113
HRESULT
slouken@8582
  1114
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
slouken@8582
  1115
{
slouken@8582
  1116
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1117
    HRESULT result = S_OK;
slouken@8582
  1118
    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
slouken@8582
  1119
slouken@8582
  1120
    // Store the window bounds so the next time we get a SizeChanged event we can
slouken@8582
  1121
    // avoid rebuilding everything if the size is identical.
slouken@8582
  1122
    ABI::Windows::Foundation::Rect nativeWindowBounds;
slouken@8582
  1123
    if (coreWindow) {
slouken@8582
  1124
        result = coreWindow->get_Bounds(&nativeWindowBounds);
slouken@8582
  1125
        if (FAILED(result)) {
slouken@8582
  1126
            WIN_SetErrorFromHRESULT(__FUNCTION__", ICoreWindow::get_Bounds [get native-window bounds]", result);
slouken@8582
  1127
            return result;
slouken@8582
  1128
        }
slouken@8582
  1129
    } else {
slouken@8582
  1130
        // TODO, WinRT, XAML: clean up window-bounds code in D3D11_CreateWindowSizeDependentResources
dludwig@8505
  1131
        SDL_DisplayMode displayMode;
dludwig@8505
  1132
        if (SDL_GetDesktopDisplayMode(0, &displayMode) < 0) {
dludwig@8505
  1133
            SDL_SetError(__FUNCTION__", Get Window Bounds (XAML): Unable to retrieve the native window's size");
dludwig@8505
  1134
            return E_FAIL;
dludwig@8505
  1135
        }
dludwig@8505
  1136
dludwig@8505
  1137
        nativeWindowBounds.Width = (FLOAT) displayMode.w;
slouken@8582
  1138
        nativeWindowBounds.Height = (FLOAT) displayMode.h;
slouken@8582
  1139
    }
slouken@8582
  1140
slouken@8582
  1141
    // TODO, WinRT, XAML: see if window/control sizes are in DIPs, or something else.  If something else, then adjust renderer size tracking accordingly.
slouken@8582
  1142
    data->windowSizeInDIPs.x = nativeWindowBounds.Width;
slouken@8582
  1143
    data->windowSizeInDIPs.y = nativeWindowBounds.Height;
slouken@8582
  1144
slouken@8582
  1145
    // Calculate the necessary swap chain and render target size in pixels.
slouken@8582
  1146
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
slouken@8582
  1147
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
slouken@8582
  1148
slouken@8582
  1149
    // The width and height of the swap chain must be based on the window's
slouken@8582
  1150
    // landscape-oriented width and height. If the window is in a portrait
slouken@8582
  1151
    // orientation, the dimensions must be reversed.
slouken@8582
  1152
    data->orientation = DisplayProperties::CurrentOrientation;
slouken@8582
  1153
slouken@8582
  1154
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
  1155
    const bool swapDimensions = false;
slouken@8582
  1156
#else
slouken@8582
  1157
    const bool swapDimensions = D3D11_IsDisplayRotated90Degrees(data->orientation);
slouken@8582
  1158
#endif
slouken@8582
  1159
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
slouken@8582
  1160
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
slouken@8582
  1161
slouken@8582
  1162
    if(data->swapChain != nullptr)
slouken@8582
  1163
    {
slouken@8582
  1164
        // If the swap chain already exists, resize it.
slouken@8582
  1165
        result = data->swapChain->ResizeBuffers(
slouken@8582
  1166
            2, // Double-buffered swap chain.
slouken@8582
  1167
            static_cast<UINT>(data->renderTargetSize.x),
slouken@8582
  1168
            static_cast<UINT>(data->renderTargetSize.y),
slouken@8582
  1169
            DXGI_FORMAT_B8G8R8A8_UNORM,
slouken@8582
  1170
            0
slouken@8582
  1171
            );
slouken@8582
  1172
        if (FAILED(result)) {
slouken@8582
  1173
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::ResizeBuffers", result);
slouken@8582
  1174
            return result;
slouken@8582
  1175
        }
slouken@8582
  1176
    }
slouken@8582
  1177
    else
slouken@8582
  1178
    {
slouken@8582
  1179
        const bool usingXAML = (coreWindow == nullptr);
slouken@8582
  1180
slouken@8582
  1181
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
slouken@8582
  1182
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
slouken@8582
  1183
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
slouken@8582
  1184
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
slouken@8582
  1185
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
slouken@8582
  1186
        swapChainDesc.Stereo = false;
slouken@8582
  1187
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
slouken@8582
  1188
        swapChainDesc.SampleDesc.Quality = 0;
slouken@8582
  1189
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
slouken@8582
  1190
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
slouken@8582
  1191
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
  1192
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
slouken@8582
  1193
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
slouken@8582
  1194
#else
slouken@8582
  1195
        if (usingXAML) {
slouken@8582
  1196
            swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
slouken@8582
  1197
        } else {
slouken@8582
  1198
            swapChainDesc.Scaling = DXGI_SCALING_NONE;
slouken@8582
  1199
        }
slouken@8582
  1200
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
slouken@8582
  1201
#endif
slouken@8582
  1202
        swapChainDesc.Flags = 0;
slouken@8582
  1203
slouken@8582
  1204
        ComPtr<IDXGIDevice1> dxgiDevice;
slouken@8582
  1205
        result = data->d3dDevice.As(&dxgiDevice);
slouken@8582
  1206
        if (FAILED(result)) {
slouken@8582
  1207
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1 to IDXGIDevice1", result);
slouken@8582
  1208
            return result;
slouken@8582
  1209
        }
slouken@8582
  1210
slouken@8582
  1211
        ComPtr<IDXGIAdapter> dxgiAdapter;
slouken@8582
  1212
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
slouken@8582
  1213
        if (FAILED(result)) {
slouken@8582
  1214
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::GetAdapter", result);
slouken@8582
  1215
            return result;
slouken@8582
  1216
        }
slouken@8582
  1217
slouken@8582
  1218
        ComPtr<IDXGIFactory2> dxgiFactory;
slouken@8582
  1219
        result = dxgiAdapter->GetParent(
slouken@8582
  1220
            __uuidof(IDXGIFactory2), 
slouken@8582
  1221
            &dxgiFactory
slouken@8582
  1222
            );
slouken@8582
  1223
        if (FAILED(result)) {
slouken@8582
  1224
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIAdapter::GetParent", result);
slouken@8582
  1225
            return result;
slouken@8582
  1226
        }
slouken@8582
  1227
slouken@8582
  1228
        if (usingXAML) {
slouken@8582
  1229
            result = dxgiFactory->CreateSwapChainForComposition(
slouken@8582
  1230
                data->d3dDevice.Get(),
slouken@8582
  1231
                &swapChainDesc,
slouken@8582
  1232
                nullptr,
slouken@8582
  1233
                &data->swapChain);
slouken@8582
  1234
            if (FAILED(result)) {
slouken@8582
  1235
                WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForComposition", result);
slouken@8582
  1236
                return result;
slouken@8582
  1237
            }
slouken@8582
  1238
slouken@8582
  1239
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
slouken@8582
  1240
            result = WINRT_GlobalSwapChainBackgroundPanelNative->SetSwapChain(data->swapChain.Get());
slouken@8582
  1241
            if (FAILED(result)) {
slouken@8582
  1242
                WIN_SetErrorFromHRESULT(__FUNCTION__ ", ISwapChainBackgroundPanelNative::SetSwapChain", result);
slouken@8582
  1243
                return result;
slouken@8582
  1244
            }
slouken@8582
  1245
#else
slouken@8582
  1246
            SDL_SetError(__FUNCTION__ ", XAML support is not yet available for Windows Phone");
slouken@8582
  1247
            return E_FAIL;
slouken@8582
  1248
#endif
slouken@8582
  1249
        } else {
slouken@8582
  1250
            IUnknown * coreWindowAsIUnknown = nullptr;
slouken@8582
  1251
            result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
slouken@8582
  1252
            if (FAILED(result)) {
slouken@8582
  1253
                WIN_SetErrorFromHRESULT(__FUNCTION__ ", ICoreWindow to IUnknown", result);
slouken@8582
  1254
                return result;
slouken@8582
  1255
            }
slouken@8582
  1256
slouken@8582
  1257
            result = dxgiFactory->CreateSwapChainForCoreWindow(
slouken@8582
  1258
                data->d3dDevice.Get(),
slouken@8582
  1259
                coreWindowAsIUnknown,
slouken@8582
  1260
                &swapChainDesc,
slouken@8582
  1261
                nullptr, // Allow on all displays.
slouken@8582
  1262
                &data->swapChain
slouken@8582
  1263
                );
slouken@8582
  1264
            if (FAILED(result)) {
slouken@8582
  1265
                WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory2::CreateSwapChainForCoreWindow", result);
slouken@8582
  1266
                return result;
slouken@8582
  1267
            }
slouken@8582
  1268
        }
slouken@8582
  1269
        
slouken@8582
  1270
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
slouken@8582
  1271
        // ensures that the application will only render after each VSync, minimizing power consumption.
slouken@8582
  1272
        result = dxgiDevice->SetMaximumFrameLatency(1);
slouken@8582
  1273
        if (FAILED(result)) {
slouken@8582
  1274
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIDevice1::SetMaximumFrameLatency", result);
slouken@8582
  1275
            return result;
slouken@8582
  1276
        }
slouken@8582
  1277
    }
slouken@8582
  1278
    
slouken@8582
  1279
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
  1280
    // Set the proper orientation for the swap chain, and generate the
slouken@8582
  1281
    // 3D matrix transformation for rendering to the rotated swap chain.
slouken@8582
  1282
    //
slouken@8582
  1283
    // To note, the call for this, IDXGISwapChain1::SetRotation, is not necessary
slouken@8582
  1284
    // on Windows Phone, nor is it supported there.  It's only needed in Windows 8/RT.
slouken@8582
  1285
    DXGI_MODE_ROTATION rotation = D3D11_GetRotationForOrientation(data->orientation);
slouken@8582
  1286
    result = data->swapChain->SetRotation(rotation);
slouken@8582
  1287
    if (FAILED(result)) {
slouken@8582
  1288
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::SetRotation" , result);
slouken@8582
  1289
        return result;
slouken@8582
  1290
    }
slouken@8582
  1291
#endif
slouken@8582
  1292
slouken@8582
  1293
    // Create a render target view of the swap chain back buffer.
slouken@8582
  1294
    ComPtr<ID3D11Texture2D> backBuffer;
slouken@8582
  1295
    result = data->swapChain->GetBuffer(
slouken@8582
  1296
        0,
slouken@8582
  1297
        __uuidof(ID3D11Texture2D),
slouken@8582
  1298
        &backBuffer
slouken@8582
  1299
        );
slouken@8582
  1300
    if (FAILED(result)) {
slouken@8582
  1301
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::GetBuffer [back-buffer]", result);
slouken@8582
  1302
        return result;
slouken@8582
  1303
    }
slouken@8582
  1304
slouken@8582
  1305
    result = data->d3dDevice->CreateRenderTargetView(
slouken@8582
  1306
        backBuffer.Get(),
slouken@8582
  1307
        nullptr,
slouken@8582
  1308
        &data->mainRenderTargetView
slouken@8582
  1309
        );
slouken@8582
  1310
    if (FAILED(result)) {
slouken@8582
  1311
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRenderTargetView", result);
slouken@8582
  1312
        return result;
slouken@8582
  1313
    }
slouken@8582
  1314
slouken@8582
  1315
    if (D3D11_UpdateViewport(renderer) != 0) {
slouken@8582
  1316
        // D3D11_UpdateViewport will set the SDL error if it fails.
slouken@8582
  1317
        return E_FAIL;
slouken@8582
  1318
    }
slouken@8582
  1319
slouken@8582
  1320
    return S_OK;
dludwig@8412
  1321
}
dludwig@8412
  1322
dludwig@8415
  1323
// This method is called when the window's size changes.
slouken@8582
  1324
HRESULT
slouken@8582
  1325
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
slouken@8582
  1326
{
slouken@8582
  1327
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1328
    HRESULT result = S_OK;
slouken@8582
  1329
    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
slouken@8582
  1330
    ABI::Windows::Foundation::Rect coreWindowBounds;
slouken@8582
  1331
slouken@8582
  1332
    result = coreWindow->get_Bounds(&coreWindowBounds);
slouken@8582
  1333
    if (FAILED(result)) {
slouken@8582
  1334
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ICoreWindow::get_Bounds [get window bounds]", result);
slouken@8582
  1335
        return result;
slouken@8582
  1336
    }
slouken@8582
  1337
slouken@8582
  1338
    if (coreWindowBounds.Width  != data->windowSizeInDIPs.x ||
slouken@8582
  1339
        coreWindowBounds.Height != data->windowSizeInDIPs.y ||
slouken@8582
  1340
        data->orientation != DisplayProperties::CurrentOrientation)
slouken@8582
  1341
    {
slouken@8582
  1342
        ID3D11RenderTargetView* nullViews[] = {nullptr};
slouken@8582
  1343
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
slouken@8582
  1344
        data->mainRenderTargetView = nullptr;
slouken@8582
  1345
        data->d3dContext->Flush();
slouken@8582
  1346
        result = D3D11_CreateWindowSizeDependentResources(renderer);
slouken@8582
  1347
        if (FAILED(result)) {
slouken@8582
  1348
            /* D3D11_CreateWindowSizeDependentResources will set the SDL error */
slouken@8582
  1349
            return result;
slouken@8582
  1350
        }
slouken@8582
  1351
    }
slouken@8582
  1352
slouken@8582
  1353
    return S_OK;
dludwig@8414
  1354
}
dludwig@8414
  1355
dludwig@8414
  1356
HRESULT
dludwig@8414
  1357
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
  1358
{
dludwig@8414
  1359
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
  1360
    HRESULT result = S_OK;
dludwig@8414
  1361
slouken@8582
  1362
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
slouken@8582
  1363
    data->windowSizeInDIPs.x = 0;
slouken@8582
  1364
    data->windowSizeInDIPs.y = 0;
slouken@8582
  1365
    data->swapChain = nullptr;
slouken@8582
  1366
slouken@8582
  1367
    result = D3D11_CreateDeviceResources(renderer);
slouken@8582
  1368
    if (FAILED(result)) {
slouken@8582
  1369
        /* D3D11_CreateDeviceResources will set the SDL error */
slouken@8582
  1370
        return result;
slouken@8582
  1371
    }
slouken@8582
  1372
slouken@8582
  1373
    result = D3D11_UpdateForWindowSizeChange(renderer);
slouken@8582
  1374
    if (FAILED(result)) {
slouken@8582
  1375
        /* D3D11_UpdateForWindowSizeChange will set the SDL error */
slouken@8582
  1376
        return result;
slouken@8582
  1377
    }
dludwig@8414
  1378
dludwig@8414
  1379
    return S_OK;
dludwig@8414
  1380
}
dludwig@8414
  1381
dludwig@8415
  1382
static void
dludwig@8415
  1383
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
  1384
{
slouken@8582
  1385
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1386
slouken@8582
  1387
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@8582
  1388
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
  1389
    }
dludwig@8415
  1390
}
dludwig@8415
  1391
slouken@8582
  1392
static D3D11_FILTER
slouken@8582
  1393
GetScaleQuality(void)
slouken@8582
  1394
{
slouken@8582
  1395
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@8582
  1396
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@8582
  1397
        return SDL_D3D11_NEAREST_PIXEL_FILTER;
slouken@8582
  1398
    } else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
slouken@8582
  1399
        return SDL_D3D11_LINEAR_FILTER;
slouken@8582
  1400
    }
dludwig@8540
  1401
}
dludwig@8540
  1402
slouken@8582
  1403
static int
slouken@8582
  1404
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@8582
  1405
{
slouken@8582
  1406
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1407
    D3D11_TextureData *textureData;
slouken@8582
  1408
    HRESULT result;
slouken@8582
  1409
    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
slouken@8582
  1410
    if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
slouken@8582
  1411
        return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
slouken@8582
  1412
            __FUNCTION__, texture->format);
slouken@8582
  1413
    }
slouken@8582
  1414
slouken@8582
  1415
    textureData = new D3D11_TextureData;
slouken@8582
  1416
    if (!textureData) {
slouken@8582
  1417
        SDL_OutOfMemory();
slouken@8582
  1418
        return -1;
slouken@8582
  1419
    }
slouken@8582
  1420
    textureData->pixelFormat = SDL_AllocFormat(texture->format);
slouken@8582
  1421
    textureData->lockedTexturePosition = XMINT2(0, 0);
slouken@8582
  1422
    textureData->scaleMode = GetScaleQuality();
slouken@8582
  1423
slouken@8582
  1424
    texture->driverdata = textureData;
slouken@8582
  1425
slouken@8582
  1426
    D3D11_TEXTURE2D_DESC textureDesc = {0};
slouken@8582
  1427
    textureDesc.Width = texture->w;
slouken@8582
  1428
    textureDesc.Height = texture->h;
slouken@8582
  1429
    textureDesc.MipLevels = 1;
slouken@8582
  1430
    textureDesc.ArraySize = 1;
slouken@8582
  1431
    textureDesc.Format = textureFormat;
slouken@8582
  1432
    textureDesc.SampleDesc.Count = 1;
slouken@8582
  1433
    textureDesc.SampleDesc.Quality = 0;
slouken@8582
  1434
    textureDesc.MiscFlags = 0;
slouken@8582
  1435
slouken@8582
  1436
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@8582
  1437
        textureDesc.Usage = D3D11_USAGE_DYNAMIC;
slouken@8582
  1438
        textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
slouken@8582
  1439
    } else {
slouken@8582
  1440
        textureDesc.Usage = D3D11_USAGE_DEFAULT;
slouken@8582
  1441
        textureDesc.CPUAccessFlags = 0;
slouken@8582
  1442
    }
slouken@8582
  1443
slouken@8582
  1444
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@8582
  1445
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
slouken@8582
  1446
    } else {
slouken@8582
  1447
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
slouken@8582
  1448
    }
slouken@8582
  1449
slouken@8582
  1450
#if 0
slouken@8582
  1451
    // Fill the texture with a non-black color, for debugging purposes:
slouken@8582
  1452
    const int numPixels = textureDesc.Width * textureDesc.Height;
slouken@8582
  1453
    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
slouken@8582
  1454
    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
slouken@8582
  1455
    for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
slouken@8582
  1456
        initialTexturePixels[i+0] = 0xff;
slouken@8582
  1457
        initialTexturePixels[i+1] = 0xff;
slouken@8582
  1458
        initialTexturePixels[i+2] = 0x00;
slouken@8582
  1459
        initialTexturePixels[i+3] = 0xff;
slouken@8582
  1460
    }
slouken@8582
  1461
    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
slouken@8582
  1462
    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
slouken@8582
  1463
    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
slouken@8582
  1464
    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
slouken@8582
  1465
#endif
slouken@8582
  1466
slouken@8582
  1467
    result = rendererData->d3dDevice->CreateTexture2D(
slouken@8582
  1468
        &textureDesc,
slouken@8582
  1469
        NULL,   // &initialTextureData,
slouken@8582
  1470
        &textureData->mainTexture
slouken@8582
  1471
        );
slouken@8582
  1472
    if (FAILED(result)) {
slouken@8582
  1473
        D3D11_DestroyTexture(renderer, texture);
slouken@8582
  1474
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D", result);
slouken@8582
  1475
        return -1;
slouken@8582
  1476
    }
slouken@8582
  1477
slouken@8582
  1478
    if (texture->access & SDL_TEXTUREACCESS_TARGET) {
slouken@8582
  1479
        D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
slouken@8582
  1480
        renderTargetViewDesc.Format = textureDesc.Format;
slouken@8582
  1481
        renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
slouken@8582
  1482
        renderTargetViewDesc.Texture2D.MipSlice = 0;
slouken@8582
  1483
slouken@8582
  1484
        result = rendererData->d3dDevice->CreateRenderTargetView(
slouken@8582
  1485
            textureData->mainTexture.Get(),
slouken@8582
  1486
            &renderTargetViewDesc,
slouken@8582
  1487
            &textureData->mainTextureRenderTargetView);
slouken@8582
  1488
        if (FAILED(result)) {
slouken@8582
  1489
            D3D11_DestroyTexture(renderer, texture);
slouken@8582
  1490
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRenderTargetView", result);
slouken@8582
  1491
            return -1;
slouken@8582
  1492
        }
slouken@8582
  1493
    }
slouken@8582
  1494
slouken@8582
  1495
    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
slouken@8582
  1496
    resourceViewDesc.Format = textureDesc.Format;
slouken@8582
  1497
    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
slouken@8582
  1498
    resourceViewDesc.Texture2D.MostDetailedMip = 0;
slouken@8582
  1499
    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
slouken@8582
  1500
    result = rendererData->d3dDevice->CreateShaderResourceView(
slouken@8582
  1501
        textureData->mainTexture.Get(),
slouken@8582
  1502
        &resourceViewDesc,
slouken@8582
  1503
        &textureData->mainTextureResourceView
slouken@8582
  1504
        );
slouken@8582
  1505
    if (FAILED(result)) {
slouken@8582
  1506
        D3D11_DestroyTexture(renderer, texture);
slouken@8582
  1507
        WIN_SetErrorFromHRESULT(__FUNCTION__ "ID3D11Device1::CreateShaderResourceView", result);
slouken@8582
  1508
        return -1;
slouken@8582
  1509
    }
slouken@8582
  1510
slouken@8582
  1511
    return 0;
dludwig@8416
  1512
}
dludwig@8416
  1513
dludwig@8416
  1514
static void
dludwig@8416
  1515
D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
  1516
                     SDL_Texture * texture)
dludwig@8416
  1517
{
slouken@8582
  1518
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
  1519
dludwig@8416
  1520
    if (textureData) {
dludwig@8416
  1521
        if (textureData->pixelFormat) {
dludwig@8416
  1522
            SDL_FreeFormat(textureData->pixelFormat);
dludwig@8416
  1523
            textureData->pixelFormat = NULL;
dludwig@8416
  1524
        }
dludwig@8416
  1525
dludwig@8416
  1526
        delete textureData;
dludwig@8416
  1527
        texture->driverdata = NULL;
dludwig@8416
  1528
    }
dludwig@8416
  1529
}
dludwig@8416
  1530
dludwig@8416
  1531
static int
dludwig@8416
  1532
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8459
  1533
                    const SDL_Rect * rect, const void * srcPixels,
dludwig@8459
  1534
                    int srcPitch)
dludwig@8416
  1535
{
slouken@8582
  1536
    // Lock the texture, retrieving a buffer to write pixel data to:
slouken@8582
  1537
    void * destPixels = NULL;
slouken@8582
  1538
    int destPitch = 0;
slouken@8582
  1539
    if (D3D11_LockTexture(renderer, texture, rect, &destPixels, &destPitch) != 0) {
slouken@8582
  1540
        // An error is already set.  Attach some info to it, then return to
slouken@8582
  1541
        // the caller.
slouken@8582
  1542
        std::string errorMessage = string(__FUNCTION__ ", Lock Texture Failed: ") + SDL_GetError();
slouken@8582
  1543
        return SDL_SetError(errorMessage.c_str());
slouken@8582
  1544
    }
slouken@8582
  1545
slouken@8582
  1546
    // Copy pixel data to the locked texture's memory:
slouken@8582
  1547
    for (int y = 0; y < rect->h; ++y) {
slouken@8582
  1548
        memcpy(
slouken@8582
  1549
            ((Uint8 *)destPixels) + (destPitch * y),
slouken@8582
  1550
            ((Uint8 *)srcPixels) + (srcPitch * y),
slouken@8582
  1551
            srcPitch
slouken@8582
  1552
            );
slouken@8582
  1553
    }
slouken@8582
  1554
slouken@8582
  1555
    // Commit the texture's memory back to Direct3D:
slouken@8582
  1556
    D3D11_UnlockTexture(renderer, texture);
dludwig@8416
  1557
dludwig@8459
  1558
    // Return to the caller:
dludwig@8416
  1559
    return 0;
dludwig@8416
  1560
}
dludwig@8416
  1561
dludwig@8400
  1562
static int
dludwig@8451
  1563
D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8451
  1564
                  const SDL_Rect * rect, void **pixels, int *pitch)
dludwig@8451
  1565
{
dludwig@8451
  1566
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1567
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
slouken@8582
  1568
    HRESULT result = S_OK;
slouken@8582
  1569
dludwig@8451
  1570
    if (textureData->stagingTexture) {
dludwig@8556
  1571
        return SDL_SetError("texture is already locked");
dludwig@8451
  1572
    }
dludwig@8451
  1573
    
dludwig@8451
  1574
    // Create a 'staging' texture, which will be used to write to a portion
dludwig@8451
  1575
    // of the main texture.  This is necessary, as Direct3D 11.1 does not
dludwig@8451
  1576
    // have the ability to write a CPU-bound pixel buffer to a rectangular
dludwig@8451
  1577
    // subrect of a texture.  Direct3D 11.1 can, however, write a pixel
dludwig@8451
  1578
    // buffer to an entire texture, hence the use of a staging texture.
dludwig@8561
  1579
    //
dludwig@8561
  1580
    // TODO, WinRT: consider avoiding the use of a staging texture in D3D11_LockTexture if/when the entire texture is being updated
dludwig@8451
  1581
    D3D11_TEXTURE2D_DESC stagingTextureDesc;
dludwig@8451
  1582
    textureData->mainTexture->GetDesc(&stagingTextureDesc);
dludwig@8451
  1583
    stagingTextureDesc.Width = rect->w;
dludwig@8451
  1584
    stagingTextureDesc.Height = rect->h;
dludwig@8451
  1585
    stagingTextureDesc.BindFlags = 0;
dludwig@8451
  1586
    stagingTextureDesc.MiscFlags = 0;
dludwig@8451
  1587
    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8451
  1588
    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
dludwig@8451
  1589
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8451
  1590
        &stagingTextureDesc,
dludwig@8451
  1591
        NULL,
dludwig@8451
  1592
        &textureData->stagingTexture);
dludwig@8451
  1593
    if (FAILED(result)) {
dludwig@8553
  1594
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
dludwig@8451
  1595
        return -1;
dludwig@8451
  1596
    }
dludwig@8451
  1597
dludwig@8451
  1598
    // Get a write-only pointer to data in the staging texture:
slouken@8582
  1599
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
slouken@8582
  1600
    result = rendererData->d3dContext->Map(
slouken@8582
  1601
        textureData->stagingTexture.Get(),
slouken@8582
  1602
        D3D11CalcSubresource(0, 0, 0),
slouken@8582
  1603
        D3D11_MAP_WRITE,
slouken@8582
  1604
        0,
slouken@8582
  1605
        &textureMemory
slouken@8582
  1606
        );
slouken@8582
  1607
    if (FAILED(result)) {
slouken@8582
  1608
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
slouken@8582
  1609
        textureData->stagingTexture = nullptr;
slouken@8582
  1610
        return -1;
dludwig@8451
  1611
    }
dludwig@8451
  1612
dludwig@8451
  1613
    // Make note of where the staging texture will be written to (on a
dludwig@8451
  1614
    // call to SDL_UnlockTexture):
dludwig@8451
  1615
    textureData->lockedTexturePosition = XMINT2(rect->x, rect->y);
dludwig@8451
  1616
dludwig@8451
  1617
    // Make sure the caller has information on the texture's pixel buffer,
dludwig@8451
  1618
    // then return:
dludwig@8451
  1619
    *pixels = textureMemory.pData;
dludwig@8451
  1620
    *pitch = textureMemory.RowPitch;
dludwig@8451
  1621
    return 0;
dludwig@8451
  1622
}
dludwig@8451
  1623
dludwig@8451
  1624
static void
dludwig@8451
  1625
D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8451
  1626
{
dludwig@8451
  1627
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8451
  1628
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8451
  1629
dludwig@8451
  1630
    // Commit the pixel buffer's changes back to the staging texture:
dludwig@8451
  1631
    rendererData->d3dContext->Unmap(
dludwig@8451
  1632
        textureData->stagingTexture.Get(),
dludwig@8451
  1633
        0);
dludwig@8451
  1634
dludwig@8451
  1635
    // Copy the staging texture's contents back to the main texture:
dludwig@8451
  1636
    rendererData->d3dContext->CopySubresourceRegion(
dludwig@8451
  1637
        textureData->mainTexture.Get(),
dludwig@8451
  1638
        D3D11CalcSubresource(0, 0, 0),
dludwig@8451
  1639
        textureData->lockedTexturePosition.x,
dludwig@8451
  1640
        textureData->lockedTexturePosition.y,
dludwig@8451
  1641
        0,
dludwig@8451
  1642
        textureData->stagingTexture.Get(),
dludwig@8451
  1643
        D3D11CalcSubresource(0, 0, 0),
dludwig@8451
  1644
        NULL);
dludwig@8451
  1645
dludwig@8451
  1646
    // Clean up and return:
dludwig@8451
  1647
    textureData->stagingTexture = nullptr;
dludwig@8451
  1648
    textureData->lockedTexturePosition = XMINT2(0, 0);
dludwig@8451
  1649
}
dludwig@8451
  1650
dludwig@8451
  1651
static int
slouken@8582
  1652
D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@8582
  1653
{
slouken@8582
  1654
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1655
slouken@8582
  1656
    if (texture == NULL) {
slouken@8582
  1657
        rendererData->currentOffscreenRenderTargetView = nullptr;
slouken@8582
  1658
        return 0;
slouken@8582
  1659
    }
slouken@8582
  1660
slouken@8582
  1661
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
slouken@8582
  1662
slouken@8582
  1663
    if (!textureData->mainTextureRenderTargetView) {
slouken@8582
  1664
        return SDL_SetError("specified texture is not a render target");
slouken@8582
  1665
    }
slouken@8582
  1666
slouken@8582
  1667
    rendererData->currentOffscreenRenderTargetView = textureData->mainTextureRenderTargetView;
slouken@8582
  1668
slouken@8582
  1669
    return 0;
dludwig@8548
  1670
}
dludwig@8459
  1671
dludwig@8459
  1672
static int
dludwig@8400
  1673
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
  1674
{
dludwig@8432
  1675
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8432
  1676
dludwig@8432
  1677
    if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
dludwig@8432
  1678
        // If the viewport is empty, assume that it is because
dludwig@8432
  1679
        // SDL_CreateRenderer is calling it, and will call it again later
dludwig@8432
  1680
        // with a non-empty viewport.
dludwig@8432
  1681
        return 0;
dludwig@8432
  1682
    }
dludwig@8432
  1683
dludwig@8548
  1684
    // Make sure the SDL viewport gets rotated to that of the physical display's orientation.
dludwig@8548
  1685
    // Keep in mind here that the Y-axis will be been inverted (from Direct3D's
dludwig@8548
  1686
    // default coordinate system) so rotations will be done in the opposite
dludwig@8548
  1687
    // direction of the DXGI_MODE_ROTATION enumeration.
dludwig@8570
  1688
    DirectX::XMMATRIX projection;
slouken@8582
  1689
    switch (D3D11_GetRotationForOrientation(data->orientation))
slouken@8582
  1690
    {
slouken@8582
  1691
        case DXGI_MODE_ROTATION_IDENTITY:
slouken@8582
  1692
            projection = XMMatrixIdentity();
slouken@8582
  1693
            break;
slouken@8582
  1694
        case DXGI_MODE_ROTATION_ROTATE270:
slouken@8582
  1695
            projection = XMMatrixRotationZ(XM_PIDIV2);
slouken@8582
  1696
            break;
slouken@8582
  1697
        case DXGI_MODE_ROTATION_ROTATE180:
slouken@8582
  1698
            projection = XMMatrixRotationZ(XM_PI);
slouken@8582
  1699
            break;
slouken@8582
  1700
        case DXGI_MODE_ROTATION_ROTATE90:
slouken@8582
  1701
            projection = XMMatrixRotationZ(-XM_PIDIV2);
slouken@8582
  1702
            break;
slouken@8582
  1703
        default:
slouken@8582
  1704
            return SDL_SetError("An unknown DisplayOrientation is being used");
dludwig@8432
  1705
    }
dludwig@8432
  1706
slouken@8582
  1707
    //
slouken@8582
  1708
    // Update the view matrix
slouken@8582
  1709
    //
slouken@8582
  1710
    float viewportWidth = (float) renderer->viewport.w;
slouken@8582
  1711
    float viewportHeight = (float) renderer->viewport.h;
slouken@8582
  1712
    DirectX::XMMATRIX view = XMMatrixMultiply(
slouken@8582
  1713
        XMMatrixScaling(2.0f / viewportWidth, 2.0f / viewportHeight, 1.0f),
slouken@8582
  1714
        XMMatrixMultiply(
slouken@8582
  1715
            XMMatrixTranslation(-1, -1, 0),
slouken@8582
  1716
            XMMatrixRotationX(XM_PI)
dludwig@8570
  1717
            ));
dludwig@8570
  1718
dludwig@8570
  1719
    //
dludwig@8570
  1720
    // Combine the projection + view matrix together now, as both only get
dludwig@8570
  1721
    // set here (as of this writing, on Dec 26, 2013).  When done, store it
dludwig@8570
  1722
    // for eventual transfer to the GPU.
dludwig@8570
  1723
    //
dludwig@8570
  1724
    XMStoreFloat4x4(&data->vertexShaderConstantsData.projectionAndView,
dludwig@8570
  1725
        XMMatrixMultiply(
dludwig@8570
  1726
            view,
dludwig@8570
  1727
            projection));
dludwig@8433
  1728
dludwig@8433
  1729
    //
dludwig@8456
  1730
    // Reset the model matrix
dludwig@8456
  1731
    //
dludwig@8456
  1732
    XMStoreFloat4x4(&data->vertexShaderConstantsData.model, XMMatrixIdentity());
dludwig@8456
  1733
dludwig@8456
  1734
    //
dludwig@8433
  1735
    // Update the Direct3D viewport, which seems to be aligned to the
dludwig@8510
  1736
    // swap buffer's coordinate space, which is always in either
dludwig@8510
  1737
    // a landscape mode, for all Windows 8/RT devices, or a portrait mode,
dludwig@8510
  1738
    // for Windows Phone devices.
dludwig@8433
  1739
    //
slouken@8582
  1740
    SDL_FRect orientationAlignedViewport;
slouken@8582
  1741
    const bool swapDimensions = D3D11_IsDisplayRotated90Degrees(data->orientation);
slouken@8582
  1742
    if (swapDimensions) {
slouken@8582
  1743
        orientationAlignedViewport.x = (float) renderer->viewport.y;
slouken@8582
  1744
        orientationAlignedViewport.y = (float) renderer->viewport.x;
slouken@8582
  1745
        orientationAlignedViewport.w = (float) renderer->viewport.h;
slouken@8582
  1746
        orientationAlignedViewport.h = (float) renderer->viewport.w;
slouken@8582
  1747
    } else {
slouken@8582
  1748
        orientationAlignedViewport.x = (float) renderer->viewport.x;
slouken@8582
  1749
        orientationAlignedViewport.y = (float) renderer->viewport.y;
slouken@8582
  1750
        orientationAlignedViewport.w = (float) renderer->viewport.w;
slouken@8582
  1751
        orientationAlignedViewport.h = (float) renderer->viewport.h;
slouken@8582
  1752
    }
slouken@8582
  1753
    // TODO, WinRT: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped)
slouken@8582
  1754
slouken@8582
  1755
    D3D11_VIEWPORT viewport;
slouken@8582
  1756
    memset(&viewport, 0, sizeof(viewport));
slouken@8582
  1757
    viewport.TopLeftX = orientationAlignedViewport.x;
slouken@8582
  1758
    viewport.TopLeftY = orientationAlignedViewport.y;
slouken@8582
  1759
    viewport.Width = orientationAlignedViewport.w;
slouken@8582
  1760
    viewport.Height = orientationAlignedViewport.h;
slouken@8582
  1761
    viewport.MinDepth = 0.0f;
slouken@8582
  1762
    viewport.MaxDepth = 1.0f;
dludwig@8432
  1763
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8432
  1764
dludwig@8433
  1765
#if 0
slouken@8582
  1766
    SDL_Log("%s, oav={%.0f,%.0f,%.0f,%.0f}, rend={%.0f,%.0f}\n",
slouken@8582
  1767
        __FUNCTION__,
slouken@8582
  1768
        orientationAlignedViewport.x,
slouken@8582
  1769
        orientationAlignedViewport.y,
slouken@8582
  1770
        orientationAlignedViewport.w,
slouken@8582
  1771
        orientationAlignedViewport.h,
slouken@8582
  1772
        data->renderTargetSize.x,
dludwig@8433
  1773
        data->renderTargetSize.y);
dludwig@8433
  1774
#endif
dludwig@8433
  1775
dludwig@8400
  1776
    return 0;
dludwig@8400
  1777
}
dludwig@8400
  1778
dludwig@8482
  1779
static int
dludwig@8482
  1780
D3D11_UpdateClipRect(SDL_Renderer * renderer)
dludwig@8482
  1781
{
dludwig@8569
  1782
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8569
  1783
    const SDL_Rect *rect = &renderer->clip_rect;
dludwig@8569
  1784
dludwig@8569
  1785
    if (SDL_RectEmpty(rect)) {
dludwig@8569
  1786
        data->d3dContext->RSSetScissorRects(0, 0);
dludwig@8569
  1787
    } else {
dludwig@8569
  1788
        D3D11_RECT scissorRect;
dludwig@8569
  1789
        if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &scissorRect) != 0) {
dludwig@8569
  1790
            /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
dludwig@8569
  1791
            return -1;
dludwig@8569
  1792
        }
dludwig@8569
  1793
        data->d3dContext->RSSetScissorRects(1, &scissorRect);
dludwig@8569
  1794
    }
dludwig@8569
  1795
dludwig@8482
  1796
    return 0;
dludwig@8482
  1797
}
dludwig@8482
  1798
slouken@8582
  1799
static ComPtr<ID3D11RenderTargetView> &
slouken@8582
  1800
D3D11_GetCurrentRenderTargetView(SDL_Renderer * renderer)
slouken@8582
  1801
{
slouken@8582
  1802
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1803
    if (data->currentOffscreenRenderTargetView) {
slouken@8582
  1804
        return data->currentOffscreenRenderTargetView;
slouken@8582
  1805
    } else {
slouken@8582
  1806
        return data->mainRenderTargetView;
slouken@8582
  1807
    }
dludwig@8459
  1808
}
dludwig@8459
  1809
slouken@8582
  1810
static int
slouken@8582
  1811
D3D11_RenderClear(SDL_Renderer * renderer)
slouken@8582
  1812
{
slouken@8582
  1813
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1814
    const float colorRGBA[] = {
slouken@8582
  1815
        (renderer->r / 255.0f),
slouken@8582
  1816
        (renderer->g / 255.0f),
slouken@8582
  1817
        (renderer->b / 255.0f),
slouken@8582
  1818
        (renderer->a / 255.0f)
slouken@8582
  1819
    };
slouken@8582
  1820
    data->d3dContext->ClearRenderTargetView(
slouken@8582
  1821
        D3D11_GetCurrentRenderTargetView(renderer).Get(),
slouken@8582
  1822
        colorRGBA
slouken@8582
  1823
        );
slouken@8582
  1824
    return 0;
dludwig@8416
  1825
}
dludwig@8416
  1826
dludwig@8429
  1827
static int
dludwig@8429
  1828
D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
dludwig@8550
  1829
                         const void * vertexData, size_t dataSizeInBytes)
dludwig@8416
  1830
{
dludwig@8416
  1831
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8425
  1832
    HRESULT result = S_OK;
dludwig@8449
  1833
    D3D11_BUFFER_DESC vertexBufferDesc;
dludwig@8449
  1834
dludwig@8449
  1835
    if (rendererData->vertexBuffer) {
dludwig@8449
  1836
        rendererData->vertexBuffer->GetDesc(&vertexBufferDesc);
dludwig@8449
  1837
    } else {
dludwig@8449
  1838
        memset(&vertexBufferDesc, 0, sizeof(vertexBufferDesc));
dludwig@8449
  1839
    }
dludwig@8449
  1840
dludwig@8449
  1841
    if (vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
slouken@8582
  1842
        D3D11_MAPPED_SUBRESOURCE mappedResource;
slouken@8582
  1843
        ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
slouken@8582
  1844
        result = rendererData->d3dContext->Map(rendererData->vertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
slouken@8582
  1845
        if (FAILED(result)) {
slouken@8582
  1846
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [vertex buffer]", result);
slouken@8582
  1847
            return -1;
slouken@8582
  1848
        }
slouken@8582
  1849
        memcpy(mappedResource.pData, vertexData, dataSizeInBytes);
slouken@8582
  1850
        rendererData->d3dContext->Unmap(rendererData->vertexBuffer.Get(), 0);
slouken@8582
  1851
    } else {
slouken@8582
  1852
        vertexBufferDesc.ByteWidth = dataSizeInBytes;
slouken@8582
  1853
        vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
slouken@8582
  1854
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
slouken@8582
  1855
        vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
slouken@8582
  1856
slouken@8582
  1857
        D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
slouken@8582
  1858
        vertexBufferData.pSysMem = vertexData;
slouken@8582
  1859
        vertexBufferData.SysMemPitch = 0;
slouken@8582
  1860
        vertexBufferData.SysMemSlicePitch = 0;
slouken@8582
  1861
slouken@8582
  1862
        result = rendererData->d3dDevice->CreateBuffer(
slouken@8582
  1863
            &vertexBufferDesc,
slouken@8582
  1864
            &vertexBufferData,
slouken@8582
  1865
            &rendererData->vertexBuffer
slouken@8582
  1866
            );
slouken@8582
  1867
        if (FAILED(result)) {
slouken@8582
  1868
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex buffer]", result);
slouken@8582
  1869
            return -1;
slouken@8582
  1870
        }
slouken@8582
  1871
    }
slouken@8582
  1872
slouken@8582
  1873
    UINT stride = sizeof(VertexPositionColor);
slouken@8582
  1874
    UINT offset = 0;
slouken@8582
  1875
    rendererData->d3dContext->IASetVertexBuffers(
slouken@8582
  1876
        0,
slouken@8582
  1877
        1,
slouken@8582
  1878
        rendererData->vertexBuffer.GetAddressOf(),
slouken@8582
  1879
        &stride,
slouken@8582
  1880
        &offset
dludwig@8429
  1881
        );
dludwig@8429
  1882
dludwig@8429
  1883
    return 0;
dludwig@8429
  1884
}
dludwig@8429
  1885
dludwig@8429
  1886
static void
dludwig@8429
  1887
D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
dludwig@8429
  1888
{
dludwig@8429
  1889
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1890
slouken@8582
  1891
    rendererData->d3dContext->OMSetRenderTargets(
slouken@8582
  1892
        1,
slouken@8582
  1893
        D3D11_GetCurrentRenderTargetView(renderer).GetAddressOf(),
slouken@8582
  1894
        nullptr
slouken@8582
  1895
        );
slouken@8582
  1896
}
slouken@8582
  1897
slouken@8582
  1898
static void
slouken@8582
  1899
D3D11_RenderSetBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@8582
  1900
{
slouken@8582
  1901
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1902
    switch (blendMode) {
slouken@8582
  1903
        case SDL_BLENDMODE_BLEND:
slouken@8582
  1904
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeBlend.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1905
            break;
slouken@8582
  1906
        case SDL_BLENDMODE_ADD:
slouken@8582
  1907
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeAdd.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1908
            break;
slouken@8582
  1909
        case SDL_BLENDMODE_MOD:
slouken@8582
  1910
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeMod.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1911
            break;
dludwig@8431
  1912
        case SDL_BLENDMODE_NONE:
slouken@8582
  1913
            rendererData->d3dContext->OMSetBlendState(NULL, 0, 0xFFFFFFFF);
slouken@8582
  1914
            break;
slouken@8582
  1915
    }
slouken@8582
  1916
}
dludwig@8429
  1917
dludwig@8429
  1918
static void
dludwig@8429
  1919
D3D11_SetPixelShader(SDL_Renderer * renderer,
dludwig@8429
  1920
                     ID3D11PixelShader * shader,
dludwig@8429
  1921
                     ID3D11ShaderResourceView * shaderResource,
dludwig@8429
  1922
                     ID3D11SamplerState * sampler)
dludwig@8429
  1923
{
slouken@8582
  1924
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1925
    rendererData->d3dContext->PSSetShader(shader, nullptr, 0);
slouken@8582
  1926
    rendererData->d3dContext->PSSetShaderResources(0, 1, &shaderResource);
slouken@8582
  1927
    rendererData->d3dContext->PSSetSamplers(0, 1, &sampler);
dludwig@8429
  1928
}
dludwig@8429
  1929
dludwig@8429
  1930
static void
dludwig@8429
  1931
D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
dludwig@8449
  1932
                         D3D11_PRIMITIVE_TOPOLOGY primitiveTopology,
dludwig@8449
  1933
                         UINT vertexCount)
dludwig@8429
  1934
{
dludwig@8429
  1935
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
slouken@8582
  1936
slouken@8582
  1937
    rendererData->d3dContext->UpdateSubresource(
slouken@8582
  1938
        rendererData->vertexShaderConstants.Get(),
slouken@8582
  1939
        0,
slouken@8582
  1940
        NULL,
slouken@8582
  1941
        &rendererData->vertexShaderConstantsData,
slouken@8582
  1942
        0,
slouken@8582
  1943
        0
slouken@8582
  1944
        );
slouken@8582
  1945
slouken@8582
  1946
    rendererData->d3dContext->IASetPrimitiveTopology(primitiveTopology);
slouken@8582
  1947
    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
slouken@8582
  1948
    rendererData->d3dContext->VSSetShader(rendererData->vertexShader.Get(), nullptr, 0);
slouken@8582
  1949
    rendererData->d3dContext->VSSetConstantBuffers(0, 1, rendererData->vertexShaderConstants.GetAddressOf());
slouken@8582
  1950
    if (SDL_RectEmpty(&(renderer->clip_rect))) {
slouken@8582
  1951
        rendererData->d3dContext->RSSetState(rendererData->mainRasterizer.Get());
slouken@8582
  1952
    } else {
slouken@8582
  1953
        rendererData->d3dContext->RSSetState(rendererData->clippedRasterizer.Get());
slouken@8582
  1954
    }
dludwig@8449
  1955
    rendererData->d3dContext->Draw(vertexCount, 0);
dludwig@8449
  1956
}
dludwig@8449
  1957
dludwig@8449
  1958
static int
dludwig@8450
  1959
D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
  1960
                       const SDL_FPoint * points, int count)
dludwig@8450
  1961
{
dludwig@8450
  1962
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8450
  1963
    float r, g, b, a;
dludwig@8450
  1964
dludwig@8450
  1965
    r = (float)(renderer->r / 255.0f);
dludwig@8450
  1966
    g = (float)(renderer->g / 255.0f);
dludwig@8450
  1967
    b = (float)(renderer->b / 255.0f);
dludwig@8450
  1968
    a = (float)(renderer->a / 255.0f);
dludwig@8450
  1969
dludwig@8507
  1970
    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
slouken@8582
  1971
    for (int i = 0; i < min(count, 128); ++i) {
slouken@8582
  1972
        const VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
slouken@8582
  1973
        vertices[i] = v;
slouken@8582
  1974
    }
slouken@8582
  1975
slouken@8582
  1976
    D3D11_RenderStartDrawOp(renderer);
slouken@8582
  1977
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
slouken@8582
  1978
    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
slouken@8582
  1979
        SDL_stack_free(vertices);
slouken@8582
  1980
        return -1;
slouken@8582
  1981
    }
slouken@8582
  1982
slouken@8582
  1983
    D3D11_SetPixelShader(
slouken@8582
  1984
        renderer,
slouken@8582
  1985
        rendererData->colorPixelShader.Get(),
slouken@8582
  1986
        nullptr,
slouken@8582
  1987
        nullptr);
slouken@8582
  1988
slouken@8582
  1989
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, count);
dludwig@8507
  1990
    SDL_stack_free(vertices);
dludwig@8450
  1991
    return 0;
dludwig@8450
  1992
}
dludwig@8450
  1993
dludwig@8450
  1994
static int
dludwig@8449
  1995
D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
  1996
                      const SDL_FPoint * points, int count)
dludwig@8449
  1997
{
dludwig@8449
  1998
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8449
  1999
    float r, g, b, a;
dludwig@8449
  2000
dludwig@8449
  2001
    r = (float)(renderer->r / 255.0f);
dludwig@8449
  2002
    g = (float)(renderer->g / 255.0f);
dludwig@8449
  2003
    b = (float)(renderer->b / 255.0f);
dludwig@8449
  2004
    a = (float)(renderer->a / 255.0f);
dludwig@8449
  2005
dludwig@8507
  2006
    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
slouken@8582
  2007
    for (int i = 0; i < count; ++i) {
slouken@8582
  2008
        const VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
slouken@8582
  2009
        vertices[i] = v;
slouken@8582
  2010
    }
slouken@8582
  2011
slouken@8582
  2012
    D3D11_RenderStartDrawOp(renderer);
slouken@8582
  2013
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
slouken@8582
  2014
    if (D3D11_UpdateVertexBuffer(renderer, vertices, (unsigned int)count * sizeof(VertexPositionColor)) != 0) {
slouken@8582
  2015
        SDL_stack_free(vertices);
slouken@8582
  2016
        return -1;
slouken@8582
  2017
    }
slouken@8582
  2018
slouken@8582
  2019
    D3D11_SetPixelShader(
slouken@8582
  2020
        renderer,
slouken@8582
  2021
        rendererData->colorPixelShader.Get(),
slouken@8582
  2022
        nullptr,
slouken@8582
  2023
        nullptr);
slouken@8582
  2024
slouken@8582
  2025
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, count);
dludwig@8507
  2026
    SDL_stack_free(vertices);
dludwig@8449
  2027
    return 0;
dludwig@8429
  2028
}
dludwig@8429
  2029
dludwig@8429
  2030
static int
dludwig@8429
  2031
D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
  2032
                      const SDL_FRect * rects, int count)
dludwig@8429
  2033
{
dludwig@8429
  2034
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  2035
    float r, g, b, a;
dludwig@8429
  2036
dludwig@8431
  2037
    r = (float)(renderer->r / 255.0f);
dludwig@8431
  2038
    g = (float)(renderer->g / 255.0f);
dludwig@8431
  2039
    b = (float)(renderer->b / 255.0f);
dludwig@8431
  2040
    a = (float)(renderer->a / 255.0f);
dludwig@8429
  2041
dludwig@8433
  2042
#if 0
dludwig@8433
  2043
    // Set up a test pattern:
dludwig@8434
  2044
    SDL_FRect _rects[] = {
dludwig@8433
  2045
        {-1.1f, 1.1f, 1.1f, -1.1f},
dludwig@8433
  2046
        {-1.0f, 1.0f, 1.0f, -1.0f},     // red
dludwig@8433
  2047
        {0.0f, 1.0f, 1.0f, -1.0f},      // green
dludwig@8433
  2048
        {-1.0f, 0.0f, 1.0f, -1.0f},     // blue
dludwig@8433
  2049
        {0.0f, 0.0f, 1.0f, -1.0f}       // white
dludwig@8433
  2050
    };
dludwig@8434
  2051
    count = sizeof(_rects) / sizeof(SDL_FRect);
dludwig@8434
  2052
#define rects _rects
dludwig@8433
  2053
#endif
dludwig@8429
  2054
slouken@8582
  2055
    for (int i = 0; i < count; ++i) {
slouken@8582
  2056
        D3D11_RenderStartDrawOp(renderer);
slouken@8582
  2057
        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
slouken@8582
  2058
slouken@8582
  2059
#if 0
slouken@8582
  2060
        // Set colors for the test pattern:
slouken@8582
  2061
        a = 1.0f;
slouken@8582
  2062
        switch (i) {
slouken@8582
  2063
            case 0: r = 1.0f; g = 1.0f; b = 0.0f; break;
slouken@8582
  2064
            case 1: r = 1.0f; g = 0.0f; b = 0.0f; break;
slouken@8582
  2065
            case 2: r = 0.0f; g = 1.0f; b = 0.0f; break;
slouken@8582
  2066
            case 3: r = 0.0f; g = 0.0f; b = 1.0f; break;
slouken@8582
  2067
            case 4: r = 1.0f; g = 1.0f; b = 1.0f; break;
slouken@8582
  2068
        }
slouken@8582
  2069
#endif
slouken@8582
  2070
slouken@8582
  2071
        VertexPositionColor vertices[] = {
slouken@8582
  2072
            {XMFLOAT3(rects[i].x, rects[i].y, 0.0f),                           XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
slouken@8582
  2073
            {XMFLOAT3(rects[i].x, rects[i].y + rects[i].h, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
slouken@8582
  2074
            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
slouken@8582
  2075
            {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)},
slouken@8582
  2076
        };
slouken@8582
  2077
        if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
slouken@8582
  2078
            return -1;
slouken@8582
  2079
        }
slouken@8582
  2080
slouken@8582
  2081
        D3D11_SetPixelShader(
slouken@8582
  2082
            renderer,
slouken@8582
  2083
            rendererData->colorPixelShader.Get(),
slouken@8582
  2084
            nullptr,
slouken@8582
  2085
            nullptr);
slouken@8582
  2086
slouken@8582
  2087
        D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
slouken@8582
  2088
    }
dludwig@8429
  2089
dludwig@8429
  2090
    return 0;
dludwig@8429
  2091
}
dludwig@8429
  2092
dludwig@8540
  2093
static ID3D11SamplerState *
dludwig@8540
  2094
D3D11_RenderGetSampler(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8540
  2095
{
dludwig@8540
  2096
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8540
  2097
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8540
  2098
dludwig@8540
  2099
    switch (textureData->scaleMode) {
dludwig@8540
  2100
        case SDL_D3D11_NEAREST_PIXEL_FILTER:
dludwig@8540
  2101
            return rendererData->nearestPixelSampler.Get();
dludwig@8540
  2102
        case SDL_D3D11_LINEAR_FILTER:
dludwig@8540
  2103
            return rendererData->linearSampler.Get();
dludwig@8540
  2104
        default:
dludwig@8540
  2105
            return NULL;
dludwig@8540
  2106
    }
dludwig@8540
  2107
}
dludwig@8540
  2108
dludwig@8429
  2109
static int
dludwig@8429
  2110
D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8429
  2111
                 const SDL_Rect * srcrect, const SDL_FRect * dstrect)
dludwig@8429
  2112
{
dludwig@8429
  2113
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  2114
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8429
  2115
dludwig@8429
  2116
    D3D11_RenderStartDrawOp(renderer);
dludwig@8442
  2117
    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
dludwig@8448
  2118
slouken@8582
  2119
    float minu = (float) srcrect->x / texture->w;
slouken@8582
  2120
    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@8582
  2121
    float minv = (float) srcrect->y / texture->h;
dludwig@8448
  2122
    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
dludwig@8458
  2123
dludwig@8458
  2124
    float r = 1.0f;
dludwig@8458
  2125
    float g = 1.0f;
dludwig@8458
  2126
    float b = 1.0f;
dludwig@8458
  2127
    float a = 1.0f;
dludwig@8458
  2128
    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
dludwig@8458
  2129
        r = (float)(texture->r / 255.0f);
dludwig@8458
  2130
        g = (float)(texture->g / 255.0f);
dludwig@8458
  2131
        b = (float)(texture->b / 255.0f);
dludwig@8458
  2132
    }
dludwig@8458
  2133
    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
dludwig@8458
  2134
        a = (float)(texture->a / 255.0f);
dludwig@8458
  2135
    }
slouken@8582
  2136
slouken@8582
  2137
    VertexPositionColor vertices[] = {
slouken@8582
  2138
        {XMFLOAT3(dstrect->x, dstrect->y, 0.0f),                           XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2139
        {XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f),              XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2140
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y, 0.0f),              XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2141
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2142
    };
slouken@8582
  2143
    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
slouken@8582
  2144
        return -1;
slouken@8582
  2145
    }
slouken@8582
  2146
slouken@8582
  2147
    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
slouken@8582
  2148
    D3D11_SetPixelShader(
slouken@8582
  2149
        renderer,
slouken@8582
  2150
        rendererData->texturePixelShader.Get(),
slouken@8582
  2151
        textureData->mainTextureResourceView.Get(),
slouken@8582
  2152
        textureSampler);
slouken@8582
  2153
slouken@8582
  2154
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8416
  2155
dludwig@8416
  2156
    return 0;
dludwig@8416
  2157
}
dludwig@8416
  2158
dludwig@8454
  2159
static int
dludwig@8455
  2160
D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8455
  2161
                   const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8455
  2162
                   const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
dludwig@8455
  2163
{
dludwig@8455
  2164
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8455
  2165
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8455
  2166
dludwig@8455
  2167
    D3D11_RenderStartDrawOp(renderer);
dludwig@8455
  2168
    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
dludwig@8455
  2169
slouken@8582
  2170
    float minu = (float) srcrect->x / texture->w;
slouken@8582
  2171
    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@8582
  2172
    float minv = (float) srcrect->y / texture->h;
dludwig@8455
  2173
    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
dludwig@8455
  2174
dludwig@8458
  2175
    float r = 1.0f;
dludwig@8458
  2176
    float g = 1.0f;
dludwig@8458
  2177
    float b = 1.0f;
dludwig@8458
  2178
    float a = 1.0f;
dludwig@8458
  2179
    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
dludwig@8458
  2180
        r = (float)(texture->r / 255.0f);
dludwig@8458
  2181
        g = (float)(texture->g / 255.0f);
dludwig@8458
  2182
        b = (float)(texture->b / 255.0f);
dludwig@8458
  2183
    }
dludwig@8458
  2184
    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
dludwig@8458
  2185
        a = (float)(texture->a / 255.0f);
dludwig@8458
  2186
    }
dludwig@8458
  2187
dludwig@8455
  2188
    if (flip & SDL_FLIP_HORIZONTAL) {
dludwig@8455
  2189
        float tmp = maxu;
dludwig@8455
  2190
        maxu = minu;
dludwig@8455
  2191
        minu = tmp;
dludwig@8455
  2192
    }
dludwig@8455
  2193
    if (flip & SDL_FLIP_VERTICAL) {
dludwig@8455
  2194
        float tmp = maxv;
dludwig@8455
  2195
        maxv = minv;
dludwig@8455
  2196
        minv = tmp;
dludwig@8455
  2197
    }
dludwig@8456
  2198
dludwig@8456
  2199
    XMFLOAT4X4 oldModelMatrix = rendererData->vertexShaderConstantsData.model;
dludwig@8456
  2200
    XMStoreFloat4x4(
dludwig@8456
  2201
        &rendererData->vertexShaderConstantsData.model,
dludwig@8456
  2202
        XMMatrixMultiply(
dludwig@8456
  2203
            XMMatrixRotationZ((float)(XM_PI * (float) angle / 180.0f)),
dludwig@8456
  2204
            XMMatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
dludwig@8456
  2205
            ));
dludwig@8456
  2206
slouken@8582
  2207
    const float minx = -center->x;
slouken@8582
  2208
    const float maxx = dstrect->w - center->x;
slouken@8582
  2209
    const float miny = -center->y;
slouken@8582
  2210
    const float maxy = dstrect->h - center->y;
slouken@8582
  2211
slouken@8582
  2212
    VertexPositionColor vertices[] = {
slouken@8582
  2213
        {XMFLOAT3(minx, miny, 0.0f), XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2214
        {XMFLOAT3(minx, maxy, 0.0f), XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2215
        {XMFLOAT3(maxx, miny, 0.0f), XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2216
        {XMFLOAT3(maxx, maxy, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
slouken@8582
  2217
    };
slouken@8582
  2218
    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
slouken@8582
  2219
        return -1;
slouken@8582
  2220
    }
slouken@8582
  2221
slouken@8582
  2222
    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
slouken@8582
  2223
    D3D11_SetPixelShader(
slouken@8582
  2224
        renderer,
slouken@8582
  2225
        rendererData->texturePixelShader.Get(),
slouken@8582
  2226
        textureData->mainTextureResourceView.Get(),
slouken@8582
  2227
        textureSampler);
slouken@8582
  2228
slouken@8582
  2229
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
slouken@8582
  2230
slouken@8582
  2231
    rendererData->vertexShaderConstantsData.model = oldModelMatrix;
dludwig@8455
  2232
dludwig@8455
  2233
    return 0;
dludwig@8455
  2234
}
dludwig@8455
  2235
dludwig@8455
  2236
static int
dludwig@8454
  2237
D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8454
  2238
                       Uint32 format, void * pixels, int pitch)
dludwig@8454
  2239
{
dludwig@8454
  2240
    D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8454
  2241
    HRESULT result = S_OK;
dludwig@8454
  2242
dludwig@8454
  2243
    // Retrieve a pointer to the back buffer:
slouken@8582
  2244
    ComPtr<ID3D11Texture2D> backBuffer;
slouken@8582
  2245
    result = data->swapChain->GetBuffer(
slouken@8582
  2246
        0,
slouken@8582
  2247
        __uuidof(ID3D11Texture2D),
slouken@8582
  2248
        &backBuffer
slouken@8582
  2249
        );
slouken@8582
  2250
    if (FAILED(result)) {
slouken@8582
  2251
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain1::GetBuffer [get back buffer]", result);
slouken@8582
  2252
        return -1;
dludwig@8454
  2253
    }
dludwig@8454
  2254
dludwig@8454
  2255
    // Create a staging texture to copy the screen's data to:
dludwig@8454
  2256
    ComPtr<ID3D11Texture2D> stagingTexture;
dludwig@8454
  2257
    D3D11_TEXTURE2D_DESC stagingTextureDesc;
dludwig@8454
  2258
    backBuffer->GetDesc(&stagingTextureDesc);
dludwig@8454
  2259
    stagingTextureDesc.Width = rect->w;
dludwig@8454
  2260
    stagingTextureDesc.Height = rect->h;
dludwig@8454
  2261
    stagingTextureDesc.BindFlags = 0;
dludwig@8454
  2262
    stagingTextureDesc.MiscFlags = 0;
dludwig@8454
  2263
    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
dludwig@8454
  2264
    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
dludwig@8454
  2265
    result = data->d3dDevice->CreateTexture2D(
dludwig@8454
  2266
        &stagingTextureDesc,
dludwig@8454
  2267
        NULL,
dludwig@8454
  2268
        &stagingTexture);
dludwig@8454
  2269
    if (FAILED(result)) {
dludwig@8553
  2270
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateTexture2D [create staging texture]", result);
dludwig@8454
  2271
        return -1;
dludwig@8454
  2272
    }
dludwig@8454
  2273
dludwig@8454
  2274
    // Copy the desired portion of the back buffer to the staging texture:
dludwig@8569
  2275
    D3D11_RECT srcRect;
dludwig@8569
  2276
    if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
dludwig@8569
  2277
        /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
dludwig@8569
  2278
        return -1;
dludwig@8569
  2279
    }
dludwig@8569
  2280
dludwig@8454
  2281
    D3D11_BOX srcBox;
dludwig@8569
  2282
    srcBox.left = srcRect.left;
dludwig@8569
  2283
    srcBox.right = srcRect.right;
dludwig@8569
  2284
    srcBox.top = srcRect.top;
dludwig@8569
  2285
    srcBox.bottom = srcRect.bottom;
dludwig@8454
  2286
    srcBox.front = 0;
dludwig@8454
  2287
    srcBox.back = 1;
dludwig@8454
  2288
    data->d3dContext->CopySubresourceRegion(
dludwig@8454
  2289
        stagingTexture.Get(),
dludwig@8454
  2290
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  2291
        0, 0, 0,
dludwig@8454
  2292
        backBuffer.Get(),
dludwig@8454
  2293
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  2294
        &srcBox);
dludwig@8454
  2295
dludwig@8454
  2296
    // Map the staging texture's data to CPU-accessible memory:
dludwig@8454
  2297
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8454
  2298
    result = data->d3dContext->Map(
dludwig@8454
  2299
        stagingTexture.Get(),
dludwig@8454
  2300
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  2301
        D3D11_MAP_READ,
dludwig@8454
  2302
        0,
dludwig@8454
  2303
        &textureMemory);
dludwig@8454
  2304
    if (FAILED(result)) {
dludwig@8553
  2305
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::Map [map staging texture]", result);
dludwig@8454
  2306
        return -1;
dludwig@8454
  2307
    }
dludwig@8454
  2308
dludwig@8454
  2309
    // Copy the data into the desired buffer, converting pixels to the
dludwig@8454
  2310
    // desired format at the same time:
slouken@8582
  2311
    if (SDL_ConvertPixels(
slouken@8582
  2312
        rect->w, rect->h,
slouken@8582
  2313
        DXGIFormatToSDLPixelFormat(stagingTextureDesc.Format),
slouken@8582
  2314
        textureMemory.pData,
slouken@8582
  2315
        textureMemory.RowPitch,
slouken@8582
  2316
        format,
slouken@8582
  2317
        pixels,
slouken@8582
  2318
        pitch) != 0)
slouken@8582
  2319
    {
slouken@8582
  2320
        // When SDL_ConvertPixels fails, it'll have already set the format.
slouken@8582
  2321
        // Get the error message, and attach some extra data to it.
slouken@8582
  2322
        std::string errorMessage = string(__FUNCTION__ ", Convert Pixels failed: ") + SDL_GetError();
slouken@8582
  2323
        return SDL_SetError(errorMessage.c_str());
slouken@8582
  2324
    }
slouken@8582
  2325
slouken@8582
  2326
    // Unmap the texture:
slouken@8582
  2327
    data->d3dContext->Unmap(
slouken@8582
  2328
        stagingTexture.Get(),
slouken@8582
  2329
        D3D11CalcSubresource(0, 0, 0));
slouken@8582
  2330
slouken@8582
  2331
    // All done.  The staging texture will be cleaned up in it's container
slouken@8582
  2332
    // ComPtr<>'s destructor.
slouken@8582
  2333
    return 0;
dludwig@8454
  2334
}
dludwig@8454
  2335
dludwig@8401
  2336
static void
dludwig@8401
  2337
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
  2338
{
dludwig@8401
  2339
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
  2340
slouken@8582
  2341
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
  2342
    // The first argument instructs DXGI to block until VSync, putting the application
slouken@8582
  2343
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
slouken@8582
  2344
    // frames that will never be displayed to the screen.
slouken@8582
  2345
    HRESULT hr = data->swapChain->Present(1, 0);
slouken@8582
  2346
#else
slouken@8582
  2347
    // The application may optionally specify "dirty" or "scroll"
slouken@8582
  2348
    // rects to improve efficiency in certain scenarios.
slouken@8582
  2349
    // This option is not available on Windows Phone 8, to note.
slouken@8582
  2350
    DXGI_PRESENT_PARAMETERS parameters = {0};
slouken@8582
  2351
    parameters.DirtyRectsCount = 0;
slouken@8582
  2352
    parameters.pDirtyRects = nullptr;
slouken@8582
  2353
    parameters.pScrollRect = nullptr;
slouken@8582
  2354
    parameters.pScrollOffset = nullptr;
slouken@8582
  2355
    
slouken@8582
  2356
    // The first argument instructs DXGI to block until VSync, putting the application
slouken@8582
  2357
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
slouken@8582
  2358
    // frames that will never be displayed to the screen.
slouken@8582
  2359
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
slouken@8582
  2360
#endif
slouken@8582
  2361
slouken@8582
  2362
    // Discard the contents of the render target.
slouken@8582
  2363
    // This is a valid operation only when the existing contents will be entirely
slouken@8582
  2364
    // overwritten. If dirty or scroll rects are used, this call should be removed.
slouken@8582
  2365
    data->d3dContext->DiscardView(data->mainRenderTargetView.Get());
slouken@8582
  2366
slouken@8582
  2367
    // If the device was removed either by a disconnect or a driver upgrade, we 
slouken@8582
  2368
    // must recreate all device resources.
slouken@8582
  2369
    //
slouken@8582
  2370
    // TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvedge debug info from users' machines
slouken@8582
  2371
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
slouken@8582
  2372
    {
slouken@8582
  2373
        hr = D3D11_HandleDeviceLost(renderer);
slouken@8582
  2374
        if (FAILED(hr)) {
slouken@8582
  2375
            /* D3D11_HandleDeviceLost will set the SDL error */
slouken@8582
  2376
        }
slouken@8582
  2377
    }
slouken@8582
  2378
    else
slouken@8582
  2379
    {
slouken@8582
  2380
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext1::DiscardView", hr);
dludwig@8401
  2381
    }
dludwig@8401
  2382
}
dludwig@8401
  2383
dludwig@8568
  2384
#endif /* SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED */
dludwig@8400
  2385
dludwig@8400
  2386
/* vi: set ts=4 sw=4 expandtab: */