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