1 /*****************************************************************************
2 * direct3d.c: Windows Direct3D video output module
3 *****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
7 * Authors: Damien Fouilleul <damienf@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
27 * This plugin will use YUV surface if supported, using YUV will result in
28 * the best video quality (hardware filering when rescaling the picture)
29 * and the fastest display as it requires less processing.
31 * If YUV overlay is not supported this plugin will use RGB offscreen video
32 * surfaces that will be blitted onto the primary surface (display) to
33 * effectively display the pictures.
35 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_playlist.h>
43 #include <vlc_vout_display.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int OpenVideoXP(vlc_object_t *);
54 static int OpenVideoVista(vlc_object_t *);
55 static void Close(vlc_object_t *);
57 #define DESKTOP_TEXT N_("Enable desktop mode ")
58 #define DESKTOP_LONGTEXT N_(\
59 "The desktop mode allows you to display the video on the desktop.")
61 #define D3D_HELP N_("Recommended video output for Windows Vista and later versions")
64 set_shortname("Direct3D")
65 set_description(N_("Direct3D video output"))
67 set_category(CAT_VIDEO)
68 set_subcategory(SUBCAT_VIDEO_VOUT)
70 add_bool("direct3d-desktop", false, NULL, DESKTOP_TEXT, DESKTOP_LONGTEXT, true)
72 set_capability("vout display", 150)
73 add_shortcut("direct3d")
74 set_callbacks(OpenVideoVista, Close)
77 set_description(N_("Direct3D video output (XP)"))
78 set_capability("vout display", 70)
79 add_shortcut("direct3d_xp")
80 set_callbacks(OpenVideoXP, Close)
84 /*****************************************************************************
86 *****************************************************************************/
89 LPDIRECT3DSURFACE9 surface;
93 static int Open(vlc_object_t *);
95 static picture_pool_t *Pool (vout_display_t *, unsigned);
96 static void Prepare(vout_display_t *, picture_t *);
97 static void Display(vout_display_t *, picture_t *);
98 static int Control(vout_display_t *, int, va_list);
99 static void Manage (vout_display_t *);
101 static int Direct3DCreate (vout_display_t *);
102 static int Direct3DReset (vout_display_t *);
103 static void Direct3DDestroy(vout_display_t *);
105 static int Direct3DOpen (vout_display_t *, video_format_t *);
106 static void Direct3DClose(vout_display_t *);
108 static void Direct3DRenderScene(vout_display_t *vd, LPDIRECT3DSURFACE9 surface);
111 static int DesktopCallback(vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *);
114 * It creates a Direct3D vout display.
116 static int Open(vlc_object_t *object)
118 vout_display_t *vd = (vout_display_t *)object;
119 vout_display_sys_t *sys;
121 /* Allocate structure */
122 vd->sys = sys = calloc(1, sizeof(vout_display_sys_t));
126 if (Direct3DCreate(vd)) {
127 msg_Err(vd, "Direct3D could not be initialized");
133 sys->use_desktop = var_CreateGetBool(vd, "direct3d-desktop");
134 sys->reset_device = false;
135 sys->reset_device = false;
136 sys->allow_hw_yuv = var_CreateGetBool(vd, "directx-hw-yuv");
137 sys->desktop_save.is_fullscreen = vd->cfg->is_fullscreen;
138 sys->desktop_save.is_on_top = false;
139 sys->desktop_save.win.left = var_InheritInteger(vd, "video-x");
140 sys->desktop_save.win.right = vd->cfg->display.width;
141 sys->desktop_save.win.top = var_InheritInteger(vd, "video-y");
142 sys->desktop_save.win.bottom = vd->cfg->display.height;
149 if (Direct3DOpen(vd, &fmt)) {
150 msg_Err(vd, "Direct3D could not be opened");
155 vout_display_info_t info = vd->info;
157 info.has_double_click = true;
158 info.has_hide_mouse = false;
159 info.has_pictures_invalid = true;
160 info.has_event_thread = true;
163 vlc_mutex_init(&sys->lock);
164 sys->ch_desktop = false;
165 sys->desktop_requested = sys->use_desktop;
168 val.psz_string = _("Desktop");
169 var_Change(vd, "direct3d-desktop", VLC_VAR_SETTEXT, &val, NULL);
170 var_AddCallback(vd, "direct3d-desktop", DesktopCallback, NULL);
172 /* Setup vout_display now that everything is fine */
177 vd->prepare = Prepare;
178 vd->display = Display;
179 vd->control = Control;
182 /* Fix state in case of desktop mode */
183 if (sys->use_desktop && vd->cfg->is_fullscreen)
184 vout_display_SendEventFullscreen(vd, false);
195 static bool IsVistaOrAbove(void)
197 OSVERSIONINFO winVer;
198 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
199 return GetVersionEx(&winVer) && winVer.dwMajorVersion > 5;
202 static int OpenVideoXP(vlc_object_t *obj)
204 /* Windows XP or lower, make sure this module isn't the default */
205 return IsVistaOrAbove() ? VLC_EGENERIC : Open(obj);
208 static int OpenVideoVista(vlc_object_t *obj)
210 /* Windows Vista or above, make this module the default */
211 return IsVistaOrAbove() ? Open(obj) : VLC_EGENERIC;
215 * It destroyes a Direct3D vout display.
217 static void Close(vlc_object_t *object)
219 vout_display_t * vd = (vout_display_t *)object;
221 var_DelCallback(vd, "direct3d-desktop", DesktopCallback, NULL);
233 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
236 return vd->sys->pool;
239 static int Direct3DLockSurface(picture_t *);
240 static void Direct3DUnlockSurface(picture_t *);
242 static void Prepare(vout_display_t *vd, picture_t *picture)
244 LPDIRECT3DSURFACE9 surface = picture->p_sys->surface;
246 picture_Release(picture);
247 Direct3DRenderScene(vd, surface);
249 /* FIXME it is a bit ugly, we need the surface to be unlocked for
251 * The clean way would be to release the picture (and ensure that
252 * the vout doesn't keep a reference). But because of the vout
253 * wrapper, we can't */
255 Direct3DUnlockSurface(picture);
257 Direct3DRenderScene(vd, surface);
261 static void Display(vout_display_t *vd, picture_t *picture)
263 vout_display_sys_t *sys = vd->sys;
264 LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
266 // Present the back buffer contents to the display
267 // No stretching should happen here !
268 const RECT src = sys->rect_dest_clipped;
269 const RECT dst = sys->rect_dest_clipped;
270 HRESULT hr = IDirect3DDevice9_Present(d3ddev, &src, &dst, NULL, NULL);
272 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
278 /* XXX See Prepare() */
279 Direct3DLockSurface(picture);
280 picture_Release(picture);
285 static int ControlResetDevice(vout_display_t *vd)
287 return Direct3DReset(vd);
289 static int ControlReopenDevice(vout_display_t *vd)
291 vout_display_sys_t *sys = vd->sys;
293 if (!sys->use_desktop) {
294 /* Save non-desktop state */
295 sys->desktop_save.is_fullscreen = vd->cfg->is_fullscreen;
296 sys->desktop_save.is_on_top = sys->is_on_top;
298 WINDOWPLACEMENT wp = { .length = sizeof(wp), };
299 GetWindowPlacement(sys->hparent ? sys->hparent : sys->hwnd, &wp);
300 sys->desktop_save.win = wp.rcNormalPosition;
305 EventThreadStop(sys->event);
308 vlc_mutex_lock(&sys->lock);
309 sys->use_desktop = sys->desktop_requested;
310 sys->ch_desktop = false;
311 vlc_mutex_unlock(&sys->lock);
315 memset(&cfg, 0, sizeof(cfg));
316 cfg.use_desktop = sys->use_desktop;
317 if (!sys->use_desktop) {
318 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
319 cfg.win.x = sys->desktop_save.win.left;
320 cfg.win.y = sys->desktop_save.win.top;
321 cfg.win.width = sys->desktop_save.win.right - sys->desktop_save.win.left;
322 cfg.win.height = sys->desktop_save.win.bottom - sys->desktop_save.win.top;
326 if (EventThreadStart(sys->event, &hwnd, &cfg)) {
327 msg_Err(vd, "Failed to restart event thread");
330 sys->parent_window = hwnd.parent_window;
331 sys->hparent = hwnd.hparent;
332 sys->hwnd = hwnd.hwnd;
333 sys->hvideownd = hwnd.hvideownd;
334 sys->hfswnd = hwnd.hfswnd;
335 SetRectEmpty(&sys->rect_parent);
339 if (Direct3DOpen(vd, &fmt)) {
341 msg_Err(vd, "Failed to reopen device");
345 sys->is_first_display = true;
347 if (sys->use_desktop) {
348 /* Disable fullscreen/on_top while using desktop */
349 if (sys->desktop_save.is_fullscreen)
350 vout_display_SendEventFullscreen(vd, false);
351 if (sys->desktop_save.is_on_top)
352 vout_display_SendWindowState(vd, VOUT_WINDOW_STATE_NORMAL);
354 /* Restore fullscreen/on_top */
355 if (sys->desktop_save.is_fullscreen)
356 vout_display_SendEventFullscreen(vd, true);
357 if (sys->desktop_save.is_on_top)
358 vout_display_SendWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
362 static int Control(vout_display_t *vd, int query, va_list args)
364 vout_display_sys_t *sys = vd->sys;
367 case VOUT_DISPLAY_RESET_PICTURES:
368 /* FIXME what to do here in case of failure */
369 if (sys->reset_device) {
370 if (ControlResetDevice(vd)) {
371 msg_Err(vd, "Failed to reset device");
374 sys->reset_device = false;
375 } else if(sys->reopen_device) {
376 if (ControlReopenDevice(vd)) {
377 msg_Err(vd, "Failed to reopen device");
380 sys->reopen_device = false;
384 return CommonControl(vd, query, args);
387 static void Manage (vout_display_t *vd)
389 vout_display_sys_t *sys = vd->sys;
393 /* Desktop mode change */
394 vlc_mutex_lock(&sys->lock);
395 const bool ch_desktop = sys->ch_desktop;
396 sys->ch_desktop = false;
397 vlc_mutex_unlock(&sys->lock);
400 sys->reopen_device = true;
401 vout_display_SendEventPicturesInvalid(vd);
408 if (sys->changes & DX_POSITION_CHANGE) {
409 #if 0 /* need that when bicubic filter is available */
413 GetClientRect(p_sys->hvideownd, &rect);
414 width = rect.right-rect.left;
415 height = rect.bottom-rect.top;
417 if (width != p_sys->d3dpp.BackBufferWidth || height != p_sys->d3dpp.BackBufferHeight)
419 msg_Dbg(vd, "resizing device back buffers to (%lux%lu)", width, height);
420 // need to reset D3D device to resize back buffer
421 if (VLC_SUCCESS != Direct3DResetDevice(vd, width, height))
425 sys->changes &= ~DX_POSITION_CHANGE;
431 * It initializes an instance of Direct3D9
433 static int Direct3DCreate(vout_display_t *vd)
435 vout_display_sys_t *sys = vd->sys;
437 sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
438 if (!sys->hd3d9_dll) {
439 msg_Warn(vd, "cannot load d3d9.dll, aborting");
443 LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
445 (void *)GetProcAddress(sys->hd3d9_dll, TEXT("Direct3DCreate9"));
446 if (!OurDirect3DCreate9) {
447 msg_Err(vd, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
451 /* Create the D3D object. */
452 LPDIRECT3D9 d3dobj = OurDirect3DCreate9(D3D_SDK_VERSION);
454 msg_Err(vd, "Could not create Direct3D9 instance.");
457 sys->d3dobj = d3dobj;
460 ** Get device capabilities
463 ZeroMemory(&d3dCaps, sizeof(d3dCaps));
464 HRESULT hr = IDirect3D9_GetDeviceCaps(d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
466 msg_Err(vd, "Could not read adapter capabilities. (hr=0x%lX)", hr);
469 /* TODO: need to test device capabilities and select the right render function */
475 * It releases an instance of Direct3D9
477 static void Direct3DDestroy(vout_display_t *vd)
479 vout_display_sys_t *sys = vd->sys;
482 IDirect3D9_Release(sys->d3dobj);
484 FreeLibrary(sys->hd3d9_dll);
487 sys->hd3d9_dll = NULL;
492 * It setup vout_display_sys_t::d3dpp and vout_display_sys_t::rect_display
493 * from the default adapter.
495 static int Direct3DFillPresentationParameters(vout_display_t *vd)
497 vout_display_sys_t *sys = vd->sys;
500 ** Get the current desktop display mode, so we can set up a back
501 ** buffer of the same format
503 D3DDISPLAYMODE d3ddm;
504 HRESULT hr = IDirect3D9_GetAdapterDisplayMode(sys->d3dobj,
505 D3DADAPTER_DEFAULT, &d3ddm);
507 msg_Err(vd, "Could not read adapter display mode. (hr=0x%lX)", hr);
511 /* Set up the structure used to create the D3DDevice. */
512 D3DPRESENT_PARAMETERS *d3dpp = &vd->sys->d3dpp;
513 ZeroMemory(d3dpp, sizeof(D3DPRESENT_PARAMETERS));
514 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
515 d3dpp->Windowed = TRUE;
516 d3dpp->hDeviceWindow = vd->sys->hvideownd;
517 d3dpp->BackBufferWidth = __MAX((unsigned int)GetSystemMetrics(SM_CXVIRTUALSCREEN),
519 d3dpp->BackBufferHeight = __MAX((unsigned int)GetSystemMetrics(SM_CYVIRTUALSCREEN),
521 d3dpp->SwapEffect = D3DSWAPEFFECT_COPY;
522 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
523 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
524 d3dpp->BackBufferFormat = d3ddm.Format;
525 d3dpp->BackBufferCount = 1;
526 d3dpp->EnableAutoDepthStencil = FALSE;
529 RECT *display = &vd->sys->rect_display;
532 display->right = d3dpp->BackBufferWidth;
533 display->bottom = d3dpp->BackBufferHeight;
539 static int Direct3DCreateResources (vout_display_t *, video_format_t *);
540 static void Direct3DDestroyResources(vout_display_t *);
543 * It creates a Direct3D device and the associated resources.
545 static int Direct3DOpen(vout_display_t *vd, video_format_t *fmt)
547 vout_display_sys_t *sys = vd->sys;
548 LPDIRECT3D9 d3dobj = sys->d3dobj;
550 if (Direct3DFillPresentationParameters(vd))
553 // Create the D3DDevice
554 LPDIRECT3DDEVICE9 d3ddev;
556 UINT AdapterToUse = D3DADAPTER_DEFAULT;
557 D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
560 // Look for 'NVIDIA PerfHUD' adapter
561 // If it is present, override default settings
562 for (UINT Adapter=0; Adapter< IDirect3D9_GetAdapterCount(d3dobj); ++Adapter) {
563 D3DADAPTER_IDENTIFIER9 Identifier;
565 Res = IDirect3D9_GetAdapterIdentifier(d3dobj,Adapter,0,&Identifier);
566 if (strstr(Identifier.Description,"PerfHUD") != 0) {
567 AdapterToUse = Adapter;
568 DeviceType = D3DDEVTYPE_REF;
574 HRESULT hr = IDirect3D9_CreateDevice(d3dobj, AdapterToUse,
575 DeviceType, sys->hvideownd,
576 D3DCREATE_SOFTWARE_VERTEXPROCESSING|
577 D3DCREATE_MULTITHREADED,
578 &sys->d3dpp, &d3ddev);
580 msg_Err(vd, "Could not create the D3D device! (hr=0x%lX)", hr);
583 sys->d3ddev = d3ddev;
585 UpdateRects(vd, NULL, NULL, true);
587 if (Direct3DCreateResources(vd, fmt)) {
588 msg_Err(vd, "Failed to allocate resources");
592 /* Change the window title bar text */
593 EventThreadUpdateTitle(sys->event, VOUT_TITLE " (Direct3D output)");
595 msg_Dbg(vd, "Direct3D device adapter successfully initialized");
600 * It releases the Direct3D9 device and its resources.
602 static void Direct3DClose(vout_display_t *vd)
604 vout_display_sys_t *sys = vd->sys;
606 Direct3DDestroyResources(vd);
609 IDirect3DDevice9_Release(sys->d3ddev);
615 * It reset the Direct3D9 device and its resources.
617 static int Direct3DReset(vout_display_t *vd)
619 vout_display_sys_t *sys = vd->sys;
620 LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
622 if (Direct3DFillPresentationParameters(vd))
625 /* release all D3D objects */
626 Direct3DDestroyResources(vd);
629 HRESULT hr = IDirect3DDevice9_Reset(d3ddev, &sys->d3dpp);
631 msg_Err(vd, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
635 UpdateRects(vd, NULL, NULL, true);
638 if (Direct3DCreateResources(vd, &vd->fmt)) {
639 msg_Dbg(vd, "%s failed !", __FUNCTION__);
646 static int Direct3DCreatePool(vout_display_t *vd, video_format_t *fmt);
647 static void Direct3DDestroyPool(vout_display_t *vd);
649 static int Direct3DCreateScene(vout_display_t *vd);
650 static void Direct3DDestroyScene(vout_display_t *vd);
653 * It creates the picture and scene resources.
655 static int Direct3DCreateResources(vout_display_t *vd, video_format_t *fmt)
657 if (Direct3DCreatePool(vd, fmt)) {
658 msg_Err(vd, "Direct3D picture pool initialization failed");
661 if (Direct3DCreateScene(vd)) {
662 msg_Err(vd, "Direct3D scene initialization failed !");
668 * It destroys the picture and scene resources.
670 static void Direct3DDestroyResources(vout_display_t *vd)
672 Direct3DDestroyScene(vd);
673 Direct3DDestroyPool(vd);
677 * It tests if the conversion from src to dst is supported.
679 static int Direct3DCheckConversion(vout_display_t *vd,
680 D3DFORMAT src, D3DFORMAT dst)
682 vout_display_sys_t *sys = vd->sys;
683 LPDIRECT3D9 d3dobj = sys->d3dobj;
686 /* test whether device can create a surface of that format */
687 hr = IDirect3D9_CheckDeviceFormat(d3dobj, D3DADAPTER_DEFAULT,
688 D3DDEVTYPE_HAL, dst, 0,
689 D3DRTYPE_SURFACE, src);
691 /* test whether device can perform color-conversion
692 ** from that format to target format
694 hr = IDirect3D9_CheckDeviceFormatConversion(d3dobj,
699 if (!SUCCEEDED(hr)) {
700 if (D3DERR_NOTAVAILABLE != hr)
701 msg_Err(vd, "Could not query adapter supported formats. (hr=0x%lX)", hr);
710 D3DFORMAT format; /* D3D format */
711 vlc_fourcc_t fourcc; /* VLC fourcc */
717 static const d3d_format_t d3d_formats[] = {
718 /* YV12 is always used for planar 420, the planes are then swapped in Lock() */
719 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_YV12, 0,0,0 },
720 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_I420, 0,0,0 },
721 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_J420, 0,0,0 },
722 { "UYVY", D3DFMT_UYVY, VLC_CODEC_UYVY, 0,0,0 },
723 { "YUY2", D3DFMT_YUY2, VLC_CODEC_YUYV, 0,0,0 },
724 { "X8R8G8B8", D3DFMT_X8R8G8B8,VLC_CODEC_RGB32, 0xff0000, 0x00ff00, 0x0000ff },
725 { "A8R8G8B8", D3DFMT_A8R8G8B8,VLC_CODEC_RGB32, 0xff0000, 0x00ff00, 0x0000ff },
726 { "8G8B8", D3DFMT_R8G8B8, VLC_CODEC_RGB24, 0xff0000, 0x00ff00, 0x0000ff },
727 { "R5G6B5", D3DFMT_R5G6B5, VLC_CODEC_RGB16, 0x1f<<11, 0x3f<<5, 0x1f<<0 },
728 { "X1R5G5B5", D3DFMT_X1R5G5B5,VLC_CODEC_RGB15, 0x1f<<10, 0x1f<<5, 0x1f<<0 },
734 * It returns the format (closest to chroma) that can be converted to target */
735 static const d3d_format_t *Direct3DFindFormat(vout_display_t *vd, vlc_fourcc_t chroma, D3DFORMAT target)
737 vout_display_sys_t *sys = vd->sys;
739 for (unsigned pass = 0; pass < 2; pass++) {
740 const vlc_fourcc_t *list;
742 if (pass == 0 && sys->allow_hw_yuv && vlc_fourcc_IsYUV(chroma))
743 list = vlc_fourcc_GetYUVFallback(chroma);
745 list = vlc_fourcc_GetRGBFallback(chroma);
749 for (unsigned i = 0; list[i] != 0; i++) {
750 for (unsigned j = 0; d3d_formats[j].name; j++) {
751 const d3d_format_t *format = &d3d_formats[j];
753 if (format->fourcc != list[i])
756 msg_Warn(vd, "trying surface pixel format: %s",
758 if (!Direct3DCheckConversion(vd, format->format, target)) {
759 msg_Dbg(vd, "selected surface pixel format is %s",
770 * It locks the surface associated to the picture and get the surface
771 * descriptor which amongst other things has the pointer to the picture
772 * data and its pitch.
774 static int Direct3DLockSurface(picture_t *picture)
776 /* Lock the surface to get a valid pointer to the picture buffer */
777 D3DLOCKED_RECT d3drect;
778 HRESULT hr = IDirect3DSurface9_LockRect(picture->p_sys->surface, &d3drect, NULL, 0);
780 //msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
781 return CommonUpdatePicture(picture, &picture->p_sys->fallback, NULL, 0);
784 CommonUpdatePicture(picture, NULL, d3drect.pBits, d3drect.Pitch);
788 * It unlocks the surface associated to the picture.
790 static void Direct3DUnlockSurface(picture_t *picture)
792 /* Unlock the Surface */
793 HRESULT hr = IDirect3DSurface9_UnlockRect(picture->p_sys->surface);
795 //msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
800 * It creates the pool of picture (only 1).
802 * Each picture has an associated offscreen surface in video memory
803 * depending on hardware capabilities the picture chroma will be as close
804 * as possible to the orginal render chroma to reduce CPU conversion overhead
805 * and delegate this work to video card GPU
807 static int Direct3DCreatePool(vout_display_t *vd, video_format_t *fmt)
809 vout_display_sys_t *sys = vd->sys;
810 LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
815 /* Find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
816 * the requested chroma which is usable by the hardware in an offscreen surface, as they
817 * typically support more formats than textures */
818 const d3d_format_t *d3dfmt = Direct3DFindFormat(vd, fmt->i_chroma, sys->d3dpp.BackBufferFormat);
820 msg_Err(vd, "surface pixel format is not supported.");
823 fmt->i_chroma = d3dfmt->fourcc;
824 fmt->i_rmask = d3dfmt->rmask;
825 fmt->i_gmask = d3dfmt->gmask;
826 fmt->i_bmask = d3dfmt->bmask;
828 /* We create one picture.
829 * It is useless to create more as we can't be used for direct rendering */
831 /* Create a surface */
832 LPDIRECT3DSURFACE9 surface;
833 HRESULT hr = IDirect3DDevice9_CreateOffscreenPlainSurface(d3ddev,
841 msg_Err(vd, "Failed to create picture surface. (hr=0x%lx)", hr);
844 /* fill surface with black color */
845 IDirect3DDevice9_ColorFill(d3ddev, surface, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0));
847 /* Create the associated picture */
848 picture_resource_t *rsc = &sys->resource;
849 rsc->p_sys = malloc(sizeof(*rsc->p_sys));
851 IDirect3DSurface9_Release(surface);
854 rsc->p_sys->surface = surface;
855 rsc->p_sys->fallback = NULL;
856 for (int i = 0; i < PICTURE_PLANE_MAX; i++) {
857 rsc->p[i].p_pixels = NULL;
858 rsc->p[i].i_pitch = 0;
859 rsc->p[i].i_lines = fmt->i_height / (i > 0 ? 2 : 1);
861 picture_t *picture = picture_NewFromResource(fmt, rsc);
863 IDirect3DSurface9_Release(surface);
868 /* Wrap it into a picture pool */
869 picture_pool_configuration_t pool_cfg;
870 memset(&pool_cfg, 0, sizeof(pool_cfg));
871 pool_cfg.picture_count = 1;
872 pool_cfg.picture = &picture;
873 pool_cfg.lock = Direct3DLockSurface;
874 pool_cfg.unlock = Direct3DUnlockSurface;
876 sys->pool = picture_pool_NewExtended(&pool_cfg);
878 picture_Release(picture);
879 IDirect3DSurface9_Release(surface);
885 * It destroys the pool of picture and its resources.
887 static void Direct3DDestroyPool(vout_display_t *vd)
889 vout_display_sys_t *sys = vd->sys;
892 picture_resource_t *rsc = &sys->resource;
893 IDirect3DSurface9_Release(rsc->p_sys->surface);
894 if (rsc->p_sys->fallback)
895 picture_Release(rsc->p_sys->fallback);
896 picture_pool_Delete(sys->pool);
904 FLOAT x,y,z; // vertex untransformed position
905 FLOAT rhw; // eye distance
906 D3DCOLOR diffuse; // diffuse color
907 FLOAT tu, tv; // texture relative coordinates
910 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
913 * It allocates and initializes the resources needed to render the scene.
915 static int Direct3DCreateScene(vout_display_t *vd)
917 vout_display_sys_t *sys = vd->sys;
918 LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
922 * Create a texture for use when rendering a scene
923 * for performance reason, texture format is identical to backbuffer
924 * which would usually be a RGB format
926 LPDIRECT3DTEXTURE9 d3dtex;
927 hr = IDirect3DDevice9_CreateTexture(d3ddev,
928 sys->d3dpp.BackBufferWidth,
929 sys->d3dpp.BackBufferHeight,
931 D3DUSAGE_RENDERTARGET,
932 sys->d3dpp.BackBufferFormat,
937 msg_Err(vd, "Failed to create texture. (hr=0x%lx)", hr);
942 ** Create a vertex buffer for use when rendering scene
944 LPDIRECT3DVERTEXBUFFER9 d3dvtc;
945 hr = IDirect3DDevice9_CreateVertexBuffer(d3ddev,
946 sizeof(CUSTOMVERTEX)*4,
947 D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
953 msg_Err(vd, "Failed to create vertex buffer. (hr=0x%lx)", hr);
954 IDirect3DTexture9_Release(d3dtex);
959 sys->d3dtex = d3dtex;
960 sys->d3dvtc = d3dvtc;
962 // Texture coordinates outside the range [0.0, 1.0] are set
963 // to the texture color at 0.0 or 1.0, respectively.
964 IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
965 IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
967 // Set linear filtering quality
968 IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
969 IDirect3DDevice9_SetSamplerState(d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
971 // set maximum ambient light
972 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
975 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
977 // Turn off the zbuffer
978 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
981 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_LIGHTING, FALSE);
984 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_DITHERENABLE, TRUE);
987 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_STENCILENABLE, FALSE);
990 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
991 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
992 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
993 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
994 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHAREF, 0x10);
995 IDirect3DDevice9_SetRenderState(d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
997 // Set texture states
998 IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
999 IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1000 IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1002 // turn off alpha operation
1003 IDirect3DDevice9_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1005 msg_Dbg(vd, "Direct3D scene created successfully");
1011 * It releases the scene resources.
1013 static void Direct3DDestroyScene(vout_display_t *vd)
1015 vout_display_sys_t *sys = vd->sys;
1017 LPDIRECT3DVERTEXBUFFER9 d3dvtc = sys->d3dvtc;
1019 IDirect3DVertexBuffer9_Release(d3dvtc);
1021 LPDIRECT3DTEXTURE9 d3dtex = sys->d3dtex;
1023 IDirect3DTexture9_Release(d3dtex);
1027 msg_Dbg(vd, "Direct3D scene released successfully");
1031 * It copies picture surface into a texture and renders into a scene.
1033 * This function is intented for higher end 3D cards, with pixel shader support
1034 * and at least 64 MiB of video RAM.
1036 static void Direct3DRenderScene(vout_display_t *vd, LPDIRECT3DSURFACE9 surface)
1038 vout_display_sys_t *sys = vd->sys;
1039 LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
1042 // check if device is still available
1043 hr = IDirect3DDevice9_TestCooperativeLevel(d3ddev);
1045 if (hr == D3DERR_DEVICENOTRESET && !sys->reset_device) {
1046 vout_display_SendEventPicturesInvalid(vd);
1047 sys->reset_device = true;
1052 LPDIRECT3DTEXTURE9 d3dtex = sys->d3dtex;
1053 LPDIRECT3DVERTEXBUFFER9 d3dvtc = sys->d3dvtc;
1055 /* Clear the backbuffer and the zbuffer */
1056 hr = IDirect3DDevice9_Clear(d3ddev, 0, NULL, D3DCLEAR_TARGET,
1057 D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
1059 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1062 /* retrieve picture surface */
1063 LPDIRECT3DSURFACE9 d3dsrc = surface;
1065 msg_Dbg(vd, "no surface to render ?");
1069 /* retrieve texture top-level surface */
1070 LPDIRECT3DSURFACE9 d3ddest;
1071 hr = IDirect3DTexture9_GetSurfaceLevel(d3dtex, 0, &d3ddest);
1073 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1077 /* Copy picture surface into texture surface
1078 * color space conversion and scaling happen here */
1079 RECT src = vd->sys->rect_src_clipped;
1080 RECT dst = vd->sys->rect_dest_clipped;
1082 hr = IDirect3DDevice9_StretchRect(d3ddev, d3dsrc, &src, d3ddest, &dst, D3DTEXF_LINEAR);
1083 IDirect3DSurface9_Release(d3ddest);
1085 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1089 /* Update the vertex buffer */
1090 CUSTOMVERTEX *vertices;
1091 hr = IDirect3DVertexBuffer9_Lock(d3dvtc, 0, 0, (void **)&vertices, D3DLOCK_DISCARD);
1093 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1097 /* Setup vertices */
1098 const float f_width = vd->sys->d3dpp.BackBufferWidth;
1099 const float f_height = vd->sys->d3dpp.BackBufferHeight;
1101 /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
1102 /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
1103 vertices[0].x = -0.5f; // left
1104 vertices[0].y = -0.5f; // top
1105 vertices[0].z = 0.0f;
1106 vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1107 vertices[0].rhw = 1.0f;
1108 vertices[0].tu = 0.0f;
1109 vertices[0].tv = 0.0f;
1111 vertices[1].x = f_width - 0.5f; // right
1112 vertices[1].y = -0.5f; // top
1113 vertices[1].z = 0.0f;
1114 vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1115 vertices[1].rhw = 1.0f;
1116 vertices[1].tu = 1.0f;
1117 vertices[1].tv = 0.0f;
1119 vertices[2].x = f_width - 0.5f; // right
1120 vertices[2].y = f_height - 0.5f; // bottom
1121 vertices[2].z = 0.0f;
1122 vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1123 vertices[2].rhw = 1.0f;
1124 vertices[2].tu = 1.0f;
1125 vertices[2].tv = 1.0f;
1127 vertices[3].x = -0.5f; // left
1128 vertices[3].y = f_height - 0.5f; // bottom
1129 vertices[3].z = 0.0f;
1130 vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1131 vertices[3].rhw = 1.0f;
1132 vertices[3].tu = 0.0f;
1133 vertices[3].tv = 1.0f;
1135 hr= IDirect3DVertexBuffer9_Unlock(d3dvtc);
1137 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1142 hr = IDirect3DDevice9_BeginScene(d3ddev);
1144 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1148 // Setup our texture. Using textures introduces the texture stage states,
1149 // which govern how textures get blended together (in the case of multiple
1150 // textures) and lighting information. In this case, we are modulating
1151 // (blending) our texture with the diffuse color of the vertices.
1152 hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex);
1154 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1155 IDirect3DDevice9_EndScene(d3ddev);
1159 // Render the vertex buffer contents
1160 hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
1162 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1163 IDirect3DDevice9_EndScene(d3ddev);
1167 // we use FVF instead of vertex shader
1168 hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
1170 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1171 IDirect3DDevice9_EndScene(d3ddev);
1176 hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1178 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1179 IDirect3DDevice9_EndScene(d3ddev);
1184 hr = IDirect3DDevice9_EndScene(d3ddev);
1186 msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1191 /*****************************************************************************
1192 * DesktopCallback: desktop mode variable callback
1193 *****************************************************************************/
1194 static int DesktopCallback(vlc_object_t *object, char const *psz_cmd,
1195 vlc_value_t oldval, vlc_value_t newval,
1198 vout_display_t *vd = (vout_display_t *)object;
1199 vout_display_sys_t *sys = vd->sys;
1200 VLC_UNUSED(psz_cmd);
1204 vlc_mutex_lock(&sys->lock);
1205 const bool ch_desktop = !sys->desktop_requested != !newval.b_bool;
1206 sys->ch_desktop |= ch_desktop;
1207 sys->desktop_requested = newval.b_bool;
1208 vlc_mutex_unlock(&sys->lock);
1210 /* FIXME we should have a way to export variable to be saved */
1212 playlist_t *p_playlist = pl_Get(vd);
1213 /* Modify playlist as well because the vout might have to be
1215 var_Create(p_playlist, "direct3d-desktop", VLC_VAR_BOOL);
1216 var_SetBool(p_playlist, "direct3d-desktop", newval.b_bool);