src/render/direct3d11/SDL_render_d3d11.c
author David Ludwig <dludwig@pobox.com>
Sat, 15 Mar 2014 13:27:18 -0400
changeset 8620 a27695b26b57
parent 8615 097646deaef2
child 8668 07dce0097f29
permissions -rw-r--r--
Fixed a crash on Windows Phone 8 that occurred after rotating a device

This changeset prevents IDXGISwapChain::ResizeBuffers from being invoked on
Windows Phone 8, a function that isn't available on the platform (but is
available on other Windows platforms). The call would fail, which ultimately
led to a crash.

This changeset also attempts to make sure that the D3D11 swap chain is created
at the correct size, when using Windows Phone 8.

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