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