src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 24 Mar 2013 21:57:40 -0400
changeset 8450 6f34a798cb54
parent 8449 2e4e5085e5b1
child 8451 092ea651523a
permissions -rw-r--r--
WinRT: added point drawing support to the Direct3D 11.1 rendering backend
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@8433
    33
#include "SDL_log.h"
dludwig@8433
    34
#include "../../video/SDL_sysvideo.h"
dludwig@8400
    35
//#include "stdio.h"
dludwig@8400
    36
}
dludwig@8400
    37
dludwig@8410
    38
#include <fstream>
dludwig@8410
    39
#include <string>
dludwig@8410
    40
#include <vector>
dludwig@8410
    41
dludwig@8400
    42
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    43
dludwig@8410
    44
using namespace DirectX;
dludwig@8410
    45
using namespace Microsoft::WRL;
dludwig@8410
    46
using namespace std;
dludwig@8410
    47
dludwig@8412
    48
#ifdef __WINRT__
dludwig@8412
    49
using namespace Windows::Graphics::Display;
dludwig@8412
    50
using namespace Windows::UI::Core;
dludwig@8412
    51
#endif
dludwig@8412
    52
dludwig@8410
    53
/* Direct3D 11.1 renderer implementation */
dludwig@8400
    54
dludwig@8400
    55
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8415
    56
static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8415
    57
                            const SDL_WindowEvent *event);
dludwig@8416
    58
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8416
    59
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    60
                             const SDL_Rect * rect, const void *pixels,
dludwig@8416
    61
                             int pitch);
dludwig@8400
    62
//static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    63
//                           const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8400
    64
//static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    65
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    66
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8416
    67
static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8450
    68
static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
    69
                                  const SDL_FPoint * points, int count);
dludwig@8449
    70
static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
    71
                                 const SDL_FPoint * points, int count);
dludwig@8429
    72
static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
    73
                                 const SDL_FRect * rects, int count);
dludwig@8416
    74
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    75
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8400
    76
//static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    77
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8400
    78
//                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8400
    79
//static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8400
    80
//                                Uint32 format, void * pixels, int pitch);
dludwig@8401
    81
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8416
    82
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
    83
                                 SDL_Texture * texture);
dludwig@8413
    84
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    85
dludwig@8410
    86
/* Direct3D 11.1 Internal Functions */
dludwig@8412
    87
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
    88
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8414
    89
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
dludwig@8414
    90
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
dludwig@8400
    91
dludwig@8442
    92
extern "C" SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    93
    D3D11_CreateRenderer,
dludwig@8400
    94
    {
dludwig@8442
    95
        "direct3d 11.1",
dludwig@8445
    96
        (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),    // flags.  see SDL_RendererFlags
dludwig@8442
    97
        2,                          // num_texture_formats
dludwig@8442
    98
        {                           // texture_formats
dludwig@8442
    99
            SDL_PIXELFORMAT_RGB888,
dludwig@8442
   100
            SDL_PIXELFORMAT_ARGB8888
dludwig@8442
   101
        },
dludwig@8446
   102
        0,                          // max_texture_width: will be filled in later
dludwig@8446
   103
        0                           // max_texture_height: will be filled in later
dludwig@8442
   104
    }
dludwig@8442
   105
};
dludwig@8442
   106
dludwig@8400
   107
dludwig@8400
   108
//typedef struct
dludwig@8400
   109
//{
dludwig@8400
   110
//    float x, y, z;
dludwig@8400
   111
//    DWORD color;
dludwig@8400
   112
//    float u, v;
dludwig@8400
   113
//} Vertex;
dludwig@8400
   114
dludwig@8400
   115
SDL_Renderer *
dludwig@8400
   116
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
   117
{
dludwig@8400
   118
    SDL_Renderer *renderer;
dludwig@8400
   119
    D3D11_RenderData *data;
dludwig@8400
   120
dludwig@8400
   121
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   122
    if (!renderer) {
dludwig@8400
   123
        SDL_OutOfMemory();
dludwig@8400
   124
        return NULL;
dludwig@8400
   125
    }
dludwig@8400
   126
    SDL_zerop(renderer);
dludwig@8400
   127
dludwig@8400
   128
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   129
    if (!data) {
dludwig@8400
   130
        SDL_OutOfMemory();
dludwig@8400
   131
        return NULL;
dludwig@8400
   132
    }
dludwig@8410
   133
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
dludwig@8410
   134
    data->loadingComplete = false;
dludwig@8412
   135
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
dludwig@8412
   136
    data->renderTargetSize = XMFLOAT2(0, 0);
dludwig@8400
   137
dludwig@8415
   138
    renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8416
   139
    renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8416
   140
    renderer->UpdateTexture = D3D11_UpdateTexture;
dludwig@8400
   141
    //renderer->LockTexture = D3D11_LockTexture;
dludwig@8400
   142
    //renderer->UnlockTexture = D3D11_UnlockTexture;
dludwig@8400
   143
    //renderer->SetRenderTarget = D3D11_SetRenderTarget;
dludwig@8400
   144
    renderer->UpdateViewport = D3D11_UpdateViewport;
dludwig@8416
   145
    renderer->RenderClear = D3D11_RenderClear;
dludwig@8450
   146
    renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
dludwig@8449
   147
    renderer->RenderDrawLines = D3D11_RenderDrawLines;
dludwig@8429
   148
    renderer->RenderFillRects = D3D11_RenderFillRects;
dludwig@8416
   149
    renderer->RenderCopy = D3D11_RenderCopy;
dludwig@8400
   150
    //renderer->RenderCopyEx = D3D11_RenderCopyEx;
dludwig@8400
   151
    //renderer->RenderReadPixels = D3D11_RenderReadPixels;
dludwig@8401
   152
    renderer->RenderPresent = D3D11_RenderPresent;
dludwig@8416
   153
    renderer->DestroyTexture = D3D11_DestroyTexture;
dludwig@8413
   154
    renderer->DestroyRenderer = D3D11_DestroyRenderer;
dludwig@8400
   155
    renderer->info = D3D11_RenderDriver.info;
dludwig@8413
   156
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
dludwig@8400
   157
    renderer->driverdata = data;
dludwig@8400
   158
dludwig@8413
   159
    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
dludwig@8413
   160
    // order to give init functions access to the underlying window handle:
dludwig@8413
   161
    renderer->window = window;
dludwig@8400
   162
dludwig@8413
   163
    /* Initialize Direct3D resources */
dludwig@8413
   164
    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
dludwig@8413
   165
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   166
        return NULL;
dludwig@8413
   167
    }
dludwig@8413
   168
    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
dludwig@8413
   169
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   170
        return NULL;
dludwig@8413
   171
    }
dludwig@8400
   172
dludwig@8416
   173
    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
dludwig@8416
   174
dludwig@8400
   175
    return renderer;
dludwig@8400
   176
}
dludwig@8400
   177
dludwig@8413
   178
static void
dludwig@8413
   179
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   180
{
dludwig@8413
   181
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   182
    if (data) {
dludwig@8413
   183
        delete data;
dludwig@8413
   184
        data = NULL;
dludwig@8413
   185
    }
dludwig@8413
   186
}
dludwig@8413
   187
dludwig@8410
   188
static bool
dludwig@8410
   189
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
dludwig@8410
   190
{
dludwig@8410
   191
    ifstream in(fileName, ios::in | ios::binary);
dludwig@8410
   192
    if (!in) {
dludwig@8410
   193
        return false;
dludwig@8410
   194
    }
dludwig@8410
   195
dludwig@8410
   196
    in.seekg(0, ios::end);
dludwig@8410
   197
    out.resize((size_t) in.tellg());
dludwig@8410
   198
    in.seekg(0, ios::beg);
dludwig@8410
   199
    in.read(&out[0], out.size());
dludwig@8410
   200
    return in.good();
dludwig@8410
   201
}
dludwig@8410
   202
dludwig@8410
   203
static bool
dludwig@8410
   204
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
dludwig@8410
   205
{
dludwig@8410
   206
    wstring fileName;
dludwig@8410
   207
dludwig@8410
   208
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8420
   209
    fileName = SDL_WinRTGetFileSystemPath(SDL_WINRT_PATH_INSTALLED_LOCATION);
dludwig@8410
   210
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   211
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8420
   212
    fileName = SDL_WinRTGetFileSystemPath(SDL_WINRT_PATH_INSTALLED_LOCATION);
dludwig@8410
   213
    fileName += L"\\";
dludwig@8410
   214
#endif
dludwig@8410
   215
    // WinRT, TODO: test Direct3D 11.1 shader loading on Win32
dludwig@8410
   216
    fileName += shaderName;
dludwig@8410
   217
    return D3D11_ReadFileContents(fileName, out);
dludwig@8410
   218
}
dludwig@8410
   219
dludwig@8429
   220
static HRESULT
dludwig@8429
   221
D3D11_LoadPixelShader(SDL_Renderer * renderer,
dludwig@8429
   222
                      const wstring & shaderName,
dludwig@8429
   223
                      ID3D11PixelShader ** shaderOutput)
dludwig@8429
   224
{
dludwig@8429
   225
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
   226
    HRESULT result = S_OK;
dludwig@8429
   227
    vector<char> fileData;
dludwig@8429
   228
    
dludwig@8429
   229
    if (!D3D11_ReadShaderContents(shaderName, fileData)) {
dludwig@8429
   230
        SDL_SetError("Unable to open SDL's pixel shader file.");
dludwig@8429
   231
        return E_FAIL;
dludwig@8429
   232
    }
dludwig@8429
   233
dludwig@8429
   234
    result = data->d3dDevice->CreatePixelShader(
dludwig@8429
   235
        &fileData[0],
dludwig@8429
   236
        fileData.size(),
dludwig@8429
   237
        nullptr,
dludwig@8429
   238
        shaderOutput
dludwig@8429
   239
        );
dludwig@8429
   240
    if (FAILED(result)) {
dludwig@8429
   241
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8429
   242
        return result;
dludwig@8429
   243
    }
dludwig@8429
   244
dludwig@8429
   245
    return S_OK;
dludwig@8429
   246
}
dludwig@8429
   247
dludwig@8431
   248
static HRESULT
dludwig@8431
   249
D3D11_CreateBlendMode(SDL_Renderer * renderer,
dludwig@8431
   250
                      BOOL enableBlending,
dludwig@8431
   251
                      D3D11_BLEND srcBlend,
dludwig@8431
   252
                      D3D11_BLEND destBlend,
dludwig@8431
   253
                      ID3D11BlendState ** blendStateOutput)
dludwig@8431
   254
{
dludwig@8431
   255
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8431
   256
    HRESULT result = S_OK;
dludwig@8431
   257
dludwig@8431
   258
    D3D11_BLEND_DESC blendDesc;
dludwig@8431
   259
    memset(&blendDesc, 0, sizeof(blendDesc));
dludwig@8431
   260
    blendDesc.AlphaToCoverageEnable = FALSE;
dludwig@8431
   261
    blendDesc.IndependentBlendEnable = FALSE;
dludwig@8431
   262
    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
dludwig@8431
   263
    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
dludwig@8431
   264
    blendDesc.RenderTarget[0].DestBlend = destBlend;
dludwig@8431
   265
    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
dludwig@8431
   266
    blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
dludwig@8431
   267
    blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
dludwig@8431
   268
    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
dludwig@8431
   269
    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
dludwig@8431
   270
    result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
dludwig@8431
   271
    if (FAILED(result)) {
dludwig@8431
   272
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8431
   273
        return result;
dludwig@8431
   274
    }
dludwig@8431
   275
dludwig@8431
   276
    return S_OK;
dludwig@8431
   277
}
dludwig@8431
   278
dludwig@8414
   279
// Create resources that depend on the device.
dludwig@8410
   280
HRESULT
dludwig@8412
   281
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
dludwig@8410
   282
{
dludwig@8410
   283
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8410
   284
dludwig@8410
   285
    // This flag adds support for surfaces with a different color channel ordering
dludwig@8410
   286
    // than the API default. It is required for compatibility with Direct2D.
dludwig@8410
   287
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
dludwig@8410
   288
dludwig@8410
   289
#if defined(_DEBUG)
dludwig@8410
   290
    // If the project is in a debug build, enable debugging via SDK Layers with this flag.
dludwig@8410
   291
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8410
   292
#endif
dludwig@8410
   293
dludwig@8410
   294
    // This array defines the set of DirectX hardware feature levels this app will support.
dludwig@8410
   295
    // Note the ordering should be preserved.
dludwig@8410
   296
    // Don't forget to declare your application's minimum required feature level in its
dludwig@8410
   297
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
dludwig@8410
   298
    D3D_FEATURE_LEVEL featureLevels[] = 
dludwig@8410
   299
    {
dludwig@8410
   300
        D3D_FEATURE_LEVEL_11_1,
dludwig@8410
   301
        D3D_FEATURE_LEVEL_11_0,
dludwig@8410
   302
        D3D_FEATURE_LEVEL_10_1,
dludwig@8410
   303
        D3D_FEATURE_LEVEL_10_0,
dludwig@8410
   304
        D3D_FEATURE_LEVEL_9_3,
dludwig@8410
   305
        D3D_FEATURE_LEVEL_9_2,
dludwig@8410
   306
        D3D_FEATURE_LEVEL_9_1
dludwig@8410
   307
    };
dludwig@8410
   308
dludwig@8410
   309
    // Create the Direct3D 11 API device object and a corresponding context.
dludwig@8410
   310
    ComPtr<ID3D11Device> device;
dludwig@8410
   311
    ComPtr<ID3D11DeviceContext> context;
dludwig@8410
   312
    HRESULT result = S_OK;
dludwig@8410
   313
    result = D3D11CreateDevice(
dludwig@8410
   314
        nullptr, // Specify nullptr to use the default adapter.
dludwig@8410
   315
        D3D_DRIVER_TYPE_HARDWARE,
dludwig@8410
   316
        nullptr,
dludwig@8410
   317
        creationFlags, // Set set debug and Direct2D compatibility flags.
dludwig@8410
   318
        featureLevels, // List of feature levels this app can support.
dludwig@8410
   319
        ARRAYSIZE(featureLevels),
dludwig@8410
   320
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
dludwig@8410
   321
        &device, // Returns the Direct3D device created.
dludwig@8410
   322
        &data->featureLevel, // Returns feature level of device created.
dludwig@8410
   323
        &context // Returns the device immediate context.
dludwig@8410
   324
        );
dludwig@8410
   325
    if (FAILED(result)) {
dludwig@8410
   326
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   327
        return result;
dludwig@8410
   328
    }
dludwig@8410
   329
dludwig@8410
   330
    // Get the Direct3D 11.1 API device and context interfaces.
dludwig@8410
   331
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
dludwig@8410
   332
    result = device.As(&(data->d3dDevice));
dludwig@8410
   333
    if (FAILED(result)) {
dludwig@8410
   334
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   335
        return result;
dludwig@8410
   336
    }
dludwig@8410
   337
dludwig@8410
   338
    result = context.As(&data->d3dContext);
dludwig@8410
   339
    if (FAILED(result)) {
dludwig@8410
   340
        return result;
dludwig@8410
   341
    }
dludwig@8410
   342
dludwig@8410
   343
    //
dludwig@8446
   344
    // Make note of the maximum texture size
dludwig@8446
   345
    // Max texture sizes are documented on MSDN, at:
dludwig@8446
   346
    // http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
dludwig@8446
   347
    //
dludwig@8446
   348
    switch (data->d3dDevice->GetFeatureLevel()) {
dludwig@8446
   349
        case D3D_FEATURE_LEVEL_11_1:
dludwig@8446
   350
        case D3D_FEATURE_LEVEL_11_0:
dludwig@8446
   351
            renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
dludwig@8446
   352
            break;
dludwig@8446
   353
dludwig@8446
   354
        case D3D_FEATURE_LEVEL_10_1:
dludwig@8446
   355
        case D3D_FEATURE_LEVEL_10_0:
dludwig@8446
   356
            renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
dludwig@8446
   357
            break;
dludwig@8446
   358
dludwig@8446
   359
        case D3D_FEATURE_LEVEL_9_3:
dludwig@8446
   360
            renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
dludwig@8446
   361
            break;
dludwig@8446
   362
dludwig@8446
   363
        case D3D_FEATURE_LEVEL_9_2:
dludwig@8446
   364
        case D3D_FEATURE_LEVEL_9_1:
dludwig@8446
   365
            renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
dludwig@8446
   366
            break;
dludwig@8446
   367
    }
dludwig@8446
   368
dludwig@8446
   369
    //
dludwig@8410
   370
    // Load in SDL's one and only vertex shader:
dludwig@8410
   371
    //
dludwig@8429
   372
    vector<char> fileData;
dludwig@8444
   373
    if (!D3D11_ReadShaderContents(L"SDL_D3D11_VertexShader_Default.cso", fileData)) {
dludwig@8410
   374
        SDL_SetError("Unable to open SDL's vertex shader file.");
dludwig@8410
   375
        return E_FAIL;
dludwig@8410
   376
    }
dludwig@8410
   377
dludwig@8410
   378
    result = data->d3dDevice->CreateVertexShader(
dludwig@8410
   379
        &fileData[0],
dludwig@8410
   380
        fileData.size(),
dludwig@8410
   381
        nullptr,
dludwig@8410
   382
        &data->vertexShader
dludwig@8410
   383
        );
dludwig@8410
   384
    if (FAILED(result)) {
dludwig@8410
   385
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   386
        return result;
dludwig@8410
   387
    }
dludwig@8410
   388
dludwig@8410
   389
    //
dludwig@8410
   390
    // Create an input layout for SDL's vertex shader:
dludwig@8410
   391
    //
dludwig@8410
   392
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8410
   393
    {
dludwig@8429
   394
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   395
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8429
   396
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   397
    };
dludwig@8410
   398
dludwig@8410
   399
    result = data->d3dDevice->CreateInputLayout(
dludwig@8410
   400
        vertexDesc,
dludwig@8410
   401
        ARRAYSIZE(vertexDesc),
dludwig@8410
   402
        &fileData[0],
dludwig@8410
   403
        fileData.size(),
dludwig@8410
   404
        &data->inputLayout
dludwig@8410
   405
        );
dludwig@8410
   406
    if (FAILED(result)) {
dludwig@8410
   407
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   408
        return result;
dludwig@8410
   409
    }
dludwig@8410
   410
dludwig@8410
   411
    //
dludwig@8429
   412
    // Load in SDL's pixel shaders
dludwig@8410
   413
    //
dludwig@8429
   414
    result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_TextureCopy.cso", &data->texturePixelShader);
dludwig@8429
   415
    if (FAILED(result)) {
dludwig@8429
   416
        // D3D11_LoadPixelShader will have aleady set the SDL error
dludwig@8429
   417
        return result;
dludwig@8410
   418
    }
dludwig@8410
   419
dludwig@8429
   420
    result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_FixedColor.cso", &data->colorPixelShader);
dludwig@8410
   421
    if (FAILED(result)) {
dludwig@8429
   422
        // D3D11_LoadPixelShader will have aleady set the SDL error
dludwig@8410
   423
        return result;
dludwig@8410
   424
    }
dludwig@8410
   425
dludwig@8410
   426
    //
dludwig@8418
   427
    // Setup space to hold vertex shader constants:
dludwig@8418
   428
    //
dludwig@8418
   429
    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
dludwig@8418
   430
    result = data->d3dDevice->CreateBuffer(
dludwig@8418
   431
		&constantBufferDesc,
dludwig@8418
   432
		nullptr,
dludwig@8418
   433
        &data->vertexShaderConstants
dludwig@8418
   434
		);
dludwig@8418
   435
    if (FAILED(result)) {
dludwig@8418
   436
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8418
   437
        return result;
dludwig@8418
   438
    }
dludwig@8418
   439
dludwig@8418
   440
    //
dludwig@8425
   441
    // Make sure that the vertex buffer, if already created, gets freed.
dludwig@8425
   442
    // It will be recreated later.
dludwig@8410
   443
    //
dludwig@8425
   444
    data->vertexBuffer = nullptr;
dludwig@8410
   445
dludwig@8410
   446
    //
dludwig@8410
   447
    // Create a sampler to use when drawing textures:
dludwig@8410
   448
    //
dludwig@8410
   449
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   450
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   451
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   452
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   453
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   454
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   455
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   456
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   457
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   458
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   459
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   460
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   461
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   462
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   463
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   464
        &samplerDesc,
dludwig@8410
   465
        &data->mainSampler
dludwig@8410
   466
        );
dludwig@8410
   467
    if (FAILED(result)) {
dludwig@8410
   468
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   469
        return result;
dludwig@8410
   470
    }
dludwig@8410
   471
dludwig@8410
   472
    //
dludwig@8426
   473
    // Setup the Direct3D rasterizer
dludwig@8426
   474
    //
dludwig@8426
   475
    D3D11_RASTERIZER_DESC rasterDesc;
dludwig@8426
   476
    memset(&rasterDesc, 0, sizeof(rasterDesc));
dludwig@8426
   477
	rasterDesc.AntialiasedLineEnable = false;
dludwig@8426
   478
	rasterDesc.CullMode = D3D11_CULL_NONE;
dludwig@8426
   479
	rasterDesc.DepthBias = 0;
dludwig@8426
   480
	rasterDesc.DepthBiasClamp = 0.0f;
dludwig@8426
   481
	rasterDesc.DepthClipEnable = true;
dludwig@8426
   482
	rasterDesc.FillMode = D3D11_FILL_SOLID;
dludwig@8426
   483
	rasterDesc.FrontCounterClockwise = false;
dludwig@8426
   484
	rasterDesc.MultisampleEnable = false;
dludwig@8426
   485
	rasterDesc.ScissorEnable = false;
dludwig@8426
   486
	rasterDesc.SlopeScaledDepthBias = 0.0f;
dludwig@8426
   487
	result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
dludwig@8426
   488
	if (FAILED(result)) {
dludwig@8426
   489
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8426
   490
        return result;
dludwig@8426
   491
    }
dludwig@8426
   492
dludwig@8426
   493
    //
dludwig@8431
   494
    // Create blending states:
dludwig@8431
   495
    //
dludwig@8431
   496
    result = D3D11_CreateBlendMode(
dludwig@8431
   497
        renderer,
dludwig@8431
   498
        TRUE,
dludwig@8431
   499
        D3D11_BLEND_SRC_ALPHA,
dludwig@8431
   500
        D3D11_BLEND_INV_SRC_ALPHA,
dludwig@8431
   501
        &data->blendModeBlend);
dludwig@8431
   502
    if (FAILED(result)) {
dludwig@8431
   503
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   504
        return result;
dludwig@8431
   505
    }
dludwig@8431
   506
dludwig@8431
   507
    result = D3D11_CreateBlendMode(
dludwig@8431
   508
        renderer,
dludwig@8431
   509
        TRUE,
dludwig@8431
   510
        D3D11_BLEND_SRC_ALPHA,
dludwig@8431
   511
        D3D11_BLEND_ONE,
dludwig@8431
   512
        &data->blendModeAdd);
dludwig@8431
   513
    if (FAILED(result)) {
dludwig@8431
   514
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   515
        return result;
dludwig@8431
   516
    }
dludwig@8431
   517
dludwig@8431
   518
    result = D3D11_CreateBlendMode(
dludwig@8431
   519
        renderer,
dludwig@8431
   520
        TRUE,
dludwig@8431
   521
        D3D11_BLEND_ZERO,
dludwig@8431
   522
        D3D11_BLEND_SRC_COLOR,
dludwig@8431
   523
        &data->blendModeMod);
dludwig@8431
   524
    if (FAILED(result)) {
dludwig@8431
   525
        // D3D11_CreateBlendMode will set the SDL error, if it fails
dludwig@8431
   526
        return result;
dludwig@8431
   527
    }
dludwig@8431
   528
dludwig@8431
   529
    //
dludwig@8410
   530
    // All done!
dludwig@8410
   531
    //
dludwig@8410
   532
    data->loadingComplete = true;       // This variable can probably be factored-out
dludwig@8410
   533
    return S_OK;
dludwig@8410
   534
}
dludwig@8410
   535
dludwig@8412
   536
#ifdef __WINRT__
dludwig@8412
   537
dludwig@8415
   538
static CoreWindow ^
dludwig@8412
   539
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   540
{
dludwig@8412
   541
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   542
    if ( ! renderer->window ) {
dludwig@8412
   543
        return nullptr;
dludwig@8412
   544
    }
dludwig@8412
   545
dludwig@8412
   546
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   547
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   548
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   549
        return nullptr;
dludwig@8412
   550
    }
dludwig@8412
   551
dludwig@8412
   552
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   553
        return nullptr;
dludwig@8412
   554
    }
dludwig@8412
   555
dludwig@8412
   556
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   557
    if ( ! coreWindowPointer ) {
dludwig@8412
   558
        return nullptr;
dludwig@8412
   559
    }
dludwig@8412
   560
dludwig@8412
   561
    return *coreWindowPointer;
dludwig@8412
   562
}
dludwig@8412
   563
dludwig@8414
   564
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
dludwig@8412
   565
static float
dludwig@8412
   566
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   567
{
dludwig@8412
   568
    static const float dipsPerInch = 96.0f;
dludwig@8412
   569
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   570
}
dludwig@8412
   571
#endif
dludwig@8412
   572
dludwig@8414
   573
// Initialize all resources that change when the window's size changes.
dludwig@8412
   574
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   575
HRESULT
dludwig@8412
   576
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   577
{
dludwig@8412
   578
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   579
    HRESULT result = S_OK;
dludwig@8412
   580
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   581
dludwig@8412
   582
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   583
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   584
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   585
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   586
dludwig@8412
   587
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   588
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   589
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   590
dludwig@8412
   591
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   592
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   593
    // orientation, the dimensions must be reversed.
dludwig@8412
   594
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8433
   595
    const bool swapDimensions =
dludwig@8412
   596
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   597
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   598
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   599
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   600
dludwig@8412
   601
    if(data->swapChain != nullptr)
dludwig@8412
   602
    {
dludwig@8412
   603
        // If the swap chain already exists, resize it.
dludwig@8412
   604
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   605
            2, // Double-buffered swap chain.
dludwig@8412
   606
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   607
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   608
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   609
            0
dludwig@8412
   610
            );
dludwig@8412
   611
        if (FAILED(result)) {
dludwig@8412
   612
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   613
            return result;
dludwig@8412
   614
        }
dludwig@8412
   615
    }
dludwig@8412
   616
    else
dludwig@8412
   617
    {
dludwig@8412
   618
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   619
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   620
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   621
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   622
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   623
        swapChainDesc.Stereo = false;
dludwig@8412
   624
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   625
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   626
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   627
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   628
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   629
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   630
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   631
#else
dludwig@8412
   632
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   633
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   634
#endif
dludwig@8412
   635
        swapChainDesc.Flags = 0;
dludwig@8412
   636
dludwig@8412
   637
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   638
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   639
        if (FAILED(result)) {
dludwig@8412
   640
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   641
            return result;
dludwig@8412
   642
        }
dludwig@8412
   643
dludwig@8412
   644
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   645
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   646
        if (FAILED(result)) {
dludwig@8412
   647
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   648
            return result;
dludwig@8412
   649
        }
dludwig@8412
   650
dludwig@8412
   651
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   652
        result = dxgiAdapter->GetParent(
dludwig@8412
   653
            __uuidof(IDXGIFactory2), 
dludwig@8412
   654
            &dxgiFactory
dludwig@8412
   655
            );
dludwig@8412
   656
        if (FAILED(result)) {
dludwig@8412
   657
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   658
            return result;
dludwig@8412
   659
        }
dludwig@8412
   660
dludwig@8412
   661
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   662
            data->d3dDevice.Get(),
dludwig@8412
   663
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   664
            &swapChainDesc,
dludwig@8412
   665
            nullptr, // Allow on all displays.
dludwig@8412
   666
            &data->swapChain
dludwig@8412
   667
            );
dludwig@8412
   668
        if (FAILED(result)) {
dludwig@8412
   669
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   670
            return result;
dludwig@8412
   671
        }
dludwig@8412
   672
            
dludwig@8412
   673
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   674
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   675
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   676
        if (FAILED(result)) {
dludwig@8412
   677
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   678
            return result;
dludwig@8412
   679
        }
dludwig@8412
   680
    }
dludwig@8412
   681
    
dludwig@8412
   682
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   683
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   684
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   685
    switch (data->orientation)
dludwig@8412
   686
    {
dludwig@8412
   687
        case DisplayOrientations::Landscape:
dludwig@8412
   688
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8412
   689
            break;
dludwig@8412
   690
dludwig@8412
   691
        case DisplayOrientations::Portrait:
dludwig@8412
   692
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8412
   693
            break;
dludwig@8412
   694
dludwig@8412
   695
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   696
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8412
   697
            break;
dludwig@8412
   698
dludwig@8412
   699
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   700
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8412
   701
            break;
dludwig@8412
   702
dludwig@8412
   703
        default:
dludwig@8412
   704
            throw ref new Platform::FailureException();
dludwig@8412
   705
    }
dludwig@8412
   706
dludwig@8412
   707
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   708
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   709
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   710
    if (FAILED(result)) {
dludwig@8412
   711
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   712
        return result;
dludwig@8412
   713
    }
dludwig@8412
   714
#endif
dludwig@8412
   715
dludwig@8412
   716
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   717
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   718
    result = data->swapChain->GetBuffer(
dludwig@8412
   719
        0,
dludwig@8412
   720
        __uuidof(ID3D11Texture2D),
dludwig@8412
   721
        &backBuffer
dludwig@8412
   722
        );
dludwig@8412
   723
    if (FAILED(result)) {
dludwig@8412
   724
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   725
        return result;
dludwig@8412
   726
    }
dludwig@8412
   727
dludwig@8412
   728
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   729
        backBuffer.Get(),
dludwig@8412
   730
        nullptr,
dludwig@8412
   731
        &data->renderTargetView
dludwig@8412
   732
        );
dludwig@8412
   733
    if (FAILED(result)) {
dludwig@8412
   734
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   735
        return result;
dludwig@8412
   736
    }
dludwig@8412
   737
dludwig@8432
   738
    if (D3D11_UpdateViewport(renderer) != 0) {
dludwig@8432
   739
        // D3D11_UpdateViewport will set the SDL error if it fails.
dludwig@8432
   740
        return E_FAIL;
dludwig@8432
   741
    }
dludwig@8412
   742
dludwig@8412
   743
    return S_OK;
dludwig@8412
   744
}
dludwig@8412
   745
dludwig@8415
   746
// This method is called when the window's size changes.
dludwig@8414
   747
HRESULT
dludwig@8414
   748
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
dludwig@8414
   749
{
dludwig@8414
   750
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   751
    HRESULT result = S_OK;
dludwig@8414
   752
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8414
   753
dludwig@8414
   754
    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
dludwig@8414
   755
        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
dludwig@8414
   756
        data->orientation != DisplayProperties::CurrentOrientation)
dludwig@8414
   757
    {
dludwig@8414
   758
        ID3D11RenderTargetView* nullViews[] = {nullptr};
dludwig@8414
   759
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
dludwig@8414
   760
        data->renderTargetView = nullptr;
dludwig@8414
   761
        data->d3dContext->Flush();
dludwig@8414
   762
        result = D3D11_CreateWindowSizeDependentResources(renderer);
dludwig@8414
   763
        if (FAILED(result)) {
dludwig@8414
   764
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   765
            return result;
dludwig@8414
   766
        }
dludwig@8414
   767
    }
dludwig@8414
   768
dludwig@8414
   769
    return S_OK;
dludwig@8414
   770
}
dludwig@8414
   771
dludwig@8414
   772
HRESULT
dludwig@8414
   773
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
   774
{
dludwig@8414
   775
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   776
    HRESULT result = S_OK;
dludwig@8414
   777
dludwig@8415
   778
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
dludwig@8414
   779
    data->windowSizeInDIPs.x = 0;
dludwig@8414
   780
    data->windowSizeInDIPs.y = 0;
dludwig@8414
   781
    data->swapChain = nullptr;
dludwig@8414
   782
dludwig@8414
   783
    result = D3D11_CreateDeviceResources(renderer);
dludwig@8414
   784
    if (FAILED(result)) {
dludwig@8414
   785
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   786
        return result;
dludwig@8414
   787
    }
dludwig@8414
   788
dludwig@8414
   789
    result = D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8414
   790
    if (FAILED(result)) {
dludwig@8414
   791
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   792
        return result;
dludwig@8414
   793
    }
dludwig@8414
   794
dludwig@8414
   795
    return S_OK;
dludwig@8414
   796
}
dludwig@8414
   797
dludwig@8415
   798
static void
dludwig@8415
   799
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
   800
{
dludwig@8415
   801
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8415
   802
dludwig@8415
   803
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
dludwig@8415
   804
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
   805
    }
dludwig@8415
   806
}
dludwig@8415
   807
dludwig@8416
   808
static int
dludwig@8416
   809
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8416
   810
{
dludwig@8416
   811
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   812
    D3D11_TextureData *textureData;
dludwig@8416
   813
    HRESULT result;
dludwig@8442
   814
    DXGI_FORMAT textureFormat = DXGI_FORMAT_UNKNOWN;
dludwig@8442
   815
dludwig@8442
   816
    switch (texture->format) {
dludwig@8442
   817
        case SDL_PIXELFORMAT_ARGB8888:
dludwig@8442
   818
            textureFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@8442
   819
            break;
dludwig@8442
   820
        case SDL_PIXELFORMAT_RGB888:
dludwig@8442
   821
            textureFormat = DXGI_FORMAT_B8G8R8X8_UNORM;
dludwig@8442
   822
            break;
dludwig@8442
   823
        default:
dludwig@8442
   824
            SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
dludwig@8442
   825
                __FUNCTION__, texture->format);
dludwig@8442
   826
            return -1;
dludwig@8442
   827
    }
dludwig@8416
   828
dludwig@8416
   829
    textureData = new D3D11_TextureData;
dludwig@8416
   830
    if (!textureData) {
dludwig@8416
   831
        SDL_OutOfMemory();
dludwig@8416
   832
        return -1;
dludwig@8416
   833
    }
dludwig@8416
   834
    textureData->pixelFormat = SDL_AllocFormat(texture->format);
dludwig@8416
   835
dludwig@8416
   836
    texture->driverdata = textureData;
dludwig@8416
   837
dludwig@8416
   838
    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
dludwig@8416
   839
dludwig@8416
   840
    D3D11_TEXTURE2D_DESC textureDesc = {0};
dludwig@8416
   841
    textureDesc.Width = texture->w;
dludwig@8416
   842
    textureDesc.Height = texture->h;
dludwig@8416
   843
    textureDesc.MipLevels = 1;
dludwig@8416
   844
    textureDesc.ArraySize = 1;
dludwig@8442
   845
    textureDesc.Format = textureFormat;
dludwig@8416
   846
    textureDesc.SampleDesc.Count = 1;
dludwig@8416
   847
    textureDesc.SampleDesc.Quality = 0;
dludwig@8416
   848
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;
dludwig@8416
   849
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dludwig@8416
   850
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8416
   851
    textureDesc.MiscFlags = 0;
dludwig@8416
   852
dludwig@8416
   853
    const int numPixels = textureDesc.Width * textureDesc.Height;
dludwig@8416
   854
    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
dludwig@8416
   855
dludwig@8416
   856
    // Fill the texture with a non-black color, for debugging purposes:
dludwig@8416
   857
    //for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
dludwig@8416
   858
    //    initialTexturePixels[i+0] = 0xff;
dludwig@8416
   859
    //    initialTexturePixels[i+1] = 0xff;
dludwig@8416
   860
    //    initialTexturePixels[i+2] = 0x00;
dludwig@8416
   861
    //    initialTexturePixels[i+3] = 0xff;
dludwig@8416
   862
    //}
dludwig@8416
   863
dludwig@8416
   864
    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
dludwig@8416
   865
    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
dludwig@8416
   866
    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
dludwig@8416
   867
    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
dludwig@8416
   868
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8416
   869
        &textureDesc,
dludwig@8416
   870
        &initialTextureData,
dludwig@8416
   871
        &textureData->mainTexture
dludwig@8416
   872
        );
dludwig@8416
   873
    if (FAILED(result)) {
dludwig@8416
   874
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   875
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   876
        return -1;
dludwig@8416
   877
    }
dludwig@8416
   878
dludwig@8416
   879
    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
dludwig@8416
   880
    resourceViewDesc.Format = textureDesc.Format;
dludwig@8416
   881
    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dludwig@8416
   882
    resourceViewDesc.Texture2D.MostDetailedMip = 0;
dludwig@8416
   883
    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
dludwig@8416
   884
    result = rendererData->d3dDevice->CreateShaderResourceView(
dludwig@8416
   885
        textureData->mainTexture.Get(),
dludwig@8416
   886
        &resourceViewDesc,
dludwig@8416
   887
        &textureData->mainTextureResourceView
dludwig@8416
   888
        );
dludwig@8416
   889
    if (FAILED(result)) {
dludwig@8416
   890
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   891
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   892
        return -1;
dludwig@8416
   893
    }
dludwig@8416
   894
dludwig@8416
   895
    return 0;
dludwig@8416
   896
}
dludwig@8416
   897
dludwig@8416
   898
static void
dludwig@8416
   899
D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
   900
                     SDL_Texture * texture)
dludwig@8416
   901
{
dludwig@8416
   902
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   903
dludwig@8416
   904
    if (textureData) {
dludwig@8416
   905
        if (textureData->pixelFormat) {
dludwig@8416
   906
            SDL_FreeFormat(textureData->pixelFormat);
dludwig@8416
   907
            textureData->pixelFormat = NULL;
dludwig@8416
   908
        }
dludwig@8416
   909
dludwig@8416
   910
        delete textureData;
dludwig@8416
   911
        texture->driverdata = NULL;
dludwig@8416
   912
    }
dludwig@8416
   913
}
dludwig@8416
   914
dludwig@8416
   915
static int
dludwig@8416
   916
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   917
                    const SDL_Rect * rect, const void *pixels,
dludwig@8416
   918
                    int pitch)
dludwig@8416
   919
{
dludwig@8416
   920
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   921
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   922
    HRESULT result = S_OK;
dludwig@8416
   923
dludwig@8416
   924
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8416
   925
    result = rendererData->d3dContext->Map(
dludwig@8416
   926
        textureData->mainTexture.Get(),
dludwig@8416
   927
        0,
dludwig@8416
   928
        D3D11_MAP_WRITE_DISCARD,
dludwig@8416
   929
        0,
dludwig@8416
   930
        &textureMemory
dludwig@8416
   931
        );
dludwig@8416
   932
    if (FAILED(result)) {
dludwig@8416
   933
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   934
        return -1;
dludwig@8416
   935
    }
dludwig@8416
   936
dludwig@8416
   937
    // Copy pixel data to the locked texture's memory:
dludwig@8416
   938
    for (int y = 0; y < rect->h; ++y) {
dludwig@8416
   939
        memcpy(
dludwig@8416
   940
            ((Uint8 *)textureMemory.pData) + (textureMemory.RowPitch * y),
dludwig@8416
   941
            ((Uint8 *)pixels) + (pitch * y),
dludwig@8416
   942
            pitch
dludwig@8416
   943
            );
dludwig@8416
   944
    }
dludwig@8416
   945
dludwig@8416
   946
    // Clean up a bit, then commit the texture's memory back to Direct3D:
dludwig@8416
   947
    rendererData->d3dContext->Unmap(
dludwig@8416
   948
        textureData->mainTexture.Get(),
dludwig@8416
   949
        0);
dludwig@8416
   950
dludwig@8416
   951
    return 0;
dludwig@8416
   952
}
dludwig@8416
   953
dludwig@8400
   954
static int
dludwig@8400
   955
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   956
{
dludwig@8432
   957
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8432
   958
dludwig@8432
   959
    if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
dludwig@8432
   960
        // If the viewport is empty, assume that it is because
dludwig@8432
   961
        // SDL_CreateRenderer is calling it, and will call it again later
dludwig@8432
   962
        // with a non-empty viewport.
dludwig@8432
   963
        return 0;
dludwig@8432
   964
    }
dludwig@8432
   965
dludwig@8432
   966
    switch (data->orientation)
dludwig@8432
   967
    {
dludwig@8432
   968
        case DisplayOrientations::Landscape:
dludwig@8432
   969
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8432
   970
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
   971
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8432
   972
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
   973
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
   974
                );
dludwig@8432
   975
            break;
dludwig@8432
   976
dludwig@8432
   977
        case DisplayOrientations::Portrait:
dludwig@8433
   978
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8433
   979
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8433
   980
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
   981
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
   982
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
   983
                );
dludwig@8432
   984
            break;
dludwig@8432
   985
dludwig@8432
   986
        case DisplayOrientations::LandscapeFlipped:
dludwig@8432
   987
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8432
   988
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
   989
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8432
   990
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
   991
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
   992
                );
dludwig@8432
   993
            break;
dludwig@8432
   994
dludwig@8432
   995
        case DisplayOrientations::PortraitFlipped:
dludwig@8433
   996
            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8433
   997
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8433
   998
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8432
   999
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8432
  1000
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8432
  1001
                );
dludwig@8432
  1002
            break;
dludwig@8432
  1003
dludwig@8432
  1004
        default:
dludwig@8432
  1005
            SDL_SetError("An unknown DisplayOrientation is being used");
dludwig@8432
  1006
            return -1;
dludwig@8432
  1007
    }
dludwig@8432
  1008
dludwig@8432
  1009
    //
dludwig@8432
  1010
    // Update the view matrix
dludwig@8432
  1011
    //
dludwig@8433
  1012
    float viewportWidth = (float) renderer->viewport.w;
dludwig@8433
  1013
    float viewportHeight = (float) renderer->viewport.h;
dludwig@8433
  1014
    XMStoreFloat4x4(&data->vertexShaderConstantsData.view,
dludwig@8432
  1015
        XMMatrixMultiply(
dludwig@8433
  1016
            XMMatrixScaling(2.0f / viewportWidth, 2.0f / viewportHeight, 1.0f),
dludwig@8432
  1017
            XMMatrixMultiply(
dludwig@8432
  1018
                XMMatrixTranslation(-1, -1, 0),
dludwig@8432
  1019
                XMMatrixRotationX(XM_PI)
dludwig@8432
  1020
                )));
dludwig@8434
  1021
#if 0
dludwig@8434
  1022
    data->vertexShaderConstantsData.view = XMMatrixIdentity();
dludwig@8434
  1023
#endif
dludwig@8433
  1024
dludwig@8433
  1025
    //
dludwig@8433
  1026
    // Update the Direct3D viewport, which seems to be aligned to the
dludwig@8433
  1027
    // swap buffer's coordinate space, which is always in landscape:
dludwig@8433
  1028
    //
dludwig@8433
  1029
    SDL_FRect orientationAlignedViewport;
dludwig@8433
  1030
    const bool swapDimensions =
dludwig@8433
  1031
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8433
  1032
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8433
  1033
    if (swapDimensions) {
dludwig@8433
  1034
        orientationAlignedViewport.x = (float) renderer->viewport.y;
dludwig@8433
  1035
        orientationAlignedViewport.y = (float) renderer->viewport.x;
dludwig@8433
  1036
        orientationAlignedViewport.w = (float) renderer->viewport.h;
dludwig@8433
  1037
        orientationAlignedViewport.h = (float) renderer->viewport.w;
dludwig@8433
  1038
    } else {
dludwig@8433
  1039
        orientationAlignedViewport.x = (float) renderer->viewport.x;
dludwig@8433
  1040
        orientationAlignedViewport.y = (float) renderer->viewport.y;
dludwig@8433
  1041
        orientationAlignedViewport.w = (float) renderer->viewport.w;
dludwig@8433
  1042
        orientationAlignedViewport.h = (float) renderer->viewport.h;
dludwig@8433
  1043
    }
dludwig@8433
  1044
    // WinRT, TODO: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped)
dludwig@8432
  1045
dludwig@8432
  1046
    D3D11_VIEWPORT viewport;
dludwig@8432
  1047
    memset(&viewport, 0, sizeof(viewport));
dludwig@8433
  1048
    viewport.TopLeftX = orientationAlignedViewport.x;
dludwig@8433
  1049
    viewport.TopLeftY = orientationAlignedViewport.y;
dludwig@8433
  1050
    viewport.Width = orientationAlignedViewport.w;
dludwig@8433
  1051
    viewport.Height = orientationAlignedViewport.h;
dludwig@8432
  1052
    viewport.MinDepth = 0.0f;
dludwig@8432
  1053
    viewport.MaxDepth = 1.0f;
dludwig@8432
  1054
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8432
  1055
dludwig@8433
  1056
#if 0
dludwig@8433
  1057
    SDL_Log("%s, oav={%.0f,%.0f,%.0f,%.0f}, rend={%.0f,%.0f}\n",
dludwig@8433
  1058
        __FUNCTION__,
dludwig@8433
  1059
        orientationAlignedViewport.x,
dludwig@8433
  1060
        orientationAlignedViewport.y,
dludwig@8433
  1061
        orientationAlignedViewport.w,
dludwig@8433
  1062
        orientationAlignedViewport.h,
dludwig@8433
  1063
        data->renderTargetSize.x,
dludwig@8433
  1064
        data->renderTargetSize.y);
dludwig@8433
  1065
#endif
dludwig@8433
  1066
dludwig@8400
  1067
    return 0;
dludwig@8400
  1068
}
dludwig@8400
  1069
dludwig@8416
  1070
static int
dludwig@8416
  1071
D3D11_RenderClear(SDL_Renderer * renderer)
dludwig@8416
  1072
{
dludwig@8416
  1073
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
  1074
    const float colorRGBA[] = {
dludwig@8423
  1075
        (renderer->r / 255.0f),
dludwig@8423
  1076
        (renderer->g / 255.0f),
dludwig@8423
  1077
        (renderer->b / 255.0f),
dludwig@8423
  1078
        (renderer->a / 255.0f)
dludwig@8416
  1079
    };
dludwig@8416
  1080
    data->d3dContext->ClearRenderTargetView(
dludwig@8416
  1081
        data->renderTargetView.Get(),
dludwig@8416
  1082
        colorRGBA
dludwig@8416
  1083
        );
dludwig@8416
  1084
    return 0;
dludwig@8416
  1085
}
dludwig@8416
  1086
dludwig@8429
  1087
static int
dludwig@8429
  1088
D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
dludwig@8429
  1089
                         const void * vertexData, unsigned int dataSizeInBytes)
dludwig@8416
  1090
{
dludwig@8416
  1091
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8425
  1092
    HRESULT result = S_OK;
dludwig@8449
  1093
    D3D11_BUFFER_DESC vertexBufferDesc;
dludwig@8449
  1094
dludwig@8449
  1095
    if (rendererData->vertexBuffer) {
dludwig@8449
  1096
        rendererData->vertexBuffer->GetDesc(&vertexBufferDesc);
dludwig@8449
  1097
    } else {
dludwig@8449
  1098
        memset(&vertexBufferDesc, 0, sizeof(vertexBufferDesc));
dludwig@8449
  1099
    }
dludwig@8449
  1100
dludwig@8449
  1101
    if (vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
dludwig@8429
  1102
        rendererData->d3dContext->UpdateSubresource(rendererData->vertexBuffer.Get(), 0, NULL, vertexData, dataSizeInBytes, 0);
dludwig@8425
  1103
    } else {
dludwig@8449
  1104
        vertexBufferDesc.ByteWidth = dataSizeInBytes;
dludwig@8449
  1105
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
dludwig@8449
  1106
dludwig@8425
  1107
        D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8429
  1108
        vertexBufferData.pSysMem = vertexData;
dludwig@8425
  1109
        vertexBufferData.SysMemPitch = 0;
dludwig@8425
  1110
        vertexBufferData.SysMemSlicePitch = 0;
dludwig@8449
  1111
dludwig@8425
  1112
        result = rendererData->d3dDevice->CreateBuffer(
dludwig@8425
  1113
            &vertexBufferDesc,
dludwig@8425
  1114
            &vertexBufferData,
dludwig@8425
  1115
            &rendererData->vertexBuffer
dludwig@8425
  1116
            );
dludwig@8425
  1117
        if (FAILED(result)) {
dludwig@8425
  1118
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8425
  1119
            return -1;
dludwig@8425
  1120
        }
dludwig@8425
  1121
    }
dludwig@8425
  1122
dludwig@8416
  1123
    UINT stride = sizeof(VertexPositionColor);
dludwig@8416
  1124
    UINT offset = 0;
dludwig@8416
  1125
    rendererData->d3dContext->IASetVertexBuffers(
dludwig@8416
  1126
        0,
dludwig@8416
  1127
        1,
dludwig@8416
  1128
        rendererData->vertexBuffer.GetAddressOf(),
dludwig@8416
  1129
        &stride,
dludwig@8416
  1130
        &offset
dludwig@8429
  1131
        );
dludwig@8429
  1132
dludwig@8429
  1133
    return 0;
dludwig@8429
  1134
}
dludwig@8429
  1135
dludwig@8429
  1136
static void
dludwig@8429
  1137
D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
dludwig@8429
  1138
{
dludwig@8429
  1139
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1140
dludwig@8429
  1141
    rendererData->d3dContext->OMSetRenderTargets(
dludwig@8429
  1142
        1,
dludwig@8429
  1143
        rendererData->renderTargetView.GetAddressOf(),
dludwig@8429
  1144
        nullptr
dludwig@8416
  1145
        );
dludwig@8416
  1146
dludwig@8429
  1147
    rendererData->d3dContext->UpdateSubresource(
dludwig@8429
  1148
        rendererData->vertexShaderConstants.Get(),
dludwig@8429
  1149
		0,
dludwig@8429
  1150
		NULL,
dludwig@8429
  1151
		&rendererData->vertexShaderConstantsData,
dludwig@8429
  1152
		0,
dludwig@8429
  1153
		0
dludwig@8429
  1154
		);
dludwig@8442
  1155
}
dludwig@8431
  1156
dludwig@8442
  1157
static void
dludwig@8442
  1158
D3D11_RenderSetBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
dludwig@8442
  1159
{
dludwig@8442
  1160
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8442
  1161
    switch (blendMode) {
dludwig@8431
  1162
        case SDL_BLENDMODE_BLEND:
dludwig@8431
  1163
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeBlend.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1164
            break;
dludwig@8431
  1165
        case SDL_BLENDMODE_ADD:
dludwig@8431
  1166
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeAdd.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1167
            break;
dludwig@8431
  1168
        case SDL_BLENDMODE_MOD:
dludwig@8431
  1169
            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeMod.Get(), 0, 0xFFFFFFFF);
dludwig@8431
  1170
            break;
dludwig@8431
  1171
        case SDL_BLENDMODE_NONE:
dludwig@8431
  1172
            rendererData->d3dContext->OMSetBlendState(NULL, 0, 0xFFFFFFFF);
dludwig@8431
  1173
            break;
dludwig@8431
  1174
    }
dludwig@8429
  1175
}
dludwig@8429
  1176
dludwig@8429
  1177
static void
dludwig@8429
  1178
D3D11_SetPixelShader(SDL_Renderer * renderer,
dludwig@8429
  1179
                     ID3D11PixelShader * shader,
dludwig@8429
  1180
                     ID3D11ShaderResourceView * shaderResource,
dludwig@8429
  1181
                     ID3D11SamplerState * sampler)
dludwig@8429
  1182
{
dludwig@8429
  1183
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1184
    rendererData->d3dContext->PSSetShader(shader, nullptr, 0);
dludwig@8429
  1185
    rendererData->d3dContext->PSSetShaderResources(0, 1, &shaderResource);
dludwig@8429
  1186
    rendererData->d3dContext->PSSetSamplers(0, 1, &sampler);
dludwig@8429
  1187
}
dludwig@8429
  1188
dludwig@8429
  1189
static void
dludwig@8429
  1190
D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
dludwig@8449
  1191
                         D3D11_PRIMITIVE_TOPOLOGY primitiveTopology,
dludwig@8449
  1192
                         UINT vertexCount)
dludwig@8429
  1193
{
dludwig@8429
  1194
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1195
    rendererData->d3dContext->IASetPrimitiveTopology(primitiveTopology);
dludwig@8429
  1196
    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
dludwig@8429
  1197
    rendererData->d3dContext->VSSetShader(rendererData->vertexShader.Get(), nullptr, 0);
dludwig@8429
  1198
    rendererData->d3dContext->VSSetConstantBuffers(0, 1, rendererData->vertexShaderConstants.GetAddressOf());
dludwig@8429
  1199
    rendererData->d3dContext->RSSetState(rendererData->mainRasterizer.Get());
dludwig@8449
  1200
    rendererData->d3dContext->Draw(vertexCount, 0);
dludwig@8449
  1201
}
dludwig@8449
  1202
dludwig@8449
  1203
static int
dludwig@8450
  1204
D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8450
  1205
                       const SDL_FPoint * points, int count)
dludwig@8450
  1206
{
dludwig@8450
  1207
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8450
  1208
    float r, g, b, a;
dludwig@8450
  1209
dludwig@8450
  1210
    r = (float)(renderer->r / 255.0f);
dludwig@8450
  1211
    g = (float)(renderer->g / 255.0f);
dludwig@8450
  1212
    b = (float)(renderer->b / 255.0f);
dludwig@8450
  1213
    a = (float)(renderer->a / 255.0f);
dludwig@8450
  1214
dludwig@8450
  1215
    vector<VertexPositionColor> vertices;
dludwig@8450
  1216
    vertices.reserve(count);
dludwig@8450
  1217
    for (int i = 0; i < count; ++i) {
dludwig@8450
  1218
        VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
dludwig@8450
  1219
        vertices.push_back(v);
dludwig@8450
  1220
    }
dludwig@8450
  1221
dludwig@8450
  1222
    D3D11_RenderStartDrawOp(renderer);
dludwig@8450
  1223
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8450
  1224
    if (D3D11_UpdateVertexBuffer(renderer, &vertices[0], vertices.size() * sizeof(VertexPositionColor)) != 0) {
dludwig@8450
  1225
        return -1;
dludwig@8450
  1226
    }
dludwig@8450
  1227
dludwig@8450
  1228
    D3D11_SetPixelShader(
dludwig@8450
  1229
        renderer,
dludwig@8450
  1230
        rendererData->colorPixelShader.Get(),
dludwig@8450
  1231
        nullptr,
dludwig@8450
  1232
        nullptr);
dludwig@8450
  1233
dludwig@8450
  1234
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, vertices.size());
dludwig@8450
  1235
dludwig@8450
  1236
    return 0;
dludwig@8450
  1237
}
dludwig@8450
  1238
dludwig@8450
  1239
static int
dludwig@8449
  1240
D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8449
  1241
                      const SDL_FPoint * points, int count)
dludwig@8449
  1242
{
dludwig@8449
  1243
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8449
  1244
    float r, g, b, a;
dludwig@8449
  1245
dludwig@8449
  1246
    r = (float)(renderer->r / 255.0f);
dludwig@8449
  1247
    g = (float)(renderer->g / 255.0f);
dludwig@8449
  1248
    b = (float)(renderer->b / 255.0f);
dludwig@8449
  1249
    a = (float)(renderer->a / 255.0f);
dludwig@8449
  1250
dludwig@8449
  1251
    vector<VertexPositionColor> vertices;
dludwig@8449
  1252
    vertices.reserve(count);
dludwig@8449
  1253
    for (int i = 0; i < count; ++i) {
dludwig@8449
  1254
        VertexPositionColor v = {XMFLOAT3(points[i].x, points[i].y, 0.0f),  XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)};
dludwig@8449
  1255
        vertices.push_back(v);
dludwig@8449
  1256
    }
dludwig@8449
  1257
dludwig@8449
  1258
    D3D11_RenderStartDrawOp(renderer);
dludwig@8449
  1259
    D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8449
  1260
    if (D3D11_UpdateVertexBuffer(renderer, &vertices[0], vertices.size() * sizeof(VertexPositionColor)) != 0) {
dludwig@8449
  1261
        return -1;
dludwig@8449
  1262
    }
dludwig@8449
  1263
dludwig@8449
  1264
    D3D11_SetPixelShader(
dludwig@8449
  1265
        renderer,
dludwig@8449
  1266
        rendererData->colorPixelShader.Get(),
dludwig@8449
  1267
        nullptr,
dludwig@8449
  1268
        nullptr);
dludwig@8449
  1269
dludwig@8449
  1270
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, vertices.size());
dludwig@8449
  1271
dludwig@8449
  1272
    return 0;
dludwig@8429
  1273
}
dludwig@8429
  1274
dludwig@8429
  1275
static int
dludwig@8429
  1276
D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8429
  1277
                      const SDL_FRect * rects, int count)
dludwig@8429
  1278
{
dludwig@8429
  1279
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1280
    float r, g, b, a;
dludwig@8429
  1281
dludwig@8431
  1282
    r = (float)(renderer->r / 255.0f);
dludwig@8431
  1283
    g = (float)(renderer->g / 255.0f);
dludwig@8431
  1284
    b = (float)(renderer->b / 255.0f);
dludwig@8431
  1285
    a = (float)(renderer->a / 255.0f);
dludwig@8429
  1286
dludwig@8433
  1287
#if 0
dludwig@8433
  1288
    // Set up a test pattern:
dludwig@8434
  1289
    SDL_FRect _rects[] = {
dludwig@8433
  1290
        {-1.1f, 1.1f, 1.1f, -1.1f},
dludwig@8433
  1291
        {-1.0f, 1.0f, 1.0f, -1.0f},     // red
dludwig@8433
  1292
        {0.0f, 1.0f, 1.0f, -1.0f},      // green
dludwig@8433
  1293
        {-1.0f, 0.0f, 1.0f, -1.0f},     // blue
dludwig@8433
  1294
        {0.0f, 0.0f, 1.0f, -1.0f}       // white
dludwig@8433
  1295
    };
dludwig@8434
  1296
    count = sizeof(_rects) / sizeof(SDL_FRect);
dludwig@8434
  1297
#define rects _rects
dludwig@8433
  1298
#endif
dludwig@8429
  1299
dludwig@8429
  1300
    for (int i = 0; i < count; ++i) {
dludwig@8433
  1301
        D3D11_RenderStartDrawOp(renderer);
dludwig@8442
  1302
        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
dludwig@8433
  1303
dludwig@8433
  1304
#if 0
dludwig@8433
  1305
        // Set colors for the test pattern:
dludwig@8433
  1306
        a = 1.0f;
dludwig@8433
  1307
        switch (i) {
dludwig@8433
  1308
            case 0: r = 1.0f; g = 1.0f; b = 0.0f; break;
dludwig@8433
  1309
            case 1: r = 1.0f; g = 0.0f; b = 0.0f; break;
dludwig@8433
  1310
            case 2: r = 0.0f; g = 1.0f; b = 0.0f; break;
dludwig@8433
  1311
            case 3: r = 0.0f; g = 0.0f; b = 1.0f; break;
dludwig@8433
  1312
            case 4: r = 1.0f; g = 1.0f; b = 1.0f; break;
dludwig@8433
  1313
        }
dludwig@8433
  1314
#endif
dludwig@8433
  1315
dludwig@8429
  1316
        VertexPositionColor vertices[] = {
dludwig@8429
  1317
            {XMFLOAT3(rects[i].x, rects[i].y, 0.0f),                           XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1318
            {XMFLOAT3(rects[i].x, rects[i].y + rects[i].h, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1319
            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y, 0.0f),              XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1320
            {XMFLOAT3(rects[i].x + rects[i].w, rects[i].y + rects[i].h, 0.0f), XMFLOAT2(0.0f, 0.0f), XMFLOAT4(r, g, b, a)},
dludwig@8429
  1321
        };
dludwig@8429
  1322
        if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
dludwig@8429
  1323
            return -1;
dludwig@8429
  1324
        }
dludwig@8416
  1325
dludwig@8429
  1326
        D3D11_SetPixelShader(
dludwig@8429
  1327
            renderer,
dludwig@8429
  1328
            rendererData->colorPixelShader.Get(),
dludwig@8429
  1329
            nullptr,
dludwig@8429
  1330
            nullptr);
dludwig@8416
  1331
dludwig@8449
  1332
        D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8429
  1333
    }
dludwig@8429
  1334
dludwig@8429
  1335
    return 0;
dludwig@8429
  1336
}
dludwig@8429
  1337
dludwig@8429
  1338
static int
dludwig@8429
  1339
D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8429
  1340
                 const SDL_Rect * srcrect, const SDL_FRect * dstrect)
dludwig@8429
  1341
{
dludwig@8429
  1342
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8429
  1343
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8429
  1344
dludwig@8429
  1345
    D3D11_RenderStartDrawOp(renderer);
dludwig@8442
  1346
    D3D11_RenderSetBlendMode(renderer, texture->blendMode);
dludwig@8448
  1347
dludwig@8448
  1348
    float minu = (float) srcrect->x / texture->w;
dludwig@8448
  1349
    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
dludwig@8448
  1350
    float minv = (float) srcrect->y / texture->h;
dludwig@8448
  1351
    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
dludwig@8416
  1352
dludwig@8429
  1353
    VertexPositionColor vertices[] = {
dludwig@8448
  1354
        {XMFLOAT3(dstrect->x, dstrect->y, 0.0f),                           XMFLOAT2(minu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
dludwig@8448
  1355
        {XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f),              XMFLOAT2(minu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
dludwig@8448
  1356
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y, 0.0f),              XMFLOAT2(maxu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
dludwig@8448
  1357
        {XMFLOAT3(dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
dludwig@8429
  1358
    };
dludwig@8429
  1359
    if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
dludwig@8429
  1360
        return -1;
dludwig@8429
  1361
    }
dludwig@8418
  1362
dludwig@8429
  1363
    D3D11_SetPixelShader(
dludwig@8429
  1364
        renderer,
dludwig@8429
  1365
        rendererData->texturePixelShader.Get(),
dludwig@8429
  1366
        textureData->mainTextureResourceView.Get(),
dludwig@8429
  1367
        rendererData->mainSampler.Get());
dludwig@8416
  1368
dludwig@8449
  1369
    D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
dludwig@8416
  1370
dludwig@8416
  1371
    return 0;
dludwig@8416
  1372
}
dludwig@8416
  1373
dludwig@8401
  1374
static void
dludwig@8401
  1375
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
  1376
{
dludwig@8401
  1377
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
  1378
dludwig@8401
  1379
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
  1380
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
  1381
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
  1382
    // frames that will never be displayed to the screen.
dludwig@8401
  1383
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
  1384
#else
dludwig@8401
  1385
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
  1386
    // rects to improve efficiency in certain scenarios.
dludwig@8401
  1387
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
  1388
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
  1389
    parameters.DirtyRectsCount = 0;
dludwig@8401
  1390
    parameters.pDirtyRects = nullptr;
dludwig@8401
  1391
    parameters.pScrollRect = nullptr;
dludwig@8401
  1392
    parameters.pScrollOffset = nullptr;
dludwig@8401
  1393
    
dludwig@8401
  1394
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
  1395
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
  1396
    // frames that will never be displayed to the screen.
dludwig@8401
  1397
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
  1398
#endif
dludwig@8401
  1399
dludwig@8401
  1400
    // Discard the contents of the render target.
dludwig@8401
  1401
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
  1402
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8401
  1403
    data->d3dContext->DiscardView(data->renderTargetView.Get());
dludwig@8401
  1404
dludwig@8401
  1405
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
  1406
    // must recreate all device resources.
dludwig@8414
  1407
    //
dludwig@8414
  1408
    // 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
  1409
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
  1410
    {
dludwig@8414
  1411
        hr = D3D11_HandleDeviceLost(renderer);
dludwig@8414
  1412
        if (FAILED(hr)) {
dludwig@8414
  1413
            WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8414
  1414
        }
dludwig@8401
  1415
    }
dludwig@8401
  1416
    else
dludwig@8401
  1417
    {
dludwig@8401
  1418
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
  1419
    }
dludwig@8401
  1420
}
dludwig@8401
  1421
dludwig@8400
  1422
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
  1423
dludwig@8400
  1424
/* vi: set ts=4 sw=4 expandtab: */