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