direct3d11.c 58.3 KB
Newer Older
Martell Malone's avatar
Martell Malone committed
1 2 3 4 5 6
/*****************************************************************************
 * direct3d11.c: Windows Direct3D11 video output module
 *****************************************************************************
 * Copyright (C) 2014-2015 VLC authors and VideoLAN
 *
 * Authors: Martell Malone <martellmalone@gmail.com>
7
 *          Steve Lhomme <robux4@gmail.com>
Martell Malone's avatar
Martell Malone committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>

32 33
#include <assert.h>

Martell Malone's avatar
Martell Malone committed
34
#define COBJMACROS
35
#define INITGUID
Martell Malone's avatar
Martell Malone committed
36 37 38 39 40 41 42
#include <d3d11.h>

/* avoided until we can pass ISwapchainPanel without c++/cx mode
# include <windows.ui.xaml.media.dxinterop.h> */

#include "common.h"

43 44
#include "../../video_chroma/dxgi_fmt.h"

Martell Malone's avatar
Martell Malone committed
45
#if !VLC_WINSTORE_APP
46 47 48 49 50
# if USE_DXGI
#  define D3D11CreateDeviceAndSwapChain(args...) sys->OurD3D11CreateDeviceAndSwapChain(args)
# else
#  define D3D11CreateDevice(args...)             sys->OurD3D11CreateDevice(args)
# endif
Martell Malone's avatar
Martell Malone committed
51 52 53
# define D3DCompile(args...)                    sys->OurD3DCompile(args)
#endif

54 55 56
DEFINE_GUID(GUID_SWAPCHAIN_WIDTH,  0xf1b59347, 0x1643, 0x411a, 0xad, 0x6b, 0xc7, 0x80, 0x17, 0x7a, 0x06, 0xb6);
DEFINE_GUID(GUID_SWAPCHAIN_HEIGHT, 0x6ea976a0, 0x9d60, 0x4bb7, 0xa5, 0xa9, 0x7d, 0xd1, 0x18, 0x7f, 0xc9, 0xbd);

Martell Malone's avatar
Martell Malone committed
57 58 59 60
static int  Open(vlc_object_t *);
static void Close(vlc_object_t *);

#define D3D11_HELP N_("Recommended video output for Windows 8 and later versions")
61 62 63
#define HW_BLENDING_TEXT N_("Use hardware blending support")
#define HW_BLENDING_LONGTEXT N_(\
    "Try to use hardware acceleration for subtitle/OSD blending.")
Martell Malone's avatar
Martell Malone committed
64 65 66 67 68 69 70

vlc_module_begin ()
    set_shortname("Direct3D11")
    set_description(N_("Direct3D11 video output"))
    set_help(D3D11_HELP)
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
71 72 73

    add_bool("direct3d11-hw-blending", true, HW_BLENDING_TEXT, HW_BLENDING_LONGTEXT, true)

74
#if VLC_WINSTORE_APP
75 76 77
    add_integer("winrt-d3ddevice",     0x0, NULL, NULL, true); /* ID3D11Device*        */
    add_integer("winrt-d3dcontext",    0x0, NULL, NULL, true); /* ID3D11DeviceContext* */
    add_integer("winrt-swapchain",     0x0, NULL, NULL, true); /* IDXGISwapChain1*     */
78 79
#endif

Martell Malone's avatar
Martell Malone committed
80 81 82 83 84
    set_capability("vout display", 240)
    add_shortcut("direct3d11")
    set_callbacks(Open, Close)
vlc_module_end ()

85
#ifdef HAVE_ID3D11VIDEODECODER 
86
/* VLC_CODEC_D3D11_OPAQUE */
Martell Malone's avatar
Martell Malone committed
87 88
struct picture_sys_t
{
89 90 91
    ID3D11VideoDecoderOutputView  *decoder; /* may be NULL for pictures from the pool */
    ID3D11Texture2D               *texture;
    ID3D11DeviceContext           *context;
Martell Malone's avatar
Martell Malone committed
92
};
93
#endif
Martell Malone's avatar
Martell Malone committed
94

95 96 97 98 99 100 101
/* internal picture_t pool  */
typedef struct
{
    ID3D11Texture2D               *texture;
    vout_display_t                *vd;
} picture_sys_pool_t;

102 103
/* matches the D3D11_INPUT_ELEMENT_DESC we setup */
typedef struct d3d_vertex_t {
104
    struct {
105 106 107 108 109 110 111
        FLOAT x;
        FLOAT y;
        FLOAT z;
    } position;
    struct {
        FLOAT x;
        FLOAT y;
112
    } texture;
113
    FLOAT       opacity;
114 115
} d3d_vertex_t;

116 117
#define RECTWidth(r)   (int)((r).right - (r).left)
#define RECTHeight(r)  (int)((r).bottom - (r).top)
118

Martell Malone's avatar
Martell Malone committed
119 120 121
static int  Open(vlc_object_t *);
static void Close(vlc_object_t *object);

122 123
static picture_pool_t *Pool(vout_display_t *vd, unsigned count);

Martell Malone's avatar
Martell Malone committed
124 125 126
static void Prepare(vout_display_t *, picture_t *, subpicture_t *subpicture);
static void Display(vout_display_t *, picture_t *, subpicture_t *subpicture);

127
static HINSTANCE Direct3D11LoadShaderLibrary(void);
Martell Malone's avatar
Martell Malone committed
128 129 130 131 132 133 134 135
static void Direct3D11Destroy(vout_display_t *);

static int  Direct3D11Open (vout_display_t *, video_format_t *);
static void Direct3D11Close(vout_display_t *);

static int  Direct3D11CreateResources (vout_display_t *, video_format_t *);
static void Direct3D11DestroyResources(vout_display_t *);

136 137 138 139 140
static int  Direct3D11CreatePool (vout_display_t *, video_format_t *);
static void Direct3D11DestroyPool(vout_display_t *);

static void DestroyDisplayPicture(picture_t *);
static void DestroyDisplayPoolPicture(picture_t *);
Martell Malone's avatar
Martell Malone committed
141
static int  Direct3D11MapTexture(picture_t *);
142 143
static void Direct3D11DeleteRegions(int, picture_t **);
static int Direct3D11MapSubpicture(vout_display_t *, int *, picture_t ***, subpicture_t *);
Martell Malone's avatar
Martell Malone committed
144

145
static int AllocQuad(vout_display_t *, const video_format_t *, d3d_quad_t *,
146
                     d3d_quad_cfg_t *, ID3D11PixelShader *);
147
static void ReleaseQuad(d3d_quad_t *);
148
static void UpdatePicQuadPosition(vout_display_t *);
149
static void UpdateQuadOpacity(vout_display_t *, const d3d_quad_t *, float);
150

151 152
static void Manage(vout_display_t *vd);

153
/* All the #if USE_DXGI contain an alternative method to setup dx11
Martell Malone's avatar
Martell Malone committed
154
   They both need to be benchmarked to see which performs better */
155
#if USE_DXGI
Martell Malone's avatar
Martell Malone committed
156 157 158 159 160 161 162 163 164 165 166
/* I have no idea why MS decided dxgi headers do not define this
   As they do have prototypes for d3d11 functions */
typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
#endif

/* TODO: Move to a direct3d11_shaders header */
static const char* globVertexShaderDefault = "\
  struct VS_INPUT\
  {\
    float4 Position   : POSITION;\
    float2 Texture    : TEXCOORD0;\
167
    float  Opacity    : OPACITY;\
Martell Malone's avatar
Martell Malone committed
168 169 170 171 172 173
  };\
  \
  struct VS_OUTPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
174
    float  Opacity    : OPACITY;\
Martell Malone's avatar
Martell Malone committed
175 176 177 178 179 180 181
  };\
  \
  VS_OUTPUT VS( VS_INPUT In )\
  {\
    VS_OUTPUT Output;\
    Output.Position = float4(In.Position.xy, 0.0f, 1.0f);\
    Output.Texture = In.Texture;\
182
    Output.Opacity = In.Opacity;\
Martell Malone's avatar
Martell Malone committed
183 184 185 186 187 188 189 190 191 192 193 194
    return Output;\
  }\
";

static const char* globPixelShaderDefault = "\
  Texture2D shaderTexture;\
  SamplerState SampleType;\
  \
  struct PS_INPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
195
    float  Opacity    : OPACITY;\
Martell Malone's avatar
Martell Malone committed
196 197 198 199
  };\
  \
  float4 PS( PS_INPUT In ) : SV_TARGET\
  {\
200 201 202 203 204
    float4 rgba; \
    \
    rgba = shaderTexture.Sample(SampleType, In.Texture);\
    rgba.a = rgba.a * In.Opacity;\
    return rgba; \
Martell Malone's avatar
Martell Malone committed
205 206 207
  }\
";

208 209 210 211 212 213 214 215 216
static const char *globPixelShaderBiplanarI420_BT601_2RGB = "\
  Texture2D shaderTextureY;\
  Texture2D shaderTextureUV;\
  SamplerState SampleType;\
  \
  struct PS_INPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
217
    float  Opacity    : OPACITY;\
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  };\
  \
  float4 PS( PS_INPUT In ) : SV_TARGET\
  {\
    float Y;\
    float UCb;\
    float VCr;\
    float2 UCbPos;\
    float2 VCrPos;\
    float4 rgba;\
    \
    Y  = shaderTextureY.Sample(SampleType, In.Texture).x;\
    \
    VCrPos = In.Texture / 2;\
    VCr = shaderTextureUV.Sample(SampleType, VCrPos).x;\
    \
    UCbPos = In.Texture / 2;\
    UCbPos.y = UCbPos.y + 0.5;\
    UCb = shaderTextureUV.Sample(SampleType, UCbPos).x;\
    \
    Y = 1.164383561643836 * (Y - 0.0625);\
    UCb = UCb - 0.5;\
    VCr = VCr - 0.5;\
    \
    rgba.x = saturate(Y + 1.596026785714286 * VCr);\
    rgba.y = saturate(Y - 0.812967647237771 * VCr - 0.391762290094914 * UCb);\
    rgba.z = saturate(Y + 2.017232142857142 * UCb);\
245
    rgba.a = In.Opacity;\
246 247 248 249 250 251 252 253 254 255 256 257 258
    return rgba;\
  }\
";

static const char *globPixelShaderBiplanarI420_BT709_2RGB = "\
  Texture2D shaderTextureY;\
  Texture2D shaderTextureUV;\
  SamplerState SampleType;\
  \
  struct PS_INPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
259
    float  Opacity    : OPACITY;\
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  };\
  \
  float4 PS( PS_INPUT In ) : SV_TARGET\
  {\
    float Y;\
    float UCb;\
    float VCr;\
    float2 UCbPos;\
    float2 VCrPos;\
    float4 rgba;\
    \
    Y  = shaderTextureY.Sample(SampleType, In.Texture).x;\
    \
    VCrPos = In.Texture / 2;\
    VCr = shaderTextureUV.Sample(SampleType, VCrPos).x;\
    \
    UCbPos = In.Texture / 2;\
    UCbPos.y = UCbPos.y + 0.5;\
    UCb = shaderTextureUV.Sample(SampleType, UCbPos).x;\
    \
    Y = 1.164383561643836 * (Y - 0.0625);\
    UCb = UCb - 0.5;\
    VCr = VCr - 0.5;\
    \
    rgba.x = saturate(Y + 1.792741071428571 * VCr);\
    rgba.y = saturate(Y - 0.532909328559444 * VCr - 0.21324861427373 * UCb);\
    rgba.z = saturate(Y + 2.112401785714286 * UCb);\
287
    rgba.a = In.Opacity;\
288 289 290 291
    return rgba;\
  }\
";

292
static const char *globPixelShaderBiplanarYUV_BT601_2RGB = "\
Martell Malone's avatar
Martell Malone committed
293 294 295 296 297 298 299 300
  Texture2D shaderTextureY;\
  Texture2D shaderTextureUV;\
  SamplerState SampleType;\
  \
  struct PS_INPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
301
    float  Opacity    : OPACITY;\
Martell Malone's avatar
Martell Malone committed
302 303 304 305 306 307 308 309
  };\
  \
  float4 PS( PS_INPUT In ) : SV_TARGET\
  {\
    float3 yuv;\
    float4 rgba;\
    yuv.x  = shaderTextureY.Sample(SampleType, In.Texture).x;\
    yuv.yz = shaderTextureUV.Sample(SampleType, In.Texture).xy;\
310
    yuv.x  = 1.164383561643836 * (yuv.x-0.0625);\
Martell Malone's avatar
Martell Malone committed
311 312
    yuv.y  = yuv.y - 0.5;\
    yuv.z  = yuv.z - 0.5;\
313 314 315
    rgba.x = saturate(yuv.x + 1.596026785714286 * yuv.z);\
    rgba.y = saturate(yuv.x - 0.812967647237771 * yuv.z - 0.391762290094914 * yuv.y);\
    rgba.z = saturate(yuv.x + 2.017232142857142 * yuv.y);\
316
    rgba.a = In.Opacity;\
Martell Malone's avatar
Martell Malone committed
317 318 319 320
    return rgba;\
  }\
";

321 322 323 324 325 326 327 328 329
static const char *globPixelShaderBiplanarYUV_BT709_2RGB = "\
  Texture2D shaderTextureY;\
  Texture2D shaderTextureUV;\
  SamplerState SampleType;\
  \
  struct PS_INPUT\
  {\
    float4 Position   : SV_POSITION;\
    float2 Texture    : TEXCOORD0;\
330
    float  Opacity    : OPACITY;\
331 332 333 334 335 336 337 338 339 340 341 342 343 344
  };\
  \
  float4 PS( PS_INPUT In ) : SV_TARGET\
  {\
    float3 yuv;\
    float4 rgba;\
    yuv.x  = shaderTextureY.Sample(SampleType, In.Texture).x;\
    yuv.yz = shaderTextureUV.Sample(SampleType, In.Texture).xy;\
    yuv.x  = 1.164383561643836 * (yuv.x-0.0625);\
    yuv.y  = yuv.y - 0.5;\
    yuv.z  = yuv.z - 0.5;\
    rgba.x = saturate(yuv.x + 1.792741071428571 * yuv.z);\
    rgba.y = saturate(yuv.x - 0.532909328559444 * yuv.z - 0.21324861427373 * yuv.y);\
    rgba.z = saturate(yuv.x + 2.112401785714286 * yuv.y);\
345
    rgba.a = In.Opacity;\
346 347 348
    return rgba;\
  }\
";
349

Martell Malone's avatar
Martell Malone committed
350 351 352 353
static int Open(vlc_object_t *object)
{
    vout_display_t *vd = (vout_display_t *)object;

354 355 356 357 358 359 360 361 362 363 364 365 366 367
#if !VLC_WINSTORE_APP
    HINSTANCE hd3d11_dll = LoadLibrary(TEXT("D3D11.DLL"));
    if (!hd3d11_dll) {
        msg_Warn(vd, "cannot load d3d11.dll, aborting");
        return VLC_EGENERIC;
    }

    HINSTANCE hd3dcompiler_dll = Direct3D11LoadShaderLibrary();
    if (!hd3dcompiler_dll) {
        msg_Err(vd, "cannot load d3dcompiler.dll, aborting");
        FreeLibrary(hd3d11_dll);
        return VLC_EGENERIC;
    }

368
# if USE_DXGI
369 370 371 372 373 374 375 376
    HINSTANCE hdxgi_dll = LoadLibrary(TEXT("DXGI.DLL"));
    if (!hdxgi_dll) {
        msg_Warn(vd, "cannot load dxgi.dll, aborting");
        return VLC_EGENERIC;
    }
# endif

#else
377
    IDXGISwapChain1* dxgiswapChain  = var_InheritInteger(vd, "winrt-swapchain");
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    if (!dxgiswapChain)
        return VLC_EGENERIC;
    ID3D11Device* d3ddevice         = var_InheritInteger(vd, "winrt-d3ddevice");
    if (!d3ddevice)
        return VLC_EGENERIC;
    ID3D11DeviceContext* d3dcontext = var_InheritInteger(vd, "winrt-d3dcontext");
    if (!d3dcontext)
        return VLC_EGENERIC;
#endif

    vout_display_sys_t *sys = vd->sys = calloc(1, sizeof(vout_display_sys_t));
    if (!sys)
        return VLC_ENOMEM;

#if !VLC_WINSTORE_APP
    sys->hd3d11_dll       = hd3d11_dll;
    sys->hd3dcompiler_dll = hd3dcompiler_dll;

    sys->OurD3DCompile = (void *)GetProcAddress(sys->hd3dcompiler_dll, "D3DCompile");
    if (!sys->OurD3DCompile) {
        msg_Err(vd, "Cannot locate reference to D3DCompile in d3dcompiler DLL");
Martell Malone's avatar
Martell Malone committed
399 400 401 402
        Direct3D11Destroy(vd);
        return VLC_EGENERIC;
    }

403
# if USE_DXGI
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    sys->hdxgi_dll = hdxgi_dll;

    /* TODO : enable all dxgi versions from 1.3 -> 1.1 */
    PFN_CREATE_DXGI_FACTORY OurCreateDXGIFactory =
        (void *)GetProcAddress(sys->hdxgi_dll, "CreateDXGIFactory");
    if (!OurCreateDXGIFactory) {
        msg_Err(vd, "Cannot locate reference to CreateDXGIFactory in dxgi DLL");
        Direct3D11Destroy(vd);
        return VLC_EGENERIC;
    }

    /* TODO : detect the directx version supported and use IID_IDXGIFactory3 or 2 */
    HRESULT hr = OurCreateDXGIFactory(&IID_IDXGIFactory, (void **)&sys->dxgifactory);
    if (FAILED(hr)) {
        msg_Err(vd, "Could not create dxgi factory. (hr=0x%lX)", hr);
        Direct3D11Destroy(vd);
        return VLC_EGENERIC;
    }

    sys->OurD3D11CreateDeviceAndSwapChain =
        (void *)GetProcAddress(sys->hd3d11_dll, "D3D11CreateDeviceAndSwapChain");
    if (!sys->OurD3D11CreateDeviceAndSwapChain) {
        msg_Err(vd, "Cannot locate reference to D3D11CreateDeviceAndSwapChain in d3d11 DLL");
        Direct3D11Destroy(vd);
        return VLC_EGENERIC;
    }

# else
    sys->OurD3D11CreateDevice =
        (void *)GetProcAddress(sys->hd3d11_dll, "D3D11CreateDevice");
    if (!sys->OurD3D11CreateDevice) {
        msg_Err(vd, "Cannot locate reference to D3D11CreateDevice in d3d11 DLL");
        Direct3D11Destroy(vd);
        return VLC_EGENERIC;
    }
# endif

#else
    sys->dxgiswapChain = dxgiswapChain;
    sys->d3ddevice     = d3ddevice;
    sys->d3dcontext    = d3dcontext;
445 446 447
    IDXGISwapChain_AddRef     (sys->dxgiswapChain);
    ID3D11Device_AddRef       (sys->d3ddevice);
    ID3D11DeviceContext_AddRef(sys->d3dcontext);
448 449
#endif

Martell Malone's avatar
Martell Malone committed
450 451 452 453 454 455 456 457 458 459
    if (CommonInit(vd))
        goto error;

    video_format_t fmt;
    if (Direct3D11Open(vd, &fmt)) {
        msg_Err(vd, "Direct3D11 could not be opened");
        goto error;
    }

    vout_display_info_t info  = vd->info;
460
    info.is_slow              = fmt.i_chroma != VLC_CODEC_D3D11_OPAQUE;
Martell Malone's avatar
Martell Malone committed
461 462 463
    info.has_double_click     = true;
    info.has_hide_mouse       = false;
    info.has_event_thread     = true;
464
    info.has_pictures_invalid = fmt.i_chroma != VLC_CODEC_D3D11_OPAQUE;
Martell Malone's avatar
Martell Malone committed
465

466 467 468 469 470
    if (var_InheritBool(vd, "direct3d11-hw-blending") &&
        sys->d3dregion_format != DXGI_FORMAT_UNKNOWN)
        info.subpicture_chromas = sys->pSubpictureChromas;
    else
        info.subpicture_chromas = NULL;
Martell Malone's avatar
Martell Malone committed
471 472 473 474 475

    video_format_Clean(&vd->fmt);
    video_format_Copy(&vd->fmt, &fmt);
    vd->info = info;

476
    vd->pool    = Pool;
Martell Malone's avatar
Martell Malone committed
477 478 479
    vd->prepare = Prepare;
    vd->display = Display;
    vd->control = CommonControl;
480
    vd->manage  = Manage;
Martell Malone's avatar
Martell Malone committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

    msg_Dbg(vd, "Direct3D11 Open Succeeded");

    return VLC_SUCCESS;
error:
    Direct3D11Close(vd);
    CommonClean(vd);
    Direct3D11Destroy(vd);
    free(vd->sys);
    return VLC_EGENERIC;
}

static void Close(vlc_object_t *object)
{
    vout_display_t * vd = (vout_display_t *)object;

    Direct3D11Close(vd);
    CommonClean(vd);
    Direct3D11Destroy(vd);
    free(vd->sys);
}

503 504 505 506 507
static picture_pool_t *Pool(vout_display_t *vd, unsigned pool_size)
{
    if ( vd->sys->pool != NULL )
        return vd->sys->pool;

508
#ifdef HAVE_ID3D11VIDEODECODER 
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    picture_t**       pictures = NULL;
    unsigned          picture_count = 0;
    HRESULT           hr;

    ID3D10Multithread *pMultithread;
    hr = ID3D11Device_QueryInterface( vd->sys->d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
    if (SUCCEEDED(hr)) {
        ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
        ID3D10Multithread_Release(pMultithread);
    }

    pictures = calloc(pool_size, sizeof(*pictures));
    if (!pictures)
        goto error;

    D3D11_TEXTURE2D_DESC texDesc;
    ZeroMemory(&texDesc, sizeof(texDesc));
    texDesc.Width = vd->fmt.i_width;
    texDesc.Height = vd->fmt.i_height;
    texDesc.MipLevels = 1;
    texDesc.Format = vd->sys->picQuadConfig.textureFormat;
    texDesc.SampleDesc.Count = 1;
    texDesc.MiscFlags = 0; //D3D11_RESOURCE_MISC_SHARED;
    texDesc.ArraySize = 1;
    texDesc.Usage = D3D11_USAGE_DYNAMIC;
    texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

537
    for (picture_count = 0; picture_count < pool_size; picture_count++) {
538 539 540 541 542 543
        picture_sys_t *picsys = calloc(1, sizeof(*picsys));
        if (unlikely(picsys == NULL))
            goto error;

        hr = ID3D11Device_CreateTexture2D( vd->sys->d3ddevice, &texDesc, NULL, &picsys->texture );
        if (FAILED(hr)) {
544
            msg_Err(vd, "CreateTexture2D %d failed on picture %d of the pool. (hr=0x%0lx)", pool_size, picture_count, hr);
545 546 547 548 549 550 551 552 553 554 555 556 557
            goto error;
        }

        picsys->context = vd->sys->d3dcontext;

        picture_resource_t resource = {
            .p_sys = picsys,
            .pf_destroy = DestroyDisplayPoolPicture,
        };

        picture_t *picture = picture_NewFromResource(&vd->fmt, &resource);
        if (unlikely(picture == NULL)) {
            free(picsys);
558
            msg_Err( vd, "Failed to create picture %d in the pool.", picture_count );
559 560 561
            goto error;
        }

562
        pictures[picture_count] = picture;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
        /* each picture_t holds a ref to the context and release it on Destroy */
        ID3D11DeviceContext_AddRef(picsys->context);
    }
    msg_Dbg(vd, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
            pool_size, vd->fmt.i_width, vd->fmt.i_height);

    picture_pool_configuration_t pool_cfg;
    memset(&pool_cfg, 0, sizeof(pool_cfg));
    pool_cfg.picture_count = pool_size;
    pool_cfg.picture       = pictures;

    vd->sys->pool = picture_pool_NewExtended( &pool_cfg );

error:
    if (vd->sys->pool ==NULL && pictures) {
578
        msg_Dbg(vd, "Failed to create the picture d3d11 pool");
579 580 581
        for (unsigned i=0;i<picture_count; ++i)
            DestroyDisplayPoolPicture(pictures[i]);
        free(pictures);
582 583 584 585 586 587 588

        /* create an empty pool to avoid crashing */
        picture_pool_configuration_t pool_cfg;
        memset( &pool_cfg, 0, sizeof( pool_cfg ) );
        pool_cfg.picture_count = 0;

        vd->sys->pool = picture_pool_NewExtended( &pool_cfg );
589
    }
590
#endif
591 592 593
    return vd->sys->pool;
}

594
#ifdef HAVE_ID3D11VIDEODECODER 
595 596 597 598 599 600 601 602 603 604
static void DestroyDisplayPoolPicture(picture_t *picture)
{
    picture_sys_t *p_sys = (picture_sys_t*) picture->p_sys;

    if (p_sys->texture)
        ID3D11Texture2D_Release(p_sys->texture);

    free(p_sys);
    free(picture);
}
605
#endif
606 607 608 609 610 611 612 613 614 615 616 617

static void DestroyDisplayPicture(picture_t *picture)
{
    picture_sys_pool_t *p_sys = (picture_sys_pool_t*) picture->p_sys;

    if (p_sys->texture)
        ID3D11Texture2D_Release(p_sys->texture);

    free(p_sys);
    free(picture);
}

618 619 620 621 622 623
static HRESULT UpdateBackBuffer(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;
    HRESULT hr;
    ID3D11Texture2D* pDepthStencil;
    ID3D11Texture2D* pBackBuffer;
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    uint32_t i_width  = RECTWidth(sys->rect_dest_clipped);
    uint32_t i_height = RECTHeight(sys->rect_dest_clipped);
#if VLC_WINSTORE_APP
    UINT dataSize = sizeof(i_width);
    hr = IDXGISwapChain_GetPrivateData(sys->dxgiswapChain, &GUID_SWAPCHAIN_WIDTH, &dataSize, &i_width);
    if (FAILED(hr)) {
        msg_Err(vd, "Can't get swapchain width, size %d. (hr=0x%lX)", hr, dataSize);
        return hr;
    }
    dataSize = sizeof(i_height);
    hr = IDXGISwapChain_GetPrivateData(sys->dxgiswapChain, &GUID_SWAPCHAIN_HEIGHT, &dataSize, &i_height);
    if (FAILED(hr)) {
        msg_Err(vd, "Can't get swapchain height, size %d. (hr=0x%lX)", hr, dataSize);
        return hr;
    }
#endif
640 641 642 643 644 645 646 647 648 649

    if (sys->d3drenderTargetView) {
        ID3D11RenderTargetView_Release(sys->d3drenderTargetView);
        sys->d3drenderTargetView = NULL;
    }
    if (sys->d3ddepthStencilView) {
        ID3D11DepthStencilView_Release(sys->d3ddepthStencilView);
        sys->d3ddepthStencilView = NULL;
    }

650 651
    hr = IDXGISwapChain_ResizeBuffers(sys->dxgiswapChain, 0, i_width, i_height,
        DXGI_FORMAT_UNKNOWN, 0);
652 653 654 655 656 657 658 659 660 661 662 663 664
    if (FAILED(hr)) {
       msg_Err(vd, "Failed to resize the backbuffer. (hr=0x%lX)", hr);
       return hr;
    }

    hr = IDXGISwapChain_GetBuffer(sys->dxgiswapChain, 0, &IID_ID3D11Texture2D, (LPVOID *)&pBackBuffer);
    if (FAILED(hr)) {
       msg_Err(vd, "Could not get the backbuffer for the Swapchain. (hr=0x%lX)", hr);
       return hr;
    }

    hr = ID3D11Device_CreateRenderTargetView(sys->d3ddevice, (ID3D11Resource *)pBackBuffer, NULL, &sys->d3drenderTargetView);
    ID3D11Texture2D_Release(pBackBuffer);
665 666 667 668
    if (FAILED(hr)) {
        msg_Err(vd, "Failed to create the target view. (hr=0x%lX)", hr);
        return hr;
    }
669 670 671 672 673 674 675

    D3D11_TEXTURE2D_DESC deptTexDesc;
    memset(&deptTexDesc, 0,sizeof(deptTexDesc));
    deptTexDesc.ArraySize = 1;
    deptTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    deptTexDesc.CPUAccessFlags = 0;
    deptTexDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
676 677
    deptTexDesc.Width = i_width;
    deptTexDesc.Height = i_height;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    deptTexDesc.MipLevels = 1;
    deptTexDesc.MiscFlags = 0;
    deptTexDesc.SampleDesc.Count = 1;
    deptTexDesc.SampleDesc.Quality = 0;
    deptTexDesc.Usage = D3D11_USAGE_DEFAULT;

    hr = ID3D11Device_CreateTexture2D(sys->d3ddevice, &deptTexDesc, NULL, &pDepthStencil);
    if (FAILED(hr)) {
       msg_Err(vd, "Could not create the depth stencil texture. (hr=0x%lX)", hr);
       return hr;
    }

    D3D11_DEPTH_STENCIL_VIEW_DESC depthViewDesc;
    memset(&depthViewDesc, 0, sizeof(depthViewDesc));

    depthViewDesc.Format = deptTexDesc.Format;
    depthViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    depthViewDesc.Texture2D.MipSlice = 0;

    hr = ID3D11Device_CreateDepthStencilView(sys->d3ddevice, (ID3D11Resource *)pDepthStencil, &depthViewDesc, &sys->d3ddepthStencilView);
    ID3D11Texture2D_Release(pDepthStencil);

    if (FAILED(hr)) {
       msg_Err(vd, "Could not create the depth stencil view. (hr=0x%lX)", hr);
       return hr;
    }

    return S_OK;
}

static void Manage(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;
    RECT size_before = sys->rect_dest_clipped;

    CommonManage(vd);

    if (RECTWidth(size_before)  != RECTWidth(sys->rect_dest_clipped) ||
        RECTHeight(size_before) != RECTHeight(sys->rect_dest_clipped))
    {
        msg_Dbg(vd, "Manage detected size change %dx%d", RECTWidth(sys->rect_dest_clipped),
                RECTHeight(sys->rect_dest_clipped));

        UpdateBackBuffer(vd);
722 723

        UpdatePicQuadPosition(vd);
724 725 726
    }
}

Martell Malone's avatar
Martell Malone committed
727 728 729
static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;
730

731
#ifdef HAVE_ID3D11VIDEODECODER 
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    if (picture->format.i_chroma == VLC_CODEC_D3D11_OPAQUE) {
        D3D11_BOX box;
        box.left = 0;
        box.right = picture->format.i_visible_width;
        box.top = 0;
        box.bottom = picture->format.i_visible_height;
        box.back = 1;
        box.front = 0;

        picture_sys_t *p_sys = picture->p_sys;
        ID3D11DeviceContext_CopySubresourceRegion(sys->d3dcontext,
                                                  (ID3D11Resource*) sys->picQuad.pTexture,
                                                  0, 0, 0, 0,
                                                  (ID3D11Resource*) p_sys->texture,
                                                  0, &box);
    }
748
#endif
Martell Malone's avatar
Martell Malone committed
749

750 751 752 753 754 755 756 757
    if (subpicture) {
        int subpicture_region_count    = 0;
        picture_t **subpicture_regions = NULL;
        Direct3D11MapSubpicture(vd, &subpicture_region_count, &subpicture_regions, subpicture);
        Direct3D11DeleteRegions(sys->d3dregion_count, sys->d3dregions);
        sys->d3dregion_count = subpicture_region_count;
        sys->d3dregions      = subpicture_regions;
    }
758 759 760 761 762 763
}

static void DisplayD3DPicture(vout_display_sys_t *sys, d3d_quad_t *quad)
{
    UINT stride = sizeof(d3d_vertex_t);
    UINT offset = 0;
Martell Malone's avatar
Martell Malone committed
764 765

    /* Render the quad */
766
    ID3D11DeviceContext_RSSetViewports(sys->d3dcontext, 1, &quad->cropViewport);
767 768
    ID3D11DeviceContext_PSSetShader(sys->d3dcontext, quad->d3dpixelShader, NULL, 0);
    ID3D11DeviceContext_PSSetShaderResources(sys->d3dcontext, 0, 1, &quad->d3dresViewY);
Martell Malone's avatar
Martell Malone committed
769

770 771
    if( quad->d3dresViewUV )
        ID3D11DeviceContext_PSSetShaderResources(sys->d3dcontext, 1, 1, &quad->d3dresViewUV);
Martell Malone's avatar
Martell Malone committed
772

773
    ID3D11DeviceContext_IASetVertexBuffers(sys->d3dcontext, 0, 1, &quad->pVertexBuffer, &stride, &offset);
Martell Malone's avatar
Martell Malone committed
774 775 776 777 778 779 780
    ID3D11DeviceContext_DrawIndexed(sys->d3dcontext, 6, 0, 0);
}

static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;

781 782 783
    FLOAT blackRGBA[4] = {0.0f, 0.0f, 0.0f, 1.0f};
    ID3D11DeviceContext_ClearRenderTargetView(sys->d3dcontext, sys->d3drenderTargetView, blackRGBA);

784 785
    /* no ID3D11Device operations should come here */

Steve Lhomme's avatar
Steve Lhomme committed
786 787 788 789
    ID3D11DeviceContext_OMSetRenderTargets(sys->d3dcontext, 1, &sys->d3drenderTargetView, sys->d3ddepthStencilView);

    ID3D11DeviceContext_ClearDepthStencilView(sys->d3dcontext, sys->d3ddepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

790 791 792
    /* Render the quad */
    DisplayD3DPicture(sys, &sys->picQuad);

793 794 795 796 797 798 799
    if (subpicture) {
        // draw the additional vertices
        for (int i = 0; i < sys->d3dregion_count; ++i) {
            DisplayD3DPicture(sys, (d3d_quad_t *) sys->d3dregions[i]->p_sys);
        }
    }

800 801 802 803 804
    HRESULT hr = IDXGISwapChain_Present(sys->dxgiswapChain, 0, 0);
    if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
    {
        /* TODO device lost */
    }
Martell Malone's avatar
Martell Malone committed
805 806 807 808 809 810 811 812 813 814 815 816 817

    picture_Release(picture);
    if (subpicture)
        subpicture_Delete(subpicture);

    CommonDisplay(vd);
}

static void Direct3D11Destroy(vout_display_t *vd)
{
#if !VLC_WINSTORE_APP
    vout_display_sys_t *sys = vd->sys;

818
# if USE_DXGI
Martell Malone's avatar
Martell Malone committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    if (sys->hdxgi_dll)
        FreeLibrary(sys->hdxgi_dll);
# endif

    if (sys->hd3d11_dll)
        FreeLibrary(sys->hd3d11_dll);
    if (sys->hd3dcompiler_dll)
        FreeLibrary(sys->hd3dcompiler_dll);

    /* TODO : add release of d3d11 objects here */

    sys->OurD3D11CreateDevice = NULL;
    sys->OurD3D11CreateDeviceAndSwapChain = NULL;
    sys->OurD3DCompile = NULL;
    sys->hdxgi_dll = NULL;
    sys->hd3d11_dll = NULL;
    sys->hd3dcompiler_dll = NULL;
#else
    VLC_UNUSED(vd);
#endif
839
}
Martell Malone's avatar
Martell Malone committed
840

841 842 843 844 845 846 847 848 849 850 851 852
#if !VLC_WINSTORE_APP
static HINSTANCE Direct3D11LoadShaderLibrary(void)
{
    HINSTANCE instance = NULL;
    /* d3dcompiler_47 is the latest on windows 8.1 */
    for (int i = 47; i > 41; --i) {
        TCHAR filename[19];
        _sntprintf(filename, 19, TEXT("D3DCOMPILER_%d.dll"), i);
        instance = LoadLibrary(filename);
        if (instance) break;
    }
    return instance;
Martell Malone's avatar
Martell Malone committed
853
}
854 855
#endif

Martell Malone's avatar
Martell Malone committed
856 857 858 859 860 861 862 863 864 865 866

static int Direct3D11Open(vout_display_t *vd, video_format_t *fmt)
{
    vout_display_sys_t *sys = vd->sys;
    *fmt = vd->source;

#if !VLC_WINSTORE_APP

    UINT creationFlags = 0;
    HRESULT hr = S_OK;

867
# if !defined(NDEBUG) && defined(_MSC_VER)
Martell Malone's avatar
Martell Malone committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
# endif

    DXGI_SWAP_CHAIN_DESC scd;
    memset(&scd, 0, sizeof(scd));
    scd.BufferCount = 1;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.SampleDesc.Count = 1;
    scd.SampleDesc.Quality = 0;
    scd.BufferDesc.Width = fmt->i_visible_width;
    scd.BufferDesc.Height = fmt->i_visible_height;
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

    scd.Windowed = TRUE;
    scd.OutputWindow = sys->hvideownd;

884
    IDXGIAdapter *dxgiadapter;
885
# if USE_DXGI
886 887 888 889 890 891 892 893 894 895
    static const D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
        D3D_FEATURE_LEVEL_9_3,
        D3D_FEATURE_LEVEL_9_2,
        D3D_FEATURE_LEVEL_9_1
    };
Martell Malone's avatar
Martell Malone committed
896 897

    /* TODO : list adapters for the user to choose from */
898
    hr = IDXGIFactory_EnumAdapters(sys->dxgifactory, 0, &dxgiadapter);
Martell Malone's avatar
Martell Malone committed
899 900 901 902 903 904
    if (FAILED(hr)) {
       msg_Err(vd, "Could not create find factory. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

    IDXGIOutput* output;
905
    hr = IDXGIAdapter_EnumOutputs(dxgiadapter, 0, &output);
Martell Malone's avatar
Martell Malone committed
906 907
    if (FAILED(hr)) {
       msg_Err(vd, "Could not Enumerate DXGI Outputs. (hr=0x%lX)", hr);
Steve Lhomme's avatar
Steve Lhomme committed
908
       IDXGIAdapter_Release(dxgiadapter);
Martell Malone's avatar
Martell Malone committed
909 910 911 912 913 914 915 916 917 918 919 920 921
       return VLC_EGENERIC;
    }

    DXGI_MODE_DESC md;
    memset(&md, 0, sizeof(md));
    md.Width  = fmt->i_visible_width;
    md.Height = fmt->i_visible_height;
    md.Format = scd.BufferDesc.Format;
    md.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    hr = IDXGIOutput_FindClosestMatchingMode(output, &md, &scd.BufferDesc, NULL);
    if (FAILED(hr)) {
       msg_Err(vd, "Failed to find a supported video mode. (hr=0x%lX)", hr);
Steve Lhomme's avatar
Steve Lhomme committed
922
       IDXGIAdapter_Release(dxgiadapter);
Martell Malone's avatar
Martell Malone committed
923 924 925 926 927 928 929
       return VLC_EGENERIC;
    }

    /* mode desc doesn't carry over the width and height*/
    scd.BufferDesc.Width = fmt->i_visible_width;
    scd.BufferDesc.Height = fmt->i_visible_height;

930
    hr = D3D11CreateDeviceAndSwapChain(dxgiadapter,
Martell Malone's avatar
Martell Malone committed
931 932 933
                    D3D_DRIVER_TYPE_UNKNOWN, NULL, creationFlags,
                    featureLevels, ARRAYSIZE(featureLevels),
                    D3D11_SDK_VERSION, &scd, &sys->dxgiswapChain,
934
                    &sys->d3ddevice, NULL, &sys->d3dcontext);
Steve Lhomme's avatar
Steve Lhomme committed
935
    IDXGIAdapter_Release(dxgiadapter);
Martell Malone's avatar
Martell Malone committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    if (FAILED(hr)) {
       msg_Err(vd, "Could not Create the D3D11 device and SwapChain. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

# else

    static const D3D_DRIVER_TYPE driverAttempts[] = {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };

    for (UINT driver = 0; driver < ARRAYSIZE(driverAttempts); driver++) {
        hr = D3D11CreateDevice(NULL, driverAttempts[driver], NULL, creationFlags,
951 952
                    NULL, 0, D3D11_SDK_VERSION,
                    &sys->d3ddevice, NULL, &sys->d3dcontext);
953 954
        if (SUCCEEDED(hr)) {
#ifndef NDEBUG
955 956 957
            msg_Dbg(vd, "Created the D3D11 device 0x%p ctx 0x%p type %d.",
                    (void *)sys->d3ddevice, (void *)sys->d3dcontext,
                    driverAttempts[driver]);
958 959 960
#endif
            break;
        }
Martell Malone's avatar
Martell Malone committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974
    }

    if (FAILED(hr)) {
       msg_Err(vd, "Could not Create the D3D11 device. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

    IDXGIDevice *pDXGIDevice = NULL;
    hr = ID3D11Device_QueryInterface(sys->d3ddevice, &IID_IDXGIDevice, (void **)&pDXGIDevice);
    if (FAILED(hr)) {
       msg_Err(vd, "Could not Query DXGI Interface. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

975
    hr = IDXGIDevice_GetAdapter(pDXGIDevice, &dxgiadapter);
Steve Lhomme's avatar
Steve Lhomme committed
976
    IDXGIAdapter_Release(pDXGIDevice);
Martell Malone's avatar
Martell Malone committed
977 978 979 980 981
    if (FAILED(hr)) {
       msg_Err(vd, "Could not get the DXGI Adapter. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

982
    hr = IDXGIAdapter_GetParent(dxgiadapter, &IID_IDXGIFactory, (void **)&sys->dxgifactory);
Steve Lhomme's avatar
Steve Lhomme committed
983
    IDXGIAdapter_Release(dxgiadapter);
Martell Malone's avatar
Martell Malone committed
984 985 986 987 988 989
    if (FAILED(hr)) {
       msg_Err(vd, "Could not get the DXGI Factory. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

    hr = IDXGIFactory_CreateSwapChain(sys->dxgifactory, (IUnknown *)sys->d3ddevice, &scd, &sys->dxgiswapChain);
Steve Lhomme's avatar
Steve Lhomme committed
990
    IDXGIFactory_Release(sys->dxgifactory);
Martell Malone's avatar
Martell Malone committed
991 992 993 994 995 996 997 998
    if (FAILED(hr)) {
       msg_Err(vd, "Could not create the SwapChain. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }

# endif
#endif

999 1000 1001
    vlc_fourcc_t i_src_chroma = fmt->i_chroma;
    fmt->i_chroma = 0;

1002
    // look for the request pixel format first
1003 1004
    UINT i_quadSupportFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_SHADER_LOAD;
    UINT i_formatSupport;
1005 1006
    for (const d3d_format_t *output_format = GetRenderFormatList();
         output_format->name != NULL; ++output_format)
1007
    {
1008
        if( i_src_chroma == output_format->fourcc)
1009
        {
1010
            if( SUCCEEDED( ID3D11Device_CheckFormatSupport(sys->d3ddevice,
1011
                                                           output_format->formatTexture,
1012
                                                           &i_formatSupport)) &&
1013
                    ( i_formatSupport & i_quadSupportFlags ) == i_quadSupportFlags )
1014
            {
1015
                msg_Dbg(vd, "Using pixel format %s with chroma %4.4s for source %4.4s", output_format->name, (char *)&output_format->fourcc, (char *)&i_src_chroma );
1016 1017 1018 1019
                fmt->i_chroma = output_format->fourcc;
                sys->picQuadConfig.textureFormat      = output_format->formatTexture;
                sys->picQuadConfig.resourceFormatYRGB = output_format->formatY;
                sys->picQuadConfig.resourceFormatUV   = output_format->formatUV;
1020
                break;
1021 1022 1023 1024 1025
            }
        }
    }

    // look for any pixel format that we can handle
1026
    if ( !fmt->i_chroma )
1027
    {
1028 1029
        for (const d3d_format_t *output_format = GetRenderFormatList();
             output_format->name != NULL; ++output_format)
1030 1031
        {
            if( SUCCEEDED( ID3D11Device_CheckFormatSupport(sys->d3ddevice,
1032
                                                           output_format->formatTexture,
1033
                                                           &i_formatSupport)) &&
1034
                    ( i_formatSupport & i_quadSupportFlags ) == i_quadSupportFlags )
1035
            {
1036
                msg_Dbg(vd, "Using pixel format %s for chroma %4.4s", output_format->name, (char *)&output_format->fourcc );
1037 1038 1039 1040
                fmt->i_chroma = output_format->fourcc;
                sys->picQuadConfig.textureFormat      = output_format->formatTexture;
                sys->picQuadConfig.resourceFormatYRGB = output_format->formatY;
                sys->picQuadConfig.resourceFormatUV   = output_format->formatUV;
1041 1042
                break;
            }
1043 1044
        }
    }
1045
    if ( !fmt->i_chroma )
1046
    {
1047 1048 1049 1050
       msg_Err(vd, "Could not get a suitable texture pixel format");
       return VLC_EGENERIC;
    }

1051 1052 1053 1054 1055
    /* check the region pixel format */
    i_quadSupportFlags |= D3D11_FORMAT_SUPPORT_BLENDABLE;
    if( SUCCEEDED( ID3D11Device_CheckFormatSupport(sys->d3ddevice,
                                                   DXGI_FORMAT_R8G8B8A8_UNORM,
                                                   &i_formatSupport)) &&
1056
            ( i_formatSupport & i_quadSupportFlags ) == i_quadSupportFlags) {
1057 1058 1059 1060 1061 1062
        sys->d3dregion_format = DXGI_FORMAT_R8G8B8A8_UNORM;
        sys->pSubpictureChromas[0] = VLC_CODEC_RGBA;
        sys->pSubpictureChromas[1] = 0;
    } else if( SUCCEEDED( ID3D11Device_CheckFormatSupport(sys->d3ddevice,
                                                          DXGI_FORMAT_B8G8R8A8_UNORM,
                                                          &i_formatSupport)) &&
1063
                   ( i_formatSupport & i_quadSupportFlags ) == i_quadSupportFlags) {
1064 1065 1066 1067 1068 1069 1070
        sys->d3dregion_format = DXGI_FORMAT_B8G8R8A8_UNORM;
        sys->pSubpictureChromas[0] = VLC_CODEC_BGRA;
        sys->pSubpictureChromas[1] = 0;
    } else {
        sys->d3dregion_format = DXGI_FORMAT_UNKNOWN;
    }

1071 1072
    sys->d3dPxShader = globPixelShaderDefault;
    if (sys->picQuadConfig.resourceFormatYRGB == DXGI_FORMAT_R8_UNORM)
1073
    {
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        switch (fmt->i_chroma)
        {
        case VLC_CODEC_NV12:
        case VLC_CODEC_D3D11_OPAQUE:
            if( fmt->i_height > 576 )
                sys->d3dPxShader = globPixelShaderBiplanarYUV_BT709_2RGB;
            else
                sys->d3dPxShader = globPixelShaderBiplanarYUV_BT601_2RGB;
            break;
        case VLC_CODEC_YV12:
        case VLC_CODEC_I420:
            if( fmt->i_height > 576 )
                sys->d3dPxShader = globPixelShaderBiplanarI420_BT709_2RGB;
            else
                sys->d3dPxShader = globPixelShaderBiplanarI420_BT601_2RGB;
            break;
        default:
            vlc_assert_unreachable();
            break;
        }
1094
    }
1095

1096 1097 1098 1099
    if (sys->d3dregion_format != DXGI_FORMAT_UNKNOWN)
        sys->psz_rgbaPxShader = globPixelShaderDefault;
    else
        sys->psz_rgbaPxShader = NULL;
1100

Martell Malone's avatar
Martell Malone committed
1101 1102 1103 1104 1105 1106 1107 1108
    UpdateRects(vd, NULL, NULL, true);

    if (Direct3D11CreateResources(vd, fmt)) {
        msg_Err(vd, "Failed to allocate resources");
        Direct3D11DestroyResources(vd);
        return VLC_EGENERIC;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1109
#if !VLC_WINSTORE_APP
Martell Malone's avatar
Martell Malone committed
1110
    EventThreadUpdateTitle(sys->event, VOUT_TITLE " (Direct3D11 output)");
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1111
#endif
Martell Malone's avatar
Martell Malone committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

    msg_Dbg(vd, "Direct3D11 device adapter successfully initialized");
    return VLC_SUCCESS;
}

static void Direct3D11Close(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    Direct3D11DestroyResources(vd);
1122 1123 1124
    if (sys->d3dcontext)
    {
        ID3D11DeviceContext_Flush(sys->d3dcontext);
1125
        ID3D11DeviceContext_Release(sys->d3dcontext);
1126 1127 1128 1129
        sys->d3dcontext = NULL;
    }
    if (sys->d3ddevice)
    {
1130
        ID3D11Device_Release(sys->d3ddevice);
1131 1132 1133 1134 1135 1136 1137
        sys->d3ddevice = NULL;
    }
    if (sys->dxgiswapChain)
    {
        IDXGISwapChain_Release(sys->dxgiswapChain);
        sys->dxgiswapChain = NULL;
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1138

Martell Malone's avatar
Martell Malone committed
1139 1140 1141
    msg_Dbg(vd, "Direct3D11 device adapter closed");
}

1142 1143 1144 1145 1146 1147
static void UpdatePicQuadPosition(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;
    int i_width  = RECTWidth(sys->rect_dest_clipped);
    int i_height = RECTHeight(sys->rect_dest_clipped);

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    int i_top = sys->rect_src_clipped.top * i_height;
    i_top /= vd->source.i_visible_height;
    i_top -= sys->rect_dest_clipped.top;
    int i_left = sys->rect_src_clipped.left * i_width;
    i_left /= vd->source.i_visible_width;
    i_left -= sys->rect_dest_clipped.left;

    sys->picQuad.cropViewport.Width =  (FLOAT) vd->source.i_width  * i_width  / vd->source.i_visible_width;
    sys->picQuad.cropViewport.Height = (FLOAT) vd->source.i_height * i_height / vd->source.i_visible_height;
    sys->picQuad.cropViewport.MinDepth = 0.0f;
    sys->picQuad.cropViewport.MaxDepth = 1.0f;
    sys->picQuad.cropViewport.TopLeftX = -i_left;
    sys->picQuad.cropViewport.TopLeftY = -i_top;

#ifndef NDEBUG
    msg_Dbg(vd, "picQuad position (%.02f,%.02f) %.02fx%.02f", sys->picQuad.cropViewport.TopLeftX, sys->picQuad.cropViewport.TopLeftY, sys->picQuad.cropViewport.Width, sys->picQuad.cropViewport.Height );
#endif
1165 1166
}

Martell Malone's avatar
Martell Malone committed
1167 1168 1169 1170 1171 1172 1173
/* TODO : handle errors better
   TODO : seperate out into smaller functions like createshaders */
static int Direct3D11CreateResources(vout_display_t *vd, video_format_t *fmt)
{
    vout_display_sys_t *sys = vd->sys;
    HRESULT hr;

1174
    hr = UpdateBackBuffer(vd);
Martell Malone's avatar
Martell Malone committed
1175
    if (FAILED(hr)) {
1176
       msg_Err(vd, "Could not update the backbuffer. (hr=0x%lX)", hr);
Martell Malone's avatar
Martell Malone committed
1177 1178 1179
       return VLC_EGENERIC;
    }

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    ID3D11BlendState *pSpuBlendState;
    D3D11_BLEND_DESC spuBlendDesc = { 0 };
    spuBlendDesc.RenderTarget[0].BlendEnable = TRUE;
    spuBlendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
    spuBlendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
    spuBlendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;

    spuBlendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
    spuBlendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
    spuBlendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;