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