src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 13 Apr 2013 23:03:46 -0400
changeset 8459 1178e27580c7
parent 8458 9b25393568f3
child 8463 98dbf81ed5e9
permissions -rw-r--r--
WinRT: added render-to-texture support for D3D 11.1, via SDL_SetRenderTarget
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8400
    26
extern "C" {
dludwig@8400
    27
#include "../../core/windows/SDL_windows.h"
dludwig@8400
    28
//#include "SDL_hints.h"
dludwig@8400
    29
//#include "SDL_loadso.h"
dludwig@8410
    30
#include "SDL_system.h"
dludwig@8400
    31
#include "SDL_syswm.h"
dludwig@8400
    32
#include "../SDL_sysrender.h"
dludwig@8433
    33
#include "SDL_log.h"
dludwig@8433
    34
#include "../../video/SDL_sysvideo.h"
dludwig@8400
    35
//#include "stdio.h"
dludwig@8400
    36
}
dludwig@8400
    37
dludwig@8410
    38
#include <fstream>
dludwig@8410
    39
#include <string>
dludwig@8410
    40
#include <vector>
dludwig@8410
    41
dludwig@8400
    42
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    43
dludwig@8410
    44
using namespace DirectX;
dludwig@8410
    45
using namespace Microsoft::WRL;
dludwig@8410
    46
using namespace std;
dludwig@8410
    47
dludwig@8412
    48
#ifdef __WINRT__
dludwig@8412
    49
using namespace Windows::Graphics::Display;
dludwig@8412
    50
using namespace Windows::UI::Core;
dludwig@8412
    51
#endif
dludwig@8412
    52
dludwig@8410
    53
/* Direct3D 11.1 renderer implementation */
dludwig@8400
    54
dludwig@8400
    55
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8415
    56
static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8415
    57
                            const SDL_WindowEvent *event);
dludwig@8416
    58
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8416
    59
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8459
    60
                             const SDL_Rect * rect, const void *srcPixels,
dludwig@8459
    61
                             int srcPitch);
dludwig@8451
    62
static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8451
    63
                             const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8451
    64
static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8459
    65
static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    66
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8416
    67
static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8450
    68
static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
    69
                                  const SDL_FPoint * points, int count);
dludwig@8449
    70
static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
    71
                                 const SDL_FPoint * points, int count);
dludwig@8429
    72
static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
    73
                                 const SDL_FRect * rects, int count);
dludwig@8416
    74
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    75
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8455
    76
static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8455
    77
                              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8455
    78
                              const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8454
    79
static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8454
    80
                                  Uint32 format, void * pixels, int pitch);
dludwig@8401
    81
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8416
    82
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
    83
                                 SDL_Texture * texture);
dludwig@8413
    84
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    85
dludwig@8410
    86
/* Direct3D 11.1 Internal Functions */
dludwig@8412
    87
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
    88
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8414
    89
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
dludwig@8414
    90
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
dludwig@8400
    91
dludwig@8442
    92
extern "C" SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    93
    D3D11_CreateRenderer,
dludwig@8400
    94
    {
dludwig@8442
    95
        "direct3d 11.1",
dludwig@8459
    96
        (
dludwig@8459
    97
            SDL_RENDERER_ACCELERATED |
dludwig@8459
    98
            SDL_RENDERER_PRESENTVSYNC |
dludwig@8459
    99
            SDL_RENDERER_TARGETTEXTURE
dludwig@8459
   100
        ),                          // flags.  see SDL_RendererFlags
dludwig@8442
   101
        2,                          // num_texture_formats
dludwig@8442
   102
        {                           // texture_formats
dludwig@8442
   103
            SDL_PIXELFORMAT_RGB888,
dludwig@8442
   104
            SDL_PIXELFORMAT_ARGB8888
dludwig@8442
   105
        },
dludwig@8446
   106
        0,                          // max_texture_width: will be filled in later
dludwig@8446
   107
        0                           // max_texture_height: will be filled in later
dludwig@8442
   108
    }
dludwig@8442
   109
};
dludwig@8442
   110
dludwig@8400
   111
dludwig@8454
   112
static Uint32
dludwig@8454
   113
DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat) {
dludwig@8454
   114
    switch (dxgiFormat) {
dludwig@8454
   115
        case DXGI_FORMAT_B8G8R8A8_UNORM:
dludwig@8454
   116
            return SDL_PIXELFORMAT_ARGB8888;
dludwig@8454
   117
        case DXGI_FORMAT_B8G8R8X8_UNORM:
dludwig@8454
   118
            return SDL_PIXELFORMAT_RGB888;
dludwig@8454
   119
        default:
dludwig@8454
   120
            return SDL_PIXELFORMAT_UNKNOWN;
dludwig@8454
   121
    }
dludwig@8454
   122
}
dludwig@8454
   123
dludwig@8454
   124
static DXGI_FORMAT
dludwig@8454
   125
SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
dludwig@8454
   126
{
dludwig@8454
   127
    switch (sdlFormat) {
dludwig@8454
   128
        case SDL_PIXELFORMAT_ARGB8888:
dludwig@8454
   129
            return DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@8454
   130
        case SDL_PIXELFORMAT_RGB888:
dludwig@8454
   131
            return DXGI_FORMAT_B8G8R8X8_UNORM;
dludwig@8454
   132
        default:
dludwig@8454
   133
            return DXGI_FORMAT_UNKNOWN;
dludwig@8454
   134
    }
dludwig@8454
   135
}
dludwig@8454
   136
dludwig@8454
   137
dludwig@8400
   138
//typedef struct
dludwig@8400
   139
//{
dludwig@8400
   140
//    float x, y, z;
dludwig@8400
   141
//    DWORD color;
dludwig@8400
   142
//    float u, v;
dludwig@8400
   143
//} Vertex;
dludwig@8400
   144
dludwig@8400
   145
SDL_Renderer *
dludwig@8400
   146
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
   147
{
dludwig@8400
   148
    SDL_Renderer *renderer;
dludwig@8400
   149
    D3D11_RenderData *data;
dludwig@8400
   150
dludwig@8400
   151
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   152
    if (!renderer) {
dludwig@8400
   153
        SDL_OutOfMemory();
dludwig@8400
   154
        return NULL;
dludwig@8400
   155
    }
dludwig@8400
   156
    SDL_zerop(renderer);
dludwig@8400
   157
dludwig@8400
   158
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   159
    if (!data) {
dludwig@8400
   160
        SDL_OutOfMemory();
dludwig@8400
   161
        return NULL;
dludwig@8400
   162
    }
dludwig@8410
   163
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
dludwig@8412
   164
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
dludwig@8412
   165
    data->renderTargetSize = XMFLOAT2(0, 0);
dludwig@8400
   166
dludwig@8415
   167
    renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8416
   168
    renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8416
   169
    renderer->UpdateTexture = D3D11_UpdateTexture;
dludwig@8451
   170
    renderer->LockTexture = D3D11_LockTexture;
dludwig@8451
   171
    renderer->UnlockTexture = D3D11_UnlockTexture;
dludwig@8459
   172
    renderer->SetRenderTarget = D3D11_SetRenderTarget;
dludwig@8400
   173
    renderer->UpdateViewport = D3D11_UpdateViewport;
dludwig@8416
   174
    renderer->RenderClear = D3D11_RenderClear;
dludwig@8450
   175
    renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
dludwig@8449
   176
    renderer->RenderDrawLines = D3D11_RenderDrawLines;
dludwig@8429
   177
    renderer->RenderFillRects = D3D11_RenderFillRects;
dludwig@8416
   178
    renderer->RenderCopy = D3D11_RenderCopy;
dludwig@8455
   179
    renderer->RenderCopyEx = D3D11_RenderCopyEx;
dludwig@8454
   180
    renderer->RenderReadPixels = D3D11_RenderReadPixels;
dludwig@8401
   181
    renderer->RenderPresent = D3D11_RenderPresent;
dludwig@8416
   182
    renderer->DestroyTexture = D3D11_DestroyTexture;
dludwig@8413
   183
    renderer->DestroyRenderer = D3D11_DestroyRenderer;
dludwig@8400
   184
    renderer->info = D3D11_RenderDriver.info;
dludwig@8400
   185
    renderer->driverdata = data;
dludwig@8400
   186
dludwig@8413
   187
    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
dludwig@8413
   188
    // order to give init functions access to the underlying window handle:
dludwig@8413
   189
    renderer->window = window;
dludwig@8400
   190
dludwig@8413
   191
    /* Initialize Direct3D resources */
dludwig@8413
   192
    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
dludwig@8413
   193
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   194
        return NULL;
dludwig@8413
   195
    }
dludwig@8413
   196
    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
dludwig@8413
   197
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   198
        return NULL;
dludwig@8413
   199
    }
dludwig@8400
   200
dludwig@8416
   201
    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
dludwig@8416
   202
dludwig@8400
   203
    return renderer;
dludwig@8400
   204
}
dludwig@8400
   205
dludwig@8413
   206
static void
dludwig@8413
   207
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   208
{
dludwig@8413
   209
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   210
    if (data) {
dludwig@8413
   211
        delete data;
dludwig@8413
   212
        data = NULL;
dludwig@8413
   213
    }
dludwig@8413
   214
}
dludwig@8413
   215
dludwig@8410
   216
static bool
dludwig@8410
   217
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
dludwig@8410
   218
{
dludwig@8410
   219
    ifstream in(fileName, ios::in | ios::binary);
dludwig@8410
   220
    if (!in) {
dludwig@8410
   221
        return false;
dludwig@8410
   222
    }
dludwig@8410
   223
dludwig@8410
   224
    in.seekg(0, ios::end);
dludwig@8410
   225
    out.resize((size_t) in.tellg());
dludwig@8410
   226
    in.seekg(0, ios::beg);
dludwig@8410
   227
    in.read(&out[0], out.size());
dludwig@8410
   228
    return in.good();
dludwig@8410
   229
}
dludwig@8410
   230
dludwig@8410
   231
static bool
dludwig@8410
   232
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
dludwig@8410
   233
{
dludwig@8410
   234
    wstring fileName;
dludwig@8410
   235
dludwig@8410
   236
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8454
   237
    fileName = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_INSTALLED_LOCATION);
dludwig@8410
   238
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   239
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8454
   240
    fileName = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_INSTALLED_LOCATION);
dludwig@8410
   241
    fileName += L"\\";
dludwig@8410
   242
#endif
dludwig@8410
   243
    // WinRT, TODO: test Direct3D 11.1 shader loading on Win32
dludwig@8410
   244
    fileName += shaderName;
dludwig@8410
   245
    return D3D11_ReadFileContents(fileName, out);
dludwig@8410
   246
}
dludwig@8410
   247
dludwig@8429
   248
static HRESULT
dludwig@8429
   249
D3D11_LoadPixelShader(SDL_Renderer * renderer,
dludwig@8429
   250
                      const wstring & shaderName,
dludwig@8429
   251
                      ID3D11PixelShader ** shaderOutput)
dludwig@8429
   252
{
dludwig@8429
   253
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
   254
    HRESULT result = S_OK;
dludwig@8429
   255
    vector<char> fileData;
dludwig@8429
   256
    
dludwig@8429
   257
    if (!D3D11_ReadShaderContents(shaderName, fileData)) {
dludwig@8429
   258
        SDL_SetError("Unable to open SDL's pixel shader file.");
dludwig@8429
   259
        return E_FAIL;
dludwig@8429
   260
    }
dludwig@8429
   261
dludwig@8429
   262
    result = data->d3dDevice->CreatePixelShader(
dludwig@8429
   263
        &fileData[0],
dludwig@8429
   264
        fileData.size(),
dludwig@8429
   265
        nullptr,
dludwig@8429
   266
        shaderOutput
dludwig@8429
   267
        );
dludwig@8429
   268
    if (FAILED(result)) {
dludwig@8429
   269
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8429
   270
        return result;
dludwig@8429
   271
    }
dludwig@8429
   272
dludwig@8429
   273
    return S_OK;
dludwig@8429
   274
}
dludwig@8429
   275
dludwig@8431
   276
static HRESULT
dludwig@8431
   277
D3D11_CreateBlendMode(SDL_Renderer * renderer,
dludwig@8431
   278
                      BOOL enableBlending,
dludwig@8431
   279
                      D3D11_BLEND srcBlend,
dludwig@8431
   280
                      D3D11_BLEND destBlend,
dludwig@8431
   281
                      ID3D11BlendState ** blendStateOutput)
dludwig@8431
   282
{
dludwig@8431
   283
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8431
   284
    HRESULT result = S_OK;
dludwig@8431
   285
dludwig@8431
   286
    D3D11_BLEND_DESC blendDesc;
dludwig@8431
   287
    memset(&blendDesc, 0, sizeof(blendDesc));
dludwig@8431
   288
    blendDesc.AlphaToCoverageEnable = FALSE;
dludwig@8431
   289
    blendDesc.IndependentBlendEnable = FALSE;
dludwig@8431
   290
    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
dludwig@8431
   291
    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
dludwig@8431
   292
    blendDesc.RenderTarget[0].DestBlend = destBlend;
dludwig@8431
   293
    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
dludwig@8431
   294
    blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
dludwig@8431
   295
    blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
dludwig@8431
   296
    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
dludwig@8431
   297
    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
dludwig@8431
   298
    result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
dludwig@8431
   299
    if (FAILED(result)) {
dludwig@8431
   300
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8431
   301
        return result;
dludwig@8431
   302
    }
dludwig@8431
   303
dludwig@8431
   304
    return S_OK;
dludwig@8431
   305
}
dludwig@8431
   306
dludwig@8414
   307
// Create resources that depend on the device.
dludwig@8410
   308
HRESULT
dludwig@8412
   309
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
dludwig@8410
   310
{
dludwig@8410
   311
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8410
   312
dludwig@8410
   313
    // This flag adds support for surfaces with a different color channel ordering
dludwig@8410
   314
    // than the API default. It is required for compatibility with Direct2D.
dludwig@8410
   315
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
dludwig@8410
   316
dludwig@8410
   317
#if defined(_DEBUG)
dludwig@8410
   318
    // If the project is in a debug build, enable debugging via SDK Layers with this flag.
dludwig@8410
   319
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8410
   320
#endif
dludwig@8410
   321
dludwig@8410
   322
    // This array defines the set of DirectX hardware feature levels this app will support.
dludwig@8410
   323
    // Note the ordering should be preserved.
dludwig@8410
   324
    // Don't forget to declare your application's minimum required feature level in its
dludwig@8410
   325
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
dludwig@8410
   326
    D3D_FEATURE_LEVEL featureLevels[] = 
dludwig@8410
   327
    {
dludwig@8410
   328
        D3D_FEATURE_LEVEL_11_1,
dludwig@8410
   329
        D3D_FEATURE_LEVEL_11_0,
dludwig@8410
   330
        D3D_FEATURE_LEVEL_10_1,
dludwig@8410
   331
        D3D_FEATURE_LEVEL_10_0,
dludwig@8410
   332
        D3D_FEATURE_LEVEL_9_3,
dludwig@8410
   333
        D3D_FEATURE_LEVEL_9_2,
dludwig@8410
   334
        D3D_FEATURE_LEVEL_9_1
dludwig@8410
   335
    };
dludwig@8410
   336
dludwig@8410
   337
    // Create the Direct3D 11 API device object and a corresponding context.
dludwig@8410
   338
    ComPtr<ID3D11Device> device;
dludwig@8410
   339
    ComPtr<ID3D11DeviceContext> context;
dludwig@8410
   340
    HRESULT result = S_OK;
dludwig@8410
   341
    result = D3D11CreateDevice(
dludwig@8410
   342
        nullptr, // Specify nullptr to use the default adapter.
dludwig@8410
   343
        D3D_DRIVER_TYPE_HARDWARE,
dludwig@8410
   344
        nullptr,
dludwig@8410
   345
        creationFlags, // Set set debug and Direct2D compatibility flags.
dludwig@8410
   346
        featureLevels, // List of feature levels this app can support.
dludwig@8410
   347
        ARRAYSIZE(featureLevels),
dludwig@8410
   348
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
dludwig@8410
   349
        &device, // Returns the Direct3D device created.
dludwig@8410
   350
        &data->featureLevel, // Returns feature level of device created.
dludwig@8410
   351
        &context // Returns the device immediate context.
dludwig@8410
   352
        );
dludwig@8410
   353
    if (FAILED(result)) {
dludwig@8410
   354
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   355
        return result;
dludwig@8410
   356
    }
dludwig@8410
   357
dludwig@8410
   358
    // Get the Direct3D 11.1 API device and context interfaces.
dludwig@8410
   359
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
dludwig@8410
   360
    result = device.As(&(data->d3dDevice));
dludwig@8410
   361
    if (FAILED(result)) {
dludwig@8410
   362
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   363
        return result;
dludwig@8410
   364
    }
dludwig@8410
   365
dludwig@8410
   366
    result = context.As(&data->d3dContext);
dludwig@8410
   367
    if (FAILED(result)) {
dludwig@8410
   368
        return result;
dludwig@8410
   369
    }
dludwig@8410
   370
dludwig@8410
   371
    //
dludwig@8446
   372
    // Make note of the maximum texture size
dludwig@8446
   373
    // Max texture sizes are documented on MSDN, at:
dludwig@8446
   374
    // http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
dludwig@8446
   375
    //
dludwig@8446
   376
    switch (data->d3dDevice->GetFeatureLevel()) {
dludwig@8446
   377
        case D3D_FEATURE_LEVEL_11_1:
dludwig@8446
   378
        case D3D_FEATURE_LEVEL_11_0:
dludwig@8446
   379
            renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
dludwig@8446
   380
            break;
dludwig@8446
   381
dludwig@8446
   382
        case D3D_FEATURE_LEVEL_10_1:
dludwig@8446
   383
        case D3D_FEATURE_LEVEL_10_0:
dludwig@8446
   384
            renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
dludwig@8446
   385
            break;
dludwig@8446
   386
dludwig@8446
   387
        case D3D_FEATURE_LEVEL_9_3:
dludwig@8446
   388
            renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
dludwig@8446
   389
            break;
dludwig@8446
   390
dludwig@8446
   391
        case D3D_FEATURE_LEVEL_9_2:
dludwig@8446
   392
        case D3D_FEATURE_LEVEL_9_1:
dludwig@8446
   393
            renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
dludwig@8446
   394
            break;
dludwig@8446
   395
    }
dludwig@8446
   396
dludwig@8446
   397
    //
dludwig@8410
   398
    // Load in SDL's one and only vertex shader:
dludwig@8410
   399
    //
dludwig@8429
   400
    vector<char> fileData;
dludwig@8444
   401
    if (!D3D11_ReadShaderContents(L"SDL_D3D11_VertexShader_Default.cso", fileData)) {
dludwig@8410
   402
        SDL_SetError("Unable to open SDL's vertex shader file.");
dludwig@8410
   403
        return E_FAIL;
dludwig@8410
   404
    }
dludwig@8410
   405
dludwig@8410
   406
    result = data->d3dDevice->CreateVertexShader(
dludwig@8410
   407
        &fileData[0],
dludwig@8410
   408
        fileData.size(),
dludwig@8410
   409
        nullptr,
dludwig@8410
   410
        &data->vertexShader
dludwig@8410
   411
        );
dludwig@8410
   412
    if (FAILED(result)) {
dludwig@8410
   413
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   414
        return result;
dludwig@8410
   415
    }
dludwig@8410
   416
dludwig@8410
   417
    //
dludwig@8410
   418
    // Create an input layout for SDL's vertex shader:
dludwig@8410
   419
    //
dludwig@8410
   420
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8410
   421
    {
dludwig@8429
   422
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   423
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8429
   424
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   425
    };
dludwig@8410
   426
dludwig@8410
   427
    result = data->d3dDevice->CreateInputLayout(
dludwig@8410
   428
        vertexDesc,
dludwig@8410
   429
        ARRAYSIZE(vertexDesc),
dludwig@8410
   430
        &fileData[0],
dludwig@8410
   431
        fileData.size(),
dludwig@8410
   432
        &data->inputLayout
dludwig@8410
   433
        );
dludwig@8410
   434
    if (FAILED(result)) {
dludwig@8410
   435
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   436
        return result;
dludwig@8410
   437
    }
dludwig@8410
   438
dludwig@8410
   439
    //
dludwig@8429
   440
    // Load in SDL's pixel shaders
dludwig@8410
   441
    //
dludwig@8458
   442
    result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_TextureColored.cso", &data->texturePixelShader);
dludwig@8429
   443
    if (FAILED(result)) {
dludwig@8429
   444
        // D3D11_LoadPixelShader will have aleady set the SDL error
dludwig@8429
   445
        return result;
dludwig@8410
   446
    }
dludwig@8410
   447
dludwig@8429
   448
    result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_FixedColor.cso", &data->colorPixelShader);
dludwig@8410
   449
    if (FAILED(result)) {
dludwig@8429
   450
        // D3D11_LoadPixelShader will have aleady set the SDL error
dludwig@8410
   451
        return result;
dludwig@8410
   452
    }
dludwig@8410
   453
dludwig@8410
   454
    //
dludwig@8418
   455
    // Setup space to hold vertex shader constants:
dludwig@8418
   456
    //
dludwig@8418
   457
    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
dludwig@8418
   458
    result = data->d3dDevice->CreateBuffer(
dludwig@8418
   459
		&constantBufferDesc,
dludwig@8418
   460
		nullptr,
dludwig@8418
   461
        &data->vertexShaderConstants
dludwig@8418
   462
		);
dludwig@8418
   463
    if (FAILED(result)) {
dludwig@8418
   464
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8418
   465
        return result;
dludwig@8418
   466
    }
dludwig@8418
   467
dludwig@8418
   468
    //
dludwig@8425
   469
    // Make sure that the vertex buffer, if already created, gets freed.
dludwig@8425
   470
    // It will be recreated later.
dludwig@8410
   471
    //
dludwig@8425
   472
    data->vertexBuffer = nullptr;
dludwig@8410
   473
dludwig@8410
   474
    //
dludwig@8410
   475
    // Create a sampler to use when drawing textures:
dludwig@8410
   476
    //
dludwig@8410
   477
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   478
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   479
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   480
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   481
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   482
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   483
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   484
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   485
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   486
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   487
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   488
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   489
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   490
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   491
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   492
        &samplerDesc,
dludwig@8410
   493
        &data->mainSampler
dludwig@8410
   494
        );
dludwig@8410
   495
    if (FAILED(result)) {
dludwig@8410
   496
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   497
        return result;
dludwig@8410
   498
    }
dludwig@8410
   499
dludwig@8410
   500
    //
dludwig@8426
   501
    // Setup the Direct3D rasterizer
dludwig@8426
   502
    //
dludwig@8426
   503
    D3D11_RASTERIZER_DESC rasterDesc;
dludwig@8426
   504
    memset(&rasterDesc, 0, sizeof(rasterDesc));
dludwig@8426
   505
	rasterDesc.AntialiasedLineEnable = false;
dludwig@8426
   506
	rasterDesc.CullMode = D3D11_CULL_NONE;
dludwig@8426
   507
	rasterDesc.DepthBias = 0;
dludwig@8426
   508
	rasterDesc.DepthBiasClamp = 0.0f;
dludwig@8426
   509
	rasterDesc.DepthClipEnable = true;
dludwig@8426
   510
	rasterDesc.FillMode = D3D11_FILL_SOLID;
dludwig@8426
   511
	rasterDesc.FrontCounterClockwise = false;
dludwig@8426
   512
	rasterDesc.MultisampleEnable = false;
dludwig@8426
   513
	rasterDesc.ScissorEnable = false;
dludwig@8426
   514
	rasterDesc.SlopeScaledDepthBias = 0.0f;
dludwig@8426
   515
	result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
dludwig@8426
   516
	if (FAILED(result)) {
dludwig@8426
   517
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8426
   518
        return result;
dludwig@8426
   519
    }
dludwig@8426
   520
dludwig@8426
   521
    //
dludwig@8431
   522
    // Create blending states:
dludwig@8431
   523
    //
dludwig@8431
   524
    result = D3D11_CreateBlendMode(
dludwig@8431
   525
        renderer,
dludwig@8431
   526
        TRUE,
dludwig@8431
   527
        D3D11_BLEND_SRC_ALPHA,
dludwig@8431
   528
        D3D11_BLEND_INV_SRC_ALPHA,
dludwig@8431
   529
        &data->blendModeBlend);
dludwig@8431
   530
    if (FAILED(result)) {
dludwig@8431
   531
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   532
        return result;
dludwig@8431
   533
    }
dludwig@8431
   534
dludwig@8431
   535
    result = D3D11_CreateBlendMode(
dludwig@8431
   536
        renderer,
dludwig@8431
   537
        TRUE,
dludwig@8431
   538
        D3D11_BLEND_SRC_ALPHA,
dludwig@8431
   539
        D3D11_BLEND_ONE,
dludwig@8431
   540
        &data->blendModeAdd);
dludwig@8431
   541
    if (FAILED(result)) {
dludwig@8431
   542
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   543
        return result;
dludwig@8431
   544
    }
dludwig@8431
   545
dludwig@8431
   546
    result = D3D11_CreateBlendMode(
dludwig@8431
   547
        renderer,
dludwig@8431
   548
        TRUE,
dludwig@8431
   549
        D3D11_BLEND_ZERO,
dludwig@8431
   550
        D3D11_BLEND_SRC_COLOR,
dludwig@8431
   551
        &data->blendModeMod);
dludwig@8431
   552
    if (FAILED(result)) {
dludwig@8431
   553
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   554
        return result;
dludwig@8431
   555
    }
dludwig@8431
   556
dludwig@8431
   557
    //
dludwig@8410
   558
    // All done!
dludwig@8410
   559
    //
dludwig@8410
   560
    return S_OK;
dludwig@8410
   561
}
dludwig@8410
   562
dludwig@8412
   563
#ifdef __WINRT__
dludwig@8412
   564
dludwig@8415
   565
static CoreWindow ^
dludwig@8412
   566
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   567
{
dludwig@8412
   568
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   569
    if ( ! renderer->window ) {
dludwig@8412
   570
        return nullptr;
dludwig@8412
   571
    }
dludwig@8412
   572
dludwig@8412
   573
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   574
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   575
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   576
        return nullptr;
dludwig@8412
   577
    }
dludwig@8412
   578
dludwig@8412
   579
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   580
        return nullptr;
dludwig@8412
   581
    }
dludwig@8412
   582
dludwig@8412
   583
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   584
    if ( ! coreWindowPointer ) {
dludwig@8412
   585
        return nullptr;
dludwig@8412
   586
    }
dludwig@8412
   587
dludwig@8412
   588
    return *coreWindowPointer;
dludwig@8412
   589
}
dludwig@8412
   590
dludwig@8414
   591
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
dludwig@8412
   592
static float
dludwig@8412
   593
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   594
{
dludwig@8412
   595
    static const float dipsPerInch = 96.0f;
dludwig@8412
   596
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   597
}
dludwig@8412
   598
#endif
dludwig@8412
   599
dludwig@8414
   600
// Initialize all resources that change when the window's size changes.
dludwig@8412
   601
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   602
HRESULT
dludwig@8412
   603
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   604
{
dludwig@8412
   605
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   606
    HRESULT result = S_OK;
dludwig@8412
   607
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   608
dludwig@8412
   609
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   610
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   611
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   612
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   613
dludwig@8412
   614
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   615
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   616
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   617
dludwig@8412
   618
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   619
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   620
    // orientation, the dimensions must be reversed.
dludwig@8412
   621
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8433
   622
    const bool swapDimensions =
dludwig@8412
   623
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   624
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   625
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   626
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   627
dludwig@8412
   628
    if(data->swapChain != nullptr)
dludwig@8412
   629
    {
dludwig@8412
   630
        // If the swap chain already exists, resize it.
dludwig@8412
   631
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   632
            2, // Double-buffered swap chain.
dludwig@8412
   633
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   634
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   635
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   636
            0
dludwig@8412
   637
            );
dludwig@8412
   638
        if (FAILED(result)) {
dludwig@8412
   639
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   640
            return result;
dludwig@8412
   641
        }
dludwig@8412
   642
    }
dludwig@8412
   643
    else
dludwig@8412
   644
    {
dludwig@8412
   645
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   646
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   647
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   648
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   649
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   650
        swapChainDesc.Stereo = false;
dludwig@8412
   651
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   652
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   653
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   654
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   655
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   656
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   657
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   658
#else
dludwig@8412
   659
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   660
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   661
#endif
dludwig@8412
   662
        swapChainDesc.Flags = 0;
dludwig@8412
   663
dludwig@8412
   664
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   665
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   666
        if (FAILED(result)) {
dludwig@8412
   667
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   668
            return result;
dludwig@8412
   669
        }
dludwig@8412
   670
dludwig@8412
   671
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   672
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   673
        if (FAILED(result)) {
dludwig@8412
   674
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   675
            return result;
dludwig@8412
   676
        }
dludwig@8412
   677
dludwig@8412
   678
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   679
        result = dxgiAdapter->GetParent(
dludwig@8412
   680
            __uuidof(IDXGIFactory2), 
dludwig@8412
   681
            &dxgiFactory
dludwig@8412
   682
            );
dludwig@8412
   683
        if (FAILED(result)) {
dludwig@8412
   684
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   685
            return result;
dludwig@8412
   686
        }
dludwig@8412
   687
dludwig@8412
   688
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   689
            data->d3dDevice.Get(),
dludwig@8412
   690
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   691
            &swapChainDesc,
dludwig@8412
   692
            nullptr, // Allow on all displays.
dludwig@8412
   693
            &data->swapChain
dludwig@8412
   694
            );
dludwig@8412
   695
        if (FAILED(result)) {
dludwig@8412
   696
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   697
            return result;
dludwig@8412
   698
        }
dludwig@8412
   699
            
dludwig@8412
   700
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   701
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   702
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   703
        if (FAILED(result)) {
dludwig@8412
   704
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   705
            return result;
dludwig@8412
   706
        }
dludwig@8412
   707
    }
dludwig@8412
   708
    
dludwig@8412
   709
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   710
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   711
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   712
    switch (data->orientation)
dludwig@8412
   713
    {
dludwig@8412
   714
        case DisplayOrientations::Landscape:
dludwig@8412
   715
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8412
   716
            break;
dludwig@8412
   717
dludwig@8412
   718
        case DisplayOrientations::Portrait:
dludwig@8412
   719
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8412
   720
            break;
dludwig@8412
   721
dludwig@8412
   722
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   723
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8412
   724
            break;
dludwig@8412
   725
dludwig@8412
   726
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   727
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8412
   728
            break;
dludwig@8412
   729
dludwig@8412
   730
        default:
dludwig@8412
   731
            throw ref new Platform::FailureException();
dludwig@8412
   732
    }
dludwig@8412
   733
dludwig@8412
   734
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   735
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   736
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   737
    if (FAILED(result)) {
dludwig@8412
   738
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   739
        return result;
dludwig@8412
   740
    }
dludwig@8412
   741
#endif
dludwig@8412
   742
dludwig@8412
   743
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   744
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   745
    result = data->swapChain->GetBuffer(
dludwig@8412
   746
        0,
dludwig@8412
   747
        __uuidof(ID3D11Texture2D),
dludwig@8412
   748
        &backBuffer
dludwig@8412
   749
        );
dludwig@8412
   750
    if (FAILED(result)) {
dludwig@8412
   751
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   752
        return result;
dludwig@8412
   753
    }
dludwig@8412
   754
dludwig@8412
   755
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   756
        backBuffer.Get(),
dludwig@8412
   757
        nullptr,
dludwig@8459
   758
        &data->mainRenderTargetView
dludwig@8412
   759
        );
dludwig@8412
   760
    if (FAILED(result)) {
dludwig@8412
   761
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   762
        return result;
dludwig@8412
   763
    }
dludwig@8412
   764
dludwig@8432
   765
    if (D3D11_UpdateViewport(renderer) != 0) {
dludwig@8432
   766
        // D3D11_UpdateViewport will set the SDL error if it fails.
dludwig@8432
   767
        return E_FAIL;
dludwig@8432
   768
    }
dludwig@8412
   769
dludwig@8412
   770
    return S_OK;
dludwig@8412
   771
}
dludwig@8412
   772
dludwig@8415
   773
// This method is called when the window's size changes.
dludwig@8414
   774
HRESULT
dludwig@8414
   775
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
dludwig@8414
   776
{
dludwig@8414
   777
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   778
    HRESULT result = S_OK;
dludwig@8414
   779
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8414
   780
dludwig@8414
   781
    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
dludwig@8414
   782
        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
dludwig@8414
   783
        data->orientation != DisplayProperties::CurrentOrientation)
dludwig@8414
   784
    {
dludwig@8414
   785
        ID3D11RenderTargetView* nullViews[] = {nullptr};
dludwig@8414
   786
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
dludwig@8459
   787
        data->mainRenderTargetView = nullptr;
dludwig@8414
   788
        data->d3dContext->Flush();
dludwig@8414
   789
        result = D3D11_CreateWindowSizeDependentResources(renderer);
dludwig@8414
   790
        if (FAILED(result)) {
dludwig@8414
   791
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   792
            return result;
dludwig@8414
   793
        }
dludwig@8414
   794
    }
dludwig@8414
   795
dludwig@8414
   796
    return S_OK;
dludwig@8414
   797
}
dludwig@8414
   798
dludwig@8414
   799
HRESULT
dludwig@8414
   800
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
   801
{
dludwig@8414
   802
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   803
    HRESULT result = S_OK;
dludwig@8414
   804
dludwig@8415
   805
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
dludwig@8414
   806
    data->windowSizeInDIPs.x = 0;
dludwig@8414
   807
    data->windowSizeInDIPs.y = 0;
dludwig@8414
   808
    data->swapChain = nullptr;
dludwig@8414
   809
dludwig@8414
   810
    result = D3D11_CreateDeviceResources(renderer);
dludwig@8414
   811
    if (FAILED(result)) {
dludwig@8414
   812
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   813
        return result;
dludwig@8414
   814
    }
dludwig@8414
   815
dludwig@8414
   816
    result = D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8414
   817
    if (FAILED(result)) {
dludwig@8414
   818
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   819
        return result;
dludwig@8414
   820
    }
dludwig@8414
   821
dludwig@8414
   822
    return S_OK;
dludwig@8414
   823
}
dludwig@8414
   824
dludwig@8415
   825
static void
dludwig@8415
   826
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
   827
{
dludwig@8415
   828
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8415
   829
dludwig@8415
   830
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
dludwig@8415
   831
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
   832
    }
dludwig@8415
   833
}
dludwig@8415
   834
dludwig@8416
   835
static int
dludwig@8416
   836
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8416
   837
{
dludwig@8416
   838
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   839
    D3D11_TextureData *textureData;
dludwig@8416
   840
    HRESULT result;
dludwig@8454
   841
    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
dludwig@8454
   842
    if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
dludwig@8454
   843
        SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
dludwig@8454
   844
            __FUNCTION__, texture->format);
dludwig@8454
   845
        return -1;
dludwig@8442
   846
    }
dludwig@8416
   847
dludwig@8416
   848
    textureData = new D3D11_TextureData;
dludwig@8416
   849
    if (!textureData) {
dludwig@8416
   850
        SDL_OutOfMemory();
dludwig@8416
   851
        return -1;
dludwig@8416
   852
    }
dludwig@8416
   853
    textureData->pixelFormat = SDL_AllocFormat(texture->format);
dludwig@8451
   854
    textureData->lockedTexturePosition = XMINT2(0, 0);
dludwig@8416
   855
dludwig@8416
   856
    texture->driverdata = textureData;
dludwig@8416
   857
dludwig@8416
   858
    D3D11_TEXTURE2D_DESC textureDesc = {0};
dludwig@8416
   859
    textureDesc.Width = texture->w;
dludwig@8416
   860
    textureDesc.Height = texture->h;
dludwig@8416
   861
    textureDesc.MipLevels = 1;
dludwig@8416
   862
    textureDesc.ArraySize = 1;
dludwig@8442
   863
    textureDesc.Format = textureFormat;
dludwig@8416
   864
    textureDesc.SampleDesc.Count = 1;
dludwig@8416
   865
    textureDesc.SampleDesc.Quality = 0;
dludwig@8416
   866
    textureDesc.MiscFlags = 0;
dludwig@8416
   867
dludwig@8459
   868
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
dludwig@8459
   869
        textureDesc.Usage = D3D11_USAGE_DYNAMIC;
dludwig@8459
   870
        textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8459
   871
    } else {
dludwig@8459
   872
        textureDesc.Usage = D3D11_USAGE_DEFAULT;
dludwig@8459
   873
        textureDesc.CPUAccessFlags = 0;
dludwig@8459
   874
    }
dludwig@8459
   875
dludwig@8459
   876
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
dludwig@8459
   877
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
dludwig@8459
   878
    } else {
dludwig@8459
   879
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dludwig@8459
   880
    }
dludwig@8459
   881
dludwig@8452
   882
#if 0
dludwig@8452
   883
    // Fill the texture with a non-black color, for debugging purposes:
dludwig@8416
   884
    const int numPixels = textureDesc.Width * textureDesc.Height;
dludwig@8452
   885
    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
dludwig@8416
   886
    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
dludwig@8452
   887
    for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
dludwig@8452
   888
        initialTexturePixels[i+0] = 0xff;
dludwig@8452
   889
        initialTexturePixels[i+1] = 0xff;
dludwig@8452
   890
        initialTexturePixels[i+2] = 0x00;
dludwig@8452
   891
        initialTexturePixels[i+3] = 0xff;
dludwig@8452
   892
    }
dludwig@8416
   893
    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
dludwig@8416
   894
    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
dludwig@8416
   895
    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
dludwig@8416
   896
    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
dludwig@8452
   897
#endif
dludwig@8452
   898
dludwig@8416
   899
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8416
   900
        &textureDesc,
dludwig@8452
   901
        NULL,   // &initialTextureData,
dludwig@8416
   902
        &textureData->mainTexture
dludwig@8416
   903
        );
dludwig@8416
   904
    if (FAILED(result)) {
dludwig@8416
   905
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   906
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   907
        return -1;
dludwig@8416
   908
    }
dludwig@8416
   909
dludwig@8459
   910
    if (texture->access & SDL_TEXTUREACCESS_TARGET) {
dludwig@8459
   911
        D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
dludwig@8459
   912
        renderTargetViewDesc.Format = textureDesc.Format;
dludwig@8459
   913
        renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
dludwig@8459
   914
        renderTargetViewDesc.Texture2D.MipSlice = 0;
dludwig@8459
   915
dludwig@8459
   916
        result = rendererData->d3dDevice->CreateRenderTargetView(
dludwig@8459
   917
            textureData->mainTexture.Get(),
dludwig@8459
   918
            &renderTargetViewDesc,
dludwig@8459
   919
            &textureData->mainTextureRenderTargetView);
dludwig@8459
   920
        if (FAILED(result)) {
dludwig@8459
   921
            D3D11_DestroyTexture(renderer, texture);
dludwig@8459
   922
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8459
   923
            return -1;
dludwig@8459
   924
        }
dludwig@8459
   925
    }
dludwig@8459
   926
dludwig@8416
   927
    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
dludwig@8416
   928
    resourceViewDesc.Format = textureDesc.Format;
dludwig@8416
   929
    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dludwig@8416
   930
    resourceViewDesc.Texture2D.MostDetailedMip = 0;
dludwig@8416
   931
    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
dludwig@8416
   932
    result = rendererData->d3dDevice->CreateShaderResourceView(
dludwig@8416
   933
        textureData->mainTexture.Get(),
dludwig@8416
   934
        &resourceViewDesc,
dludwig@8416
   935
        &textureData->mainTextureResourceView
dludwig@8416
   936
        );
dludwig@8416
   937
    if (FAILED(result)) {
dludwig@8416
   938
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   939
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   940
        return -1;
dludwig@8416
   941
    }
dludwig@8416
   942
dludwig@8416
   943
    return 0;
dludwig@8416
   944
}
dludwig@8416
   945
dludwig@8416
   946
static void
dludwig@8416
   947
D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
   948
                     SDL_Texture * texture)
dludwig@8416
   949
{
dludwig@8416
   950
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   951
dludwig@8416
   952
    if (textureData) {
dludwig@8416
   953
        if (textureData->pixelFormat) {
dludwig@8416
   954
            SDL_FreeFormat(textureData->pixelFormat);
dludwig@8416
   955
            textureData->pixelFormat = NULL;
dludwig@8416
   956
        }
dludwig@8416
   957
dludwig@8416
   958
        delete textureData;
dludwig@8416
   959
        texture->driverdata = NULL;
dludwig@8416
   960
    }
dludwig@8416
   961
}
dludwig@8416
   962
dludwig@8416
   963
static int
dludwig@8416
   964
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8459
   965
                    const SDL_Rect * rect, const void * srcPixels,
dludwig@8459
   966
                    int srcPitch)
dludwig@8416
   967
{
dludwig@8459
   968
    // Lock the texture, retrieving a buffer to write pixel data to:
dludwig@8459
   969
    void * destPixels = NULL;
dludwig@8459
   970
    int destPitch = 0;
dludwig@8459
   971
    if (D3D11_LockTexture(renderer, texture, rect, &destPixels, &destPitch) != 0) {
dludwig@8459
   972
        // An error is already set.  Attach some info to it, then return to
dludwig@8459
   973
        // the caller.
dludwig@8459
   974
        std::string errorMessage = string(__FUNCTION__ ", Lock Texture Failed: ") + SDL_GetError();
dludwig@8459
   975
        SDL_SetError(errorMessage.c_str());
dludwig@8416
   976
        return -1;
dludwig@8416
   977
    }
dludwig@8416
   978
dludwig@8416
   979
    // Copy pixel data to the locked texture's memory:
dludwig@8416
   980
    for (int y = 0; y < rect->h; ++y) {
dludwig@8416
   981
        memcpy(
dludwig@8459
   982
            ((Uint8 *)destPixels) + (destPitch * y),
dludwig@8459
   983
            ((Uint8 *)srcPixels) + (srcPitch * y),
dludwig@8459
   984
            srcPitch
dludwig@8416
   985
            );
dludwig@8416
   986
    }
dludwig@8416
   987
dludwig@8459
   988
    // Commit the texture's memory back to Direct3D:
dludwig@8459
   989
    D3D11_UnlockTexture(renderer, texture);
dludwig@8416
   990
dludwig@8459
   991
    // Return to the caller:
dludwig@8416
   992
    return 0;
dludwig@8416
   993
}
dludwig@8416
   994
dludwig@8400
   995
static int
dludwig@8451
   996
D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8451
   997
                  const SDL_Rect * rect, void **pixels, int *pitch)
dludwig@8451
   998
{
dludwig@8451
   999
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8451
  1000
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8451
  1001
    HRESULT result = S_OK;
dludwig@8451
  1002
dludwig@8451
  1003
    if (textureData->stagingTexture) {
dludwig@8451
  1004
        SDL_SetError("texture is already locked");
dludwig@8451
  1005
        return -1;
dludwig@8451
  1006
    }
dludwig@8451
  1007
    
dludwig@8451
  1008
    // Create a 'staging' texture, which will be used to write to a portion
dludwig@8451
  1009
    // of the main texture.  This is necessary, as Direct3D 11.1 does not
dludwig@8451
  1010
    // have the ability to write a CPU-bound pixel buffer to a rectangular
dludwig@8451
  1011
    // subrect of a texture.  Direct3D 11.1 can, however, write a pixel
dludwig@8451
  1012
    // buffer to an entire texture, hence the use of a staging texture.
dludwig@8451
  1013
    D3D11_TEXTURE2D_DESC stagingTextureDesc;
dludwig@8451
  1014
    textureData->mainTexture->GetDesc(&stagingTextureDesc);
dludwig@8451
  1015
    stagingTextureDesc.Width = rect->w;
dludwig@8451
  1016
    stagingTextureDesc.Height = rect->h;
dludwig@8451
  1017
    stagingTextureDesc.BindFlags = 0;
dludwig@8451
  1018
    stagingTextureDesc.MiscFlags = 0;
dludwig@8451
  1019
    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8451
  1020
    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
dludwig@8451
  1021
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8451
  1022
        &stagingTextureDesc,
dludwig@8451
  1023
        NULL,
dludwig@8451
  1024
        &textureData->stagingTexture);
dludwig@8451
  1025
    if (FAILED(result)) {
dludwig@8451
  1026
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Create Staging Texture", result);
dludwig@8451
  1027
        return -1;
dludwig@8451
  1028
    }
dludwig@8451
  1029
dludwig@8451
  1030
    // Get a write-only pointer to data in the staging texture:
dludwig@8451
  1031
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8451
  1032
    result = rendererData->d3dContext->Map(
dludwig@8451
  1033
        textureData->stagingTexture.Get(),
dludwig@8451
  1034
        D3D11CalcSubresource(0, 0, 0),
dludwig@8451
  1035
        D3D11_MAP_WRITE,
dludwig@8451
  1036
        0,
dludwig@8451
  1037
        &textureMemory
dludwig@8451
  1038
        );
dludwig@8451
  1039
    if (FAILED(result)) {
dludwig@8451
  1040
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Map Staging Texture", result);
dludwig@8451
  1041
        textureData->stagingTexture = nullptr;
dludwig@8451
  1042
        return -1;
dludwig@8451
  1043
    }
dludwig@8451
  1044
dludwig@8451
  1045
    // Make note of where the staging texture will be written to (on a
dludwig@8451
  1046
    // call to SDL_UnlockTexture):
dludwig@8451
  1047
    textureData->lockedTexturePosition = XMINT2(rect->x, rect->y);
dludwig@8451
  1048
dludwig@8451
  1049
    // Make sure the caller has information on the texture's pixel buffer,
dludwig@8451
  1050
    // then return:
dludwig@8451
  1051
    *pixels = textureMemory.pData;
dludwig@8451
  1052
    *pitch = textureMemory.RowPitch;
dludwig@8451
  1053
    return 0;
dludwig@8451
  1054
}
dludwig@8451
  1055
dludwig@8451
  1056
static void
dludwig@8451
  1057
D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8451
  1058
{
dludwig@8451
  1059
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8451
  1060
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8451
  1061
dludwig@8451
  1062
    // Commit the pixel buffer's changes back to the staging texture:
dludwig@8451
  1063
    rendererData->d3dContext->Unmap(
dludwig@8451
  1064
        textureData->stagingTexture.Get(),
dludwig@8451
  1065
        0);
dludwig@8451
  1066
dludwig@8451
  1067
    // Copy the staging texture's contents back to the main texture:
dludwig@8451
  1068
    rendererData->d3dContext->CopySubresourceRegion(
dludwig@8451
  1069
        textureData->mainTexture.Get(),
dludwig@8451
  1070
        D3D11CalcSubresource(0, 0, 0),
dludwig@8451
  1071
        textureData->lockedTexturePosition.x,
dludwig@8451
  1072
        textureData->lockedTexturePosition.y,
dludwig@8451
  1073
        0,
dludwig@8451
  1074
        textureData->stagingTexture.Get(),
dludwig@8451
  1075
        D3D11CalcSubresource(0, 0, 0),
dludwig@8451
  1076
        NULL);
dludwig@8451
  1077
dludwig@8451
  1078
    // Clean up and return:
dludwig@8451
  1079
    textureData->stagingTexture = nullptr;
dludwig@8451
  1080
    textureData->lockedTexturePosition = XMINT2(0, 0);
dludwig@8451
  1081
}
dludwig@8451
  1082
dludwig@8451
  1083
static int
dludwig@8459
  1084
D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8459
  1085
{
dludwig@8459
  1086
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8459
  1087
dludwig@8459
  1088
    if (texture == NULL) {
dludwig@8459
  1089
        rendererData->currentOffscreenRenderTargetView = nullptr;
dludwig@8459
  1090
        return 0;
dludwig@8459
  1091
    }
dludwig@8459
  1092
dludwig@8459
  1093
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8459
  1094
dludwig@8459
  1095
    if (!textureData->mainTextureRenderTargetView) {
dludwig@8459
  1096
        std::string errorMessage = string(__FUNCTION__) + ": specified texture is not a render target";
dludwig@8459
  1097
        SDL_SetError(errorMessage.c_str());
dludwig@8459
  1098
        return -1;
dludwig@8459
  1099
    }
dludwig@8459
  1100
dludwig@8459
  1101
    rendererData->currentOffscreenRenderTargetView = textureData->mainTextureRenderTargetView;
dludwig@8459
  1102
dludwig@8459
  1103
    return 0;
dludwig@8459
  1104
}
dludwig@8459
  1105
dludwig@8459
  1106
static int
dludwig@8400
  1107
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
  1108
{
dludwig@8432
  1109
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8432
  1110
dludwig@8432
  1111
    if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
dludwig@8432
  1112
        // If the viewport is empty, assume that it is because
dludwig@8432
  1113
        // SDL_CreateRenderer is calling it, and will call it again later
dludwig@8432
  1114
        // with a non-empty viewport.
dludwig@8432
  1115
        return 0;
dludwig@8432
  1116
    }
dludwig@8432
  1117
dludwig@8432
  1118
    switch (data->orientation)
dludwig@8432
  1119
    {
dludwig@8432
  1120
        case DisplayOrientations::Landscape:
dludwig@8432
  1121
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8432
  1122
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
  1123
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8432
  1124
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
  1125
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
  1126
                );
dludwig@8432
  1127
            break;
dludwig@8432
  1128
dludwig@8432
  1129
        case DisplayOrientations::Portrait:
dludwig@8433
  1130
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8433
  1131
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8433
  1132
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
  1133
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
  1134
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
  1135
                );
dludwig@8432
  1136
            break;
dludwig@8432
  1137
dludwig@8432
  1138
        case DisplayOrientations::LandscapeFlipped:
dludwig@8432
  1139
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8432
  1140
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
  1141
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8432
  1142
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
  1143
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
  1144
                );
dludwig@8432
  1145
            break;
dludwig@8432
  1146
dludwig@8432
  1147
        case DisplayOrientations::PortraitFlipped:
dludwig@8433
  1148
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8433
  1149
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8433
  1150
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
  1151
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
  1152
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
  1153
                );
dludwig@8432
  1154
            break;
dludwig@8432
  1155
dludwig@8432
  1156
        default:
dludwig@8432
  1157
            SDL_SetError("An unknown DisplayOrientation is being used");
dludwig@8432
  1158
            return -1;
dludwig@8432
  1159
    }
dludwig@8432
  1160
dludwig@8432
  1161
    //
dludwig@8432
  1162
    // Update the view matrix
dludwig@8432
  1163
    //
dludwig@8433
  1164
    float viewportWidth = (float) renderer->viewport.w;
dludwig@8433
  1165
    float viewportHeight = (float) renderer->viewport.h;
dludwig@8433
  1166
    XMStoreFloat4x4(&data->vertexShaderConstantsData.view,
dludwig@8432
  1167
        XMMatrixMultiply(
dludwig@8433
  1168
            XMMatrixScaling(2.0f / viewportWidth, 2.0f / viewportHeight, 1.0f),
dludwig@8432
  1169
            XMMatrixMultiply(
dludwig@8432
  1170
                XMMatrixTranslation(-1, -1, 0),
dludwig@8432
  1171
                XMMatrixRotationX(XM_PI)
dludwig@8432
  1172
                )));
dludwig@8434
  1173
#if 0
dludwig@8434
  1174
    data->vertexShaderConstantsData.view = XMMatrixIdentity();
dludwig@8434
  1175
#endif
dludwig@8433
  1176
dludwig@8433
  1177
    //
dludwig@8456
  1178
    // Reset the model matrix
dludwig@8456
  1179
    //
dludwig@8456
  1180
    XMStoreFloat4x4(&data->vertexShaderConstantsData.model, XMMatrixIdentity());
dludwig@8456
  1181
dludwig@8456
  1182
    //
dludwig@8433
  1183
    // Update the Direct3D viewport, which seems to be aligned to the
dludwig@8433
  1184
    // swap buffer's coordinate space, which is always in landscape:
dludwig@8433
  1185
    //
dludwig@8433
  1186
    SDL_FRect orientationAlignedViewport;
dludwig@8433
  1187
    const bool swapDimensions =
dludwig@8433
  1188
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8433
  1189
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8433
  1190
    if (swapDimensions) {
dludwig@8433
  1191
        orientationAlignedViewport.x = (float) renderer->viewport.y;
dludwig@8433
  1192
        orientationAlignedViewport.y = (float) renderer->viewport.x;
dludwig@8433
  1193
        orientationAlignedViewport.w = (float) renderer->viewport.h;
dludwig@8433
  1194
        orientationAlignedViewport.h = (float) renderer->viewport.w;
dludwig@8433
  1195
    } else {
dludwig@8433
  1196
        orientationAlignedViewport.x = (float) renderer->viewport.x;
dludwig@8433
  1197
        orientationAlignedViewport.y = (float) renderer->viewport.y;
dludwig@8433
  1198
        orientationAlignedViewport.w = (float) renderer->viewport.w;
dludwig@8433
  1199
        orientationAlignedViewport.h = (float) renderer->viewport.h;
dludwig@8433
  1200
    }
dludwig@8433
  1201
    // WinRT, TODO: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped)
dludwig@8432
  1202
dludwig@8432
  1203
    D3D11_VIEWPORT viewport;
dludwig@8432
  1204
    memset(&viewport, 0, sizeof(viewport));
dludwig@8433
  1205
    viewport.TopLeftX = orientationAlignedViewport.x;
dludwig@8433
  1206
    viewport.TopLeftY = orientationAlignedViewport.y;
dludwig@8433
  1207
    viewport.Width = orientationAlignedViewport.w;
dludwig@8433
  1208
    viewport.Height = orientationAlignedViewport.h;
dludwig@8432
  1209
    viewport.MinDepth = 0.0f;
dludwig@8432
  1210
    viewport.MaxDepth = 1.0f;
dludwig@8432
  1211
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8432
  1212
dludwig@8433
  1213
#if 0
dludwig@8433
  1214
    SDL_Log("%s, oav={%.0f,%.0f,%.0f,%.0f}, rend={%.0f,%.0f}\n",
dludwig@8433
  1215
        __FUNCTION__,
dludwig@8433
  1216
        orientationAlignedViewport.x,
dludwig@8433
  1217
        orientationAlignedViewport.y,
dludwig@8433
  1218
        orientationAlignedViewport.w,
dludwig@8433
  1219
        orientationAlignedViewport.h,
dludwig@8433
  1220
        data->renderTargetSize.x,
dludwig@8433
  1221
        data->renderTargetSize.y);
dludwig@8433
  1222
#endif
dludwig@8433
  1223
dludwig@8400
  1224
    return 0;
dludwig@8400
  1225
}
dludwig@8400
  1226
dludwig@8459
  1227
static ComPtr<ID3D11RenderTargetView> &
dludwig@8459
  1228
D3D11_GetCurrentRenderTargetView(SDL_Renderer * renderer)
dludwig@8459
  1229
{
dludwig@8459
  1230
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8459
  1231
    if (data->currentOffscreenRenderTargetView) {
dludwig@8459
  1232
        return data->currentOffscreenRenderTargetView;
dludwig@8459
  1233
    } else {
dludwig@8459
  1234
        return data->mainRenderTargetView;
dludwig@8459
  1235
    }
dludwig@8459
  1236
}
dludwig@8459
  1237
dludwig@8416
  1238
static int
dludwig@8416
  1239
D3D11_RenderClear(SDL_Renderer * renderer)
dludwig@8416
  1240
{
dludwig@8416
  1241
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
  1242
    const float colorRGBA[] = {
dludwig@8423
  1243
        (renderer->r / 255.0f),
dludwig@8423
  1244
        (renderer->g / 255.0f),
dludwig@8423
  1245
        (renderer->b / 255.0f),
dludwig@8423
  1246
        (renderer->a / 255.0f)
dludwig@8416
  1247
    };
dludwig@8416
  1248
    data->d3dContext->ClearRenderTargetView(
dludwig@8459
  1249
        D3D11_GetCurrentRenderTargetView(renderer).Get(),
dludwig@8416
  1250
        colorRGBA
dludwig@8416
  1251
        );
dludwig@8416
  1252
    return 0;
dludwig@8416
  1253
}
dludwig@8416
  1254
dludwig@8429
  1255
static int
dludwig@8429
  1256
D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
dludwig@8429
  1257
                         const void * vertexData, unsigned int dataSizeInBytes)
dludwig@8416
  1258
{
dludwig@8416
  1259
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8425
  1260
    HRESULT result = S_OK;
dludwig@8449
  1261
    D3D11_BUFFER_DESC vertexBufferDesc;
dludwig@8449
  1262
dludwig@8449
  1263
    if (rendererData->vertexBuffer) {
dludwig@8449
  1264
        rendererData->vertexBuffer->GetDesc(&vertexBufferDesc);
dludwig@8449
  1265
    } else {
dludwig@8449
  1266
        memset(&vertexBufferDesc, 0, sizeof(vertexBufferDesc));
dludwig@8449
  1267
    }
dludwig@8449
  1268
dludwig@8449
  1269
    if (vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
dludwig@8429
  1270
        rendererData->d3dContext->UpdateSubresource(rendererData->vertexBuffer.Get(), 0, NULL, vertexData, dataSizeInBytes, 0);
dludwig@8425
  1271
    } else {
dludwig@8449
  1272
        vertexBufferDesc.ByteWidth = dataSizeInBytes;
dludwig@8449
  1273
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
dludwig@8449
  1274
dludwig@8425
  1275
        D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8429
  1276
        vertexBufferData.pSysMem = vertexData;
dludwig@8425
  1277
        vertexBufferData.SysMemPitch = 0;
dludwig@8425
  1278
        vertexBufferData.SysMemSlicePitch = 0;
dludwig@8449
  1279
dludwig@8425
  1280
        result = rendererData->d3dDevice->CreateBuffer(
dludwig@8425
  1281
            &vertexBufferDesc,
dludwig@8425
  1282
            &vertexBufferData,
dludwig@8425
  1283
            &rendererData->vertexBuffer
dludwig@8425
  1284
            );
dludwig@8425
  1285
        if (FAILED(result)) {
dludwig@8425
  1286
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8425
  1287
            return -1;
dludwig@8425
  1288
        }
dludwig@8425
  1289
    }
dludwig@8425
  1290
dludwig@8416
  1291
    UINT stride = sizeof(VertexPositionColor);
dludwig@8416
  1292
    UINT offset = 0;
dludwig@8416
  1293
    rendererData->d3dContext->IASetVertexBuffers(
dludwig@8416
  1294
        0,
dludwig@8416
  1295
        1,
dludwig@8416
  1296
        rendererData->vertexBuffer.GetAddressOf(),
dludwig@8416
  1297
        &stride,
dludwig@8416
  1298
        &offset
dludwig@8429
  1299
        );
dludwig@8429
  1300
dludwig@8429
  1301
    return 0;
dludwig@8429
  1302
}
dludwig@8429
  1303
dludwig@8429
  1304
static void
dludwig@8429
  1305
D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
dludwig@8429
  1306
{
dludwig@8429
  1307
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1308
dludwig@8429
  1309
    rendererData->d3dContext->OMSetRenderTargets(
dludwig@8429
  1310
        1,
dludwig@8459
  1311
        D3D11_GetCurrentRenderTargetView(renderer).GetAddressOf(),
dludwig@8429
  1312
        nullptr
dludwig@8416
  1313
        );
dludwig@8442
  1314
}
dludwig@8431
  1315
dludwig@8442
  1316
static void
dludwig@8442
  1317
D3D11_RenderSetBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
dludwig@8442
  1318
{
dludwig@8442
  1319
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8442
  1320
    switch (blendMode) {
dludwig@8431
  1321
        case SDL_BLENDMODE_BLEND:
dludwig@8431
  1322
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeBlend.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1323
            break;
dludwig@8431
  1324
        case SDL_BLENDMODE_ADD:
dludwig@8431
  1325
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeAdd.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1326
            break;
dludwig@8431
  1327
        case SDL_BLENDMODE_MOD:
dludwig@8431
  1328
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeMod.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1329
            break;
dludwig@8431
  1330
        case SDL_BLENDMODE_NONE:
dludwig@8431
  1331
            rendererData->d3dContext->OMSetBlendState(NULL, 0, 0xFFFFFFFF);
dludwig@8431
  1332
            break;
dludwig@8431
  1333
    }
dludwig@8429
  1334
}
dludwig@8429
  1335
dludwig@8429
  1336
static void
dludwig@8429
  1337
D3D11_SetPixelShader(SDL_Renderer * renderer,
dludwig@8429
  1338
                     ID3D11PixelShader * shader,
dludwig@8429
  1339
                     ID3D11ShaderResourceView * shaderResource,
dludwig@8429
  1340
                     ID3D11SamplerState * sampler)
dludwig@8429
  1341
{
dludwig@8429
  1342
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1343
    rendererData->d3dContext->PSSetShader(shader, nullptr, 0);
dludwig@8429
  1344
    rendererData->d3dContext->PSSetShaderResources(0, 1, &shaderResource);
dludwig@8429
  1345
    rendererData->d3dContext->PSSetSamplers(0, 1, &sampler);
dludwig@8429
  1346
}
dludwig@8429
  1347
dludwig@8429
  1348
static void
dludwig@8429
  1349
D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
dludwig@8449
  1350
                         D3D11_PRIMITIVE_TOPOLOGY primitiveTopology,
dludwig@8449
  1351
                         UINT vertexCount)
dludwig@8429
  1352
{
dludwig@8429
  1353
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8456
  1354
dludwig@8456
  1355
    rendererData->d3dContext->UpdateSubresource(
dludwig@8456
  1356
        rendererData->vertexShaderConstants.Get(),
dludwig@8456
  1357
        0,
dludwig@8456
  1358
        NULL,
dludwig@8456
  1359
        &rendererData->vertexShaderConstantsData,
dludwig@8456
  1360
        0,
dludwig@8456
  1361
        0
dludwig@8456
  1362
        );
dludwig@8456
  1363
dludwig@8429
  1364
    rendererData->d3dContext->IASetPrimitiveTopology(primitiveTopology);
dludwig@8429
  1365
    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
dludwig@8429
  1366
    rendererData->d3dContext->VSSetShader(rendererData->vertexShader.Get(), nullptr, 0);
dludwig@8429
  1367
    rendererData->d3dContext->VSSetConstantBuffers(0, 1, rendererData->vertexShaderConstants.GetAddressOf());
dludwig@8429
  1368
    rendererData->d3dContext->RSSetState(rendererData->mainRasterizer.Get());
dludwig@8449
  1369
    rendererData->d3dContext->Draw(vertexCount, 0);
dludwig@8449
  1370
}
dludwig@8449
  1371
dludwig@8449
  1372
static int
dludwig@8450
  1373
D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
  1374
                       const SDL_FPoint * points, int count)
dludwig@8450
  1375
{
dludwig@8450
  1376
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8450
  1377
    float r, g, b, a;
dludwig@8450
  1378
dludwig@8450
  1379
    r = (float)(renderer->r / 255.0f);
dludwig@8450
  1380
    g = (float)(renderer->g / 255.0f);
dludwig@8450
  1381
    b = (float)(renderer->b / 255.0f);
dludwig@8450
  1382
    a = (float)(renderer->a / 255.0f);
dludwig@8450
  1383
dludwig@8450
  1384
    vector<VertexPositionColor> vertices;
dludwig@8450
  1385
    vertices.reserve(count);
dludwig@8450
  1386
    for (int i = 0; i < count; ++i) {
dludwig@8450
  1387
        VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
dludwig@8450
  1388
        vertices.push_back(v);
dludwig@8450
  1389
    }
dludwig@8450
  1390
dludwig@8450
  1391
    D3D11_RenderStartDrawOp(renderer);
dludwig@8450
  1392
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8450
  1393
    if (D3D11_UpdateVertexBuffer(renderer, &vertices[0], vertices.size() * sizeof(VertexPositionColor)) != 0) {
dludwig@8450
  1394
        return -1;
dludwig@8450
  1395
    }
dludwig@8450
  1396
dludwig@8450
  1397
    D3D11_SetPixelShader(
dludwig@8450
  1398
        renderer,
dludwig@8450
  1399
        rendererData->colorPixelShader.Get(),
dludwig@8450
  1400
        nullptr,
dludwig@8450
  1401
        nullptr);
dludwig@8450
  1402
dludwig@8450
  1403
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, vertices.size());
dludwig@8450
  1404
dludwig@8450
  1405
    return 0;
dludwig@8450
  1406
}
dludwig@8450
  1407
dludwig@8450
  1408
static int
dludwig@8449
  1409
D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
  1410
                      const SDL_FPoint * points, int count)
dludwig@8449
  1411
{
dludwig@8449
  1412
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8449
  1413
    float r, g, b, a;
dludwig@8449
  1414
dludwig@8449
  1415
    r = (float)(renderer->r / 255.0f);
dludwig@8449
  1416
    g = (float)(renderer->g / 255.0f);
dludwig@8449
  1417
    b = (float)(renderer->b / 255.0f);
dludwig@8449
  1418
    a = (float)(renderer->a / 255.0f);
dludwig@8449
  1419
dludwig@8449
  1420
    vector<VertexPositionColor> vertices;
dludwig@8449
  1421
    vertices.reserve(count);
dludwig@8449
  1422
    for (int i = 0; i < count; ++i) {
dludwig@8449
  1423
        VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
dludwig@8449
  1424
        vertices.push_back(v);
dludwig@8449
  1425
    }
dludwig@8449
  1426
dludwig@8449
  1427
    D3D11_RenderStartDrawOp(renderer);
dludwig@8449
  1428
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8449
  1429
    if (D3D11_UpdateVertexBuffer(renderer, &vertices[0], vertices.size() * sizeof(VertexPositionColor)) != 0) {
dludwig@8449
  1430
        return -1;
dludwig@8449
  1431
    }
dludwig@8449
  1432
dludwig@8449
  1433
    D3D11_SetPixelShader(
dludwig@8449
  1434
        renderer,
dludwig@8449
  1435
        rendererData->colorPixelShader.Get(),
dludwig@8449
  1436
        nullptr,
dludwig@8449
  1437
        nullptr);
dludwig@8449
  1438
dludwig@8449
  1439
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, vertices.size());
dludwig@8449
  1440
dludwig@8449
  1441
    return 0;
dludwig@8429
  1442
}
dludwig@8429
  1443
dludwig@8429
  1444
static int
dludwig@8429
  1445
D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
  1446
                      const SDL_FRect * rects, int count)
dludwig@8429
  1447
{
dludwig@8429
  1448
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1449
    float r, g, b, a;
dludwig@8429
  1450
dludwig@8431
  1451
    r = (float)(renderer->r / 255.0f);
dludwig@8431
  1452
    g = (float)(renderer->g / 255.0f);
dludwig@8431
  1453
    b = (float)(renderer->b / 255.0f);
dludwig@8431
  1454
    a = (float)(renderer->a / 255.0f);
dludwig@8429
  1455
dludwig@8433
  1456
#if 0
dludwig@8433
  1457
    // Set up a test pattern:
dludwig@8434
  1458
    SDL_FRect _rects[] = {
dludwig@8433
  1459
        {-1.1f, 1.1f, 1.1f, -1.1f},
dludwig@8433
  1460
        {-1.0f, 1.0f, 1.0f, -1.0f},     // red
dludwig@8433
  1461
        {0.0f, 1.0f, 1.0f, -1.0f},      // green
dludwig@8433
  1462
        {-1.0f, 0.0f, 1.0f, -1.0f},     // blue
dludwig@8433
  1463
        {0.0f, 0.0f, 1.0f, -1.0f}       // white
dludwig@8433
  1464
    };
dludwig@8434
  1465
    count = sizeof(_rects) / sizeof(SDL_FRect);
dludwig@8434
  1466
#define rects _rects
dludwig@8433
  1467
#endif
dludwig@8429
  1468
dludwig@8429
  1469
    for (int i = 0; i < count; ++i) {
dludwig@8433
  1470
        D3D11_RenderStartDrawOp(renderer);
dludwig@8442
  1471
        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8433
  1472
dludwig@8433
  1473
#if 0
dludwig@8433
  1474
        // Set colors for the test pattern:
dludwig@8433
  1475
        a = 1.0f;
dludwig@8433
  1476
        switch (i) {
dludwig@8433
  1477
            case 0: r = 1.0f; g = 1.0f; b = 0.0f; break;
dludwig@8433
  1478
            case 1: r = 1.0f; g = 0.0f; b = 0.0f; break;
dludwig@8433
  1479
            case 2: r = 0.0f; g = 1.0f; b = 0.0f; break;
dludwig@8433
  1480
            case 3: r = 0.0f; g = 0.0f; b = 1.0f; break;
dludwig@8433
  1481
            case 4: r = 1.0f; g = 1.0f; b = 1.0f; break;
dludwig@8433
  1482
        }
dludwig@8433
  1483
#endif
dludwig@8433
  1484
dludwig@8429
  1485
        VertexPositionColor vertices[] = {
dludwig@8429
  1486
            {XMFLOAT3(rects[i].x, rects[i].y, 0.0f),                           XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1487
            {XMFLOAT3(rects[i].x, rects[i].y + rects[i].h, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1488
            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1489
            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y + rects[i].h, 0.0f), XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1490
        };
dludwig@8429
  1491
        if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
dludwig@8429
  1492
            return -1;
dludwig@8429
  1493
        }
dludwig@8416
  1494
dludwig@8429
  1495
        D3D11_SetPixelShader(
dludwig@8429
  1496
            renderer,
dludwig@8429
  1497
            rendererData->colorPixelShader.Get(),
dludwig@8429
  1498
            nullptr,
dludwig@8429
  1499
            nullptr);
dludwig@8416
  1500
dludwig@8449
  1501
        D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8429
  1502
    }
dludwig@8429
  1503
dludwig@8429
  1504
    return 0;
dludwig@8429
  1505
}
dludwig@8429
  1506
dludwig@8429
  1507
static int
dludwig@8429
  1508
D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8429
  1509
                 const SDL_Rect * srcrect, const SDL_FRect * dstrect)
dludwig@8429
  1510
{
dludwig@8429
  1511
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1512
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8429
  1513
dludwig@8429
  1514
    D3D11_RenderStartDrawOp(renderer);
dludwig@8442
  1515
    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
dludwig@8448
  1516
dludwig@8448
  1517
    float minu = (float) srcrect->x / texture->w;
dludwig@8448
  1518
    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
dludwig@8448
  1519
    float minv = (float) srcrect->y / texture->h;
dludwig@8448
  1520
    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
dludwig@8458
  1521
dludwig@8458
  1522
    float r = 1.0f;
dludwig@8458
  1523
    float g = 1.0f;
dludwig@8458
  1524
    float b = 1.0f;
dludwig@8458
  1525
    float a = 1.0f;
dludwig@8458
  1526
    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
dludwig@8458
  1527
        r = (float)(texture->r / 255.0f);
dludwig@8458
  1528
        g = (float)(texture->g / 255.0f);
dludwig@8458
  1529
        b = (float)(texture->b / 255.0f);
dludwig@8458
  1530
    }
dludwig@8458
  1531
    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
dludwig@8458
  1532
        a = (float)(texture->a / 255.0f);
dludwig@8458
  1533
    }
dludwig@8416
  1534
dludwig@8429
  1535
    VertexPositionColor vertices[] = {
dludwig@8458
  1536
        {XMFLOAT3(dstrect->x, dstrect->y, 0.0f),                           XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1537
        {XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f),              XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1538
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y, 0.0f),              XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1539
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1540
    };
dludwig@8429
  1541
    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
dludwig@8429
  1542
        return -1;
dludwig@8429
  1543
    }
dludwig@8418
  1544
dludwig@8429
  1545
    D3D11_SetPixelShader(
dludwig@8429
  1546
        renderer,
dludwig@8429
  1547
        rendererData->texturePixelShader.Get(),
dludwig@8429
  1548
        textureData->mainTextureResourceView.Get(),
dludwig@8429
  1549
        rendererData->mainSampler.Get());
dludwig@8416
  1550
dludwig@8449
  1551
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8416
  1552
dludwig@8416
  1553
    return 0;
dludwig@8416
  1554
}
dludwig@8416
  1555
dludwig@8454
  1556
static int
dludwig@8455
  1557
D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8455
  1558
                   const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8455
  1559
                   const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
dludwig@8455
  1560
{
dludwig@8455
  1561
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8455
  1562
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8455
  1563
dludwig@8455
  1564
    D3D11_RenderStartDrawOp(renderer);
dludwig@8455
  1565
    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
dludwig@8455
  1566
dludwig@8455
  1567
    float minu = (float) srcrect->x / texture->w;
dludwig@8455
  1568
    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
dludwig@8455
  1569
    float minv = (float) srcrect->y / texture->h;
dludwig@8455
  1570
    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
dludwig@8455
  1571
dludwig@8458
  1572
    float r = 1.0f;
dludwig@8458
  1573
    float g = 1.0f;
dludwig@8458
  1574
    float b = 1.0f;
dludwig@8458
  1575
    float a = 1.0f;
dludwig@8458
  1576
    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
dludwig@8458
  1577
        r = (float)(texture->r / 255.0f);
dludwig@8458
  1578
        g = (float)(texture->g / 255.0f);
dludwig@8458
  1579
        b = (float)(texture->b / 255.0f);
dludwig@8458
  1580
    }
dludwig@8458
  1581
    if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
dludwig@8458
  1582
        a = (float)(texture->a / 255.0f);
dludwig@8458
  1583
    }
dludwig@8458
  1584
dludwig@8455
  1585
    if (flip & SDL_FLIP_HORIZONTAL) {
dludwig@8455
  1586
        float tmp = maxu;
dludwig@8455
  1587
        maxu = minu;
dludwig@8455
  1588
        minu = tmp;
dludwig@8455
  1589
    }
dludwig@8455
  1590
    if (flip & SDL_FLIP_VERTICAL) {
dludwig@8455
  1591
        float tmp = maxv;
dludwig@8455
  1592
        maxv = minv;
dludwig@8455
  1593
        minv = tmp;
dludwig@8455
  1594
    }
dludwig@8456
  1595
dludwig@8456
  1596
    XMFLOAT4X4 oldModelMatrix = rendererData->vertexShaderConstantsData.model;
dludwig@8456
  1597
    XMStoreFloat4x4(
dludwig@8456
  1598
        &rendererData->vertexShaderConstantsData.model,
dludwig@8456
  1599
        XMMatrixMultiply(
dludwig@8456
  1600
            XMMatrixRotationZ((float)(XM_PI * (float) angle / 180.0f)),
dludwig@8456
  1601
            XMMatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
dludwig@8456
  1602
            ));
dludwig@8456
  1603
dludwig@8456
  1604
    const float minx = -center->x;
dludwig@8456
  1605
    const float maxx = dstrect->w - center->x;
dludwig@8456
  1606
    const float miny = -center->y;
dludwig@8456
  1607
    const float maxy = dstrect->h - center->y;
dludwig@8455
  1608
dludwig@8455
  1609
    VertexPositionColor vertices[] = {
dludwig@8458
  1610
        {XMFLOAT3(minx, miny, 0.0f), XMFLOAT2(minu, minv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1611
        {XMFLOAT3(minx, maxy, 0.0f), XMFLOAT2(minu, maxv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1612
        {XMFLOAT3(maxx, miny, 0.0f), XMFLOAT2(maxu, minv), XMFLOAT4(r, g, b, a)},
dludwig@8458
  1613
        {XMFLOAT3(maxx, maxy, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(r, g, b, a)},
dludwig@8455
  1614
    };
dludwig@8455
  1615
    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
dludwig@8455
  1616
        return -1;
dludwig@8455
  1617
    }
dludwig@8455
  1618
dludwig@8455
  1619
    D3D11_SetPixelShader(
dludwig@8455
  1620
        renderer,
dludwig@8455
  1621
        rendererData->texturePixelShader.Get(),
dludwig@8455
  1622
        textureData->mainTextureResourceView.Get(),
dludwig@8455
  1623
        rendererData->mainSampler.Get());
dludwig@8455
  1624
dludwig@8455
  1625
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8456
  1626
dludwig@8456
  1627
    rendererData->vertexShaderConstantsData.model = oldModelMatrix;
dludwig@8455
  1628
dludwig@8455
  1629
    return 0;
dludwig@8455
  1630
}
dludwig@8455
  1631
dludwig@8455
  1632
static int
dludwig@8454
  1633
D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8454
  1634
                       Uint32 format, void * pixels, int pitch)
dludwig@8454
  1635
{
dludwig@8454
  1636
    D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8454
  1637
    HRESULT result = S_OK;
dludwig@8454
  1638
dludwig@8454
  1639
    // Retrieve a pointer to the back buffer:
dludwig@8454
  1640
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8454
  1641
    result = data->swapChain->GetBuffer(
dludwig@8454
  1642
        0,
dludwig@8454
  1643
        __uuidof(ID3D11Texture2D),
dludwig@8454
  1644
        &backBuffer
dludwig@8454
  1645
        );
dludwig@8454
  1646
    if (FAILED(result)) {
dludwig@8454
  1647
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Get Back Buffer", result);
dludwig@8454
  1648
        return -1;
dludwig@8454
  1649
    }
dludwig@8454
  1650
dludwig@8454
  1651
    // Create a staging texture to copy the screen's data to:
dludwig@8454
  1652
    ComPtr<ID3D11Texture2D> stagingTexture;
dludwig@8454
  1653
    D3D11_TEXTURE2D_DESC stagingTextureDesc;
dludwig@8454
  1654
    backBuffer->GetDesc(&stagingTextureDesc);
dludwig@8454
  1655
    stagingTextureDesc.Width = rect->w;
dludwig@8454
  1656
    stagingTextureDesc.Height = rect->h;
dludwig@8454
  1657
    stagingTextureDesc.BindFlags = 0;
dludwig@8454
  1658
    stagingTextureDesc.MiscFlags = 0;
dludwig@8454
  1659
    stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
dludwig@8454
  1660
    stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
dludwig@8454
  1661
    result = data->d3dDevice->CreateTexture2D(
dludwig@8454
  1662
        &stagingTextureDesc,
dludwig@8454
  1663
        NULL,
dludwig@8454
  1664
        &stagingTexture);
dludwig@8454
  1665
    if (FAILED(result)) {
dludwig@8454
  1666
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Create Staging Texture", result);
dludwig@8454
  1667
        return -1;
dludwig@8454
  1668
    }
dludwig@8454
  1669
dludwig@8454
  1670
    // Copy the desired portion of the back buffer to the staging texture:
dludwig@8454
  1671
    D3D11_BOX srcBox;
dludwig@8454
  1672
    srcBox.left = rect->x;
dludwig@8454
  1673
    srcBox.right = rect->x + rect->w;
dludwig@8454
  1674
    srcBox.top = rect->y;
dludwig@8454
  1675
    srcBox.bottom = rect->y + rect->h;
dludwig@8454
  1676
    srcBox.front = 0;
dludwig@8454
  1677
    srcBox.back = 1;
dludwig@8454
  1678
    data->d3dContext->CopySubresourceRegion(
dludwig@8454
  1679
        stagingTexture.Get(),
dludwig@8454
  1680
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  1681
        0, 0, 0,
dludwig@8454
  1682
        backBuffer.Get(),
dludwig@8454
  1683
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  1684
        &srcBox);
dludwig@8454
  1685
dludwig@8454
  1686
    // Map the staging texture's data to CPU-accessible memory:
dludwig@8454
  1687
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8454
  1688
    result = data->d3dContext->Map(
dludwig@8454
  1689
        stagingTexture.Get(),
dludwig@8454
  1690
        D3D11CalcSubresource(0, 0, 0),
dludwig@8454
  1691
        D3D11_MAP_READ,
dludwig@8454
  1692
        0,
dludwig@8454
  1693
        &textureMemory);
dludwig@8454
  1694
    if (FAILED(result)) {
dludwig@8454
  1695
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Map Staging Texture to CPU Memory", result);
dludwig@8454
  1696
        return -1;
dludwig@8454
  1697
    }
dludwig@8454
  1698
dludwig@8454
  1699
    // Copy the data into the desired buffer, converting pixels to the
dludwig@8454
  1700
    // desired format at the same time:
dludwig@8454
  1701
    if (SDL_ConvertPixels(
dludwig@8454
  1702
        rect->w, rect->h,
dludwig@8454
  1703
        DXGIFormatToSDLPixelFormat(stagingTextureDesc.Format),
dludwig@8454
  1704
        textureMemory.pData,
dludwig@8454
  1705
        textureMemory.RowPitch,
dludwig@8454
  1706
        format,
dludwig@8454
  1707
        pixels,
dludwig@8454
  1708
        pitch) != 0)
dludwig@8454
  1709
    {
dludwig@8454
  1710
        // When SDL_ConvertPixels fails, it'll have already set the format.
dludwig@8454
  1711
        // Get the error message, and attach some extra data to it.
dludwig@8454
  1712
        std::string errorMessage = string(__FUNCTION__ ", Convert Pixels failed: ") + SDL_GetError();
dludwig@8454
  1713
        SDL_SetError(errorMessage.c_str());
dludwig@8454
  1714
        return -1;
dludwig@8454
  1715
    }
dludwig@8454
  1716
dludwig@8454
  1717
    // Unmap the texture:
dludwig@8454
  1718
    data->d3dContext->Unmap(
dludwig@8454
  1719
        stagingTexture.Get(),
dludwig@8454
  1720
        D3D11CalcSubresource(0, 0, 0));
dludwig@8454
  1721
dludwig@8454
  1722
    // All done.  The staging texture will be cleaned up in it's container
dludwig@8454
  1723
    // ComPtr<>'s destructor.
dludwig@8454
  1724
    return 0;
dludwig@8454
  1725
}
dludwig@8454
  1726
dludwig@8401
  1727
static void
dludwig@8401
  1728
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
  1729
{
dludwig@8401
  1730
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
  1731
dludwig@8401
  1732
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
  1733
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
  1734
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
  1735
    // frames that will never be displayed to the screen.
dludwig@8401
  1736
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
  1737
#else
dludwig@8401
  1738
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
  1739
    // rects to improve efficiency in certain scenarios.
dludwig@8401
  1740
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
  1741
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
  1742
    parameters.DirtyRectsCount = 0;
dludwig@8401
  1743
    parameters.pDirtyRects = nullptr;
dludwig@8401
  1744
    parameters.pScrollRect = nullptr;
dludwig@8401
  1745
    parameters.pScrollOffset = nullptr;
dludwig@8401
  1746
    
dludwig@8401
  1747
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
  1748
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
  1749
    // frames that will never be displayed to the screen.
dludwig@8401
  1750
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
  1751
#endif
dludwig@8401
  1752
dludwig@8401
  1753
    // Discard the contents of the render target.
dludwig@8401
  1754
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
  1755
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8459
  1756
    data->d3dContext->DiscardView(data->mainRenderTargetView.Get());
dludwig@8401
  1757
dludwig@8401
  1758
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
  1759
    // must recreate all device resources.
dludwig@8414
  1760
    //
dludwig@8414
  1761
    // TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvedge debug info from users' machines
dludwig@8401
  1762
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
  1763
    {
dludwig@8414
  1764
        hr = D3D11_HandleDeviceLost(renderer);
dludwig@8414
  1765
        if (FAILED(hr)) {
dludwig@8414
  1766
            WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8414
  1767
        }
dludwig@8401
  1768
    }
dludwig@8401
  1769
    else
dludwig@8401
  1770
    {
dludwig@8401
  1771
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
  1772
    }
dludwig@8401
  1773
}
dludwig@8401
  1774
dludwig@8400
  1775
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
  1776
dludwig@8400
  1777
/* vi: set ts=4 sw=4 expandtab: */