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