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