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