src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 16:00:55 -0500
changeset 8415 91977f5b2135
parent 8414 25a7605097a2
child 8416 17040ce2693d
permissions -rw-r--r--
WinRT: added SDL_WINDOWEVENT_RESIZED support, and moved window-resize-handling code from WinRT rendering code to D3D 11.1 code
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8400
    26
extern "C" {
dludwig@8400
    27
#include "../../core/windows/SDL_windows.h"
dludwig@8400
    28
//#include "SDL_hints.h"
dludwig@8400
    29
//#include "SDL_loadso.h"
dludwig@8410
    30
#include "SDL_system.h"
dludwig@8400
    31
#include "SDL_syswm.h"
dludwig@8400
    32
#include "../SDL_sysrender.h"
dludwig@8400
    33
//#include "stdio.h"
dludwig@8400
    34
}
dludwig@8400
    35
dludwig@8410
    36
#include <fstream>
dludwig@8410
    37
#include <string>
dludwig@8410
    38
#include <vector>
dludwig@8410
    39
dludwig@8400
    40
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    41
dludwig@8410
    42
using namespace DirectX;
dludwig@8410
    43
using namespace Microsoft::WRL;
dludwig@8410
    44
using namespace std;
dludwig@8410
    45
dludwig@8412
    46
#ifdef __WINRT__
dludwig@8412
    47
using namespace Windows::Graphics::Display;
dludwig@8412
    48
using namespace Windows::UI::Core;
dludwig@8412
    49
#endif
dludwig@8412
    50
dludwig@8410
    51
/* Direct3D 11.1 renderer implementation */
dludwig@8400
    52
dludwig@8400
    53
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8415
    54
static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8415
    55
                            const SDL_WindowEvent *event);
dludwig@8400
    56
//static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    57
//static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    58
//                             const SDL_Rect * rect, const void *pixels,
dludwig@8400
    59
//                             int pitch);
dludwig@8400
    60
//static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    61
//                           const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8400
    62
//static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    63
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    64
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8400
    65
//static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8400
    66
//static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8400
    67
//                                const SDL_FPoint * points, int count);
dludwig@8400
    68
//static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8400
    69
//                               const SDL_FPoint * points, int count);
dludwig@8400
    70
//static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8400
    71
//                               const SDL_FRect * rects, int count);
dludwig@8400
    72
//static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    73
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8400
    74
//static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    75
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8400
    76
//                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8400
    77
//static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8400
    78
//                                Uint32 format, void * pixels, int pitch);
dludwig@8401
    79
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8400
    80
//static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8400
    81
//                               SDL_Texture * texture);
dludwig@8413
    82
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    83
dludwig@8410
    84
/* Direct3D 11.1 Internal Functions */
dludwig@8412
    85
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
    86
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8414
    87
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
dludwig@8414
    88
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
dludwig@8400
    89
dludwig@8400
    90
extern "C" {
dludwig@8400
    91
    SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    92
    D3D11_CreateRenderer,
dludwig@8400
    93
    {
dludwig@8400
    94
     "direct3d",
dludwig@8400
    95
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
dludwig@8400
    96
     1,
dludwig@8400
    97
     {SDL_PIXELFORMAT_ARGB8888},
dludwig@8400
    98
     0,
dludwig@8400
    99
     0}
dludwig@8400
   100
    };
dludwig@8400
   101
}
dludwig@8400
   102
dludwig@8400
   103
//typedef struct
dludwig@8400
   104
//{
dludwig@8400
   105
//    float x, y, z;
dludwig@8400
   106
//    DWORD color;
dludwig@8400
   107
//    float u, v;
dludwig@8400
   108
//} Vertex;
dludwig@8400
   109
dludwig@8400
   110
SDL_Renderer *
dludwig@8400
   111
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
   112
{
dludwig@8400
   113
    SDL_Renderer *renderer;
dludwig@8400
   114
    D3D11_RenderData *data;
dludwig@8400
   115
dludwig@8400
   116
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   117
    if (!renderer) {
dludwig@8400
   118
        SDL_OutOfMemory();
dludwig@8400
   119
        return NULL;
dludwig@8400
   120
    }
dludwig@8400
   121
    SDL_zerop(renderer);
dludwig@8400
   122
dludwig@8400
   123
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   124
    if (!data) {
dludwig@8400
   125
        SDL_OutOfMemory();
dludwig@8400
   126
        return NULL;
dludwig@8400
   127
    }
dludwig@8410
   128
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
dludwig@8410
   129
    data->vertexCount = 0;
dludwig@8410
   130
    data->loadingComplete = false;
dludwig@8412
   131
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
dludwig@8412
   132
    data->renderTargetSize = XMFLOAT2(0, 0);
dludwig@8400
   133
dludwig@8415
   134
    renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8400
   135
    //renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8400
   136
    //renderer->UpdateTexture = D3D11_UpdateTexture;
dludwig@8400
   137
    //renderer->LockTexture = D3D11_LockTexture;
dludwig@8400
   138
    //renderer->UnlockTexture = D3D11_UnlockTexture;
dludwig@8400
   139
    //renderer->SetRenderTarget = D3D11_SetRenderTarget;
dludwig@8400
   140
    renderer->UpdateViewport = D3D11_UpdateViewport;
dludwig@8400
   141
    //renderer->RenderClear = D3D11_RenderClear;
dludwig@8400
   142
    //renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
dludwig@8400
   143
    //renderer->RenderDrawLines = D3D11_RenderDrawLines;
dludwig@8400
   144
    //renderer->RenderFillRects = D3D11_RenderFillRects;
dludwig@8400
   145
    //renderer->RenderCopy = D3D11_RenderCopy;
dludwig@8400
   146
    //renderer->RenderCopyEx = D3D11_RenderCopyEx;
dludwig@8400
   147
    //renderer->RenderReadPixels = D3D11_RenderReadPixels;
dludwig@8401
   148
    renderer->RenderPresent = D3D11_RenderPresent;
dludwig@8400
   149
    //renderer->DestroyTexture = D3D11_DestroyTexture;
dludwig@8413
   150
    renderer->DestroyRenderer = D3D11_DestroyRenderer;
dludwig@8400
   151
    renderer->info = D3D11_RenderDriver.info;
dludwig@8413
   152
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
dludwig@8400
   153
    renderer->driverdata = data;
dludwig@8400
   154
dludwig@8413
   155
    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
dludwig@8413
   156
    // order to give init functions access to the underlying window handle:
dludwig@8413
   157
    renderer->window = window;
dludwig@8400
   158
dludwig@8413
   159
    /* Initialize Direct3D resources */
dludwig@8413
   160
    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
dludwig@8413
   161
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   162
        return NULL;
dludwig@8413
   163
    }
dludwig@8413
   164
    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
dludwig@8413
   165
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   166
        return NULL;
dludwig@8413
   167
    }
dludwig@8400
   168
dludwig@8400
   169
    return renderer;
dludwig@8400
   170
}
dludwig@8400
   171
dludwig@8413
   172
static void
dludwig@8413
   173
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   174
{
dludwig@8413
   175
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   176
    if (data) {
dludwig@8413
   177
        delete data;
dludwig@8413
   178
        data = NULL;
dludwig@8413
   179
    }
dludwig@8413
   180
}
dludwig@8413
   181
dludwig@8410
   182
static bool
dludwig@8410
   183
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
dludwig@8410
   184
{
dludwig@8410
   185
    ifstream in(fileName, ios::in | ios::binary);
dludwig@8410
   186
    if (!in) {
dludwig@8410
   187
        return false;
dludwig@8410
   188
    }
dludwig@8410
   189
dludwig@8410
   190
    in.seekg(0, ios::end);
dludwig@8410
   191
    out.resize((size_t) in.tellg());
dludwig@8410
   192
    in.seekg(0, ios::beg);
dludwig@8410
   193
    in.read(&out[0], out.size());
dludwig@8410
   194
    return in.good();
dludwig@8410
   195
}
dludwig@8410
   196
dludwig@8410
   197
static bool
dludwig@8410
   198
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
dludwig@8410
   199
{
dludwig@8410
   200
    wstring fileName;
dludwig@8410
   201
dludwig@8410
   202
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8410
   203
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   204
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   205
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8410
   206
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   207
    fileName += L"\\";
dludwig@8410
   208
#endif
dludwig@8410
   209
    // WinRT, TODO: test Direct3D 11.1 shader loading on Win32
dludwig@8410
   210
    fileName += shaderName;
dludwig@8410
   211
    return D3D11_ReadFileContents(fileName, out);
dludwig@8410
   212
}
dludwig@8410
   213
dludwig@8414
   214
// Create resources that depend on the device.
dludwig@8410
   215
HRESULT
dludwig@8412
   216
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
dludwig@8410
   217
{
dludwig@8410
   218
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8410
   219
dludwig@8410
   220
    // This flag adds support for surfaces with a different color channel ordering
dludwig@8410
   221
    // than the API default. It is required for compatibility with Direct2D.
dludwig@8410
   222
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
dludwig@8410
   223
dludwig@8410
   224
#if defined(_DEBUG)
dludwig@8410
   225
    // If the project is in a debug build, enable debugging via SDK Layers with this flag.
dludwig@8410
   226
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8410
   227
#endif
dludwig@8410
   228
dludwig@8410
   229
    // This array defines the set of DirectX hardware feature levels this app will support.
dludwig@8410
   230
    // Note the ordering should be preserved.
dludwig@8410
   231
    // Don't forget to declare your application's minimum required feature level in its
dludwig@8410
   232
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
dludwig@8410
   233
    D3D_FEATURE_LEVEL featureLevels[] = 
dludwig@8410
   234
    {
dludwig@8410
   235
        D3D_FEATURE_LEVEL_11_1,
dludwig@8410
   236
        D3D_FEATURE_LEVEL_11_0,
dludwig@8410
   237
        D3D_FEATURE_LEVEL_10_1,
dludwig@8410
   238
        D3D_FEATURE_LEVEL_10_0,
dludwig@8410
   239
        D3D_FEATURE_LEVEL_9_3,
dludwig@8410
   240
        D3D_FEATURE_LEVEL_9_2,
dludwig@8410
   241
        D3D_FEATURE_LEVEL_9_1
dludwig@8410
   242
    };
dludwig@8410
   243
dludwig@8410
   244
    // Create the Direct3D 11 API device object and a corresponding context.
dludwig@8410
   245
    ComPtr<ID3D11Device> device;
dludwig@8410
   246
    ComPtr<ID3D11DeviceContext> context;
dludwig@8410
   247
    HRESULT result = S_OK;
dludwig@8410
   248
    result = D3D11CreateDevice(
dludwig@8410
   249
        nullptr, // Specify nullptr to use the default adapter.
dludwig@8410
   250
        D3D_DRIVER_TYPE_HARDWARE,
dludwig@8410
   251
        nullptr,
dludwig@8410
   252
        creationFlags, // Set set debug and Direct2D compatibility flags.
dludwig@8410
   253
        featureLevels, // List of feature levels this app can support.
dludwig@8410
   254
        ARRAYSIZE(featureLevels),
dludwig@8410
   255
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
dludwig@8410
   256
        &device, // Returns the Direct3D device created.
dludwig@8410
   257
        &data->featureLevel, // Returns feature level of device created.
dludwig@8410
   258
        &context // Returns the device immediate context.
dludwig@8410
   259
        );
dludwig@8410
   260
    if (FAILED(result)) {
dludwig@8410
   261
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   262
        return result;
dludwig@8410
   263
    }
dludwig@8410
   264
dludwig@8410
   265
    // Get the Direct3D 11.1 API device and context interfaces.
dludwig@8410
   266
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
dludwig@8410
   267
    result = device.As(&(data->d3dDevice));
dludwig@8410
   268
    if (FAILED(result)) {
dludwig@8410
   269
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   270
        return result;
dludwig@8410
   271
    }
dludwig@8410
   272
dludwig@8410
   273
    result = context.As(&data->d3dContext);
dludwig@8410
   274
    if (FAILED(result)) {
dludwig@8410
   275
        return result;
dludwig@8410
   276
    }
dludwig@8410
   277
dludwig@8410
   278
    // Start loading GPU shaders:
dludwig@8410
   279
    vector<char> fileData;
dludwig@8410
   280
dludwig@8410
   281
    //
dludwig@8410
   282
    // Load in SDL's one and only vertex shader:
dludwig@8410
   283
    //
dludwig@8410
   284
    if (!D3D11_ReadShaderContents(L"SimpleVertexShader.cso", fileData)) {
dludwig@8410
   285
        SDL_SetError("Unable to open SDL's vertex shader file.");
dludwig@8410
   286
        return E_FAIL;
dludwig@8410
   287
    }
dludwig@8410
   288
dludwig@8410
   289
    result = data->d3dDevice->CreateVertexShader(
dludwig@8410
   290
        &fileData[0],
dludwig@8410
   291
        fileData.size(),
dludwig@8410
   292
        nullptr,
dludwig@8410
   293
        &data->vertexShader
dludwig@8410
   294
        );
dludwig@8410
   295
    if (FAILED(result)) {
dludwig@8410
   296
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   297
        return result;
dludwig@8410
   298
    }
dludwig@8410
   299
dludwig@8410
   300
    //
dludwig@8410
   301
    // Create an input layout for SDL's vertex shader:
dludwig@8410
   302
    //
dludwig@8410
   303
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8410
   304
    {
dludwig@8410
   305
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   306
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   307
    };
dludwig@8410
   308
dludwig@8410
   309
    result = data->d3dDevice->CreateInputLayout(
dludwig@8410
   310
        vertexDesc,
dludwig@8410
   311
        ARRAYSIZE(vertexDesc),
dludwig@8410
   312
        &fileData[0],
dludwig@8410
   313
        fileData.size(),
dludwig@8410
   314
        &data->inputLayout
dludwig@8410
   315
        );
dludwig@8410
   316
    if (FAILED(result)) {
dludwig@8410
   317
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   318
        return result;
dludwig@8410
   319
    }
dludwig@8410
   320
dludwig@8410
   321
    //
dludwig@8410
   322
    // Load in SDL's one and only pixel shader (for now, more are likely to follow):
dludwig@8410
   323
    //
dludwig@8410
   324
    if (!D3D11_ReadShaderContents(L"SimplePixelShader.cso", fileData)) {
dludwig@8410
   325
        SDL_SetError("Unable to open SDL's pixel shader file.");
dludwig@8410
   326
        return E_FAIL;
dludwig@8410
   327
    }
dludwig@8410
   328
dludwig@8410
   329
    result = data->d3dDevice->CreatePixelShader(
dludwig@8410
   330
        &fileData[0],
dludwig@8410
   331
        fileData.size(),
dludwig@8410
   332
        nullptr,
dludwig@8410
   333
        &data->pixelShader
dludwig@8410
   334
        );
dludwig@8410
   335
    if (FAILED(result)) {
dludwig@8410
   336
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   337
        return result;
dludwig@8410
   338
    }
dludwig@8410
   339
dludwig@8410
   340
    //
dludwig@8410
   341
    // Create a vertex buffer:
dludwig@8410
   342
    //
dludwig@8410
   343
    VertexPositionColor vertices[] = 
dludwig@8410
   344
    {
dludwig@8410
   345
        {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
dludwig@8410
   346
        {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
dludwig@8410
   347
        {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
dludwig@8410
   348
        {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
dludwig@8410
   349
    };
dludwig@8410
   350
dludwig@8410
   351
    data->vertexCount = ARRAYSIZE(vertices);
dludwig@8410
   352
dludwig@8410
   353
    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8410
   354
    vertexBufferData.pSysMem = vertices;
dludwig@8410
   355
    vertexBufferData.SysMemPitch = 0;
dludwig@8410
   356
    vertexBufferData.SysMemSlicePitch = 0;
dludwig@8410
   357
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER);
dludwig@8410
   358
    result = data->d3dDevice->CreateBuffer(
dludwig@8410
   359
        &vertexBufferDesc,
dludwig@8410
   360
        &vertexBufferData,
dludwig@8410
   361
        &data->vertexBuffer
dludwig@8410
   362
        );
dludwig@8410
   363
    if (FAILED(result)) {
dludwig@8410
   364
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   365
        return result;
dludwig@8410
   366
    }
dludwig@8410
   367
dludwig@8410
   368
    //
dludwig@8410
   369
    // Create a sampler to use when drawing textures:
dludwig@8410
   370
    //
dludwig@8410
   371
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   372
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   373
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   374
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   375
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   376
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   377
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   378
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   379
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   380
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   381
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   382
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   383
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   384
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   385
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   386
        &samplerDesc,
dludwig@8410
   387
        &data->mainSampler
dludwig@8410
   388
        );
dludwig@8410
   389
    if (FAILED(result)) {
dludwig@8410
   390
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   391
        return result;
dludwig@8410
   392
    }
dludwig@8410
   393
dludwig@8410
   394
    //
dludwig@8410
   395
    // All done!
dludwig@8410
   396
    //
dludwig@8410
   397
    data->loadingComplete = true;       // This variable can probably be factored-out
dludwig@8410
   398
    return S_OK;
dludwig@8410
   399
}
dludwig@8410
   400
dludwig@8412
   401
#ifdef __WINRT__
dludwig@8412
   402
dludwig@8415
   403
static CoreWindow ^
dludwig@8412
   404
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   405
{
dludwig@8412
   406
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   407
    if ( ! renderer->window ) {
dludwig@8412
   408
        return nullptr;
dludwig@8412
   409
    }
dludwig@8412
   410
dludwig@8412
   411
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   412
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   413
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   414
        return nullptr;
dludwig@8412
   415
    }
dludwig@8412
   416
dludwig@8412
   417
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   418
        return nullptr;
dludwig@8412
   419
    }
dludwig@8412
   420
dludwig@8412
   421
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   422
    if ( ! coreWindowPointer ) {
dludwig@8412
   423
        return nullptr;
dludwig@8412
   424
    }
dludwig@8412
   425
dludwig@8412
   426
    return *coreWindowPointer;
dludwig@8412
   427
}
dludwig@8412
   428
dludwig@8414
   429
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
dludwig@8412
   430
static float
dludwig@8412
   431
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   432
{
dludwig@8412
   433
    static const float dipsPerInch = 96.0f;
dludwig@8412
   434
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   435
}
dludwig@8412
   436
#endif
dludwig@8412
   437
dludwig@8414
   438
// Initialize all resources that change when the window's size changes.
dludwig@8412
   439
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   440
HRESULT
dludwig@8412
   441
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   442
{
dludwig@8412
   443
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   444
    HRESULT result = S_OK;
dludwig@8412
   445
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   446
dludwig@8412
   447
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   448
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   449
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   450
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   451
dludwig@8412
   452
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   453
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   454
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   455
dludwig@8412
   456
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   457
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   458
    // orientation, the dimensions must be reversed.
dludwig@8412
   459
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8412
   460
    bool swapDimensions =
dludwig@8412
   461
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   462
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   463
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   464
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   465
dludwig@8412
   466
    if(data->swapChain != nullptr)
dludwig@8412
   467
    {
dludwig@8412
   468
        // If the swap chain already exists, resize it.
dludwig@8412
   469
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   470
            2, // Double-buffered swap chain.
dludwig@8412
   471
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   472
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   473
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   474
            0
dludwig@8412
   475
            );
dludwig@8412
   476
        if (FAILED(result)) {
dludwig@8412
   477
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   478
            return result;
dludwig@8412
   479
        }
dludwig@8412
   480
    }
dludwig@8412
   481
    else
dludwig@8412
   482
    {
dludwig@8412
   483
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   484
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   485
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   486
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   487
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   488
        swapChainDesc.Stereo = false;
dludwig@8412
   489
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   490
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   491
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   492
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   493
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   494
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   495
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   496
#else
dludwig@8412
   497
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   498
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   499
#endif
dludwig@8412
   500
        swapChainDesc.Flags = 0;
dludwig@8412
   501
dludwig@8412
   502
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   503
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   504
        if (FAILED(result)) {
dludwig@8412
   505
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   506
            return result;
dludwig@8412
   507
        }
dludwig@8412
   508
dludwig@8412
   509
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   510
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   511
        if (FAILED(result)) {
dludwig@8412
   512
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   513
            return result;
dludwig@8412
   514
        }
dludwig@8412
   515
dludwig@8412
   516
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   517
        result = dxgiAdapter->GetParent(
dludwig@8412
   518
            __uuidof(IDXGIFactory2), 
dludwig@8412
   519
            &dxgiFactory
dludwig@8412
   520
            );
dludwig@8412
   521
        if (FAILED(result)) {
dludwig@8412
   522
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   523
            return result;
dludwig@8412
   524
        }
dludwig@8412
   525
dludwig@8412
   526
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   527
            data->d3dDevice.Get(),
dludwig@8412
   528
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   529
            &swapChainDesc,
dludwig@8412
   530
            nullptr, // Allow on all displays.
dludwig@8412
   531
            &data->swapChain
dludwig@8412
   532
            );
dludwig@8412
   533
        if (FAILED(result)) {
dludwig@8412
   534
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   535
            return result;
dludwig@8412
   536
        }
dludwig@8412
   537
            
dludwig@8412
   538
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   539
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   540
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   541
        if (FAILED(result)) {
dludwig@8412
   542
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   543
            return result;
dludwig@8412
   544
        }
dludwig@8412
   545
    }
dludwig@8412
   546
    
dludwig@8412
   547
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   548
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   549
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   550
    switch (data->orientation)
dludwig@8412
   551
    {
dludwig@8412
   552
        case DisplayOrientations::Landscape:
dludwig@8412
   553
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8412
   554
            data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8412
   555
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   556
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   557
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   558
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   559
                );
dludwig@8412
   560
            break;
dludwig@8412
   561
dludwig@8412
   562
        case DisplayOrientations::Portrait:
dludwig@8412
   563
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8412
   564
            data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8412
   565
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   566
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   567
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   568
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   569
                );
dludwig@8412
   570
            break;
dludwig@8412
   571
dludwig@8412
   572
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   573
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8412
   574
            data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8412
   575
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   576
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   577
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   578
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   579
                );
dludwig@8412
   580
            break;
dludwig@8412
   581
dludwig@8412
   582
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   583
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8412
   584
            data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8412
   585
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   586
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   587
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   588
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   589
                );
dludwig@8412
   590
            break;
dludwig@8412
   591
dludwig@8412
   592
        default:
dludwig@8412
   593
            throw ref new Platform::FailureException();
dludwig@8412
   594
    }
dludwig@8412
   595
dludwig@8412
   596
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   597
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   598
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   599
    if (FAILED(result)) {
dludwig@8412
   600
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   601
        return result;
dludwig@8412
   602
    }
dludwig@8412
   603
#endif
dludwig@8412
   604
dludwig@8412
   605
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   606
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   607
    result = data->swapChain->GetBuffer(
dludwig@8412
   608
        0,
dludwig@8412
   609
        __uuidof(ID3D11Texture2D),
dludwig@8412
   610
        &backBuffer
dludwig@8412
   611
        );
dludwig@8412
   612
    if (FAILED(result)) {
dludwig@8412
   613
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   614
        return result;
dludwig@8412
   615
    }
dludwig@8412
   616
dludwig@8412
   617
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   618
        backBuffer.Get(),
dludwig@8412
   619
        nullptr,
dludwig@8412
   620
        &data->renderTargetView
dludwig@8412
   621
        );
dludwig@8412
   622
    if (FAILED(result)) {
dludwig@8412
   623
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   624
        return result;
dludwig@8412
   625
    }
dludwig@8412
   626
dludwig@8412
   627
    // Create a depth stencil view.
dludwig@8412
   628
    CD3D11_TEXTURE2D_DESC depthStencilDesc(
dludwig@8412
   629
        DXGI_FORMAT_D24_UNORM_S8_UINT, 
dludwig@8412
   630
        static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   631
        static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   632
        1,
dludwig@8412
   633
        1,
dludwig@8412
   634
        D3D11_BIND_DEPTH_STENCIL
dludwig@8412
   635
        );
dludwig@8412
   636
dludwig@8412
   637
    ComPtr<ID3D11Texture2D> depthStencil;
dludwig@8412
   638
    result = data->d3dDevice->CreateTexture2D(
dludwig@8412
   639
        &depthStencilDesc,
dludwig@8412
   640
        nullptr,
dludwig@8412
   641
        &depthStencil
dludwig@8412
   642
        );
dludwig@8412
   643
    if (FAILED(result)) {
dludwig@8412
   644
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   645
        return result;
dludwig@8412
   646
    }
dludwig@8412
   647
dludwig@8412
   648
    // Set the rendering viewport to target the entire window.
dludwig@8412
   649
    CD3D11_VIEWPORT viewport(
dludwig@8412
   650
        0.0f,
dludwig@8412
   651
        0.0f,
dludwig@8412
   652
        data->renderTargetSize.x,
dludwig@8412
   653
        data->renderTargetSize.y
dludwig@8412
   654
        );
dludwig@8412
   655
dludwig@8412
   656
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8412
   657
dludwig@8412
   658
    return S_OK;
dludwig@8412
   659
}
dludwig@8412
   660
dludwig@8415
   661
// This method is called when the window's size changes.
dludwig@8414
   662
HRESULT
dludwig@8414
   663
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
dludwig@8414
   664
{
dludwig@8414
   665
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   666
    HRESULT result = S_OK;
dludwig@8414
   667
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8414
   668
dludwig@8414
   669
    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
dludwig@8414
   670
        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
dludwig@8414
   671
        data->orientation != DisplayProperties::CurrentOrientation)
dludwig@8414
   672
    {
dludwig@8414
   673
        ID3D11RenderTargetView* nullViews[] = {nullptr};
dludwig@8414
   674
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
dludwig@8414
   675
        data->renderTargetView = nullptr;
dludwig@8414
   676
        data->d3dContext->Flush();
dludwig@8414
   677
        result = D3D11_CreateWindowSizeDependentResources(renderer);
dludwig@8414
   678
        if (FAILED(result)) {
dludwig@8414
   679
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   680
            return result;
dludwig@8414
   681
        }
dludwig@8414
   682
    }
dludwig@8414
   683
dludwig@8414
   684
    return S_OK;
dludwig@8414
   685
}
dludwig@8414
   686
dludwig@8414
   687
HRESULT
dludwig@8414
   688
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
   689
{
dludwig@8414
   690
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   691
    HRESULT result = S_OK;
dludwig@8414
   692
dludwig@8415
   693
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
dludwig@8414
   694
    data->windowSizeInDIPs.x = 0;
dludwig@8414
   695
    data->windowSizeInDIPs.y = 0;
dludwig@8414
   696
    data->swapChain = nullptr;
dludwig@8414
   697
dludwig@8414
   698
    result = D3D11_CreateDeviceResources(renderer);
dludwig@8414
   699
    if (FAILED(result)) {
dludwig@8414
   700
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   701
        return result;
dludwig@8414
   702
    }
dludwig@8414
   703
dludwig@8414
   704
    result = D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8414
   705
    if (FAILED(result)) {
dludwig@8414
   706
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   707
        return result;
dludwig@8414
   708
    }
dludwig@8414
   709
dludwig@8414
   710
    return S_OK;
dludwig@8414
   711
}
dludwig@8414
   712
dludwig@8415
   713
static void
dludwig@8415
   714
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
   715
{
dludwig@8415
   716
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8415
   717
dludwig@8415
   718
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
dludwig@8415
   719
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
   720
    }
dludwig@8415
   721
}
dludwig@8415
   722
dludwig@8400
   723
static int
dludwig@8400
   724
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   725
{
dludwig@8400
   726
    return 0;
dludwig@8400
   727
}
dludwig@8400
   728
dludwig@8401
   729
static void
dludwig@8401
   730
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
   731
{
dludwig@8401
   732
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
   733
dludwig@8401
   734
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
   735
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   736
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   737
    // frames that will never be displayed to the screen.
dludwig@8401
   738
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
   739
#else
dludwig@8401
   740
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
   741
    // rects to improve efficiency in certain scenarios.
dludwig@8401
   742
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
   743
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
   744
    parameters.DirtyRectsCount = 0;
dludwig@8401
   745
    parameters.pDirtyRects = nullptr;
dludwig@8401
   746
    parameters.pScrollRect = nullptr;
dludwig@8401
   747
    parameters.pScrollOffset = nullptr;
dludwig@8401
   748
    
dludwig@8401
   749
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   750
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   751
    // frames that will never be displayed to the screen.
dludwig@8401
   752
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
   753
#endif
dludwig@8401
   754
dludwig@8401
   755
    // Discard the contents of the render target.
dludwig@8401
   756
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
   757
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8401
   758
    data->d3dContext->DiscardView(data->renderTargetView.Get());
dludwig@8401
   759
dludwig@8401
   760
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
   761
    // must recreate all device resources.
dludwig@8414
   762
    //
dludwig@8414
   763
    // 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
   764
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
   765
    {
dludwig@8414
   766
        hr = D3D11_HandleDeviceLost(renderer);
dludwig@8414
   767
        if (FAILED(hr)) {
dludwig@8414
   768
            WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8414
   769
        }
dludwig@8401
   770
    }
dludwig@8401
   771
    else
dludwig@8401
   772
    {
dludwig@8401
   773
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
   774
    }
dludwig@8401
   775
}
dludwig@8401
   776
dludwig@8400
   777
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
   778
dludwig@8400
   779
/* vi: set ts=4 sw=4 expandtab: */