]> git.sesse.net Git - vlc/blob - modules/video_output/msw/direct3d.c
skins2: fix animated image flickering
[vlc] / modules / video_output / msw / direct3d.c
1 /*****************************************************************************
2  * direct3d.c: Windows Direct3D video output module
3  *****************************************************************************
4  * Copyright (C) 2006-2014 VLC authors and VideoLAN
5  *$Id$
6  *
7  * Authors: Damien Fouilleul <damienf@videolan.org>,
8  *          Sasha Koruga <skoruga@gmail.com>,
9  *          Felix Abecassis <felix.abecassis@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble:
28  *
29  * This plugin will use YUV surface if supported, using YUV will result in
30  * the best video quality (hardware filering when rescaling the picture)
31  * and the fastest display as it requires less processing.
32  *
33  * If YUV overlay is not supported this plugin will use RGB offscreen video
34  * surfaces that will be blitted onto the primary surface (display) to
35  * effectively display the pictures.
36  *
37  *****************************************************************************/
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_vout_display.h>
45 #include <vlc_charset.h> /* ToT function */
46
47 #include <windows.h>
48 #include <d3d9.h>
49
50 #include "common.h"
51 #include "builtin_shaders.h"
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 static int  Open(vlc_object_t *);
57 static void Close(vlc_object_t *);
58
59 #define DESKTOP_LONGTEXT N_(\
60     "The desktop mode allows you to display the video on the desktop.")
61
62 #define HW_BLENDING_TEXT N_("Use hardware blending support")
63 #define HW_BLENDING_LONGTEXT N_(\
64     "Try to use hardware acceleration for subtitle/OSD blending.")
65
66 #define PIXEL_SHADER_TEXT N_("Pixel Shader")
67 #define PIXEL_SHADER_LONGTEXT N_(\
68         "Choose a pixel shader to apply.")
69 #define PIXEL_SHADER_FILE_TEXT N_("Path to HLSL file")
70 #define PIXEL_SHADER_FILE_LONGTEXT N_("Path to an HLSL file containing a single pixel shader.")
71 /* The latest option in the selection list: used for loading a shader file. */
72 #define SELECTED_SHADER_FILE N_("HLSL File")
73
74 #define D3D_HELP N_("Recommended video output for Windows Vista and later versions")
75
76 static int FindShadersCallback(vlc_object_t *, const char *,
77                                char ***, char ***);
78
79 vlc_module_begin ()
80     set_shortname("Direct3D")
81     set_description(N_("Direct3D video output"))
82     set_help(D3D_HELP)
83     set_category(CAT_VIDEO)
84     set_subcategory(SUBCAT_VIDEO_VOUT)
85
86     add_bool("direct3d-hw-blending", true, HW_BLENDING_TEXT, HW_BLENDING_LONGTEXT, true)
87
88     add_string("direct3d-shader", "", PIXEL_SHADER_TEXT, PIXEL_SHADER_LONGTEXT, true)
89         change_string_cb(FindShadersCallback)
90     add_loadfile("direct3d-shader-file", NULL, PIXEL_SHADER_FILE_TEXT, PIXEL_SHADER_FILE_LONGTEXT, false)
91
92     set_capability("vout display", 240)
93     add_shortcut("direct3d")
94     set_callbacks(Open, Close)
95
96 vlc_module_end ()
97
98 /*****************************************************************************
99  * Local prototypes.
100  *****************************************************************************/
101 static const vlc_fourcc_t d3d_subpicture_chromas[] = {
102     VLC_CODEC_RGBA,
103     0
104 };
105
106 struct picture_sys_t
107 {
108     LPDIRECT3DSURFACE9 surface;
109     picture_t          *fallback;
110 };
111
112 static int  Open(vlc_object_t *);
113
114 static picture_pool_t *Pool  (vout_display_t *, unsigned);
115 static void           Prepare(vout_display_t *, picture_t *, subpicture_t *subpicture);
116 static void           Display(vout_display_t *, picture_t *, subpicture_t *subpicture);
117 static int            Control(vout_display_t *, int, va_list);
118 static void           Manage (vout_display_t *);
119
120 static int  Direct3DCreate (vout_display_t *);
121 static int  Direct3DReset  (vout_display_t *);
122 static void Direct3DDestroy(vout_display_t *);
123
124 static int  Direct3DOpen (vout_display_t *, video_format_t *);
125 static void Direct3DClose(vout_display_t *);
126
127 /* */
128 typedef struct
129 {
130     FLOAT       x,y,z;      // vertex untransformed position
131     FLOAT       rhw;        // eye distance
132     D3DCOLOR    diffuse;    // diffuse color
133     FLOAT       tu, tv;     // texture relative coordinates
134 } CUSTOMVERTEX;
135 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
136
137 typedef struct d3d_region_t {
138     D3DFORMAT          format;
139     unsigned           width;
140     unsigned           height;
141     CUSTOMVERTEX       vertex[4];
142     LPDIRECT3DTEXTURE9 texture;
143 } d3d_region_t;
144
145 static void Direct3DDeleteRegions(int, d3d_region_t *);
146
147 static int  Direct3DImportPicture(vout_display_t *vd, d3d_region_t *, LPDIRECT3DSURFACE9 surface);
148 static void Direct3DImportSubpicture(vout_display_t *vd, int *, d3d_region_t **, subpicture_t *);
149
150 static void Direct3DRenderScene(vout_display_t *vd, d3d_region_t *, int, d3d_region_t *);
151
152 /* */
153 static int DesktopCallback(vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *);
154
155 /**
156  * It creates a Direct3D vout display.
157  */
158 static int Open(vlc_object_t *object)
159 {
160     vout_display_t *vd = (vout_display_t *)object;
161     vout_display_sys_t *sys;
162
163     OSVERSIONINFO winVer;
164     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
165     if(GetVersionEx(&winVer) && winVer.dwMajorVersion < 6 && !object->b_force)
166         return VLC_EGENERIC;
167
168     /* Allocate structure */
169     vd->sys = sys = calloc(1, sizeof(vout_display_sys_t));
170     if (!sys)
171         return VLC_ENOMEM;
172
173     if (Direct3DCreate(vd)) {
174         msg_Err(vd, "Direct3D could not be initialized");
175         Direct3DDestroy(vd);
176         free(sys);
177         return VLC_EGENERIC;
178     }
179
180     sys->use_desktop = var_CreateGetBool(vd, "video-wallpaper");
181     sys->reset_device = false;
182     sys->reopen_device = false;
183     sys->lost_not_ready = false;
184     sys->allow_hw_yuv = var_CreateGetBool(vd, "directx-hw-yuv");
185     sys->desktop_save.is_fullscreen = vd->cfg->is_fullscreen;
186     sys->desktop_save.is_on_top     = false;
187     sys->desktop_save.win.left      = var_InheritInteger(vd, "video-x");
188     sys->desktop_save.win.right     = vd->cfg->display.width;
189     sys->desktop_save.win.top       = var_InheritInteger(vd, "video-y");
190     sys->desktop_save.win.bottom    = vd->cfg->display.height;
191
192     if (CommonInit(vd))
193         goto error;
194
195     /* */
196     video_format_t fmt;
197     if (Direct3DOpen(vd, &fmt)) {
198         msg_Err(vd, "Direct3D could not be opened");
199         goto error;
200     }
201
202     /* */
203     vout_display_info_t info = vd->info;
204     info.is_slow = true;
205     info.has_double_click = true;
206     info.has_hide_mouse = false;
207     info.has_pictures_invalid = true;
208     info.has_event_thread = true;
209     if (var_InheritBool(vd, "direct3d-hw-blending") &&
210         sys->d3dregion_format != D3DFMT_UNKNOWN &&
211         (sys->d3dcaps.SrcBlendCaps  & D3DPBLENDCAPS_SRCALPHA) &&
212         (sys->d3dcaps.DestBlendCaps & D3DPBLENDCAPS_INVSRCALPHA) &&
213         (sys->d3dcaps.TextureCaps   & D3DPTEXTURECAPS_ALPHA) &&
214         (sys->d3dcaps.TextureOpCaps & D3DTEXOPCAPS_SELECTARG1) &&
215         (sys->d3dcaps.TextureOpCaps & D3DTEXOPCAPS_MODULATE))
216         info.subpicture_chromas = d3d_subpicture_chromas;
217     else
218         info.subpicture_chromas = NULL;
219
220     /* Interaction */
221     vlc_mutex_init(&sys->lock);
222     sys->ch_desktop = false;
223     sys->desktop_requested = sys->use_desktop;
224
225     vlc_value_t val;
226     val.psz_string = _("Desktop");
227     var_Change(vd, "video-wallpaper", VLC_VAR_SETTEXT, &val, NULL);
228     var_AddCallback(vd, "video-wallpaper", DesktopCallback, NULL);
229
230     /* Setup vout_display now that everything is fine */
231     video_format_Clean(&vd->fmt);
232     video_format_Copy(&vd->fmt, &fmt);
233     vd->info = info;
234
235     vd->pool    = Pool;
236     vd->prepare = Prepare;
237     vd->display = Display;
238     vd->control = Control;
239     vd->manage  = Manage;
240
241     /* Fix state in case of desktop mode */
242     if (sys->use_desktop && vd->cfg->is_fullscreen)
243         vout_display_SendEventFullscreen(vd, false);
244
245     return VLC_SUCCESS;
246 error:
247     Direct3DClose(vd);
248     CommonClean(vd);
249     Direct3DDestroy(vd);
250     free(vd->sys);
251     return VLC_EGENERIC;
252 }
253
254 /**
255  * It destroyes a Direct3D vout display.
256  */
257 static void Close(vlc_object_t *object)
258 {
259     vout_display_t * vd = (vout_display_t *)object;
260
261     var_DelCallback(vd, "video-wallpaper", DesktopCallback, NULL);
262     vlc_mutex_destroy(&vd->sys->lock);
263
264     Direct3DClose(vd);
265
266     CommonClean(vd);
267
268     Direct3DDestroy(vd);
269
270     free(vd->sys);
271 }
272
273 /* */
274 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
275 {
276     VLC_UNUSED(count);
277     return vd->sys->pool;
278 }
279
280 static int  Direct3DLockSurface(picture_t *);
281 static void Direct3DUnlockSurface(picture_t *);
282
283 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
284 {
285     vout_display_sys_t *sys = vd->sys;
286     LPDIRECT3DSURFACE9 surface = picture->p_sys->surface;
287 #if 0
288     picture_Release(picture);
289     VLC_UNUSED(subpicture);
290 #else
291     /* FIXME it is a bit ugly, we need the surface to be unlocked for
292      * rendering.
293      *  The clean way would be to release the picture (and ensure that
294      * the vout doesn't keep a reference). But because of the vout
295      * wrapper, we can't */
296
297     Direct3DUnlockSurface(picture);
298     VLC_UNUSED(subpicture);
299 #endif
300
301     /* check if device is still available */
302     HRESULT hr = IDirect3DDevice9_TestCooperativeLevel(sys->d3ddev);
303     if (FAILED(hr)) {
304         if (hr == D3DERR_DEVICENOTRESET && !sys->reset_device) {
305             vout_display_SendEventPicturesInvalid(vd);
306             sys->reset_device = true;
307             sys->lost_not_ready = false;
308         }
309         if (hr == D3DERR_DEVICELOST && !sys->lost_not_ready) {
310             /* Device is lost but not yet ready for reset. */
311             sys->lost_not_ready = true;
312         }
313         return;
314     }
315
316     d3d_region_t picture_region;
317     if (!Direct3DImportPicture(vd, &picture_region, surface)) {
318         picture_region.width = picture->format.i_visible_width;
319         picture_region.height = picture->format.i_visible_height;
320         int subpicture_region_count     = 0;
321         d3d_region_t *subpicture_region = NULL;
322         if (subpicture)
323             Direct3DImportSubpicture(vd, &subpicture_region_count, &subpicture_region,
324                                      subpicture);
325
326         Direct3DRenderScene(vd, &picture_region,
327                             subpicture_region_count, subpicture_region);
328
329         Direct3DDeleteRegions(sys->d3dregion_count, sys->d3dregion);
330         sys->d3dregion_count = subpicture_region_count;
331         sys->d3dregion       = subpicture_region;
332     }
333 }
334
335 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
336 {
337     vout_display_sys_t *sys = vd->sys;
338     LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
339
340     if (sys->lost_not_ready) {
341         picture_Release(picture);
342         if (subpicture)
343             subpicture_Delete(subpicture);
344         return;
345     }
346
347     // Present the back buffer contents to the display
348     // No stretching should happen here !
349     const RECT src = sys->rect_dest_clipped;
350     const RECT dst = sys->rect_dest_clipped;
351     HRESULT hr = IDirect3DDevice9_Present(d3ddev, &src, &dst, NULL, NULL);
352     if (FAILED(hr)) {
353         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
354     }
355
356 #if 0
357     VLC_UNUSED(picture);
358     VLC_UNUSED(subpicture);
359 #else
360     /* XXX See Prepare() */
361     Direct3DLockSurface(picture);
362     picture_Release(picture);
363 #endif
364     if (subpicture)
365         subpicture_Delete(subpicture);
366
367     CommonDisplay(vd);
368 }
369 static int ControlResetDevice(vout_display_t *vd)
370 {
371     return Direct3DReset(vd);
372 }
373 static int ControlReopenDevice(vout_display_t *vd)
374 {
375     vout_display_sys_t *sys = vd->sys;
376
377     if (!sys->use_desktop) {
378         /* Save non-desktop state */
379         sys->desktop_save.is_fullscreen = vd->cfg->is_fullscreen;
380         sys->desktop_save.is_on_top     = sys->is_on_top;
381
382         WINDOWPLACEMENT wp = { .length = sizeof(wp), };
383         GetWindowPlacement(sys->hparent ? sys->hparent : sys->hwnd, &wp);
384         sys->desktop_save.win = wp.rcNormalPosition;
385     }
386
387     /* */
388     Direct3DClose(vd);
389     EventThreadStop(sys->event);
390
391     /* */
392     vlc_mutex_lock(&sys->lock);
393     sys->use_desktop = sys->desktop_requested;
394     sys->ch_desktop = false;
395     vlc_mutex_unlock(&sys->lock);
396
397     /* */
398     event_cfg_t cfg;
399     memset(&cfg, 0, sizeof(cfg));
400     cfg.use_desktop = sys->use_desktop;
401     if (!sys->use_desktop) {
402         cfg.win.type   = VOUT_WINDOW_TYPE_HWND;
403         cfg.win.x      = sys->desktop_save.win.left;
404         cfg.win.y      = sys->desktop_save.win.top;
405         cfg.win.width  = sys->desktop_save.win.right  - sys->desktop_save.win.left;
406         cfg.win.height = sys->desktop_save.win.bottom - sys->desktop_save.win.top;
407     }
408
409     event_hwnd_t hwnd;
410     if (EventThreadStart(sys->event, &hwnd, &cfg)) {
411         msg_Err(vd, "Failed to restart event thread");
412         return VLC_EGENERIC;
413     }
414     sys->parent_window = hwnd.parent_window;
415     sys->hparent       = hwnd.hparent;
416     sys->hwnd          = hwnd.hwnd;
417     sys->hvideownd     = hwnd.hvideownd;
418     sys->hfswnd        = hwnd.hfswnd;
419     SetRectEmpty(&sys->rect_parent);
420
421     /* */
422     video_format_t fmt;
423     if (Direct3DOpen(vd, &fmt)) {
424         CommonClean(vd);
425         msg_Err(vd, "Failed to reopen device");
426         return VLC_EGENERIC;
427     }
428     vd->fmt = fmt;
429     sys->is_first_display = true;
430
431     if (sys->use_desktop) {
432         /* Disable fullscreen/on_top while using desktop */
433         if (sys->desktop_save.is_fullscreen)
434             vout_display_SendEventFullscreen(vd, false);
435         if (sys->desktop_save.is_on_top)
436             vout_display_SendWindowState(vd, VOUT_WINDOW_STATE_NORMAL);
437     } else {
438         /* Restore fullscreen/on_top */
439         if (sys->desktop_save.is_fullscreen)
440             vout_display_SendEventFullscreen(vd, true);
441         if (sys->desktop_save.is_on_top)
442             vout_display_SendWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
443     }
444     return VLC_SUCCESS;
445 }
446 static int Control(vout_display_t *vd, int query, va_list args)
447 {
448     vout_display_sys_t *sys = vd->sys;
449
450     switch (query) {
451     case VOUT_DISPLAY_RESET_PICTURES:
452         /* FIXME what to do here in case of failure */
453         if (sys->reset_device) {
454             if (ControlResetDevice(vd)) {
455                 msg_Err(vd, "Failed to reset device");
456                 return VLC_EGENERIC;
457             }
458             sys->reset_device = false;
459         } else if(sys->reopen_device) {
460             if (ControlReopenDevice(vd)) {
461                 msg_Err(vd, "Failed to reopen device");
462                 return VLC_EGENERIC;
463             }
464             sys->reopen_device = false;
465         }
466         return VLC_SUCCESS;
467     default:
468         return CommonControl(vd, query, args);
469     }
470 }
471 static void Manage (vout_display_t *vd)
472 {
473     vout_display_sys_t *sys = vd->sys;
474
475     CommonManage(vd);
476
477     /* Desktop mode change */
478     vlc_mutex_lock(&sys->lock);
479     const bool ch_desktop = sys->ch_desktop;
480     sys->ch_desktop = false;
481     vlc_mutex_unlock(&sys->lock);
482
483     if (ch_desktop) {
484         sys->reopen_device = true;
485         vout_display_SendEventPicturesInvalid(vd);
486     }
487
488     /* Position Change */
489     if (sys->changes & DX_POSITION_CHANGE) {
490 #if 0 /* need that when bicubic filter is available */
491         RECT rect;
492         UINT width, height;
493
494         GetClientRect(p_sys->hvideownd, &rect);
495         width  = rect.right-rect.left;
496         height = rect.bottom-rect.top;
497
498         if (width != p_sys->d3dpp.BackBufferWidth || height != p_sys->d3dpp.BackBufferHeight)
499         {
500             msg_Dbg(vd, "resizing device back buffers to (%lux%lu)", width, height);
501             // need to reset D3D device to resize back buffer
502             if (VLC_SUCCESS != Direct3DResetDevice(vd, width, height))
503                 return VLC_EGENERIC;
504         }
505 #endif
506         sys->clear_scene = true;
507         sys->changes &= ~DX_POSITION_CHANGE;
508     }
509 }
510
511 static HINSTANCE Direct3DLoadShaderLibrary(void)
512 {
513     HINSTANCE instance = NULL;
514     for (int i = 43; i > 23; --i) {
515         TCHAR filename[16];
516         _sntprintf(filename, 16, TEXT("D3dx9_%d.dll"), i);
517         instance = LoadLibrary(filename);
518         if (instance)
519             break;
520     }
521     return instance;
522 }
523
524 /**
525  * It initializes an instance of Direct3D9
526  */
527 static int Direct3DCreate(vout_display_t *vd)
528 {
529     vout_display_sys_t *sys = vd->sys;
530
531     sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
532     if (!sys->hd3d9_dll) {
533         msg_Warn(vd, "cannot load d3d9.dll, aborting");
534         return VLC_EGENERIC;
535     }
536
537     LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
538     OurDirect3DCreate9 =
539         (void *)GetProcAddress(sys->hd3d9_dll, "Direct3DCreate9");
540     if (!OurDirect3DCreate9) {
541         msg_Err(vd, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
542         return VLC_EGENERIC;
543     }
544
545     /* Create the D3D object. */
546     LPDIRECT3D9 d3dobj = OurDirect3DCreate9(D3D_SDK_VERSION);
547     if (!d3dobj) {
548        msg_Err(vd, "Could not create Direct3D9 instance.");
549        return VLC_EGENERIC;
550     }
551     sys->d3dobj = d3dobj;
552
553     sys->hd3d9x_dll = Direct3DLoadShaderLibrary();
554     if (!sys->hd3d9x_dll)
555         msg_Warn(vd, "cannot load Direct3D Shader Library; HLSL pixel shading will be disabled.");
556
557     /*
558     ** Get device capabilities
559     */
560     ZeroMemory(&sys->d3dcaps, sizeof(sys->d3dcaps));
561     HRESULT hr = IDirect3D9_GetDeviceCaps(d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &sys->d3dcaps);
562     if (FAILED(hr)) {
563        msg_Err(vd, "Could not read adapter capabilities. (hr=0x%lX)", hr);
564        return VLC_EGENERIC;
565     }
566
567     /* TODO: need to test device capabilities and select the right render function */
568     if (!(sys->d3dcaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) ||
569         !(sys->d3dcaps.TextureFilterCaps & (D3DPTFILTERCAPS_MAGFLINEAR)) ||
570         !(sys->d3dcaps.TextureFilterCaps & (D3DPTFILTERCAPS_MINFLINEAR))) {
571         msg_Err(vd, "Device does not support stretching from textures.");
572         return VLC_EGENERIC;
573     }
574
575     return VLC_SUCCESS;
576 }
577
578 /**
579  * It releases an instance of Direct3D9
580  */
581 static void Direct3DDestroy(vout_display_t *vd)
582 {
583     vout_display_sys_t *sys = vd->sys;
584
585     if (sys->d3dobj)
586        IDirect3D9_Release(sys->d3dobj);
587     if (sys->hd3d9_dll)
588         FreeLibrary(sys->hd3d9_dll);
589     if (sys->hd3d9x_dll)
590         FreeLibrary(sys->hd3d9x_dll);
591
592     sys->d3dobj = NULL;
593     sys->hd3d9_dll = NULL;
594     sys->hd3d9x_dll = NULL;
595 }
596
597
598 /**
599  * It setup vout_display_sys_t::d3dpp and vout_display_sys_t::rect_display
600  * from the default adapter.
601  */
602 static int Direct3DFillPresentationParameters(vout_display_t *vd)
603 {
604     vout_display_sys_t *sys = vd->sys;
605
606     /*
607     ** Get the current desktop display mode, so we can set up a back
608     ** buffer of the same format
609     */
610     D3DDISPLAYMODE d3ddm;
611     HRESULT hr = IDirect3D9_GetAdapterDisplayMode(sys->d3dobj,
612                                                   D3DADAPTER_DEFAULT, &d3ddm);
613     if (FAILED(hr)) {
614        msg_Err(vd, "Could not read adapter display mode. (hr=0x%lX)", hr);
615        return VLC_EGENERIC;
616     }
617
618     /* Set up the structure used to create the D3DDevice. */
619     D3DPRESENT_PARAMETERS *d3dpp = &vd->sys->d3dpp;
620     ZeroMemory(d3dpp, sizeof(D3DPRESENT_PARAMETERS));
621     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
622     d3dpp->Windowed               = TRUE;
623     d3dpp->hDeviceWindow          = vd->sys->hvideownd;
624     d3dpp->BackBufferWidth        = __MAX((unsigned int)GetSystemMetrics(SM_CXVIRTUALSCREEN),
625                                           d3ddm.Width);
626     d3dpp->BackBufferHeight       = __MAX((unsigned int)GetSystemMetrics(SM_CYVIRTUALSCREEN),
627                                           d3ddm.Height);
628     d3dpp->SwapEffect             = D3DSWAPEFFECT_COPY;
629     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
630     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
631     d3dpp->BackBufferFormat       = d3ddm.Format;
632     d3dpp->BackBufferCount        = 1;
633     d3dpp->EnableAutoDepthStencil = FALSE;
634
635     /* */
636     RECT *display = &vd->sys->rect_display;
637     display->left   = 0;
638     display->top    = 0;
639     display->right  = d3dpp->BackBufferWidth;
640     display->bottom = d3dpp->BackBufferHeight;
641
642     return VLC_SUCCESS;
643 }
644
645 /* */
646 static int  Direct3DCreateResources (vout_display_t *, video_format_t *);
647 static void Direct3DDestroyResources(vout_display_t *);
648
649 /**
650  * It creates a Direct3D device and the associated resources.
651  */
652 static int Direct3DOpen(vout_display_t *vd, video_format_t *fmt)
653 {
654     vout_display_sys_t *sys = vd->sys;
655     LPDIRECT3D9 d3dobj = sys->d3dobj;
656
657     if (Direct3DFillPresentationParameters(vd))
658         return VLC_EGENERIC;
659
660     // Create the D3DDevice
661     LPDIRECT3DDEVICE9 d3ddev;
662
663     UINT AdapterToUse = D3DADAPTER_DEFAULT;
664     D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
665
666 #ifndef NDEBUG
667     // Look for 'NVIDIA PerfHUD' adapter
668     // If it is present, override default settings
669     for (UINT Adapter=0; Adapter< IDirect3D9_GetAdapterCount(d3dobj); ++Adapter) {
670         D3DADAPTER_IDENTIFIER9 Identifier;
671         HRESULT Res = IDirect3D9_GetAdapterIdentifier(d3dobj,Adapter,0,&Identifier);
672         if (SUCCEEDED(Res) && strstr(Identifier.Description,"PerfHUD") != 0) {
673             AdapterToUse = Adapter;
674             DeviceType = D3DDEVTYPE_REF;
675             break;
676         }
677     }
678 #endif
679
680     /* */
681     D3DADAPTER_IDENTIFIER9 d3dai;
682     if (FAILED(IDirect3D9_GetAdapterIdentifier(d3dobj,AdapterToUse,0, &d3dai))) {
683         msg_Warn(vd, "IDirect3D9_GetAdapterIdentifier failed");
684     } else {
685         msg_Dbg(vd, "Direct3d Device: %s %lu %lu %lu", d3dai.Description,
686                 d3dai.VendorId, d3dai.DeviceId, d3dai.Revision );
687     }
688
689     HRESULT hr = IDirect3D9_CreateDevice(d3dobj, AdapterToUse,
690                                          DeviceType, sys->hvideownd,
691                                          D3DCREATE_SOFTWARE_VERTEXPROCESSING|
692                                          D3DCREATE_MULTITHREADED,
693                                          &sys->d3dpp, &d3ddev);
694     if (FAILED(hr)) {
695        msg_Err(vd, "Could not create the D3D device! (hr=0x%lX)", hr);
696        return VLC_EGENERIC;
697     }
698     sys->d3ddev = d3ddev;
699
700     UpdateRects(vd, NULL, NULL, true);
701
702     if (Direct3DCreateResources(vd, fmt)) {
703         msg_Err(vd, "Failed to allocate resources");
704         return VLC_EGENERIC;
705     }
706
707     /* Change the window title bar text */
708     EventThreadUpdateTitle(sys->event, VOUT_TITLE " (Direct3D output)");
709
710     msg_Dbg(vd, "Direct3D device adapter successfully initialized");
711     return VLC_SUCCESS;
712 }
713
714 /**
715  * It releases the Direct3D9 device and its resources.
716  */
717 static void Direct3DClose(vout_display_t *vd)
718 {
719     vout_display_sys_t *sys = vd->sys;
720
721     Direct3DDestroyResources(vd);
722
723     if (sys->d3ddev)
724        IDirect3DDevice9_Release(sys->d3ddev);
725
726     sys->d3ddev = NULL;
727 }
728
729 /**
730  * It reset the Direct3D9 device and its resources.
731  */
732 static int Direct3DReset(vout_display_t *vd)
733 {
734     vout_display_sys_t *sys = vd->sys;
735     LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
736
737     if (Direct3DFillPresentationParameters(vd))
738         return VLC_EGENERIC;
739
740     /* release all D3D objects */
741     Direct3DDestroyResources(vd);
742
743     /* */
744     HRESULT hr = IDirect3DDevice9_Reset(d3ddev, &sys->d3dpp);
745     if (FAILED(hr)) {
746         msg_Err(vd, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
747         return VLC_EGENERIC;
748     }
749
750     UpdateRects(vd, NULL, NULL, true);
751
752     /* re-create them */
753     if (Direct3DCreateResources(vd, &vd->fmt)) {
754         msg_Dbg(vd, "%s failed !", __FUNCTION__);
755         return VLC_EGENERIC;
756     }
757     return VLC_SUCCESS;
758 }
759
760 /* */
761 static int  Direct3DCreatePool(vout_display_t *vd, video_format_t *fmt);
762 static void Direct3DDestroyPool(vout_display_t *vd);
763
764 static int  Direct3DCreateScene(vout_display_t *vd, const video_format_t *fmt);
765 static void Direct3DDestroyScene(vout_display_t *vd);
766
767 static int  Direct3DCreateShaders(vout_display_t *vd);
768 static void Direct3DDestroyShaders(vout_display_t *vd);
769
770 /**
771  * It creates the picture and scene resources.
772  */
773 static int Direct3DCreateResources(vout_display_t *vd, video_format_t *fmt)
774 {
775     vout_display_sys_t *sys = vd->sys;
776
777     if (Direct3DCreatePool(vd, fmt)) {
778         msg_Err(vd, "Direct3D picture pool initialization failed");
779         return VLC_EGENERIC;
780     }
781     if (Direct3DCreateScene(vd, fmt)) {
782         msg_Err(vd, "Direct3D scene initialization failed !");
783         return VLC_EGENERIC;
784     }
785     if (Direct3DCreateShaders(vd)) {
786         /* Failing to initialize shaders is not fatal. */
787         msg_Warn(vd, "Direct3D shaders initialization failed !");
788     }
789
790     sys->d3dregion_format = D3DFMT_UNKNOWN;
791     for (int i = 0; i < 2; i++) {
792         D3DFORMAT fmt = i == 0 ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8;
793         if (SUCCEEDED(IDirect3D9_CheckDeviceFormat(sys->d3dobj,
794                                                    D3DADAPTER_DEFAULT,
795                                                    D3DDEVTYPE_HAL,
796                                                    sys->d3dpp.BackBufferFormat,
797                                                    D3DUSAGE_DYNAMIC,
798                                                    D3DRTYPE_TEXTURE,
799                                                    fmt))) {
800             sys->d3dregion_format = fmt;
801             break;
802         }
803     }
804     return VLC_SUCCESS;
805 }
806 /**
807  * It destroys the picture and scene resources.
808  */
809 static void Direct3DDestroyResources(vout_display_t *vd)
810 {
811     Direct3DDestroyScene(vd);
812     Direct3DDestroyPool(vd);
813     Direct3DDestroyShaders(vd);
814 }
815
816 /**
817  * It tests if the conversion from src to dst is supported.
818  */
819 static int Direct3DCheckConversion(vout_display_t *vd,
820                                    D3DFORMAT src, D3DFORMAT dst)
821 {
822     vout_display_sys_t *sys = vd->sys;
823     LPDIRECT3D9 d3dobj = sys->d3dobj;
824     HRESULT hr;
825
826     /* test whether device can create a surface of that format */
827     hr = IDirect3D9_CheckDeviceFormat(d3dobj, D3DADAPTER_DEFAULT,
828                                       D3DDEVTYPE_HAL, dst, 0,
829                                       D3DRTYPE_SURFACE, src);
830     if (SUCCEEDED(hr)) {
831         /* test whether device can perform color-conversion
832         ** from that format to target format
833         */
834         hr = IDirect3D9_CheckDeviceFormatConversion(d3dobj,
835                                                     D3DADAPTER_DEFAULT,
836                                                     D3DDEVTYPE_HAL,
837                                                     src, dst);
838     }
839     if (!SUCCEEDED(hr)) {
840         if (D3DERR_NOTAVAILABLE != hr)
841             msg_Err(vd, "Could not query adapter supported formats. (hr=0x%lX)", hr);
842         return VLC_EGENERIC;
843     }
844     return VLC_SUCCESS;
845 }
846
847 typedef struct
848 {
849     const char   *name;
850     D3DFORMAT    format;    /* D3D format */
851     vlc_fourcc_t fourcc;    /* VLC fourcc */
852     uint32_t     rmask;
853     uint32_t     gmask;
854     uint32_t     bmask;
855 } d3d_format_t;
856
857 static const d3d_format_t d3d_formats[] = {
858     /* YV12 is always used for planar 420, the planes are then swapped in Lock() */
859     { "YV12",       MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12,  0,0,0 },
860     { "YV12",       MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_I420,  0,0,0 },
861     { "YV12",       MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_J420,  0,0,0 },
862     { "UYVY",       D3DFMT_UYVY,    VLC_CODEC_UYVY,  0,0,0 },
863     { "YUY2",       D3DFMT_YUY2,    VLC_CODEC_YUYV,  0,0,0 },
864     { "X8R8G8B8",   D3DFMT_X8R8G8B8,VLC_CODEC_RGB32, 0xff0000, 0x00ff00, 0x0000ff },
865     { "A8R8G8B8",   D3DFMT_A8R8G8B8,VLC_CODEC_RGB32, 0xff0000, 0x00ff00, 0x0000ff },
866     { "8G8B8",      D3DFMT_R8G8B8,  VLC_CODEC_RGB24, 0xff0000, 0x00ff00, 0x0000ff },
867     { "R5G6B5",     D3DFMT_R5G6B5,  VLC_CODEC_RGB16, 0x1f<<11, 0x3f<<5,  0x1f<<0 },
868     { "X1R5G5B5",   D3DFMT_X1R5G5B5,VLC_CODEC_RGB15, 0x1f<<10, 0x1f<<5,  0x1f<<0 },
869
870     { NULL, 0, 0, 0,0,0}
871 };
872
873 /**
874  * It returns the format (closest to chroma) that can be converted to target */
875 static const d3d_format_t *Direct3DFindFormat(vout_display_t *vd, vlc_fourcc_t chroma, D3DFORMAT target)
876 {
877     vout_display_sys_t *sys = vd->sys;
878
879     for (unsigned pass = 0; pass < 2; pass++) {
880         const vlc_fourcc_t *list;
881
882         if (pass == 0 && sys->allow_hw_yuv && vlc_fourcc_IsYUV(chroma))
883             list = vlc_fourcc_GetYUVFallback(chroma);
884         else if (pass == 1)
885             list = vlc_fourcc_GetRGBFallback(chroma);
886         else
887             continue;
888
889         for (unsigned i = 0; list[i] != 0; i++) {
890             for (unsigned j = 0; d3d_formats[j].name; j++) {
891                 const d3d_format_t *format = &d3d_formats[j];
892
893                 if (format->fourcc != list[i])
894                     continue;
895
896                 msg_Warn(vd, "trying surface pixel format: %s",
897                          format->name);
898                 if (!Direct3DCheckConversion(vd, format->format, target)) {
899                     msg_Dbg(vd, "selected surface pixel format is %s",
900                             format->name);
901                     return format;
902                 }
903             }
904         }
905     }
906     return NULL;
907 }
908
909 /**
910  * It locks the surface associated to the picture and get the surface
911  * descriptor which amongst other things has the pointer to the picture
912  * data and its pitch.
913  */
914 static int Direct3DLockSurface(picture_t *picture)
915 {
916     /* Lock the surface to get a valid pointer to the picture buffer */
917     D3DLOCKED_RECT d3drect;
918     HRESULT hr = IDirect3DSurface9_LockRect(picture->p_sys->surface, &d3drect, NULL, 0);
919     if (FAILED(hr)) {
920         //msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
921         return CommonUpdatePicture(picture, &picture->p_sys->fallback, NULL, 0);
922     }
923
924     CommonUpdatePicture(picture, NULL, d3drect.pBits, d3drect.Pitch);
925     return VLC_SUCCESS;
926 }
927 /**
928  * It unlocks the surface associated to the picture.
929  */
930 static void Direct3DUnlockSurface(picture_t *picture)
931 {
932     /* Unlock the Surface */
933     HRESULT hr = IDirect3DSurface9_UnlockRect(picture->p_sys->surface);
934     if (FAILED(hr)) {
935         //msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
936     }
937 }
938
939 /**
940  * It creates the pool of picture (only 1).
941  *
942  * Each picture has an associated offscreen surface in video memory
943  * depending on hardware capabilities the picture chroma will be as close
944  * as possible to the orginal render chroma to reduce CPU conversion overhead
945  * and delegate this work to video card GPU
946  */
947 static int Direct3DCreatePool(vout_display_t *vd, video_format_t *fmt)
948 {
949     vout_display_sys_t *sys = vd->sys;
950     LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
951
952     /* */
953     *fmt = vd->source;
954
955     /* Find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
956      * the requested chroma which is usable by the hardware in an offscreen surface, as they
957      * typically support more formats than textures */
958     const d3d_format_t *d3dfmt = Direct3DFindFormat(vd, fmt->i_chroma, sys->d3dpp.BackBufferFormat);
959     if (!d3dfmt) {
960         msg_Err(vd, "surface pixel format is not supported.");
961         return VLC_EGENERIC;
962     }
963     fmt->i_chroma = d3dfmt->fourcc;
964     fmt->i_rmask  = d3dfmt->rmask;
965     fmt->i_gmask  = d3dfmt->gmask;
966     fmt->i_bmask  = d3dfmt->bmask;
967
968     /* We create one picture.
969      * It is useless to create more as we can't be used for direct rendering */
970
971     /* Create a surface */
972     LPDIRECT3DSURFACE9 surface;
973     HRESULT hr = IDirect3DDevice9_CreateOffscreenPlainSurface(d3ddev,
974                                                               fmt->i_visible_width,
975                                                               fmt->i_visible_height,
976                                                               d3dfmt->format,
977                                                               D3DPOOL_DEFAULT,
978                                                               &surface,
979                                                               NULL);
980     if (FAILED(hr)) {
981         msg_Err(vd, "Failed to create picture surface. (hr=0x%lx)", hr);
982         return VLC_EGENERIC;
983     }
984     /* fill surface with black color */
985     IDirect3DDevice9_ColorFill(d3ddev, surface, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0));
986
987     /* Create the associated picture */
988     picture_sys_t *picsys = malloc(sizeof(*picsys));
989     if (unlikely(picsys == NULL)) {
990         IDirect3DSurface9_Release(surface);
991         return VLC_ENOMEM;
992     }
993     picsys->surface = surface;
994     picsys->fallback = NULL;
995
996     picture_resource_t resource = { .p_sys = picsys };
997     for (int i = 0; i < PICTURE_PLANE_MAX; i++)
998         resource.p[i].i_lines = fmt->i_visible_height / (i > 0 ? 2 : 1);
999
1000     picture_t *picture = picture_NewFromResource(fmt, &resource);
1001     if (!picture) {
1002         IDirect3DSurface9_Release(surface);
1003         free(picsys);
1004         return VLC_ENOMEM;
1005     }
1006     sys->picsys = picsys;
1007
1008     /* Wrap it into a picture pool */
1009     picture_pool_configuration_t pool_cfg;
1010     memset(&pool_cfg, 0, sizeof(pool_cfg));
1011     pool_cfg.picture_count = 1;
1012     pool_cfg.picture       = &picture;
1013     pool_cfg.lock          = Direct3DLockSurface;
1014     pool_cfg.unlock        = Direct3DUnlockSurface;
1015
1016     sys->pool = picture_pool_NewExtended(&pool_cfg);
1017     if (!sys->pool) {
1018         picture_Release(picture);
1019         IDirect3DSurface9_Release(surface);
1020         return VLC_ENOMEM;
1021     }
1022     return VLC_SUCCESS;
1023 }
1024 /**
1025  * It destroys the pool of picture and its resources.
1026  */
1027 static void Direct3DDestroyPool(vout_display_t *vd)
1028 {
1029     vout_display_sys_t *sys = vd->sys;
1030
1031     if (sys->pool) {
1032         picture_sys_t *picsys = sys->picsys;
1033         IDirect3DSurface9_Release(picsys->surface);
1034         if (picsys->fallback)
1035             picture_Release(picsys->fallback);
1036         picture_pool_Delete(sys->pool);
1037     }
1038     sys->pool = NULL;
1039 }
1040
1041 /**
1042  * It allocates and initializes the resources needed to render the scene.
1043  */
1044 static int Direct3DCreateScene(vout_display_t *vd, const video_format_t *fmt)
1045 {
1046     vout_display_sys_t *sys = vd->sys;
1047     LPDIRECT3DDEVICE9       d3ddev = sys->d3ddev;
1048     HRESULT hr;
1049
1050     /*
1051      * Create a texture for use when rendering a scene
1052      * for performance reason, texture format is identical to backbuffer
1053      * which would usually be a RGB format
1054      */
1055     LPDIRECT3DTEXTURE9 d3dtex;
1056     hr = IDirect3DDevice9_CreateTexture(d3ddev,
1057                                         fmt->i_visible_width,
1058                                         fmt->i_visible_height,
1059                                         1,
1060                                         D3DUSAGE_RENDERTARGET,
1061                                         sys->d3dpp.BackBufferFormat,
1062                                         D3DPOOL_DEFAULT,
1063                                         &d3dtex,
1064                                         NULL);
1065     if (FAILED(hr)) {
1066         msg_Err(vd, "Failed to create texture. (hr=0x%lx)", hr);
1067         return VLC_EGENERIC;
1068     }
1069
1070     /*
1071     ** Create a vertex buffer for use when rendering scene
1072     */
1073     LPDIRECT3DVERTEXBUFFER9 d3dvtc;
1074     hr = IDirect3DDevice9_CreateVertexBuffer(d3ddev,
1075                                              sizeof(CUSTOMVERTEX)*4,
1076                                              D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1077                                              D3DFVF_CUSTOMVERTEX,
1078                                              D3DPOOL_DEFAULT,
1079                                              &d3dvtc,
1080                                              NULL);
1081     if (FAILED(hr)) {
1082         msg_Err(vd, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1083         IDirect3DTexture9_Release(d3dtex);
1084         return VLC_EGENERIC;
1085     }
1086
1087     /* */
1088     sys->d3dtex = d3dtex;
1089     sys->d3dvtc = d3dvtc;
1090
1091     sys->d3dregion_count = 0;
1092     sys->d3dregion       = NULL;
1093
1094     sys->clear_scene = true;
1095
1096     // Texture coordinates outside the range [0.0, 1.0] are set
1097     // to the texture color at 0.0 or 1.0, respectively.
1098     IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1099     IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1100
1101     // Set linear filtering quality
1102     if (sys->d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR) {
1103         msg_Dbg(vd, "Using D3DTEXF_LINEAR for minification");
1104         IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1105     } else {
1106         msg_Dbg(vd, "Using D3DTEXF_POINT for minification");
1107         IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
1108     }
1109     if (sys->d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR) {
1110         msg_Dbg(vd, "Using D3DTEXF_LINEAR for magnification");
1111         IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1112     } else {
1113         msg_Dbg(vd, "Using D3DTEXF_POINT for magnification");
1114         IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
1115     }
1116
1117     // set maximum ambient light
1118     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1119
1120     // Turn off culling
1121     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1122
1123     // Turn off the zbuffer
1124     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1125
1126     // Turn off lights
1127     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_LIGHTING, FALSE);
1128
1129     // Enable dithering
1130     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_DITHERENABLE, TRUE);
1131
1132     // disable stencil
1133     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_STENCILENABLE, FALSE);
1134
1135     // manage blending
1136     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHABLENDENABLE, FALSE);
1137     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1138     IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1139
1140     if (sys->d3dcaps.AlphaCmpCaps & D3DPCMPCAPS_GREATER) {
1141         IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1142         IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHAREF, 0x00);
1143         IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1144     }
1145
1146     // Set texture states
1147     IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_COLOROP,D3DTOP_SELECTARG1);
1148     IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1149
1150     IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
1151     IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
1152     IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
1153
1154     msg_Dbg(vd, "Direct3D scene created successfully");
1155
1156     return VLC_SUCCESS;
1157 }
1158
1159 /**
1160  * It releases the scene resources.
1161  */
1162 static void Direct3DDestroyScene(vout_display_t *vd)
1163 {
1164     vout_display_sys_t *sys = vd->sys;
1165
1166     Direct3DDeleteRegions(sys->d3dregion_count, sys->d3dregion);
1167
1168     LPDIRECT3DVERTEXBUFFER9 d3dvtc = sys->d3dvtc;
1169     if (d3dvtc)
1170         IDirect3DVertexBuffer9_Release(d3dvtc);
1171
1172     LPDIRECT3DTEXTURE9 d3dtex = sys->d3dtex;
1173     if (d3dtex)
1174         IDirect3DTexture9_Release(d3dtex);
1175
1176     sys->d3dvtc = NULL;
1177     sys->d3dtex = NULL;
1178
1179     sys->d3dregion_count = 0;
1180     sys->d3dregion       = NULL;
1181
1182     msg_Dbg(vd, "Direct3D scene released successfully");
1183 }
1184
1185 static int Direct3DCompileShader(vout_display_t *vd, const char *shader_source, size_t source_length)
1186 {
1187     vout_display_sys_t *sys = vd->sys;
1188
1189     HRESULT (WINAPI * OurD3DXCompileShader)(
1190             LPCSTR pSrcData,
1191             UINT srcDataLen,
1192             const D3DXMACRO *pDefines,
1193             LPD3DXINCLUDE pInclude,
1194             LPCSTR pFunctionName,
1195             LPCSTR pProfile,
1196             DWORD Flags,
1197             LPD3DXBUFFER *ppShader,
1198             LPD3DXBUFFER *ppErrorMsgs,
1199             LPD3DXCONSTANTTABLE *ppConstantTable);
1200
1201     OurD3DXCompileShader = (void*)GetProcAddress(sys->hd3d9x_dll, "D3DXCompileShader");
1202     if (!OurD3DXCompileShader) {
1203         msg_Warn(vd, "Cannot locate reference to D3DXCompileShader; pixel shading will be disabled");
1204         return VLC_EGENERIC;
1205     }
1206
1207     LPD3DXBUFFER error_msgs = NULL;
1208     LPD3DXBUFFER compiled_shader = NULL;
1209
1210     DWORD shader_flags = 0;
1211     HRESULT hr = OurD3DXCompileShader(shader_source, source_length, NULL, NULL,
1212                 "main", "ps_3_0", shader_flags, &compiled_shader, &error_msgs, NULL);
1213
1214     if (FAILED(hr)) {
1215         msg_Warn(vd, "D3DXCompileShader Error (hr=0x%lX)", hr);
1216         if (error_msgs)
1217             msg_Warn(vd, "HLSL Compilation Error: %s", (char*)ID3DXBuffer_GetBufferPointer(error_msgs));
1218         return VLC_EGENERIC;
1219     }
1220
1221     hr = IDirect3DDevice9_CreatePixelShader(sys->d3ddev,
1222             ID3DXBuffer_GetBufferPointer(compiled_shader),
1223             &sys->d3dx_shader);
1224
1225     if (FAILED(hr)) {
1226         msg_Warn(vd, "IDirect3DDevice9_CreatePixelShader error (hr=0x%lX)", hr);
1227         return VLC_EGENERIC;
1228     }
1229     return VLC_SUCCESS;
1230 }
1231
1232 #define MAX_SHADER_FILE_SIZE 1024*1024
1233
1234 static int Direct3DCreateShaders(vout_display_t *vd)
1235 {
1236     vout_display_sys_t *sys = vd->sys;
1237
1238     if (!sys->hd3d9x_dll)
1239         return VLC_EGENERIC;
1240
1241     /* Find which shader was selected in the list. */
1242     char *selected_shader = var_InheritString(vd, "direct3d-shader");
1243     if (!selected_shader)
1244         return VLC_SUCCESS; /* Nothing to do */
1245
1246     const char *shader_source_builtin = NULL;
1247     char *shader_source_file = NULL;
1248     FILE *fs = NULL;
1249
1250     for (size_t i = 0; i < BUILTIN_SHADERS_COUNT; ++i) {
1251         if (!strcmp(selected_shader, builtin_shaders[i].name)) {
1252             shader_source_builtin = builtin_shaders[i].code;
1253             break;
1254         }
1255     }
1256
1257     if (shader_source_builtin) {
1258         /* A builtin shader was selected. */
1259         int err = Direct3DCompileShader(vd, shader_source_builtin, strlen(shader_source_builtin));
1260         if (err)
1261             goto error;
1262     } else {
1263         if (strcmp(selected_shader, SELECTED_SHADER_FILE))
1264             goto error; /* Unrecognized entry in the list. */
1265         /* The source code of the shader needs to be read from a file. */
1266         char *filepath = var_InheritString(vd, "direct3d-shader-file");
1267         if (!filepath || !*filepath)
1268         {
1269             free(filepath);
1270             goto error;
1271         }
1272         /* Open file, find its size with fseek/ftell and read its content in a buffer. */
1273         fs = fopen(filepath, "rb");
1274         if (!fs)
1275             goto error;
1276         int ret = fseek(fs, 0, SEEK_END);
1277         if (ret == -1)
1278             goto error;
1279         long length = ftell(fs);
1280         if (length == -1 || length >= MAX_SHADER_FILE_SIZE)
1281             goto error;
1282         rewind(fs);
1283         shader_source_file = malloc(sizeof(*shader_source_file) * length);
1284         if (!shader_source_file)
1285             goto error;
1286         ret = fread(shader_source_file, length, 1, fs);
1287         if (ret != 1)
1288             goto error;
1289         ret = Direct3DCompileShader(vd, shader_source_file, length);
1290         if (ret)
1291             goto error;
1292     }
1293
1294     free(selected_shader);
1295     free(shader_source_file);
1296     fclose(fs);
1297
1298     return VLC_SUCCESS;
1299
1300 error:
1301     Direct3DDestroyShaders(vd);
1302     free(selected_shader);
1303     free(shader_source_file);
1304     if (fs)
1305         fclose(fs);
1306     return VLC_EGENERIC;
1307 }
1308
1309 static void Direct3DDestroyShaders(vout_display_t *vd)
1310 {
1311     vout_display_sys_t *sys = vd->sys;
1312
1313     if (sys->d3dx_shader)
1314         IDirect3DPixelShader9_Release(sys->d3dx_shader);
1315     sys->d3dx_shader = NULL;
1316 }
1317
1318 /**
1319  * Compute the vertex ordering needed to rotate the video. Without
1320  * rotation, the vertices of the rectangle are defined in a clockwise
1321  * order. This function computes a remapping of the coordinates to
1322  * implement the rotation, given fixed texture coordinates.
1323  * The unrotated order is the following:
1324  * 0--1
1325  * |  |
1326  * 3--2
1327  * For a 180 degrees rotation it should like this:
1328  * 2--3
1329  * |  |
1330  * 1--0
1331  * Vertex 0 should be assigned coordinates at index 2 from the
1332  * unrotated order and so on, thus yielding order: 2 3 0 1.
1333  */
1334 static void orientationVertexOrder(video_orientation_t orientation, int vertex_order[static 4])
1335 {
1336     switch (orientation) {
1337         case ORIENT_ROTATED_90:
1338             vertex_order[0] = 1;
1339             vertex_order[1] = 2;
1340             vertex_order[2] = 3;
1341             vertex_order[3] = 0;
1342             break;
1343         case ORIENT_ROTATED_270:
1344             vertex_order[0] = 3;
1345             vertex_order[1] = 0;
1346             vertex_order[2] = 1;
1347             vertex_order[3] = 2;
1348             break;
1349         case ORIENT_ROTATED_180:
1350             vertex_order[0] = 2;
1351             vertex_order[1] = 3;
1352             vertex_order[2] = 0;
1353             vertex_order[3] = 1;
1354             break;
1355         case ORIENT_TRANSPOSED:
1356             vertex_order[0] = 0;
1357             vertex_order[1] = 3;
1358             vertex_order[2] = 2;
1359             vertex_order[3] = 1;
1360             break;
1361         case ORIENT_HFLIPPED:
1362             vertex_order[0] = 3;
1363             vertex_order[1] = 2;
1364             vertex_order[2] = 1;
1365             vertex_order[3] = 0;
1366             break;
1367         case ORIENT_VFLIPPED:
1368             vertex_order[0] = 1;
1369             vertex_order[1] = 0;
1370             vertex_order[2] = 3;
1371             vertex_order[3] = 2;
1372             break;
1373         case ORIENT_ANTI_TRANSPOSED: /* transpose + vflip */
1374             vertex_order[0] = 1;
1375             vertex_order[1] = 2;
1376             vertex_order[2] = 3;
1377             vertex_order[3] = 0;
1378             break;
1379        default:
1380             vertex_order[0] = 0;
1381             vertex_order[1] = 1;
1382             vertex_order[2] = 2;
1383             vertex_order[3] = 3;
1384             break;
1385     }
1386 }
1387
1388 static void Direct3DSetupVertices(CUSTOMVERTEX *vertices,
1389                                   const RECT src_full,
1390                                   const RECT src_crop,
1391                                   const RECT dst,
1392                                   int alpha,
1393                                   video_orientation_t orientation)
1394 {
1395     const float src_full_width  = src_full.right  - src_full.left;
1396     const float src_full_height = src_full.bottom - src_full.top;
1397
1398     /* Vertices of the dst rectangle in the unrotated (clockwise) order. */
1399     const int vertices_coords[4][2] = {
1400         { dst.left,  dst.top    },
1401         { dst.right, dst.top    },
1402         { dst.right, dst.bottom },
1403         { dst.left,  dst.bottom },
1404     };
1405
1406     /* Compute index remapping necessary to implement the rotation. */
1407     int vertex_order[4];
1408     orientationVertexOrder(orientation, vertex_order);
1409
1410     for (int i = 0; i < 4; ++i) {
1411         vertices[i].x  = vertices_coords[vertex_order[i]][0];
1412         vertices[i].y  = vertices_coords[vertex_order[i]][1];
1413     }
1414
1415     vertices[0].tu = src_crop.left / src_full_width;
1416     vertices[0].tv = src_crop.top  / src_full_height;
1417
1418     vertices[1].tu = src_crop.right / src_full_width;
1419     vertices[1].tv = src_crop.top   / src_full_height;
1420
1421     vertices[2].tu = src_crop.right  / src_full_width;
1422     vertices[2].tv = src_crop.bottom / src_full_height;
1423
1424     vertices[3].tu = src_crop.left   / src_full_width;
1425     vertices[3].tv = src_crop.bottom / src_full_height;
1426
1427     for (int i = 0; i < 4; i++) {
1428         /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
1429         /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
1430         vertices[i].x -= 0.5;
1431         vertices[i].y -= 0.5;
1432
1433         vertices[i].z       = 0.0f;
1434         vertices[i].rhw     = 1.0f;
1435         vertices[i].diffuse = D3DCOLOR_ARGB(alpha, 255, 255, 255);
1436     }
1437 }
1438
1439 /**
1440  * It copies picture surface into a texture and setup the associated d3d_region_t.
1441  */
1442 static int Direct3DImportPicture(vout_display_t *vd,
1443                                  d3d_region_t *region,
1444                                  LPDIRECT3DSURFACE9 source)
1445 {
1446     vout_display_sys_t *sys = vd->sys;
1447     HRESULT hr;
1448
1449     if (!source) {
1450         msg_Dbg(vd, "no surface to render ?");
1451         return VLC_EGENERIC;
1452     }
1453
1454     /* retrieve texture top-level surface */
1455     LPDIRECT3DSURFACE9 destination;
1456     hr = IDirect3DTexture9_GetSurfaceLevel(sys->d3dtex, 0, &destination);
1457     if (FAILED(hr)) {
1458         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1459         return VLC_EGENERIC;
1460     }
1461
1462     /* Copy picture surface into texture surface
1463      * color space conversion happen here */
1464     hr = IDirect3DDevice9_StretchRect(sys->d3ddev, source, NULL, destination, NULL, D3DTEXF_LINEAR);
1465     IDirect3DSurface9_Release(destination);
1466     if (FAILED(hr)) {
1467         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1468         return VLC_EGENERIC;
1469     }
1470
1471     /* */
1472     region->texture = sys->d3dtex;
1473     Direct3DSetupVertices(region->vertex,
1474                           vd->sys->rect_src,
1475                           vd->sys->rect_src_clipped,
1476                           vd->sys->rect_dest_clipped, 255, vd->fmt.orientation);
1477     return VLC_SUCCESS;
1478 }
1479
1480 static void Direct3DDeleteRegions(int count, d3d_region_t *region)
1481 {
1482     for (int i = 0; i < count; i++) {
1483         if (region[i].texture)
1484             IDirect3DTexture9_Release(region[i].texture);
1485     }
1486     free(region);
1487 }
1488
1489 static void Direct3DImportSubpicture(vout_display_t *vd,
1490                                      int *count_ptr, d3d_region_t **region,
1491                                      subpicture_t *subpicture)
1492 {
1493     vout_display_sys_t *sys = vd->sys;
1494
1495     int count = 0;
1496     for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
1497         count++;
1498
1499     *count_ptr = count;
1500     *region    = calloc(count, sizeof(**region));
1501     if (*region == NULL) {
1502         *count_ptr = 0;
1503         return;
1504     }
1505
1506     int i = 0;
1507     for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
1508         d3d_region_t *d3dr = &(*region)[i];
1509         HRESULT hr;
1510
1511         d3dr->texture = NULL;
1512         for (int j = 0; j < sys->d3dregion_count; j++) {
1513             d3d_region_t *cache = &sys->d3dregion[j];
1514             if (cache->texture &&
1515                 cache->format == sys->d3dregion_format &&
1516                 cache->width  == r->fmt.i_visible_width &&
1517                 cache->height == r->fmt.i_visible_height) {
1518 #ifndef NDEBUG
1519                 msg_Dbg(vd, "Reusing %dx%d texture for OSD",
1520                         cache->width, cache->height);
1521 #endif
1522                 *d3dr = *cache;
1523                 memset(cache, 0, sizeof(*cache));
1524                 break;
1525             }
1526         }
1527         if (!d3dr->texture) {
1528             d3dr->format = sys->d3dregion_format;
1529             d3dr->width  = r->fmt.i_visible_width;
1530             d3dr->height = r->fmt.i_visible_height;
1531             hr = IDirect3DDevice9_CreateTexture(sys->d3ddev,
1532                                                 d3dr->width, d3dr->height,
1533                                                 1,
1534                                                 D3DUSAGE_DYNAMIC,
1535                                                 d3dr->format,
1536                                                 D3DPOOL_DEFAULT,
1537                                                 &d3dr->texture,
1538                                                 NULL);
1539             if (FAILED(hr)) {
1540                 d3dr->texture = NULL;
1541                 msg_Err(vd, "Failed to create %dx%d texture for OSD (hr=0x%0lX)",
1542                         d3dr->width, d3dr->height, hr);
1543                 continue;
1544             }
1545 #ifndef NDEBUG
1546             msg_Dbg(vd, "Created %dx%d texture for OSD",
1547                     r->fmt.i_visible_width, r->fmt.i_visible_height);
1548 #endif
1549         }
1550
1551         D3DLOCKED_RECT lock;
1552         hr = IDirect3DTexture9_LockRect(d3dr->texture, 0, &lock, NULL, 0);
1553         if (SUCCEEDED(hr)) {
1554             uint8_t  *dst_data   = lock.pBits;
1555             int       dst_pitch  = lock.Pitch;
1556             const int src_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
1557                                    r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
1558             uint8_t  *src_data   = &r->p_picture->p->p_pixels[src_offset];
1559             int       src_pitch  = r->p_picture->p->i_pitch;
1560             for (unsigned y = 0; y < r->fmt.i_visible_height; y++) {
1561                 int copy_pitch = __MIN(dst_pitch, r->p_picture->p->i_visible_pitch);
1562                 if (d3dr->format == D3DFMT_A8B8G8R8) {
1563                     memcpy(&dst_data[y * dst_pitch], &src_data[y * src_pitch],
1564                            copy_pitch);
1565                 } else {
1566                     for (int x = 0; x < copy_pitch; x += 4) {
1567                         dst_data[y * dst_pitch + x + 0] = src_data[y * src_pitch + x + 2];
1568                         dst_data[y * dst_pitch + x + 1] = src_data[y * src_pitch + x + 1];
1569                         dst_data[y * dst_pitch + x + 2] = src_data[y * src_pitch + x + 0];
1570                         dst_data[y * dst_pitch + x + 3] = src_data[y * src_pitch + x + 3];
1571                     }
1572                 }
1573             }
1574             hr = IDirect3DTexture9_UnlockRect(d3dr->texture, 0);
1575             if (FAILED(hr))
1576                 msg_Err(vd, "Failed to unlock the texture");
1577         } else {
1578             msg_Err(vd, "Failed to lock the texture");
1579         }
1580
1581         /* Map the subpicture to sys->rect_dest */
1582         RECT src;
1583         src.left   = 0;
1584         src.right  = src.left + r->fmt.i_visible_width;
1585         src.top    = 0;
1586         src.bottom = src.top  + r->fmt.i_visible_height;
1587
1588         const RECT video = sys->rect_dest;
1589         const float scale_w = (float)(video.right  - video.left) / subpicture->i_original_picture_width;
1590         const float scale_h = (float)(video.bottom - video.top)  / subpicture->i_original_picture_height;
1591
1592         RECT dst;
1593         dst.left   = video.left + scale_w * r->i_x,
1594         dst.right  = dst.left + scale_w * r->fmt.i_visible_width,
1595         dst.top    = video.top  + scale_h * r->i_y,
1596         dst.bottom = dst.top  + scale_h * r->fmt.i_visible_height,
1597         Direct3DSetupVertices(d3dr->vertex,
1598                               src, src, dst,
1599                               subpicture->i_alpha * r->i_alpha / 255, ORIENT_NORMAL);
1600     }
1601 }
1602
1603 static int Direct3DRenderRegion(vout_display_t *vd,
1604                                 d3d_region_t *region,
1605                                 bool use_pixel_shader)
1606 {
1607     vout_display_sys_t *sys = vd->sys;
1608
1609     LPDIRECT3DDEVICE9 d3ddev = vd->sys->d3ddev;
1610
1611     LPDIRECT3DVERTEXBUFFER9 d3dvtc = sys->d3dvtc;
1612     LPDIRECT3DTEXTURE9      d3dtex = region->texture;
1613
1614     HRESULT hr;
1615
1616     /* Import vertices */
1617     void *vertex;
1618     hr = IDirect3DVertexBuffer9_Lock(d3dvtc, 0, 0, &vertex, D3DLOCK_DISCARD);
1619     if (FAILED(hr)) {
1620         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1621         return -1;
1622     }
1623     memcpy(vertex, region->vertex, sizeof(region->vertex));
1624     hr = IDirect3DVertexBuffer9_Unlock(d3dvtc);
1625     if (FAILED(hr)) {
1626         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1627         return -1;
1628     }
1629
1630     // Setup our texture. Using textures introduces the texture stage states,
1631     // which govern how textures get blended together (in the case of multiple
1632     // textures) and lighting information. In this case, we are modulating
1633     // (blending) our texture with the diffuse color of the vertices.
1634     hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex);
1635     if (FAILED(hr)) {
1636         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1637         return -1;
1638     }
1639
1640     if (sys->d3dx_shader) {
1641         if (use_pixel_shader)
1642         {
1643             hr = IDirect3DDevice9_SetPixelShader(d3ddev, sys->d3dx_shader);
1644             float shader_data[4] = { region->width, region->height, 0, 0 };
1645             hr = IDirect3DDevice9_SetPixelShaderConstantF(d3ddev, 0, shader_data, 1);
1646             if (FAILED(hr)) {
1647                 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1648                 return -1;
1649             }
1650         }
1651         else /* Disable any existing pixel shader. */
1652             hr = IDirect3DDevice9_SetPixelShader(d3ddev, NULL);
1653         if (FAILED(hr)) {
1654             msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1655             return -1;
1656         }
1657     }
1658
1659     // Render the vertex buffer contents
1660     hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
1661     if (FAILED(hr)) {
1662         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1663         return -1;
1664     }
1665
1666     // we use FVF instead of vertex shader
1667     hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
1668     if (FAILED(hr)) {
1669         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1670         return -1;
1671     }
1672
1673     // draw rectangle
1674     hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1675     if (FAILED(hr)) {
1676         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1677         return -1;
1678     }
1679     return 0;
1680 }
1681
1682 /**
1683  * It renders the scene.
1684  *
1685  * This function is intented for higher end 3D cards, with pixel shader support
1686  * and at least 64 MiB of video RAM.
1687  */
1688 static void Direct3DRenderScene(vout_display_t *vd,
1689                                 d3d_region_t *picture,
1690                                 int subpicture_count,
1691                                 d3d_region_t *subpicture)
1692 {
1693     vout_display_sys_t *sys = vd->sys;
1694     LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
1695     HRESULT hr;
1696
1697     if (sys->clear_scene) {
1698         /* Clear the backbuffer and the zbuffer */
1699         hr = IDirect3DDevice9_Clear(d3ddev, 0, NULL, D3DCLEAR_TARGET,
1700                                   D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
1701         if (FAILED(hr)) {
1702             msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1703             return;
1704         }
1705         sys->clear_scene = false;
1706     }
1707
1708     // Begin the scene
1709     hr = IDirect3DDevice9_BeginScene(d3ddev);
1710     if (FAILED(hr)) {
1711         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1712         return;
1713     }
1714
1715     Direct3DRenderRegion(vd, picture, true);
1716
1717     if (subpicture_count > 0)
1718         IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1719     for (int i = 0; i < subpicture_count; i++) {
1720         d3d_region_t *r = &subpicture[i];
1721         if (r->texture)
1722             Direct3DRenderRegion(vd, r, false);
1723     }
1724     if (subpicture_count > 0)
1725         IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHABLENDENABLE, FALSE);
1726
1727     // End the scene
1728     hr = IDirect3DDevice9_EndScene(d3ddev);
1729     if (FAILED(hr)) {
1730         msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1731         return;
1732     }
1733 }
1734
1735 /*****************************************************************************
1736  * DesktopCallback: desktop mode variable callback
1737  *****************************************************************************/
1738 static int DesktopCallback(vlc_object_t *object, char const *psz_cmd,
1739                             vlc_value_t oldval, vlc_value_t newval,
1740                             void *p_data)
1741 {
1742     vout_display_t *vd = (vout_display_t *)object;
1743     vout_display_sys_t *sys = vd->sys;
1744     VLC_UNUSED(psz_cmd);
1745     VLC_UNUSED(oldval);
1746     VLC_UNUSED(p_data);
1747
1748     vlc_mutex_lock(&sys->lock);
1749     const bool ch_desktop = !sys->desktop_requested != !newval.b_bool;
1750     sys->ch_desktop |= ch_desktop;
1751     sys->desktop_requested = newval.b_bool;
1752     vlc_mutex_unlock(&sys->lock);
1753     return VLC_SUCCESS;
1754 }
1755
1756 typedef struct
1757 {
1758     char **values;
1759     char **descs;
1760     size_t count;
1761 } enum_context_t;
1762
1763 static void ListShaders(enum_context_t *ctx)
1764 {
1765     size_t num_shaders = BUILTIN_SHADERS_COUNT;
1766     ctx->values = xrealloc(ctx->values, (ctx->count + num_shaders + 1) * sizeof(char *));
1767     ctx->descs = xrealloc(ctx->descs, (ctx->count + num_shaders + 1) * sizeof(char *));
1768     for (size_t i = 0; i < num_shaders; ++i) {
1769         ctx->values[ctx->count] = strdup(builtin_shaders[i].name);
1770         ctx->descs[ctx->count] = strdup(builtin_shaders[i].name);
1771         ctx->count++;
1772     }
1773     ctx->values[ctx->count] = strdup(SELECTED_SHADER_FILE);
1774     ctx->descs[ctx->count] = strdup(SELECTED_SHADER_FILE);
1775     ctx->count++;
1776 }
1777
1778 /* Populate the list of available shader techniques in the options */
1779 static int FindShadersCallback(vlc_object_t *object, const char *name,
1780                                char ***values, char ***descs)
1781 {
1782     VLC_UNUSED(object);
1783     VLC_UNUSED(name);
1784
1785     enum_context_t ctx = { NULL, NULL, 0 };
1786
1787     ListShaders(&ctx);
1788
1789     *values = ctx.values;
1790     *descs = ctx.descs;
1791     return ctx.count;
1792
1793 }