src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 27 Aug 2013 21:21:09 -0400
changeset 8505 a76a3842c9eb
parent 8504 3205689d1fff
child 8507 a8a917981264
permissions -rw-r--r--
WinRT: experimental and preliminary support for XAML-based overlays on Windows 8/RT

The XAML support here is still rudimentary. Bugs do exist. You've been warned.

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