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