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