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