src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 18:58:13 -0500
changeset 8416 17040ce2693d
parent 8415 91977f5b2135
child 8418 5230bba62cf2
permissions -rw-r--r--
WinRT: moved texture management code from SDL_winrtrenderer to the D3D 11.1 SDL_Renderer backend
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8400
    26
extern "C" {
dludwig@8400
    27
#include "../../core/windows/SDL_windows.h"
dludwig@8400
    28
//#include "SDL_hints.h"
dludwig@8400
    29
//#include "SDL_loadso.h"
dludwig@8410
    30
#include "SDL_system.h"
dludwig@8400
    31
#include "SDL_syswm.h"
dludwig@8400
    32
#include "../SDL_sysrender.h"
dludwig@8400
    33
//#include "stdio.h"
dludwig@8400
    34
}
dludwig@8400
    35
dludwig@8410
    36
#include <fstream>
dludwig@8410
    37
#include <string>
dludwig@8410
    38
#include <vector>
dludwig@8410
    39
dludwig@8400
    40
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    41
dludwig@8410
    42
using namespace DirectX;
dludwig@8410
    43
using namespace Microsoft::WRL;
dludwig@8410
    44
using namespace std;
dludwig@8410
    45
dludwig@8412
    46
#ifdef __WINRT__
dludwig@8412
    47
using namespace Windows::Graphics::Display;
dludwig@8412
    48
using namespace Windows::UI::Core;
dludwig@8412
    49
#endif
dludwig@8412
    50
dludwig@8410
    51
/* Direct3D 11.1 renderer implementation */
dludwig@8400
    52
dludwig@8400
    53
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8415
    54
static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8415
    55
                            const SDL_WindowEvent *event);
dludwig@8416
    56
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8416
    57
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    58
                             const SDL_Rect * rect, const void *pixels,
dludwig@8416
    59
                             int pitch);
dludwig@8400
    60
//static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    61
//                           const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8400
    62
//static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    63
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    64
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8416
    65
static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8400
    66
//static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8400
    67
//                                const SDL_FPoint * points, int count);
dludwig@8400
    68
//static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8400
    69
//                               const SDL_FPoint * points, int count);
dludwig@8400
    70
//static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8400
    71
//                               const SDL_FRect * rects, int count);
dludwig@8416
    72
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
    73
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8400
    74
//static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    75
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8400
    76
//                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8400
    77
//static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8400
    78
//                                Uint32 format, void * pixels, int pitch);
dludwig@8401
    79
static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8416
    80
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
    81
                                 SDL_Texture * texture);
dludwig@8413
    82
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    83
dludwig@8410
    84
/* Direct3D 11.1 Internal Functions */
dludwig@8412
    85
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
dludwig@8413
    86
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
dludwig@8414
    87
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
dludwig@8414
    88
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
dludwig@8400
    89
dludwig@8400
    90
extern "C" {
dludwig@8400
    91
    SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    92
    D3D11_CreateRenderer,
dludwig@8400
    93
    {
dludwig@8416
    94
     "direct3d 11.1",
dludwig@8400
    95
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
dludwig@8400
    96
     1,
dludwig@8416
    97
     {SDL_PIXELFORMAT_RGB888},
dludwig@8400
    98
     0,
dludwig@8400
    99
     0}
dludwig@8400
   100
    };
dludwig@8400
   101
}
dludwig@8400
   102
dludwig@8400
   103
//typedef struct
dludwig@8400
   104
//{
dludwig@8400
   105
//    float x, y, z;
dludwig@8400
   106
//    DWORD color;
dludwig@8400
   107
//    float u, v;
dludwig@8400
   108
//} Vertex;
dludwig@8400
   109
dludwig@8400
   110
SDL_Renderer *
dludwig@8400
   111
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
   112
{
dludwig@8400
   113
    SDL_Renderer *renderer;
dludwig@8400
   114
    D3D11_RenderData *data;
dludwig@8400
   115
dludwig@8400
   116
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   117
    if (!renderer) {
dludwig@8400
   118
        SDL_OutOfMemory();
dludwig@8400
   119
        return NULL;
dludwig@8400
   120
    }
dludwig@8400
   121
    SDL_zerop(renderer);
dludwig@8400
   122
dludwig@8400
   123
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   124
    if (!data) {
dludwig@8400
   125
        SDL_OutOfMemory();
dludwig@8400
   126
        return NULL;
dludwig@8400
   127
    }
dludwig@8410
   128
    data->featureLevel = (D3D_FEATURE_LEVEL) 0;
dludwig@8410
   129
    data->vertexCount = 0;
dludwig@8410
   130
    data->loadingComplete = false;
dludwig@8412
   131
    data->windowSizeInDIPs = XMFLOAT2(0, 0);
dludwig@8412
   132
    data->renderTargetSize = XMFLOAT2(0, 0);
dludwig@8400
   133
dludwig@8415
   134
    renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8416
   135
    renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8416
   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@8416
   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@8416
   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@8416
   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@8416
   169
    // TODO, WinRT: fill in renderer->info.texture_formats where appropriate
dludwig@8416
   170
dludwig@8400
   171
    return renderer;
dludwig@8400
   172
}
dludwig@8400
   173
dludwig@8413
   174
static void
dludwig@8413
   175
D3D11_DestroyRenderer(SDL_Renderer * renderer)
dludwig@8413
   176
{
dludwig@8413
   177
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8413
   178
    if (data) {
dludwig@8413
   179
        delete data;
dludwig@8413
   180
        data = NULL;
dludwig@8413
   181
    }
dludwig@8413
   182
}
dludwig@8413
   183
dludwig@8410
   184
static bool
dludwig@8410
   185
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
dludwig@8410
   186
{
dludwig@8410
   187
    ifstream in(fileName, ios::in | ios::binary);
dludwig@8410
   188
    if (!in) {
dludwig@8410
   189
        return false;
dludwig@8410
   190
    }
dludwig@8410
   191
dludwig@8410
   192
    in.seekg(0, ios::end);
dludwig@8410
   193
    out.resize((size_t) in.tellg());
dludwig@8410
   194
    in.seekg(0, ios::beg);
dludwig@8410
   195
    in.read(&out[0], out.size());
dludwig@8410
   196
    return in.good();
dludwig@8410
   197
}
dludwig@8410
   198
dludwig@8410
   199
static bool
dludwig@8410
   200
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
dludwig@8410
   201
{
dludwig@8410
   202
    wstring fileName;
dludwig@8410
   203
dludwig@8410
   204
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8410
   205
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   206
    fileName += L"\\SDL_VS2012_WinRT\\";
dludwig@8410
   207
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
dludwig@8410
   208
    fileName = SDL_WinRTGetInstalledLocationPath();
dludwig@8410
   209
    fileName += L"\\";
dludwig@8410
   210
#endif
dludwig@8410
   211
    // WinRT, TODO: test Direct3D 11.1 shader loading on Win32
dludwig@8410
   212
    fileName += shaderName;
dludwig@8410
   213
    return D3D11_ReadFileContents(fileName, out);
dludwig@8410
   214
}
dludwig@8410
   215
dludwig@8414
   216
// Create resources that depend on the device.
dludwig@8410
   217
HRESULT
dludwig@8412
   218
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
dludwig@8410
   219
{
dludwig@8410
   220
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8410
   221
dludwig@8410
   222
    // This flag adds support for surfaces with a different color channel ordering
dludwig@8410
   223
    // than the API default. It is required for compatibility with Direct2D.
dludwig@8410
   224
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
dludwig@8410
   225
dludwig@8410
   226
#if defined(_DEBUG)
dludwig@8410
   227
    // If the project is in a debug build, enable debugging via SDK Layers with this flag.
dludwig@8410
   228
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
dludwig@8410
   229
#endif
dludwig@8410
   230
dludwig@8410
   231
    // This array defines the set of DirectX hardware feature levels this app will support.
dludwig@8410
   232
    // Note the ordering should be preserved.
dludwig@8410
   233
    // Don't forget to declare your application's minimum required feature level in its
dludwig@8410
   234
    // description.  All applications are assumed to support 9.1 unless otherwise stated.
dludwig@8410
   235
    D3D_FEATURE_LEVEL featureLevels[] = 
dludwig@8410
   236
    {
dludwig@8410
   237
        D3D_FEATURE_LEVEL_11_1,
dludwig@8410
   238
        D3D_FEATURE_LEVEL_11_0,
dludwig@8410
   239
        D3D_FEATURE_LEVEL_10_1,
dludwig@8410
   240
        D3D_FEATURE_LEVEL_10_0,
dludwig@8410
   241
        D3D_FEATURE_LEVEL_9_3,
dludwig@8410
   242
        D3D_FEATURE_LEVEL_9_2,
dludwig@8410
   243
        D3D_FEATURE_LEVEL_9_1
dludwig@8410
   244
    };
dludwig@8410
   245
dludwig@8410
   246
    // Create the Direct3D 11 API device object and a corresponding context.
dludwig@8410
   247
    ComPtr<ID3D11Device> device;
dludwig@8410
   248
    ComPtr<ID3D11DeviceContext> context;
dludwig@8410
   249
    HRESULT result = S_OK;
dludwig@8410
   250
    result = D3D11CreateDevice(
dludwig@8410
   251
        nullptr, // Specify nullptr to use the default adapter.
dludwig@8410
   252
        D3D_DRIVER_TYPE_HARDWARE,
dludwig@8410
   253
        nullptr,
dludwig@8410
   254
        creationFlags, // Set set debug and Direct2D compatibility flags.
dludwig@8410
   255
        featureLevels, // List of feature levels this app can support.
dludwig@8410
   256
        ARRAYSIZE(featureLevels),
dludwig@8410
   257
        D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
dludwig@8410
   258
        &device, // Returns the Direct3D device created.
dludwig@8410
   259
        &data->featureLevel, // Returns feature level of device created.
dludwig@8410
   260
        &context // Returns the device immediate context.
dludwig@8410
   261
        );
dludwig@8410
   262
    if (FAILED(result)) {
dludwig@8410
   263
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   264
        return result;
dludwig@8410
   265
    }
dludwig@8410
   266
dludwig@8410
   267
    // Get the Direct3D 11.1 API device and context interfaces.
dludwig@8410
   268
    Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
dludwig@8410
   269
    result = device.As(&(data->d3dDevice));
dludwig@8410
   270
    if (FAILED(result)) {
dludwig@8410
   271
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   272
        return result;
dludwig@8410
   273
    }
dludwig@8410
   274
dludwig@8410
   275
    result = context.As(&data->d3dContext);
dludwig@8410
   276
    if (FAILED(result)) {
dludwig@8410
   277
        return result;
dludwig@8410
   278
    }
dludwig@8410
   279
dludwig@8410
   280
    // Start loading GPU shaders:
dludwig@8410
   281
    vector<char> fileData;
dludwig@8410
   282
dludwig@8410
   283
    //
dludwig@8410
   284
    // Load in SDL's one and only vertex shader:
dludwig@8410
   285
    //
dludwig@8410
   286
    if (!D3D11_ReadShaderContents(L"SimpleVertexShader.cso", fileData)) {
dludwig@8410
   287
        SDL_SetError("Unable to open SDL's vertex shader file.");
dludwig@8410
   288
        return E_FAIL;
dludwig@8410
   289
    }
dludwig@8410
   290
dludwig@8410
   291
    result = data->d3dDevice->CreateVertexShader(
dludwig@8410
   292
        &fileData[0],
dludwig@8410
   293
        fileData.size(),
dludwig@8410
   294
        nullptr,
dludwig@8410
   295
        &data->vertexShader
dludwig@8410
   296
        );
dludwig@8410
   297
    if (FAILED(result)) {
dludwig@8410
   298
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   299
        return result;
dludwig@8410
   300
    }
dludwig@8410
   301
dludwig@8410
   302
    //
dludwig@8410
   303
    // Create an input layout for SDL's vertex shader:
dludwig@8410
   304
    //
dludwig@8410
   305
    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8410
   306
    {
dludwig@8410
   307
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   308
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8410
   309
    };
dludwig@8410
   310
dludwig@8410
   311
    result = data->d3dDevice->CreateInputLayout(
dludwig@8410
   312
        vertexDesc,
dludwig@8410
   313
        ARRAYSIZE(vertexDesc),
dludwig@8410
   314
        &fileData[0],
dludwig@8410
   315
        fileData.size(),
dludwig@8410
   316
        &data->inputLayout
dludwig@8410
   317
        );
dludwig@8410
   318
    if (FAILED(result)) {
dludwig@8410
   319
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   320
        return result;
dludwig@8410
   321
    }
dludwig@8410
   322
dludwig@8410
   323
    //
dludwig@8410
   324
    // Load in SDL's one and only pixel shader (for now, more are likely to follow):
dludwig@8410
   325
    //
dludwig@8410
   326
    if (!D3D11_ReadShaderContents(L"SimplePixelShader.cso", fileData)) {
dludwig@8410
   327
        SDL_SetError("Unable to open SDL's pixel shader file.");
dludwig@8410
   328
        return E_FAIL;
dludwig@8410
   329
    }
dludwig@8410
   330
dludwig@8410
   331
    result = data->d3dDevice->CreatePixelShader(
dludwig@8410
   332
        &fileData[0],
dludwig@8410
   333
        fileData.size(),
dludwig@8410
   334
        nullptr,
dludwig@8410
   335
        &data->pixelShader
dludwig@8410
   336
        );
dludwig@8410
   337
    if (FAILED(result)) {
dludwig@8410
   338
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   339
        return result;
dludwig@8410
   340
    }
dludwig@8410
   341
dludwig@8410
   342
    //
dludwig@8410
   343
    // Create a vertex buffer:
dludwig@8410
   344
    //
dludwig@8410
   345
    VertexPositionColor vertices[] = 
dludwig@8410
   346
    {
dludwig@8410
   347
        {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
dludwig@8410
   348
        {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
dludwig@8410
   349
        {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
dludwig@8410
   350
        {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
dludwig@8410
   351
    };
dludwig@8410
   352
dludwig@8410
   353
    data->vertexCount = ARRAYSIZE(vertices);
dludwig@8410
   354
dludwig@8410
   355
    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8410
   356
    vertexBufferData.pSysMem = vertices;
dludwig@8410
   357
    vertexBufferData.SysMemPitch = 0;
dludwig@8410
   358
    vertexBufferData.SysMemSlicePitch = 0;
dludwig@8410
   359
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER);
dludwig@8410
   360
    result = data->d3dDevice->CreateBuffer(
dludwig@8410
   361
        &vertexBufferDesc,
dludwig@8410
   362
        &vertexBufferData,
dludwig@8410
   363
        &data->vertexBuffer
dludwig@8410
   364
        );
dludwig@8410
   365
    if (FAILED(result)) {
dludwig@8410
   366
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   367
        return result;
dludwig@8410
   368
    }
dludwig@8410
   369
dludwig@8410
   370
    //
dludwig@8410
   371
    // Create a sampler to use when drawing textures:
dludwig@8410
   372
    //
dludwig@8410
   373
    D3D11_SAMPLER_DESC samplerDesc;
dludwig@8410
   374
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8410
   375
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   376
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   377
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8410
   378
    samplerDesc.MipLODBias = 0.0f;
dludwig@8410
   379
    samplerDesc.MaxAnisotropy = 1;
dludwig@8410
   380
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8410
   381
    samplerDesc.BorderColor[0] = 0.0f;
dludwig@8410
   382
    samplerDesc.BorderColor[1] = 0.0f;
dludwig@8410
   383
    samplerDesc.BorderColor[2] = 0.0f;
dludwig@8410
   384
    samplerDesc.BorderColor[3] = 0.0f;
dludwig@8410
   385
    samplerDesc.MinLOD = 0.0f;
dludwig@8410
   386
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8410
   387
    result = data->d3dDevice->CreateSamplerState(
dludwig@8410
   388
        &samplerDesc,
dludwig@8410
   389
        &data->mainSampler
dludwig@8410
   390
        );
dludwig@8410
   391
    if (FAILED(result)) {
dludwig@8410
   392
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8410
   393
        return result;
dludwig@8410
   394
    }
dludwig@8410
   395
dludwig@8410
   396
    //
dludwig@8410
   397
    // All done!
dludwig@8410
   398
    //
dludwig@8410
   399
    data->loadingComplete = true;       // This variable can probably be factored-out
dludwig@8410
   400
    return S_OK;
dludwig@8410
   401
}
dludwig@8410
   402
dludwig@8412
   403
#ifdef __WINRT__
dludwig@8412
   404
dludwig@8415
   405
static CoreWindow ^
dludwig@8412
   406
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
dludwig@8412
   407
{
dludwig@8412
   408
    SDL_Window * sdlWindow = renderer->window;
dludwig@8412
   409
    if ( ! renderer->window ) {
dludwig@8412
   410
        return nullptr;
dludwig@8412
   411
    }
dludwig@8412
   412
dludwig@8412
   413
    SDL_SysWMinfo sdlWindowInfo;
dludwig@8412
   414
    SDL_VERSION(&sdlWindowInfo.version);
dludwig@8412
   415
    if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
dludwig@8412
   416
        return nullptr;
dludwig@8412
   417
    }
dludwig@8412
   418
dludwig@8412
   419
    if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
dludwig@8412
   420
        return nullptr;
dludwig@8412
   421
    }
dludwig@8412
   422
dludwig@8412
   423
    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
dludwig@8412
   424
    if ( ! coreWindowPointer ) {
dludwig@8412
   425
        return nullptr;
dludwig@8412
   426
    }
dludwig@8412
   427
dludwig@8412
   428
    return *coreWindowPointer;
dludwig@8412
   429
}
dludwig@8412
   430
dludwig@8414
   431
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
dludwig@8412
   432
static float
dludwig@8412
   433
D3D11_ConvertDipsToPixels(float dips)
dludwig@8412
   434
{
dludwig@8412
   435
    static const float dipsPerInch = 96.0f;
dludwig@8412
   436
    return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
dludwig@8412
   437
}
dludwig@8412
   438
#endif
dludwig@8412
   439
dludwig@8414
   440
// Initialize all resources that change when the window's size changes.
dludwig@8412
   441
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
dludwig@8412
   442
HRESULT
dludwig@8412
   443
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
dludwig@8412
   444
{
dludwig@8412
   445
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8412
   446
    HRESULT result = S_OK;
dludwig@8412
   447
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8412
   448
dludwig@8412
   449
    // Store the window bounds so the next time we get a SizeChanged event we can
dludwig@8412
   450
    // avoid rebuilding everything if the size is identical.
dludwig@8412
   451
    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
dludwig@8412
   452
    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
dludwig@8412
   453
dludwig@8412
   454
    // Calculate the necessary swap chain and render target size in pixels.
dludwig@8412
   455
    float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
dludwig@8412
   456
    float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
dludwig@8412
   457
dludwig@8412
   458
    // The width and height of the swap chain must be based on the window's
dludwig@8412
   459
    // landscape-oriented width and height. If the window is in a portrait
dludwig@8412
   460
    // orientation, the dimensions must be reversed.
dludwig@8412
   461
    data->orientation = DisplayProperties::CurrentOrientation;
dludwig@8412
   462
    bool swapDimensions =
dludwig@8412
   463
        data->orientation == DisplayOrientations::Portrait ||
dludwig@8412
   464
        data->orientation == DisplayOrientations::PortraitFlipped;
dludwig@8412
   465
    data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
dludwig@8412
   466
    data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
dludwig@8412
   467
dludwig@8412
   468
    if(data->swapChain != nullptr)
dludwig@8412
   469
    {
dludwig@8412
   470
        // If the swap chain already exists, resize it.
dludwig@8412
   471
        result = data->swapChain->ResizeBuffers(
dludwig@8412
   472
            2, // Double-buffered swap chain.
dludwig@8412
   473
            static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   474
            static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   475
            DXGI_FORMAT_B8G8R8A8_UNORM,
dludwig@8412
   476
            0
dludwig@8412
   477
            );
dludwig@8412
   478
        if (FAILED(result)) {
dludwig@8412
   479
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   480
            return result;
dludwig@8412
   481
        }
dludwig@8412
   482
    }
dludwig@8412
   483
    else
dludwig@8412
   484
    {
dludwig@8412
   485
        // Otherwise, create a new one using the same adapter as the existing Direct3D device.
dludwig@8412
   486
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
dludwig@8412
   487
        swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
dludwig@8412
   488
        swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
dludwig@8412
   489
        swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
dludwig@8412
   490
        swapChainDesc.Stereo = false;
dludwig@8412
   491
        swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
dludwig@8412
   492
        swapChainDesc.SampleDesc.Quality = 0;
dludwig@8412
   493
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dludwig@8412
   494
        swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
dludwig@8412
   495
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8412
   496
        swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
dludwig@8412
   497
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
dludwig@8412
   498
#else
dludwig@8412
   499
        swapChainDesc.Scaling = DXGI_SCALING_NONE;
dludwig@8412
   500
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
dludwig@8412
   501
#endif
dludwig@8412
   502
        swapChainDesc.Flags = 0;
dludwig@8412
   503
dludwig@8412
   504
        ComPtr<IDXGIDevice1>  dxgiDevice;
dludwig@8412
   505
        result = data->d3dDevice.As(&dxgiDevice);
dludwig@8412
   506
        if (FAILED(result)) {
dludwig@8412
   507
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   508
            return result;
dludwig@8412
   509
        }
dludwig@8412
   510
dludwig@8412
   511
        ComPtr<IDXGIAdapter> dxgiAdapter;
dludwig@8412
   512
        result = dxgiDevice->GetAdapter(&dxgiAdapter);
dludwig@8412
   513
        if (FAILED(result)) {
dludwig@8412
   514
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   515
            return result;
dludwig@8412
   516
        }
dludwig@8412
   517
dludwig@8412
   518
        ComPtr<IDXGIFactory2> dxgiFactory;
dludwig@8412
   519
        result = dxgiAdapter->GetParent(
dludwig@8412
   520
            __uuidof(IDXGIFactory2), 
dludwig@8412
   521
            &dxgiFactory
dludwig@8412
   522
            );
dludwig@8412
   523
        if (FAILED(result)) {
dludwig@8412
   524
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   525
            return result;
dludwig@8412
   526
        }
dludwig@8412
   527
dludwig@8412
   528
        result = dxgiFactory->CreateSwapChainForCoreWindow(
dludwig@8412
   529
            data->d3dDevice.Get(),
dludwig@8412
   530
            reinterpret_cast<IUnknown*>(coreWindow),
dludwig@8412
   531
            &swapChainDesc,
dludwig@8412
   532
            nullptr, // Allow on all displays.
dludwig@8412
   533
            &data->swapChain
dludwig@8412
   534
            );
dludwig@8412
   535
        if (FAILED(result)) {
dludwig@8412
   536
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   537
            return result;
dludwig@8412
   538
        }
dludwig@8412
   539
            
dludwig@8412
   540
        // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
dludwig@8412
   541
        // ensures that the application will only render after each VSync, minimizing power consumption.
dludwig@8412
   542
        result = dxgiDevice->SetMaximumFrameLatency(1);
dludwig@8412
   543
        if (FAILED(result)) {
dludwig@8412
   544
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   545
            return result;
dludwig@8412
   546
        }
dludwig@8412
   547
    }
dludwig@8412
   548
    
dludwig@8412
   549
    // Set the proper orientation for the swap chain, and generate the
dludwig@8412
   550
    // 3D matrix transformation for rendering to the rotated swap chain.
dludwig@8412
   551
    DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
dludwig@8412
   552
    switch (data->orientation)
dludwig@8412
   553
    {
dludwig@8412
   554
        case DisplayOrientations::Landscape:
dludwig@8412
   555
            rotation = DXGI_MODE_ROTATION_IDENTITY;
dludwig@8412
   556
            data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
dludwig@8412
   557
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   558
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   559
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   560
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   561
                );
dludwig@8412
   562
            break;
dludwig@8412
   563
dludwig@8412
   564
        case DisplayOrientations::Portrait:
dludwig@8412
   565
            rotation = DXGI_MODE_ROTATION_ROTATE270;
dludwig@8412
   566
            data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
dludwig@8412
   567
                0.0f, 1.0f, 0.0f, 0.0f,
dludwig@8412
   568
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   569
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   570
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   571
                );
dludwig@8412
   572
            break;
dludwig@8412
   573
dludwig@8412
   574
        case DisplayOrientations::LandscapeFlipped:
dludwig@8412
   575
            rotation = DXGI_MODE_ROTATION_ROTATE180;
dludwig@8412
   576
            data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
dludwig@8412
   577
                -1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   578
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   579
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   580
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   581
                );
dludwig@8412
   582
            break;
dludwig@8412
   583
dludwig@8412
   584
        case DisplayOrientations::PortraitFlipped:
dludwig@8412
   585
            rotation = DXGI_MODE_ROTATION_ROTATE90;
dludwig@8412
   586
            data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
dludwig@8412
   587
                0.0f, -1.0f, 0.0f, 0.0f,
dludwig@8412
   588
                1.0f, 0.0f, 0.0f, 0.0f,
dludwig@8412
   589
                0.0f, 0.0f, 1.0f, 0.0f,
dludwig@8412
   590
                0.0f, 0.0f, 0.0f, 1.0f
dludwig@8412
   591
                );
dludwig@8412
   592
            break;
dludwig@8412
   593
dludwig@8412
   594
        default:
dludwig@8412
   595
            throw ref new Platform::FailureException();
dludwig@8412
   596
    }
dludwig@8412
   597
dludwig@8412
   598
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8412
   599
    // TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method.  Check if an alternative is available, or needed.
dludwig@8412
   600
    result = data->swapChain->SetRotation(rotation);
dludwig@8412
   601
    if (FAILED(result)) {
dludwig@8412
   602
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   603
        return result;
dludwig@8412
   604
    }
dludwig@8412
   605
#endif
dludwig@8412
   606
dludwig@8412
   607
    // Create a render target view of the swap chain back buffer.
dludwig@8412
   608
    ComPtr<ID3D11Texture2D> backBuffer;
dludwig@8412
   609
    result = data->swapChain->GetBuffer(
dludwig@8412
   610
        0,
dludwig@8412
   611
        __uuidof(ID3D11Texture2D),
dludwig@8412
   612
        &backBuffer
dludwig@8412
   613
        );
dludwig@8412
   614
    if (FAILED(result)) {
dludwig@8412
   615
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   616
        return result;
dludwig@8412
   617
    }
dludwig@8412
   618
dludwig@8412
   619
    result = data->d3dDevice->CreateRenderTargetView(
dludwig@8412
   620
        backBuffer.Get(),
dludwig@8412
   621
        nullptr,
dludwig@8412
   622
        &data->renderTargetView
dludwig@8412
   623
        );
dludwig@8412
   624
    if (FAILED(result)) {
dludwig@8412
   625
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   626
        return result;
dludwig@8412
   627
    }
dludwig@8412
   628
dludwig@8412
   629
    // Create a depth stencil view.
dludwig@8412
   630
    CD3D11_TEXTURE2D_DESC depthStencilDesc(
dludwig@8412
   631
        DXGI_FORMAT_D24_UNORM_S8_UINT, 
dludwig@8412
   632
        static_cast<UINT>(data->renderTargetSize.x),
dludwig@8412
   633
        static_cast<UINT>(data->renderTargetSize.y),
dludwig@8412
   634
        1,
dludwig@8412
   635
        1,
dludwig@8412
   636
        D3D11_BIND_DEPTH_STENCIL
dludwig@8412
   637
        );
dludwig@8412
   638
dludwig@8412
   639
    ComPtr<ID3D11Texture2D> depthStencil;
dludwig@8412
   640
    result = data->d3dDevice->CreateTexture2D(
dludwig@8412
   641
        &depthStencilDesc,
dludwig@8412
   642
        nullptr,
dludwig@8412
   643
        &depthStencil
dludwig@8412
   644
        );
dludwig@8412
   645
    if (FAILED(result)) {
dludwig@8412
   646
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8412
   647
        return result;
dludwig@8412
   648
    }
dludwig@8412
   649
dludwig@8412
   650
    // Set the rendering viewport to target the entire window.
dludwig@8412
   651
    CD3D11_VIEWPORT viewport(
dludwig@8412
   652
        0.0f,
dludwig@8412
   653
        0.0f,
dludwig@8412
   654
        data->renderTargetSize.x,
dludwig@8412
   655
        data->renderTargetSize.y
dludwig@8412
   656
        );
dludwig@8412
   657
dludwig@8412
   658
    data->d3dContext->RSSetViewports(1, &viewport);
dludwig@8412
   659
dludwig@8412
   660
    return S_OK;
dludwig@8412
   661
}
dludwig@8412
   662
dludwig@8415
   663
// This method is called when the window's size changes.
dludwig@8414
   664
HRESULT
dludwig@8414
   665
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
dludwig@8414
   666
{
dludwig@8414
   667
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   668
    HRESULT result = S_OK;
dludwig@8414
   669
    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
dludwig@8414
   670
dludwig@8414
   671
    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
dludwig@8414
   672
        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
dludwig@8414
   673
        data->orientation != DisplayProperties::CurrentOrientation)
dludwig@8414
   674
    {
dludwig@8414
   675
        ID3D11RenderTargetView* nullViews[] = {nullptr};
dludwig@8414
   676
        data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
dludwig@8414
   677
        data->renderTargetView = nullptr;
dludwig@8414
   678
        data->d3dContext->Flush();
dludwig@8414
   679
        result = D3D11_CreateWindowSizeDependentResources(renderer);
dludwig@8414
   680
        if (FAILED(result)) {
dludwig@8414
   681
            WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   682
            return result;
dludwig@8414
   683
        }
dludwig@8414
   684
    }
dludwig@8414
   685
dludwig@8414
   686
    return S_OK;
dludwig@8414
   687
}
dludwig@8414
   688
dludwig@8414
   689
HRESULT
dludwig@8414
   690
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
dludwig@8414
   691
{
dludwig@8414
   692
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8414
   693
    HRESULT result = S_OK;
dludwig@8414
   694
dludwig@8415
   695
    // Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
dludwig@8414
   696
    data->windowSizeInDIPs.x = 0;
dludwig@8414
   697
    data->windowSizeInDIPs.y = 0;
dludwig@8414
   698
    data->swapChain = nullptr;
dludwig@8414
   699
dludwig@8414
   700
    result = D3D11_CreateDeviceResources(renderer);
dludwig@8414
   701
    if (FAILED(result)) {
dludwig@8414
   702
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   703
        return result;
dludwig@8414
   704
    }
dludwig@8414
   705
dludwig@8414
   706
    result = D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8414
   707
    if (FAILED(result)) {
dludwig@8414
   708
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8414
   709
        return result;
dludwig@8414
   710
    }
dludwig@8414
   711
dludwig@8414
   712
    return S_OK;
dludwig@8414
   713
}
dludwig@8414
   714
dludwig@8415
   715
static void
dludwig@8415
   716
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
dludwig@8415
   717
{
dludwig@8415
   718
    //D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8415
   719
dludwig@8415
   720
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
dludwig@8415
   721
        D3D11_UpdateForWindowSizeChange(renderer);
dludwig@8415
   722
    }
dludwig@8415
   723
}
dludwig@8415
   724
dludwig@8416
   725
static int
dludwig@8416
   726
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dludwig@8416
   727
{
dludwig@8416
   728
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   729
    D3D11_TextureData *textureData;
dludwig@8416
   730
    HRESULT result;
dludwig@8416
   731
dludwig@8416
   732
    textureData = new D3D11_TextureData;
dludwig@8416
   733
    if (!textureData) {
dludwig@8416
   734
        SDL_OutOfMemory();
dludwig@8416
   735
        return -1;
dludwig@8416
   736
    }
dludwig@8416
   737
    textureData->pixelFormat = SDL_AllocFormat(texture->format);
dludwig@8416
   738
dludwig@8416
   739
    texture->driverdata = textureData;
dludwig@8416
   740
dludwig@8416
   741
    const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
dludwig@8416
   742
dludwig@8416
   743
    D3D11_TEXTURE2D_DESC textureDesc = {0};
dludwig@8416
   744
    textureDesc.Width = texture->w;
dludwig@8416
   745
    textureDesc.Height = texture->h;
dludwig@8416
   746
    textureDesc.MipLevels = 1;
dludwig@8416
   747
    textureDesc.ArraySize = 1;
dludwig@8416
   748
    textureDesc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
dludwig@8416
   749
    textureDesc.SampleDesc.Count = 1;
dludwig@8416
   750
    textureDesc.SampleDesc.Quality = 0;
dludwig@8416
   751
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;
dludwig@8416
   752
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dludwig@8416
   753
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8416
   754
    textureDesc.MiscFlags = 0;
dludwig@8416
   755
dludwig@8416
   756
    const int numPixels = textureDesc.Width * textureDesc.Height;
dludwig@8416
   757
    std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
dludwig@8416
   758
dludwig@8416
   759
    // Fill the texture with a non-black color, for debugging purposes:
dludwig@8416
   760
    //for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
dludwig@8416
   761
    //    initialTexturePixels[i+0] = 0xff;
dludwig@8416
   762
    //    initialTexturePixels[i+1] = 0xff;
dludwig@8416
   763
    //    initialTexturePixels[i+2] = 0x00;
dludwig@8416
   764
    //    initialTexturePixels[i+3] = 0xff;
dludwig@8416
   765
    //}
dludwig@8416
   766
dludwig@8416
   767
    D3D11_SUBRESOURCE_DATA initialTextureData = {0};
dludwig@8416
   768
    initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
dludwig@8416
   769
    initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
dludwig@8416
   770
    initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
dludwig@8416
   771
    result = rendererData->d3dDevice->CreateTexture2D(
dludwig@8416
   772
        &textureDesc,
dludwig@8416
   773
        &initialTextureData,
dludwig@8416
   774
        &textureData->mainTexture
dludwig@8416
   775
        );
dludwig@8416
   776
    if (FAILED(result)) {
dludwig@8416
   777
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   778
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   779
        return -1;
dludwig@8416
   780
    }
dludwig@8416
   781
dludwig@8416
   782
    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
dludwig@8416
   783
    resourceViewDesc.Format = textureDesc.Format;
dludwig@8416
   784
    resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dludwig@8416
   785
    resourceViewDesc.Texture2D.MostDetailedMip = 0;
dludwig@8416
   786
    resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
dludwig@8416
   787
    result = rendererData->d3dDevice->CreateShaderResourceView(
dludwig@8416
   788
        textureData->mainTexture.Get(),
dludwig@8416
   789
        &resourceViewDesc,
dludwig@8416
   790
        &textureData->mainTextureResourceView
dludwig@8416
   791
        );
dludwig@8416
   792
    if (FAILED(result)) {
dludwig@8416
   793
        D3D11_DestroyTexture(renderer, texture);
dludwig@8416
   794
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   795
        return -1;
dludwig@8416
   796
    }
dludwig@8416
   797
dludwig@8416
   798
    return 0;
dludwig@8416
   799
}
dludwig@8416
   800
dludwig@8416
   801
static void
dludwig@8416
   802
D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8416
   803
                     SDL_Texture * texture)
dludwig@8416
   804
{
dludwig@8416
   805
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   806
dludwig@8416
   807
    if (textureData) {
dludwig@8416
   808
        if (textureData->pixelFormat) {
dludwig@8416
   809
            SDL_FreeFormat(textureData->pixelFormat);
dludwig@8416
   810
            textureData->pixelFormat = NULL;
dludwig@8416
   811
        }
dludwig@8416
   812
dludwig@8416
   813
        delete textureData;
dludwig@8416
   814
        texture = NULL;
dludwig@8416
   815
        texture->driverdata = NULL;
dludwig@8416
   816
    }
dludwig@8416
   817
}
dludwig@8416
   818
dludwig@8416
   819
static int
dludwig@8416
   820
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   821
                    const SDL_Rect * rect, const void *pixels,
dludwig@8416
   822
                    int pitch)
dludwig@8416
   823
{
dludwig@8416
   824
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   825
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   826
    HRESULT result = S_OK;
dludwig@8416
   827
dludwig@8416
   828
    D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8416
   829
    result = rendererData->d3dContext->Map(
dludwig@8416
   830
        textureData->mainTexture.Get(),
dludwig@8416
   831
        0,
dludwig@8416
   832
        D3D11_MAP_WRITE_DISCARD,
dludwig@8416
   833
        0,
dludwig@8416
   834
        &textureMemory
dludwig@8416
   835
        );
dludwig@8416
   836
    if (FAILED(result)) {
dludwig@8416
   837
        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
dludwig@8416
   838
        return -1;
dludwig@8416
   839
    }
dludwig@8416
   840
dludwig@8416
   841
    // Copy pixel data to the locked texture's memory:
dludwig@8416
   842
    for (int y = 0; y < rect->h; ++y) {
dludwig@8416
   843
        memcpy(
dludwig@8416
   844
            ((Uint8 *)textureMemory.pData) + (textureMemory.RowPitch * y),
dludwig@8416
   845
            ((Uint8 *)pixels) + (pitch * y),
dludwig@8416
   846
            pitch
dludwig@8416
   847
            );
dludwig@8416
   848
    }
dludwig@8416
   849
dludwig@8416
   850
    // Clean up a bit, then commit the texture's memory back to Direct3D:
dludwig@8416
   851
    rendererData->d3dContext->Unmap(
dludwig@8416
   852
        textureData->mainTexture.Get(),
dludwig@8416
   853
        0);
dludwig@8416
   854
dludwig@8416
   855
    return 0;
dludwig@8416
   856
}
dludwig@8416
   857
dludwig@8400
   858
static int
dludwig@8400
   859
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   860
{
dludwig@8400
   861
    return 0;
dludwig@8400
   862
}
dludwig@8400
   863
dludwig@8416
   864
static int
dludwig@8416
   865
D3D11_RenderClear(SDL_Renderer * renderer)
dludwig@8416
   866
{
dludwig@8416
   867
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   868
    const float colorRGBA[] = {
dludwig@8416
   869
        renderer->r,
dludwig@8416
   870
        renderer->g,
dludwig@8416
   871
        renderer->b,
dludwig@8416
   872
        renderer->a
dludwig@8416
   873
    };
dludwig@8416
   874
    data->d3dContext->ClearRenderTargetView(
dludwig@8416
   875
        data->renderTargetView.Get(),
dludwig@8416
   876
        colorRGBA
dludwig@8416
   877
        );
dludwig@8416
   878
    return 0;
dludwig@8416
   879
}
dludwig@8416
   880
dludwig@8416
   881
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8416
   882
                            const SDL_Rect * srcrect, const SDL_FRect * dstrect)
dludwig@8416
   883
{
dludwig@8416
   884
    D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8416
   885
    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
dludwig@8416
   886
    //HRESULT result = S_OK;
dludwig@8416
   887
dludwig@8416
   888
    rendererData->d3dContext->OMSetRenderTargets(
dludwig@8416
   889
        1,
dludwig@8416
   890
        rendererData->renderTargetView.GetAddressOf(),
dludwig@8416
   891
        nullptr
dludwig@8416
   892
        );
dludwig@8416
   893
dludwig@8416
   894
    UINT stride = sizeof(VertexPositionColor);
dludwig@8416
   895
    UINT offset = 0;
dludwig@8416
   896
    rendererData->d3dContext->IASetVertexBuffers(
dludwig@8416
   897
        0,
dludwig@8416
   898
        1,
dludwig@8416
   899
        rendererData->vertexBuffer.GetAddressOf(),
dludwig@8416
   900
        &stride,
dludwig@8416
   901
        &offset
dludwig@8416
   902
        );
dludwig@8416
   903
dludwig@8416
   904
    rendererData->d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
dludwig@8416
   905
dludwig@8416
   906
    rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
dludwig@8416
   907
dludwig@8416
   908
    rendererData->d3dContext->VSSetShader(
dludwig@8416
   909
        rendererData->vertexShader.Get(),
dludwig@8416
   910
        nullptr,
dludwig@8416
   911
        0
dludwig@8416
   912
        );
dludwig@8416
   913
dludwig@8416
   914
    rendererData->d3dContext->PSSetShader(
dludwig@8416
   915
        rendererData->pixelShader.Get(),
dludwig@8416
   916
        nullptr,
dludwig@8416
   917
        0
dludwig@8416
   918
        );
dludwig@8416
   919
dludwig@8416
   920
    rendererData->d3dContext->PSSetShaderResources(0, 1, textureData->mainTextureResourceView.GetAddressOf());
dludwig@8416
   921
dludwig@8416
   922
    rendererData->d3dContext->PSSetSamplers(0, 1, rendererData->mainSampler.GetAddressOf());
dludwig@8416
   923
dludwig@8416
   924
    rendererData->d3dContext->Draw(4, 0);
dludwig@8416
   925
dludwig@8416
   926
    return 0;
dludwig@8416
   927
}
dludwig@8416
   928
dludwig@8401
   929
static void
dludwig@8401
   930
D3D11_RenderPresent(SDL_Renderer * renderer)
dludwig@8401
   931
{
dludwig@8401
   932
    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
dludwig@8401
   933
dludwig@8401
   934
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8401
   935
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   936
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   937
    // frames that will never be displayed to the screen.
dludwig@8401
   938
    HRESULT hr = data->swapChain->Present(1, 0);
dludwig@8401
   939
#else
dludwig@8401
   940
    // The application may optionally specify "dirty" or "scroll"
dludwig@8401
   941
    // rects to improve efficiency in certain scenarios.
dludwig@8401
   942
    // This option is not available on Windows Phone 8, to note.
dludwig@8401
   943
    DXGI_PRESENT_PARAMETERS parameters = {0};
dludwig@8401
   944
    parameters.DirtyRectsCount = 0;
dludwig@8401
   945
    parameters.pDirtyRects = nullptr;
dludwig@8401
   946
    parameters.pScrollRect = nullptr;
dludwig@8401
   947
    parameters.pScrollOffset = nullptr;
dludwig@8401
   948
    
dludwig@8401
   949
    // The first argument instructs DXGI to block until VSync, putting the application
dludwig@8401
   950
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
dludwig@8401
   951
    // frames that will never be displayed to the screen.
dludwig@8401
   952
    HRESULT hr = data->swapChain->Present1(1, 0, &parameters);
dludwig@8401
   953
#endif
dludwig@8401
   954
dludwig@8401
   955
    // Discard the contents of the render target.
dludwig@8401
   956
    // This is a valid operation only when the existing contents will be entirely
dludwig@8401
   957
    // overwritten. If dirty or scroll rects are used, this call should be removed.
dludwig@8401
   958
    data->d3dContext->DiscardView(data->renderTargetView.Get());
dludwig@8401
   959
dludwig@8401
   960
    // If the device was removed either by a disconnect or a driver upgrade, we 
dludwig@8401
   961
    // must recreate all device resources.
dludwig@8414
   962
    //
dludwig@8414
   963
    // 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
   964
    if (hr == DXGI_ERROR_DEVICE_REMOVED)
dludwig@8401
   965
    {
dludwig@8414
   966
        hr = D3D11_HandleDeviceLost(renderer);
dludwig@8414
   967
        if (FAILED(hr)) {
dludwig@8414
   968
            WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8414
   969
        }
dludwig@8401
   970
    }
dludwig@8401
   971
    else
dludwig@8401
   972
    {
dludwig@8401
   973
        WIN_SetErrorFromHRESULT(__FUNCTION__, hr);
dludwig@8401
   974
    }
dludwig@8401
   975
}
dludwig@8401
   976
dludwig@8400
   977
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
   978
dludwig@8400
   979
/* vi: set ts=4 sw=4 expandtab: */