src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 10 Feb 2013 17:35:38 -0500
changeset 8423 61f9a3eb6bc8
parent 8420 99dc85ad173c
child 8425 99448fee696f
permissions -rw-r--r--
WinRT: made SDL_RenderClear display the correct color via Direct3D 11.1
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@8416
    56
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8416
    57
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    58
                             const SDL_Rect * rect, const void *pixels,
dludwig@8416
    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@8416
    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@8416
    72
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    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@8416
    80
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
    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@8416
    94
     "direct3d 11.1",
dludwig@8400
    95
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
dludwig@8400
    96
     1,
dludwig@8416
    97
     {SDL_PIXELFORMAT_RGB888},
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@8416
   135
    renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8416
   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@8416
   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@8416
   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@8416
   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@8416
   169
    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
dludwig@8416
   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@8420
   205
    fileName = SDL_WinRTGetFileSystemPath(SDL_WINRT_PATH_INSTALLED_LOCATION);
dludwig@8410
   206
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   207
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8420
   208
    fileName = SDL_WinRTGetFileSystemPath(SDL_WINRT_PATH_INSTALLED_LOCATION);
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@8418
   343
    // Setup space to hold vertex shader constants:
dludwig@8418
   344
    //
dludwig@8418
   345
    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
dludwig@8418
   346
    result = data->d3dDevice->CreateBuffer(
dludwig@8418
   347
		&constantBufferDesc,
dludwig@8418
   348
		nullptr,
dludwig@8418
   349
        &data->vertexShaderConstants
dludwig@8418
   350
		);
dludwig@8418
   351
    if (FAILED(result)) {
dludwig@8418
   352
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8418
   353
        return result;
dludwig@8418
   354
    }
dludwig@8418
   355
dludwig@8418
   356
    //
dludwig@8410
   357
    // Create a vertex buffer:
dludwig@8410
   358
    //
dludwig@8410
   359
    VertexPositionColor vertices[] = 
dludwig@8410
   360
    {
dludwig@8410
   361
        {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
dludwig@8410
   362
        {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
dludwig@8410
   363
        {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
dludwig@8410
   364
        {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
dludwig@8410
   365
    };
dludwig@8410
   366
dludwig@8410
   367
    data->vertexCount = ARRAYSIZE(vertices);
dludwig@8410
   368
dludwig@8410
   369
    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8410
   370
    vertexBufferData.pSysMem = vertices;
dludwig@8410
   371
    vertexBufferData.SysMemPitch = 0;
dludwig@8410
   372
    vertexBufferData.SysMemSlicePitch = 0;
dludwig@8410
   373
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER);
dludwig@8410
   374
    result = data->d3dDevice->CreateBuffer(
dludwig@8410
   375
        &vertexBufferDesc,
dludwig@8410
   376
        &vertexBufferData,
dludwig@8410
   377
        &data->vertexBuffer
dludwig@8410
   378
        );
dludwig@8410
   379
    if (FAILED(result)) {
dludwig@8410
   380
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   381
        return result;
dludwig@8410
   382
    }
dludwig@8410
   383
dludwig@8410
   384
    //
dludwig@8410
   385
    // Create a sampler to use when drawing textures:
dludwig@8410
   386
    //
dludwig@8410
   387
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   388
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   389
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   390
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   391
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   392
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   393
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   394
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   395
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   396
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   397
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   398
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   399
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   400
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   401
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   402
        &samplerDesc,
dludwig@8410
   403
        &data->mainSampler
dludwig@8410
   404
        );
dludwig@8410
   405
    if (FAILED(result)) {
dludwig@8410
   406
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   407
        return result;
dludwig@8410
   408
    }
dludwig@8410
   409
dludwig@8410
   410
    //
dludwig@8410
   411
    // All done!
dludwig@8410
   412
    //
dludwig@8410
   413
    data->loadingComplete = true;       // This variable can probably be factored-out
dludwig@8410
   414
    return S_OK;
dludwig@8410
   415
}
dludwig@8410
   416
dludwig@8412
   417
#ifdef __WINRT__
dludwig@8412
   418
dludwig@8415
   419
static CoreWindow ^
dludwig@8412
   420
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   421
{
dludwig@8412
   422
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   423
    if ( ! renderer->window ) {
dludwig@8412
   424
        return nullptr;
dludwig@8412
   425
    }
dludwig@8412
   426
dludwig@8412
   427
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   428
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   429
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   430
        return nullptr;
dludwig@8412
   431
    }
dludwig@8412
   432
dludwig@8412
   433
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   434
        return nullptr;
dludwig@8412
   435
    }
dludwig@8412
   436
dludwig@8412
   437
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   438
    if ( ! coreWindowPointer ) {
dludwig@8412
   439
        return nullptr;
dludwig@8412
   440
    }
dludwig@8412
   441
dludwig@8412
   442
    return *coreWindowPointer;
dludwig@8412
   443
}
dludwig@8412
   444
dludwig@8414
   445
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
dludwig@8412
   446
static float
dludwig@8412
   447
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   448
{
dludwig@8412
   449
    static const float dipsPerInch = 96.0f;
dludwig@8412
   450
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   451
}
dludwig@8412
   452
#endif
dludwig@8412
   453
dludwig@8414
   454
// Initialize all resources that change when the window's size changes.
dludwig@8412
   455
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   456
HRESULT
dludwig@8412
   457
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   458
{
dludwig@8412
   459
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   460
    HRESULT result = S_OK;
dludwig@8412
   461
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   462
dludwig@8412
   463
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   464
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   465
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   466
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   467
dludwig@8412
   468
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   469
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   470
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   471
dludwig@8412
   472
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   473
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   474
    // orientation, the dimensions must be reversed.
dludwig@8412
   475
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8412
   476
    bool swapDimensions =
dludwig@8412
   477
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   478
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   479
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   480
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   481
dludwig@8412
   482
    if(data->swapChain != nullptr)
dludwig@8412
   483
    {
dludwig@8412
   484
        // If the swap chain already exists, resize it.
dludwig@8412
   485
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   486
            2, // Double-buffered swap chain.
dludwig@8412
   487
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   488
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   489
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   490
            0
dludwig@8412
   491
            );
dludwig@8412
   492
        if (FAILED(result)) {
dludwig@8412
   493
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   494
            return result;
dludwig@8412
   495
        }
dludwig@8412
   496
    }
dludwig@8412
   497
    else
dludwig@8412
   498
    {
dludwig@8412
   499
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   500
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   501
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   502
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   503
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   504
        swapChainDesc.Stereo = false;
dludwig@8412
   505
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   506
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   507
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   508
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   509
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   510
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   511
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   512
#else
dludwig@8412
   513
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   514
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   515
#endif
dludwig@8412
   516
        swapChainDesc.Flags = 0;
dludwig@8412
   517
dludwig@8412
   518
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   519
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   520
        if (FAILED(result)) {
dludwig@8412
   521
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   522
            return result;
dludwig@8412
   523
        }
dludwig@8412
   524
dludwig@8412
   525
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   526
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   527
        if (FAILED(result)) {
dludwig@8412
   528
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   529
            return result;
dludwig@8412
   530
        }
dludwig@8412
   531
dludwig@8412
   532
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   533
        result = dxgiAdapter->GetParent(
dludwig@8412
   534
            __uuidof(IDXGIFactory2), 
dludwig@8412
   535
            &dxgiFactory
dludwig@8412
   536
            );
dludwig@8412
   537
        if (FAILED(result)) {
dludwig@8412
   538
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   539
            return result;
dludwig@8412
   540
        }
dludwig@8412
   541
dludwig@8412
   542
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   543
            data->d3dDevice.Get(),
dludwig@8412
   544
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   545
            &swapChainDesc,
dludwig@8412
   546
            nullptr, // Allow on all displays.
dludwig@8412
   547
            &data->swapChain
dludwig@8412
   548
            );
dludwig@8412
   549
        if (FAILED(result)) {
dludwig@8412
   550
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   551
            return result;
dludwig@8412
   552
        }
dludwig@8412
   553
            
dludwig@8412
   554
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   555
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   556
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   557
        if (FAILED(result)) {
dludwig@8412
   558
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   559
            return result;
dludwig@8412
   560
        }
dludwig@8412
   561
    }
dludwig@8412
   562
    
dludwig@8412
   563
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   564
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   565
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   566
    switch (data->orientation)
dludwig@8412
   567
    {
dludwig@8412
   568
        case DisplayOrientations::Landscape:
dludwig@8412
   569
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8418
   570
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8412
   571
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   572
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   573
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   574
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   575
                );
dludwig@8412
   576
            break;
dludwig@8412
   577
dludwig@8412
   578
        case DisplayOrientations::Portrait:
dludwig@8412
   579
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8418
   580
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8418
   581
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8418
   582
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   583
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   584
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   585
                );
dludwig@8412
   586
            break;
dludwig@8412
   587
dludwig@8412
   588
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   589
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8418
   590
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8412
   591
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   592
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   593
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   594
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   595
                );
dludwig@8412
   596
            break;
dludwig@8412
   597
dludwig@8412
   598
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   599
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8418
   600
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8418
   601
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8418
   602
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   603
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   604
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   605
                );
dludwig@8412
   606
            break;
dludwig@8412
   607
dludwig@8412
   608
        default:
dludwig@8412
   609
            throw ref new Platform::FailureException();
dludwig@8412
   610
    }
dludwig@8412
   611
dludwig@8412
   612
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   613
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   614
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   615
    if (FAILED(result)) {
dludwig@8412
   616
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   617
        return result;
dludwig@8412
   618
    }
dludwig@8412
   619
#endif
dludwig@8412
   620
dludwig@8412
   621
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   622
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   623
    result = data->swapChain->GetBuffer(
dludwig@8412
   624
        0,
dludwig@8412
   625
        __uuidof(ID3D11Texture2D),
dludwig@8412
   626
        &backBuffer
dludwig@8412
   627
        );
dludwig@8412
   628
    if (FAILED(result)) {
dludwig@8412
   629
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   630
        return result;
dludwig@8412
   631
    }
dludwig@8412
   632
dludwig@8412
   633
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   634
        backBuffer.Get(),
dludwig@8412
   635
        nullptr,
dludwig@8412
   636
        &data->renderTargetView
dludwig@8412
   637
        );
dludwig@8412
   638
    if (FAILED(result)) {
dludwig@8412
   639
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   640
        return result;
dludwig@8412
   641
    }
dludwig@8412
   642
dludwig@8412
   643
    // Create a depth stencil view.
dludwig@8412
   644
    CD3D11_TEXTURE2D_DESC depthStencilDesc(
dludwig@8412
   645
        DXGI_FORMAT_D24_UNORM_S8_UINT, 
dludwig@8412
   646
        static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   647
        static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   648
        1,
dludwig@8412
   649
        1,
dludwig@8412
   650
        D3D11_BIND_DEPTH_STENCIL
dludwig@8412
   651
        );
dludwig@8412
   652
dludwig@8412
   653
    ComPtr<ID3D11Texture2D> depthStencil;
dludwig@8412
   654
    result = data->d3dDevice->CreateTexture2D(
dludwig@8412
   655
        &depthStencilDesc,
dludwig@8412
   656
        nullptr,
dludwig@8412
   657
        &depthStencil
dludwig@8412
   658
        );
dludwig@8412
   659
    if (FAILED(result)) {
dludwig@8412
   660
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   661
        return result;
dludwig@8412
   662
    }
dludwig@8412
   663
dludwig@8412
   664
    // Set the rendering viewport to target the entire window.
dludwig@8412
   665
    CD3D11_VIEWPORT viewport(
dludwig@8412
   666
        0.0f,
dludwig@8412
   667
        0.0f,
dludwig@8412
   668
        data->renderTargetSize.x,
dludwig@8412
   669
        data->renderTargetSize.y
dludwig@8412
   670
        );
dludwig@8412
   671
dludwig@8412
   672
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8412
   673
dludwig@8412
   674
    return S_OK;
dludwig@8412
   675
}
dludwig@8412
   676
dludwig@8415
   677
// This method is called when the window's size changes.
dludwig@8414
   678
HRESULT
dludwig@8414
   679
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
dludwig@8414
   680
{
dludwig@8414
   681
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   682
    HRESULT result = S_OK;
dludwig@8414
   683
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8414
   684
dludwig@8414
   685
    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
dludwig@8414
   686
        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
dludwig@8414
   687
        data->orientation != DisplayProperties::CurrentOrientation)
dludwig@8414
   688
    {
dludwig@8414
   689
        ID3D11RenderTargetView* nullViews[] = {nullptr};
dludwig@8414
   690
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
dludwig@8414
   691
        data->renderTargetView = nullptr;
dludwig@8414
   692
        data->d3dContext->Flush();
dludwig@8414
   693
        result = D3D11_CreateWindowSizeDependentResources(renderer);
dludwig@8414
   694
        if (FAILED(result)) {
dludwig@8414
   695
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   696
            return result;
dludwig@8414
   697
        }
dludwig@8414
   698
    }
dludwig@8414
   699
dludwig@8414
   700
    return S_OK;
dludwig@8414
   701
}
dludwig@8414
   702
dludwig@8414
   703
HRESULT
dludwig@8414
   704
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
   705
{
dludwig@8414
   706
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   707
    HRESULT result = S_OK;
dludwig@8414
   708
dludwig@8415
   709
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
dludwig@8414
   710
    data->windowSizeInDIPs.x = 0;
dludwig@8414
   711
    data->windowSizeInDIPs.y = 0;
dludwig@8414
   712
    data->swapChain = nullptr;
dludwig@8414
   713
dludwig@8414
   714
    result = D3D11_CreateDeviceResources(renderer);
dludwig@8414
   715
    if (FAILED(result)) {
dludwig@8414
   716
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   717
        return result;
dludwig@8414
   718
    }
dludwig@8414
   719
dludwig@8414
   720
    result = D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8414
   721
    if (FAILED(result)) {
dludwig@8414
   722
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   723
        return result;
dludwig@8414
   724
    }
dludwig@8414
   725
dludwig@8414
   726
    return S_OK;
dludwig@8414
   727
}
dludwig@8414
   728
dludwig@8415
   729
static void
dludwig@8415
   730
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
   731
{
dludwig@8415
   732
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8415
   733
dludwig@8415
   734
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
dludwig@8415
   735
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
   736
    }
dludwig@8415
   737
}
dludwig@8415
   738
dludwig@8416
   739
static int
dludwig@8416
   740
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8416
   741
{
dludwig@8416
   742
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   743
    D3D11_TextureData *textureData;
dludwig@8416
   744
    HRESULT result;
dludwig@8416
   745
dludwig@8416
   746
    textureData = new D3D11_TextureData;
dludwig@8416
   747
    if (!textureData) {
dludwig@8416
   748
        SDL_OutOfMemory();
dludwig@8416
   749
        return -1;
dludwig@8416
   750
    }
dludwig@8416
   751
    textureData->pixelFormat = SDL_AllocFormat(texture->format);
dludwig@8416
   752
dludwig@8416
   753
    texture->driverdata = textureData;
dludwig@8416
   754
dludwig@8416
   755
    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
dludwig@8416
   756
dludwig@8416
   757
    D3D11_TEXTURE2D_DESC textureDesc = {0};
dludwig@8416
   758
    textureDesc.Width = texture->w;
dludwig@8416
   759
    textureDesc.Height = texture->h;
dludwig@8416
   760
    textureDesc.MipLevels = 1;
dludwig@8416
   761
    textureDesc.ArraySize = 1;
dludwig@8416
   762
    textureDesc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
dludwig@8416
   763
    textureDesc.SampleDesc.Count = 1;
dludwig@8416
   764
    textureDesc.SampleDesc.Quality = 0;
dludwig@8416
   765
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;
dludwig@8416
   766
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dludwig@8416
   767
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8416
   768
    textureDesc.MiscFlags = 0;
dludwig@8416
   769
dludwig@8416
   770
    const int numPixels = textureDesc.Width * textureDesc.Height;
dludwig@8416
   771
    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
dludwig@8416
   772
dludwig@8416
   773
    // Fill the texture with a non-black color, for debugging purposes:
dludwig@8416
   774
    //for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
dludwig@8416
   775
    //    initialTexturePixels[i+0] = 0xff;
dludwig@8416
   776
    //    initialTexturePixels[i+1] = 0xff;
dludwig@8416
   777
    //    initialTexturePixels[i+2] = 0x00;
dludwig@8416
   778
    //    initialTexturePixels[i+3] = 0xff;
dludwig@8416
   779
    //}
dludwig@8416
   780
dludwig@8416
   781
    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
dludwig@8416
   782
    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
dludwig@8416
   783
    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
dludwig@8416
   784
    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
dludwig@8416
   785
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8416
   786
        &textureDesc,
dludwig@8416
   787
        &initialTextureData,
dludwig@8416
   788
        &textureData->mainTexture
dludwig@8416
   789
        );
dludwig@8416
   790
    if (FAILED(result)) {
dludwig@8416
   791
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   792
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   793
        return -1;
dludwig@8416
   794
    }
dludwig@8416
   795
dludwig@8416
   796
    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
dludwig@8416
   797
    resourceViewDesc.Format = textureDesc.Format;
dludwig@8416
   798
    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dludwig@8416
   799
    resourceViewDesc.Texture2D.MostDetailedMip = 0;
dludwig@8416
   800
    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
dludwig@8416
   801
    result = rendererData->d3dDevice->CreateShaderResourceView(
dludwig@8416
   802
        textureData->mainTexture.Get(),
dludwig@8416
   803
        &resourceViewDesc,
dludwig@8416
   804
        &textureData->mainTextureResourceView
dludwig@8416
   805
        );
dludwig@8416
   806
    if (FAILED(result)) {
dludwig@8416
   807
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   808
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   809
        return -1;
dludwig@8416
   810
    }
dludwig@8416
   811
dludwig@8416
   812
    return 0;
dludwig@8416
   813
}
dludwig@8416
   814
dludwig@8416
   815
static void
dludwig@8416
   816
D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
   817
                     SDL_Texture * texture)
dludwig@8416
   818
{
dludwig@8416
   819
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   820
dludwig@8416
   821
    if (textureData) {
dludwig@8416
   822
        if (textureData->pixelFormat) {
dludwig@8416
   823
            SDL_FreeFormat(textureData->pixelFormat);
dludwig@8416
   824
            textureData->pixelFormat = NULL;
dludwig@8416
   825
        }
dludwig@8416
   826
dludwig@8416
   827
        delete textureData;
dludwig@8416
   828
        texture->driverdata = NULL;
dludwig@8416
   829
    }
dludwig@8416
   830
}
dludwig@8416
   831
dludwig@8416
   832
static int
dludwig@8416
   833
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   834
                    const SDL_Rect * rect, const void *pixels,
dludwig@8416
   835
                    int pitch)
dludwig@8416
   836
{
dludwig@8416
   837
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   838
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   839
    HRESULT result = S_OK;
dludwig@8416
   840
dludwig@8416
   841
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8416
   842
    result = rendererData->d3dContext->Map(
dludwig@8416
   843
        textureData->mainTexture.Get(),
dludwig@8416
   844
        0,
dludwig@8416
   845
        D3D11_MAP_WRITE_DISCARD,
dludwig@8416
   846
        0,
dludwig@8416
   847
        &textureMemory
dludwig@8416
   848
        );
dludwig@8416
   849
    if (FAILED(result)) {
dludwig@8416
   850
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   851
        return -1;
dludwig@8416
   852
    }
dludwig@8416
   853
dludwig@8416
   854
    // Copy pixel data to the locked texture's memory:
dludwig@8416
   855
    for (int y = 0; y < rect->h; ++y) {
dludwig@8416
   856
        memcpy(
dludwig@8416
   857
            ((Uint8 *)textureMemory.pData) + (textureMemory.RowPitch * y),
dludwig@8416
   858
            ((Uint8 *)pixels) + (pitch * y),
dludwig@8416
   859
            pitch
dludwig@8416
   860
            );
dludwig@8416
   861
    }
dludwig@8416
   862
dludwig@8416
   863
    // Clean up a bit, then commit the texture's memory back to Direct3D:
dludwig@8416
   864
    rendererData->d3dContext->Unmap(
dludwig@8416
   865
        textureData->mainTexture.Get(),
dludwig@8416
   866
        0);
dludwig@8416
   867
dludwig@8416
   868
    return 0;
dludwig@8416
   869
}
dludwig@8416
   870
dludwig@8400
   871
static int
dludwig@8400
   872
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   873
{
dludwig@8400
   874
    return 0;
dludwig@8400
   875
}
dludwig@8400
   876
dludwig@8416
   877
static int
dludwig@8416
   878
D3D11_RenderClear(SDL_Renderer * renderer)
dludwig@8416
   879
{
dludwig@8416
   880
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   881
    const float colorRGBA[] = {
dludwig@8423
   882
        (renderer->r / 255.0f),
dludwig@8423
   883
        (renderer->g / 255.0f),
dludwig@8423
   884
        (renderer->b / 255.0f),
dludwig@8423
   885
        (renderer->a / 255.0f)
dludwig@8416
   886
    };
dludwig@8416
   887
    data->d3dContext->ClearRenderTargetView(
dludwig@8416
   888
        data->renderTargetView.Get(),
dludwig@8416
   889
        colorRGBA
dludwig@8416
   890
        );
dludwig@8416
   891
    return 0;
dludwig@8416
   892
}
dludwig@8416
   893
dludwig@8416
   894
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   895
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect)
dludwig@8416
   896
{
dludwig@8416
   897
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   898
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   899
    //HRESULT result = S_OK;
dludwig@8416
   900
dludwig@8416
   901
    rendererData->d3dContext->OMSetRenderTargets(
dludwig@8416
   902
        1,
dludwig@8416
   903
        rendererData->renderTargetView.GetAddressOf(),
dludwig@8416
   904
        nullptr
dludwig@8416
   905
        );
dludwig@8416
   906
dludwig@8418
   907
    rendererData->d3dContext->UpdateSubresource(
dludwig@8418
   908
        rendererData->vertexShaderConstants.Get(),
dludwig@8418
   909
		0,
dludwig@8418
   910
		NULL,
dludwig@8418
   911
		&rendererData->vertexShaderConstantsData,
dludwig@8418
   912
		0,
dludwig@8418
   913
		0
dludwig@8418
   914
		);
dludwig@8418
   915
dludwig@8416
   916
    UINT stride = sizeof(VertexPositionColor);
dludwig@8416
   917
    UINT offset = 0;
dludwig@8416
   918
    rendererData->d3dContext->IASetVertexBuffers(
dludwig@8416
   919
        0,
dludwig@8416
   920
        1,
dludwig@8416
   921
        rendererData->vertexBuffer.GetAddressOf(),
dludwig@8416
   922
        &stride,
dludwig@8416
   923
        &offset
dludwig@8416
   924
        );
dludwig@8416
   925
dludwig@8416
   926
    rendererData->d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
dludwig@8416
   927
dludwig@8416
   928
    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
dludwig@8416
   929
dludwig@8416
   930
    rendererData->d3dContext->VSSetShader(
dludwig@8416
   931
        rendererData->vertexShader.Get(),
dludwig@8416
   932
        nullptr,
dludwig@8416
   933
        0
dludwig@8416
   934
        );
dludwig@8416
   935
dludwig@8418
   936
    rendererData->d3dContext->VSSetConstantBuffers(
dludwig@8418
   937
		0,
dludwig@8418
   938
		1,
dludwig@8418
   939
        rendererData->vertexShaderConstants.GetAddressOf()
dludwig@8418
   940
		);
dludwig@8418
   941
dludwig@8416
   942
    rendererData->d3dContext->PSSetShader(
dludwig@8416
   943
        rendererData->pixelShader.Get(),
dludwig@8416
   944
        nullptr,
dludwig@8416
   945
        0
dludwig@8416
   946
        );
dludwig@8416
   947
dludwig@8416
   948
    rendererData->d3dContext->PSSetShaderResources(0, 1, textureData->mainTextureResourceView.GetAddressOf());
dludwig@8416
   949
dludwig@8416
   950
    rendererData->d3dContext->PSSetSamplers(0, 1, rendererData->mainSampler.GetAddressOf());
dludwig@8416
   951
dludwig@8416
   952
    rendererData->d3dContext->Draw(4, 0);
dludwig@8416
   953
dludwig@8416
   954
    return 0;
dludwig@8416
   955
}
dludwig@8416
   956
dludwig@8401
   957
static void
dludwig@8401
   958
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
   959
{
dludwig@8401
   960
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
   961
dludwig@8401
   962
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
   963
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   964
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   965
    // frames that will never be displayed to the screen.
dludwig@8401
   966
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
   967
#else
dludwig@8401
   968
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
   969
    // rects to improve efficiency in certain scenarios.
dludwig@8401
   970
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
   971
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
   972
    parameters.DirtyRectsCount = 0;
dludwig@8401
   973
    parameters.pDirtyRects = nullptr;
dludwig@8401
   974
    parameters.pScrollRect = nullptr;
dludwig@8401
   975
    parameters.pScrollOffset = nullptr;
dludwig@8401
   976
    
dludwig@8401
   977
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   978
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   979
    // frames that will never be displayed to the screen.
dludwig@8401
   980
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
   981
#endif
dludwig@8401
   982
dludwig@8401
   983
    // Discard the contents of the render target.
dludwig@8401
   984
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
   985
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8401
   986
    data->d3dContext->DiscardView(data->renderTargetView.Get());
dludwig@8401
   987
dludwig@8401
   988
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
   989
    // must recreate all device resources.
dludwig@8414
   990
    //
dludwig@8414
   991
    // 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
   992
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
   993
    {
dludwig@8414
   994
        hr = D3D11_HandleDeviceLost(renderer);
dludwig@8414
   995
        if (FAILED(hr)) {
dludwig@8414
   996
            WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8414
   997
        }
dludwig@8401
   998
    }
dludwig@8401
   999
    else
dludwig@8401
  1000
    {
dludwig@8401
  1001
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
  1002
    }
dludwig@8401
  1003
}
dludwig@8401
  1004
dludwig@8400
  1005
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
  1006
dludwig@8400
  1007
/* vi: set ts=4 sw=4 expandtab: */