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