src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 15:22:49 -0500
changeset 8413 4ae81c5dc55e
parent 8412 59f1191a5cca
child 8414 25a7605097a2
permissions -rw-r--r--
WinRT: made the Direct3D 11.1 renderer directly initialize more of itself, rather than deferring to code in the WinRT video driver
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8400
    26
extern "C" {
dludwig@8400
    27
#include "../../core/windows/SDL_windows.h"
dludwig@8400
    28
//#include "SDL_hints.h"
dludwig@8400
    29
//#include "SDL_loadso.h"
dludwig@8410
    30
#include "SDL_system.h"
dludwig@8400
    31
#include "SDL_syswm.h"
dludwig@8400
    32
#include "../SDL_sysrender.h"
dludwig@8400
    33
//#include "stdio.h"
dludwig@8400
    34
}
dludwig@8400
    35
dludwig@8410
    36
#include <fstream>
dludwig@8410
    37
#include <string>
dludwig@8410
    38
#include <vector>
dludwig@8410
    39
dludwig@8400
    40
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    41
dludwig@8410
    42
using namespace DirectX;
dludwig@8410
    43
using namespace Microsoft::WRL;
dludwig@8410
    44
using namespace std;
dludwig@8410
    45
dludwig@8412
    46
#ifdef __WINRT__
dludwig@8412
    47
using namespace Windows::Graphics::Display;
dludwig@8412
    48
using namespace Windows::UI::Core;
dludwig@8412
    49
#endif
dludwig@8412
    50
dludwig@8410
    51
/* Direct3D 11.1 renderer implementation */
dludwig@8400
    52
dludwig@8400
    53
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8400
    54
//static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8400
    55
//                            const SDL_WindowEvent *event);
dludwig@8400
    56
//static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    57
//static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    58
//                             const SDL_Rect * rect, const void *pixels,
dludwig@8400
    59
//                             int pitch);
dludwig@8400
    60
//static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    61
//                           const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8400
    62
//static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    63
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    64
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8400
    65
//static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8400
    66
//static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8400
    67
//                                const SDL_FPoint * points, int count);
dludwig@8400
    68
//static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8400
    69
//                               const SDL_FPoint * points, int count);
dludwig@8400
    70
//static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8400
    71
//                               const SDL_FRect * rects, int count);
dludwig@8400
    72
//static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    73
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8400
    74
//static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    75
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8400
    76
//                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8400
    77
//static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8400
    78
//                                Uint32 format, void * pixels, int pitch);
dludwig@8401
    79
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8400
    80
//static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8400
    81
//                               SDL_Texture * texture);
dludwig@8413
    82
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    83
dludwig@8410
    84
/* Direct3D 11.1 Internal Functions */
dludwig@8412
    85
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
    86
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8400
    87
dludwig@8400
    88
extern "C" {
dludwig@8400
    89
    SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    90
    D3D11_CreateRenderer,
dludwig@8400
    91
    {
dludwig@8400
    92
     "direct3d",
dludwig@8400
    93
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
dludwig@8400
    94
     1,
dludwig@8400
    95
     {SDL_PIXELFORMAT_ARGB8888},
dludwig@8400
    96
     0,
dludwig@8400
    97
     0}
dludwig@8400
    98
    };
dludwig@8400
    99
}
dludwig@8400
   100
dludwig@8400
   101
//typedef struct
dludwig@8400
   102
//{
dludwig@8400
   103
//    float x, y, z;
dludwig@8400
   104
//    DWORD color;
dludwig@8400
   105
//    float u, v;
dludwig@8400
   106
//} Vertex;
dludwig@8400
   107
dludwig@8400
   108
SDL_Renderer *
dludwig@8400
   109
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
   110
{
dludwig@8400
   111
    SDL_Renderer *renderer;
dludwig@8400
   112
    D3D11_RenderData *data;
dludwig@8400
   113
dludwig@8400
   114
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   115
    if (!renderer) {
dludwig@8400
   116
        SDL_OutOfMemory();
dludwig@8400
   117
        return NULL;
dludwig@8400
   118
    }
dludwig@8400
   119
    SDL_zerop(renderer);
dludwig@8400
   120
dludwig@8400
   121
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   122
    if (!data) {
dludwig@8400
   123
        SDL_OutOfMemory();
dludwig@8400
   124
        return NULL;
dludwig@8400
   125
    }
dludwig@8410
   126
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
dludwig@8410
   127
    data->vertexCount = 0;
dludwig@8410
   128
    data->loadingComplete = false;
dludwig@8412
   129
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
dludwig@8412
   130
    data->renderTargetSize = XMFLOAT2(0, 0);
dludwig@8400
   131
dludwig@8400
   132
    // TODO: Create Direct3D Object(s)
dludwig@8400
   133
dludwig@8400
   134
    //renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8400
   135
    //renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8400
   136
    //renderer->UpdateTexture = D3D11_UpdateTexture;
dludwig@8400
   137
    //renderer->LockTexture = D3D11_LockTexture;
dludwig@8400
   138
    //renderer->UnlockTexture = D3D11_UnlockTexture;
dludwig@8400
   139
    //renderer->SetRenderTarget = D3D11_SetRenderTarget;
dludwig@8400
   140
    renderer->UpdateViewport = D3D11_UpdateViewport;
dludwig@8400
   141
    //renderer->RenderClear = D3D11_RenderClear;
dludwig@8400
   142
    //renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
dludwig@8400
   143
    //renderer->RenderDrawLines = D3D11_RenderDrawLines;
dludwig@8400
   144
    //renderer->RenderFillRects = D3D11_RenderFillRects;
dludwig@8400
   145
    //renderer->RenderCopy = D3D11_RenderCopy;
dludwig@8400
   146
    //renderer->RenderCopyEx = D3D11_RenderCopyEx;
dludwig@8400
   147
    //renderer->RenderReadPixels = D3D11_RenderReadPixels;
dludwig@8401
   148
    renderer->RenderPresent = D3D11_RenderPresent;
dludwig@8400
   149
    //renderer->DestroyTexture = D3D11_DestroyTexture;
dludwig@8413
   150
    renderer->DestroyRenderer = D3D11_DestroyRenderer;
dludwig@8400
   151
    renderer->info = D3D11_RenderDriver.info;
dludwig@8413
   152
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
dludwig@8400
   153
    renderer->driverdata = data;
dludwig@8400
   154
dludwig@8413
   155
    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
dludwig@8413
   156
    // order to give init functions access to the underlying window handle:
dludwig@8413
   157
    renderer->window = window;
dludwig@8400
   158
dludwig@8413
   159
    /* Initialize Direct3D resources */
dludwig@8413
   160
    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
dludwig@8413
   161
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   162
        return NULL;
dludwig@8413
   163
    }
dludwig@8413
   164
    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
dludwig@8413
   165
        D3D11_DestroyRenderer(renderer);
dludwig@8413
   166
        return NULL;
dludwig@8413
   167
    }
dludwig@8400
   168
dludwig@8400
   169
    return renderer;
dludwig@8400
   170
}
dludwig@8400
   171
dludwig@8413
   172
static void
dludwig@8413
   173
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   174
{
dludwig@8413
   175
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   176
    if (data) {
dludwig@8413
   177
        delete data;
dludwig@8413
   178
        data = NULL;
dludwig@8413
   179
    }
dludwig@8413
   180
}
dludwig@8413
   181
dludwig@8410
   182
static bool
dludwig@8410
   183
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
dludwig@8410
   184
{
dludwig@8410
   185
    ifstream in(fileName, ios::in | ios::binary);
dludwig@8410
   186
    if (!in) {
dludwig@8410
   187
        return false;
dludwig@8410
   188
    }
dludwig@8410
   189
dludwig@8410
   190
    in.seekg(0, ios::end);
dludwig@8410
   191
    out.resize((size_t) in.tellg());
dludwig@8410
   192
    in.seekg(0, ios::beg);
dludwig@8410
   193
    in.read(&out[0], out.size());
dludwig@8410
   194
    return in.good();
dludwig@8410
   195
}
dludwig@8410
   196
dludwig@8410
   197
static bool
dludwig@8410
   198
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
dludwig@8410
   199
{
dludwig@8410
   200
    wstring fileName;
dludwig@8410
   201
dludwig@8410
   202
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8410
   203
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   204
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   205
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8410
   206
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   207
    fileName += L"\\";
dludwig@8410
   208
#endif
dludwig@8410
   209
    // WinRT, TODO: test Direct3D 11.1 shader loading on Win32
dludwig@8410
   210
    fileName += shaderName;
dludwig@8410
   211
    return D3D11_ReadFileContents(fileName, out);
dludwig@8410
   212
}
dludwig@8410
   213
dludwig@8410
   214
HRESULT
dludwig@8412
   215
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
dludwig@8410
   216
{
dludwig@8410
   217
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8410
   218
dludwig@8410
   219
    // This flag adds support for surfaces with a different color channel ordering
dludwig@8410
   220
    // than the API default. It is required for compatibility with Direct2D.
dludwig@8410
   221
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
dludwig@8410
   222
dludwig@8410
   223
#if defined(_DEBUG)
dludwig@8410
   224
    // If the project is in a debug build, enable debugging via SDK Layers with this flag.
dludwig@8410
   225
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8410
   226
#endif
dludwig@8410
   227
dludwig@8410
   228
    // This array defines the set of DirectX hardware feature levels this app will support.
dludwig@8410
   229
    // Note the ordering should be preserved.
dludwig@8410
   230
    // Don't forget to declare your application's minimum required feature level in its
dludwig@8410
   231
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
dludwig@8410
   232
    D3D_FEATURE_LEVEL featureLevels[] = 
dludwig@8410
   233
    {
dludwig@8410
   234
        D3D_FEATURE_LEVEL_11_1,
dludwig@8410
   235
        D3D_FEATURE_LEVEL_11_0,
dludwig@8410
   236
        D3D_FEATURE_LEVEL_10_1,
dludwig@8410
   237
        D3D_FEATURE_LEVEL_10_0,
dludwig@8410
   238
        D3D_FEATURE_LEVEL_9_3,
dludwig@8410
   239
        D3D_FEATURE_LEVEL_9_2,
dludwig@8410
   240
        D3D_FEATURE_LEVEL_9_1
dludwig@8410
   241
    };
dludwig@8410
   242
dludwig@8410
   243
    // Create the Direct3D 11 API device object and a corresponding context.
dludwig@8410
   244
    ComPtr<ID3D11Device> device;
dludwig@8410
   245
    ComPtr<ID3D11DeviceContext> context;
dludwig@8410
   246
    HRESULT result = S_OK;
dludwig@8410
   247
    result = D3D11CreateDevice(
dludwig@8410
   248
        nullptr, // Specify nullptr to use the default adapter.
dludwig@8410
   249
        D3D_DRIVER_TYPE_HARDWARE,
dludwig@8410
   250
        nullptr,
dludwig@8410
   251
        creationFlags, // Set set debug and Direct2D compatibility flags.
dludwig@8410
   252
        featureLevels, // List of feature levels this app can support.
dludwig@8410
   253
        ARRAYSIZE(featureLevels),
dludwig@8410
   254
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
dludwig@8410
   255
        &device, // Returns the Direct3D device created.
dludwig@8410
   256
        &data->featureLevel, // Returns feature level of device created.
dludwig@8410
   257
        &context // Returns the device immediate context.
dludwig@8410
   258
        );
dludwig@8410
   259
    if (FAILED(result)) {
dludwig@8410
   260
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   261
        return result;
dludwig@8410
   262
    }
dludwig@8410
   263
dludwig@8410
   264
    // Get the Direct3D 11.1 API device and context interfaces.
dludwig@8410
   265
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
dludwig@8410
   266
    result = device.As(&(data->d3dDevice));
dludwig@8410
   267
    if (FAILED(result)) {
dludwig@8410
   268
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   269
        return result;
dludwig@8410
   270
    }
dludwig@8410
   271
dludwig@8410
   272
    result = context.As(&data->d3dContext);
dludwig@8410
   273
    if (FAILED(result)) {
dludwig@8410
   274
        return result;
dludwig@8410
   275
    }
dludwig@8410
   276
dludwig@8410
   277
    // Start loading GPU shaders:
dludwig@8410
   278
    vector<char> fileData;
dludwig@8410
   279
dludwig@8410
   280
    //
dludwig@8410
   281
    // Load in SDL's one and only vertex shader:
dludwig@8410
   282
    //
dludwig@8410
   283
    if (!D3D11_ReadShaderContents(L"SimpleVertexShader.cso", fileData)) {
dludwig@8410
   284
        SDL_SetError("Unable to open SDL's vertex shader file.");
dludwig@8410
   285
        return E_FAIL;
dludwig@8410
   286
    }
dludwig@8410
   287
dludwig@8410
   288
    result = data->d3dDevice->CreateVertexShader(
dludwig@8410
   289
        &fileData[0],
dludwig@8410
   290
        fileData.size(),
dludwig@8410
   291
        nullptr,
dludwig@8410
   292
        &data->vertexShader
dludwig@8410
   293
        );
dludwig@8410
   294
    if (FAILED(result)) {
dludwig@8410
   295
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   296
        return result;
dludwig@8410
   297
    }
dludwig@8410
   298
dludwig@8410
   299
    //
dludwig@8410
   300
    // Create an input layout for SDL's vertex shader:
dludwig@8410
   301
    //
dludwig@8410
   302
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8410
   303
    {
dludwig@8410
   304
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   305
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   306
    };
dludwig@8410
   307
dludwig@8410
   308
    result = data->d3dDevice->CreateInputLayout(
dludwig@8410
   309
        vertexDesc,
dludwig@8410
   310
        ARRAYSIZE(vertexDesc),
dludwig@8410
   311
        &fileData[0],
dludwig@8410
   312
        fileData.size(),
dludwig@8410
   313
        &data->inputLayout
dludwig@8410
   314
        );
dludwig@8410
   315
    if (FAILED(result)) {
dludwig@8410
   316
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   317
        return result;
dludwig@8410
   318
    }
dludwig@8410
   319
dludwig@8410
   320
    //
dludwig@8410
   321
    // Load in SDL's one and only pixel shader (for now, more are likely to follow):
dludwig@8410
   322
    //
dludwig@8410
   323
    if (!D3D11_ReadShaderContents(L"SimplePixelShader.cso", fileData)) {
dludwig@8410
   324
        SDL_SetError("Unable to open SDL's pixel shader file.");
dludwig@8410
   325
        return E_FAIL;
dludwig@8410
   326
    }
dludwig@8410
   327
dludwig@8410
   328
    result = data->d3dDevice->CreatePixelShader(
dludwig@8410
   329
        &fileData[0],
dludwig@8410
   330
        fileData.size(),
dludwig@8410
   331
        nullptr,
dludwig@8410
   332
        &data->pixelShader
dludwig@8410
   333
        );
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
    //
dludwig@8410
   340
    // Create a vertex buffer:
dludwig@8410
   341
    //
dludwig@8410
   342
    VertexPositionColor vertices[] = 
dludwig@8410
   343
    {
dludwig@8410
   344
        {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
dludwig@8410
   345
        {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
dludwig@8410
   346
        {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
dludwig@8410
   347
        {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
dludwig@8410
   348
    };
dludwig@8410
   349
dludwig@8410
   350
    data->vertexCount = ARRAYSIZE(vertices);
dludwig@8410
   351
dludwig@8410
   352
    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8410
   353
    vertexBufferData.pSysMem = vertices;
dludwig@8410
   354
    vertexBufferData.SysMemPitch = 0;
dludwig@8410
   355
    vertexBufferData.SysMemSlicePitch = 0;
dludwig@8410
   356
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER);
dludwig@8410
   357
    result = data->d3dDevice->CreateBuffer(
dludwig@8410
   358
        &vertexBufferDesc,
dludwig@8410
   359
        &vertexBufferData,
dludwig@8410
   360
        &data->vertexBuffer
dludwig@8410
   361
        );
dludwig@8410
   362
    if (FAILED(result)) {
dludwig@8410
   363
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   364
        return result;
dludwig@8410
   365
    }
dludwig@8410
   366
dludwig@8410
   367
    //
dludwig@8410
   368
    // Create a sampler to use when drawing textures:
dludwig@8410
   369
    //
dludwig@8410
   370
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   371
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   372
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   373
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   374
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   375
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   376
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   377
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   378
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   379
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   380
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   381
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   382
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   383
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   384
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   385
        &samplerDesc,
dludwig@8410
   386
        &data->mainSampler
dludwig@8410
   387
        );
dludwig@8410
   388
    if (FAILED(result)) {
dludwig@8410
   389
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   390
        return result;
dludwig@8410
   391
    }
dludwig@8410
   392
dludwig@8410
   393
    //
dludwig@8410
   394
    // All done!
dludwig@8410
   395
    //
dludwig@8410
   396
    data->loadingComplete = true;       // This variable can probably be factored-out
dludwig@8410
   397
    return S_OK;
dludwig@8410
   398
}
dludwig@8410
   399
dludwig@8412
   400
#ifdef __WINRT__
dludwig@8412
   401
dludwig@8412
   402
CoreWindow ^
dludwig@8412
   403
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   404
{
dludwig@8412
   405
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   406
    if ( ! renderer->window ) {
dludwig@8412
   407
        return nullptr;
dludwig@8412
   408
    }
dludwig@8412
   409
dludwig@8412
   410
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   411
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   412
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   413
        return nullptr;
dludwig@8412
   414
    }
dludwig@8412
   415
dludwig@8412
   416
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   417
        return nullptr;
dludwig@8412
   418
    }
dludwig@8412
   419
dludwig@8412
   420
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   421
    if ( ! coreWindowPointer ) {
dludwig@8412
   422
        return nullptr;
dludwig@8412
   423
    }
dludwig@8412
   424
dludwig@8412
   425
    return *coreWindowPointer;
dludwig@8412
   426
}
dludwig@8412
   427
dludwig@8412
   428
static float
dludwig@8412
   429
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   430
{
dludwig@8412
   431
    static const float dipsPerInch = 96.0f;
dludwig@8412
   432
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   433
}
dludwig@8412
   434
#endif
dludwig@8412
   435
dludwig@8412
   436
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   437
HRESULT
dludwig@8412
   438
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   439
{
dludwig@8412
   440
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   441
    HRESULT result = S_OK;
dludwig@8412
   442
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   443
dludwig@8412
   444
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   445
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   446
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   447
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   448
dludwig@8412
   449
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   450
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   451
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   452
dludwig@8412
   453
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   454
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   455
    // orientation, the dimensions must be reversed.
dludwig@8412
   456
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8412
   457
    bool swapDimensions =
dludwig@8412
   458
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   459
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   460
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   461
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   462
dludwig@8412
   463
    if(data->swapChain != nullptr)
dludwig@8412
   464
    {
dludwig@8412
   465
        // If the swap chain already exists, resize it.
dludwig@8412
   466
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   467
            2, // Double-buffered swap chain.
dludwig@8412
   468
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   469
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   470
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   471
            0
dludwig@8412
   472
            );
dludwig@8412
   473
        if (FAILED(result)) {
dludwig@8412
   474
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   475
            return result;
dludwig@8412
   476
        }
dludwig@8412
   477
    }
dludwig@8412
   478
    else
dludwig@8412
   479
    {
dludwig@8412
   480
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   481
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   482
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   483
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   484
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   485
        swapChainDesc.Stereo = false;
dludwig@8412
   486
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   487
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   488
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   489
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   490
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   491
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   492
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   493
#else
dludwig@8412
   494
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   495
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   496
#endif
dludwig@8412
   497
        swapChainDesc.Flags = 0;
dludwig@8412
   498
dludwig@8412
   499
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   500
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   501
        if (FAILED(result)) {
dludwig@8412
   502
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   503
            return result;
dludwig@8412
   504
        }
dludwig@8412
   505
dludwig@8412
   506
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   507
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   508
        if (FAILED(result)) {
dludwig@8412
   509
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   510
            return result;
dludwig@8412
   511
        }
dludwig@8412
   512
dludwig@8412
   513
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   514
        result = dxgiAdapter->GetParent(
dludwig@8412
   515
            __uuidof(IDXGIFactory2), 
dludwig@8412
   516
            &dxgiFactory
dludwig@8412
   517
            );
dludwig@8412
   518
        if (FAILED(result)) {
dludwig@8412
   519
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   520
            return result;
dludwig@8412
   521
        }
dludwig@8412
   522
dludwig@8412
   523
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   524
            data->d3dDevice.Get(),
dludwig@8412
   525
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   526
            &swapChainDesc,
dludwig@8412
   527
            nullptr, // Allow on all displays.
dludwig@8412
   528
            &data->swapChain
dludwig@8412
   529
            );
dludwig@8412
   530
        if (FAILED(result)) {
dludwig@8412
   531
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   532
            return result;
dludwig@8412
   533
        }
dludwig@8412
   534
            
dludwig@8412
   535
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   536
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   537
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   538
        if (FAILED(result)) {
dludwig@8412
   539
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   540
            return result;
dludwig@8412
   541
        }
dludwig@8412
   542
    }
dludwig@8412
   543
    
dludwig@8412
   544
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   545
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   546
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   547
    switch (data->orientation)
dludwig@8412
   548
    {
dludwig@8412
   549
        case DisplayOrientations::Landscape:
dludwig@8412
   550
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8412
   551
            data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8412
   552
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   553
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   554
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   555
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   556
                );
dludwig@8412
   557
            break;
dludwig@8412
   558
dludwig@8412
   559
        case DisplayOrientations::Portrait:
dludwig@8412
   560
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8412
   561
            data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8412
   562
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   563
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   564
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   565
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   566
                );
dludwig@8412
   567
            break;
dludwig@8412
   568
dludwig@8412
   569
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   570
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8412
   571
            data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8412
   572
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   573
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   574
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   575
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   576
                );
dludwig@8412
   577
            break;
dludwig@8412
   578
dludwig@8412
   579
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   580
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8412
   581
            data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8412
   582
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   583
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   584
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   585
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   586
                );
dludwig@8412
   587
            break;
dludwig@8412
   588
dludwig@8412
   589
        default:
dludwig@8412
   590
            throw ref new Platform::FailureException();
dludwig@8412
   591
    }
dludwig@8412
   592
dludwig@8412
   593
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   594
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   595
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   596
    if (FAILED(result)) {
dludwig@8412
   597
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   598
        return result;
dludwig@8412
   599
    }
dludwig@8412
   600
#endif
dludwig@8412
   601
dludwig@8412
   602
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   603
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   604
    result = data->swapChain->GetBuffer(
dludwig@8412
   605
        0,
dludwig@8412
   606
        __uuidof(ID3D11Texture2D),
dludwig@8412
   607
        &backBuffer
dludwig@8412
   608
        );
dludwig@8412
   609
    if (FAILED(result)) {
dludwig@8412
   610
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   611
        return result;
dludwig@8412
   612
    }
dludwig@8412
   613
dludwig@8412
   614
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   615
        backBuffer.Get(),
dludwig@8412
   616
        nullptr,
dludwig@8412
   617
        &data->renderTargetView
dludwig@8412
   618
        );
dludwig@8412
   619
    if (FAILED(result)) {
dludwig@8412
   620
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   621
        return result;
dludwig@8412
   622
    }
dludwig@8412
   623
dludwig@8412
   624
    // Create a depth stencil view.
dludwig@8412
   625
    CD3D11_TEXTURE2D_DESC depthStencilDesc(
dludwig@8412
   626
        DXGI_FORMAT_D24_UNORM_S8_UINT, 
dludwig@8412
   627
        static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   628
        static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   629
        1,
dludwig@8412
   630
        1,
dludwig@8412
   631
        D3D11_BIND_DEPTH_STENCIL
dludwig@8412
   632
        );
dludwig@8412
   633
dludwig@8412
   634
    ComPtr<ID3D11Texture2D> depthStencil;
dludwig@8412
   635
    result = data->d3dDevice->CreateTexture2D(
dludwig@8412
   636
        &depthStencilDesc,
dludwig@8412
   637
        nullptr,
dludwig@8412
   638
        &depthStencil
dludwig@8412
   639
        );
dludwig@8412
   640
    if (FAILED(result)) {
dludwig@8412
   641
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   642
        return result;
dludwig@8412
   643
    }
dludwig@8412
   644
dludwig@8412
   645
    // Set the rendering viewport to target the entire window.
dludwig@8412
   646
    CD3D11_VIEWPORT viewport(
dludwig@8412
   647
        0.0f,
dludwig@8412
   648
        0.0f,
dludwig@8412
   649
        data->renderTargetSize.x,
dludwig@8412
   650
        data->renderTargetSize.y
dludwig@8412
   651
        );
dludwig@8412
   652
dludwig@8412
   653
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8412
   654
dludwig@8412
   655
    return S_OK;
dludwig@8412
   656
}
dludwig@8412
   657
dludwig@8400
   658
static int
dludwig@8400
   659
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   660
{
dludwig@8400
   661
    return 0;
dludwig@8400
   662
}
dludwig@8400
   663
dludwig@8401
   664
static void
dludwig@8401
   665
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
   666
{
dludwig@8401
   667
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
   668
dludwig@8401
   669
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
   670
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   671
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   672
    // frames that will never be displayed to the screen.
dludwig@8401
   673
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
   674
#else
dludwig@8401
   675
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
   676
    // rects to improve efficiency in certain scenarios.
dludwig@8401
   677
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
   678
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
   679
    parameters.DirtyRectsCount = 0;
dludwig@8401
   680
    parameters.pDirtyRects = nullptr;
dludwig@8401
   681
    parameters.pScrollRect = nullptr;
dludwig@8401
   682
    parameters.pScrollOffset = nullptr;
dludwig@8401
   683
    
dludwig@8401
   684
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   685
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   686
    // frames that will never be displayed to the screen.
dludwig@8401
   687
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
   688
#endif
dludwig@8401
   689
dludwig@8401
   690
    // Discard the contents of the render target.
dludwig@8401
   691
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
   692
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8401
   693
    data->d3dContext->DiscardView(data->renderTargetView.Get());
dludwig@8401
   694
dludwig@8401
   695
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
   696
    // must recreate all device resources.
dludwig@8401
   697
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
   698
    {
dludwig@8401
   699
        extern void WINRT_HandleDeviceLost();   // TODO, WinRT: move lost-device handling into the Direct3D 11.1 renderer, as appropriate
dludwig@8401
   700
        WINRT_HandleDeviceLost();
dludwig@8401
   701
    }
dludwig@8401
   702
    else
dludwig@8401
   703
    {
dludwig@8401
   704
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
   705
        // 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
   706
    }
dludwig@8401
   707
}
dludwig@8401
   708
dludwig@8400
   709
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
   710
dludwig@8400
   711
/* vi: set ts=4 sw=4 expandtab: */