src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Thu, 26 Dec 2013 11:03:43 -0500
changeset 8570 b509e789b6a3
parent 8569 0c593385f446
child 8571 f95a0f1bc631
permissions -rw-r--r--
WinRT: simplified the d3d11 vertex shader a bit

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