src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Fri, 25 Oct 2013 20:31:43 -0400
changeset 8533 89cf2a2635d1
parent 8526 1d613b04bb17
child 8540 724af2cd53b8
permissions -rw-r--r--
WinRT: made the Direct3D 11.x 'Debug Layer' be enable-able in any app via a hint

To enable the Debug Layer, set the hint, SDL_HINT_RENDER_DIRECT3D11_DEBUG to '1'.

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