1 /*****************************************************************************
2 * direct3d.c: Windows Direct3D video output module
3 *****************************************************************************
4 * Copyright (C) 2006 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 *****************************************************************************/
36 #include <errno.h> /* ENOMEM */
43 #include <vlc_interface.h>
44 #include <vlc_playlist.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static int OpenVideo ( vlc_object_t * );
56 static void CloseVideo ( vlc_object_t * );
58 static int Init ( vout_thread_t * );
59 static void End ( vout_thread_t * );
60 static int Manage ( vout_thread_t * );
61 static void Display ( vout_thread_t *, picture_t * );
62 static void FirstDisplay( vout_thread_t *, picture_t * );
64 static int Direct3DVoutCreate ( vout_thread_t * );
65 static void Direct3DVoutRelease ( vout_thread_t * );
67 static int Direct3DVoutOpen ( vout_thread_t * );
68 static void Direct3DVoutClose ( vout_thread_t * );
70 static int Direct3DVoutResetDevice( vout_thread_t * );
72 static int Direct3DVoutCreatePictures ( vout_thread_t *, size_t );
73 static void Direct3DVoutReleasePictures ( vout_thread_t * );
75 static int Direct3DVoutLockSurface ( vout_thread_t *, picture_t * );
76 static int Direct3DVoutUnlockSurface( vout_thread_t *, picture_t * );
78 static int Direct3DVoutCreateScene ( vout_thread_t * );
79 static void Direct3DVoutReleaseScene ( vout_thread_t * );
80 static void Direct3DVoutRenderScene ( vout_thread_t *, picture_t * );
82 /*****************************************************************************
84 *****************************************************************************/
86 static vlc_bool_t _got_vista_or_above;
88 static int get_capability_for_osversion(void)
91 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
93 if( GetVersionEx(&winVer) )
95 if( winVer.dwMajorVersion > 5 )
97 /* Windows Vista or above, make this module the default */
98 _got_vista_or_above = VLC_TRUE;
102 /* Windows XP or lower, make sure this module isn't the default */
103 _got_vista_or_above = VLC_FALSE;
108 set_shortname( "Direct3D" );
109 set_category( CAT_VIDEO );
110 set_subcategory( SUBCAT_VIDEO_VOUT );
111 set_description( _("DirectX 3D video output") );
112 set_capability( "video output", get_capability_for_osversion() );
113 add_shortcut( "direct3d" );
114 set_callbacks( OpenVideo, CloseVideo );
116 /* FIXME: Hack to avoid unregistering our window class */
117 linked_with_a_crap_library_which_uses_atexit( );
121 /* check if we registered a window class because we need to
124 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
125 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
128 /*****************************************************************************
130 *****************************************************************************
131 *****************************************************************************/
134 FLOAT x,y,z; // vertex untransformed position
135 FLOAT rhw; // eye distance
136 D3DCOLOR diffuse; // diffuse color
137 FLOAT tu, tv; // texture relative coordinates
140 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
142 /*****************************************************************************
143 * OpenVideo: allocate Vout video thread output method
144 *****************************************************************************
145 * This function allocates and initialize the Direct3D vout method.
146 *****************************************************************************/
147 static int OpenVideo( vlc_object_t *p_this )
149 vout_thread_t * p_vout = (vout_thread_t *)p_this;
152 /* Allocate structure */
153 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
154 if( p_vout->p_sys == NULL )
156 msg_Err( p_vout, "out of memory" );
159 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
161 if( VLC_SUCCESS != Direct3DVoutCreate( p_vout ) )
163 msg_Err( p_vout, "Direct3D could not be initialized !");
167 /* Initialisations */
168 p_vout->pf_init = Init;
169 p_vout->pf_end = End;
170 p_vout->pf_manage = Manage;
171 p_vout->pf_render = Direct3DVoutRenderScene;
172 p_vout->pf_display = FirstDisplay;
174 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
175 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
176 p_vout->p_sys->i_changes = 0;
177 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
178 SetRectEmpty( &p_vout->p_sys->rect_display );
179 SetRectEmpty( &p_vout->p_sys->rect_parent );
181 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
182 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
184 p_vout->p_sys->b_cursor_hidden = 0;
185 p_vout->p_sys->i_lastmoved = mdate();
187 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
188 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
190 /* Set main window's size */
191 p_vout->p_sys->i_window_width = p_vout->i_window_width;
192 p_vout->p_sys->i_window_height = p_vout->i_window_height;
194 /* Create the Vout EventThread, this thread is created by us to isolate
195 * the Win32 PeekMessage function calls. We want to do this because
196 * Windows can stay blocked inside this call for a long time, and when
197 * this happens it thus blocks vlc's video_output thread.
198 * Vout EventThread will take care of the creation of the video
199 * window (because PeekMessage has to be called from the same thread which
200 * created the window). */
201 msg_Dbg( p_vout, "creating Vout EventThread" );
202 p_vout->p_sys->p_event =
203 vlc_object_create( p_vout, sizeof(event_thread_t) );
204 p_vout->p_sys->p_event->p_vout = p_vout;
205 if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
206 E_(EventThread), 0, 1 ) )
208 msg_Err( p_vout, "cannot create Vout EventThread" );
209 vlc_object_release( p_vout->p_sys->p_event );
210 p_vout->p_sys->p_event = NULL;
214 if( p_vout->p_sys->p_event->b_error )
216 msg_Err( p_vout, "Vout EventThread failed" );
220 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
222 msg_Dbg( p_vout, "Vout EventThread running" );
224 /* Variable to indicate if the window should be on top of others */
225 /* Trigger a callback right now */
226 var_Get( p_vout, "video-on-top", &val );
227 var_Set( p_vout, "video-on-top", val );
229 /* disable screensaver by temporarily changing system settings */
230 p_vout->p_sys->i_spi_lowpowertimeout = 0;
231 p_vout->p_sys->i_spi_powerofftimeout = 0;
232 p_vout->p_sys->i_spi_screensavetimeout = 0;
233 var_Get( p_vout, "disable-screensaver", &val);
235 msg_Dbg(p_vout, "disabling screen saver");
236 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
237 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
238 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
239 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
241 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
242 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
243 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
244 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
246 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
247 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
248 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
249 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
255 CloseVideo( VLC_OBJECT(p_vout) );
259 /*****************************************************************************
260 * CloseVideo: destroy Sys video thread output method
261 *****************************************************************************
262 * Terminate an output method created by Create
263 *****************************************************************************/
264 static void CloseVideo( vlc_object_t *p_this )
266 vout_thread_t * p_vout = (vout_thread_t *)p_this;
268 Direct3DVoutRelease( p_vout );
270 if( p_vout->p_sys->p_event )
272 vlc_object_detach( p_vout->p_sys->p_event );
274 /* Kill Vout EventThread */
275 vlc_object_kill( p_vout->p_sys->p_event );
277 /* we need to be sure Vout EventThread won't stay stuck in
278 * GetMessage, so we send a fake message */
279 if( p_vout->p_sys->hwnd )
281 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
284 vlc_thread_join( p_vout->p_sys->p_event );
285 vlc_object_release( p_vout->p_sys->p_event );
288 vlc_mutex_destroy( &p_vout->p_sys->lock );
290 /* restore screensaver system settings */
291 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
292 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
293 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
295 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
296 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
297 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
299 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
300 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
301 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
306 free( p_vout->p_sys );
307 p_vout->p_sys = NULL;
311 /*****************************************************************************
312 * Init: initialize Direct3D video thread output method
313 *****************************************************************************/
314 static int Init( vout_thread_t *p_vout )
319 var_Get( p_vout, "directx-hw-yuv", &val );
320 p_vout->p_sys->b_hw_yuv = val.b_bool;
322 /* Initialise Direct3D */
323 if( VLC_SUCCESS != Direct3DVoutOpen( p_vout ) )
325 msg_Err( p_vout, "cannot initialize Direct3D" );
329 /* Initialize the output structure.
330 * Since Direct3D can do rescaling for us, stick to the default
331 * coordinates and aspect. */
332 p_vout->output.i_width = p_vout->render.i_width;
333 p_vout->output.i_height = p_vout->render.i_height;
334 p_vout->output.i_aspect = p_vout->render.i_aspect;
335 p_vout->fmt_out = p_vout->fmt_in;
336 E_(UpdateRects)( p_vout, VLC_TRUE );
338 /* create picture pool */
339 p_vout->output.i_chroma = 0;
340 i_ret = Direct3DVoutCreatePictures(p_vout, 1);
341 if( VLC_SUCCESS != i_ret )
343 msg_Err(p_vout, "Direct3D picture pool initialization failed !");
348 i_ret = Direct3DVoutCreateScene(p_vout);
349 if( VLC_SUCCESS != i_ret )
351 msg_Err(p_vout, "Direct3D scene initialization failed !");
352 Direct3DVoutReleasePictures(p_vout);
356 /* Change the window title bar text */
357 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
359 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
363 /*****************************************************************************
364 * End: terminate Sys video thread output method
365 *****************************************************************************
366 * Terminate an output method created by Create.
367 * It is called at the end of the thread.
368 *****************************************************************************/
369 static void End( vout_thread_t *p_vout )
371 Direct3DVoutReleaseScene(p_vout);
372 Direct3DVoutReleasePictures(p_vout);
373 Direct3DVoutClose( p_vout );
376 /*****************************************************************************
377 * Manage: handle Sys events
378 *****************************************************************************
379 * This function should be called regularly by the video output thread.
380 * It returns a non null value if an error occurred.
381 *****************************************************************************/
382 static int Manage( vout_thread_t *p_vout )
384 /* If we do not control our window, we check for geometry changes
385 * ourselves because the parent might not send us its events. */
386 vlc_mutex_lock( &p_vout->p_sys->lock );
387 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
392 vlc_mutex_unlock( &p_vout->p_sys->lock );
394 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
395 point.x = point.y = 0;
396 ClientToScreen( p_vout->p_sys->hparent, &point );
397 OffsetRect( &rect_parent, point.x, point.y );
399 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
401 p_vout->p_sys->rect_parent = rect_parent;
403 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
404 rect_parent.right - rect_parent.left,
405 rect_parent.bottom - rect_parent.top,
411 vlc_mutex_unlock( &p_vout->p_sys->lock );
417 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
419 #if 0 /* need that when bicubic filter is available */
423 GetClientRect(p_vout->p_sys->hvideownd, &rect);
424 width = rect.right-rect.left;
425 height = rect.bottom-rect.top;
427 if( (width != p_vout->p_sys->d3dpp.BackBufferWidth)
428 || (height != p_vout->p_sys->d3dpp.BackBufferHeight) )
430 msg_Dbg(p_vout, "resizing device back buffers to (%lux%lu)", width, height);
431 // need to reset D3D device to resize back buffer
432 if( VLC_SUCCESS != Direct3DVoutResetDevice(p_vout, width, height) )
436 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
439 /* Check for cropping / aspect changes */
440 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
441 p_vout->i_changes & VOUT_ASPECT_CHANGE )
443 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
444 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
446 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
447 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
448 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
449 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
450 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
451 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
452 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
453 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
454 E_(UpdateRects)( p_vout, VLC_TRUE );
457 /* We used to call the Win32 PeekMessage function here to read the window
458 * messages. But since window can stay blocked into this function for a
459 * long time (for example when you move your window on the screen), I
460 * decided to isolate PeekMessage in another thread. */
465 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
466 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
468 Win32ToggleFullscreen( p_vout );
470 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
471 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
477 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
478 (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
483 /* Hide the cursor only if it is inside our window */
484 GetCursorPos( &point );
485 hwnd = WindowFromPoint(point);
486 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
488 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
492 p_vout->p_sys->i_lastmoved = mdate();
497 * "Always on top" status change
499 if( p_vout->p_sys->b_on_top_change )
502 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
504 var_Get( p_vout, "video-on-top", &val );
506 /* Set the window on top if necessary */
507 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
510 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
511 MF_BYCOMMAND | MFS_CHECKED );
512 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
513 SWP_NOSIZE | SWP_NOMOVE );
516 /* The window shouldn't be on top */
517 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
520 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
521 MF_BYCOMMAND | MFS_UNCHECKED );
522 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
523 SWP_NOSIZE | SWP_NOMOVE );
526 p_vout->p_sys->b_on_top_change = VLC_FALSE;
529 /* Check if the event thread is still running */
530 if( p_vout->p_sys->p_event->b_die )
532 return VLC_EGENERIC; /* exit */
538 /*****************************************************************************
539 * Display: displays previously rendered output
540 *****************************************************************************
541 * This function sends the currently rendered image to the display, wait until
542 * it is displayed and switch the two rendering buffers, preparing next frame.
543 *****************************************************************************/
544 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
546 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
547 // Present the back buffer contents to the display
548 // stretching and filtering happens here
549 HRESULT hr = IDirect3DDevice9_Present(p_d3ddev,
550 &(p_vout->p_sys->rect_src_clipped),
553 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
557 ** this function is only used once when the first picture is received
558 ** this function will show the video window once a picture is ready
561 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
563 /* get initial picture presented through D3D */
564 Display(p_vout, p_pic);
567 ** Video window is initially hidden, show it now since we got a
570 SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
578 /* use and restores proper display function for further pictures */
579 p_vout->pf_display = Display;
582 /*****************************************************************************
583 * DirectD3DVoutCreate: Initialize and instance of Direct3D9
584 *****************************************************************************
585 * This function initialize Direct3D and analyze available resources from
587 *****************************************************************************/
588 static int Direct3DVoutCreate( vout_thread_t *p_vout )
591 LPDIRECT3D9 p_d3dobj;
594 LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
596 p_vout->p_sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
597 if( NULL == p_vout->p_sys->hd3d9_dll )
599 msg_Warn( p_vout, "cannot load d3d9.dll, aborting" );
604 (void *)GetProcAddress( p_vout->p_sys->hd3d9_dll,
605 TEXT("Direct3DCreate9") );
606 if( OurDirect3DCreate9 == NULL )
608 msg_Err( p_vout, "Cannot locate reference to Direct3DCreate9 ABI in DLL" );
612 /* Create the D3D object. */
613 p_d3dobj = OurDirect3DCreate9( D3D_SDK_VERSION );
614 if( NULL == p_d3dobj )
616 msg_Err( p_vout, "Could not create Direct3D9 instance.");
619 p_vout->p_sys->p_d3dobj = p_d3dobj;
622 ** Get device capabilities
624 ZeroMemory(&d3dCaps, sizeof(d3dCaps));
625 hr = IDirect3D9_GetDeviceCaps(p_d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
628 msg_Err( p_vout, "Could not read adapter capabilities. (hr=0x%lX)", hr);
631 /* TODO: need to test device capabilities and select the right render function */
636 /*****************************************************************************
637 * DirectD3DVoutRelease: release an instance of Direct3D9
638 *****************************************************************************/
640 static void Direct3DVoutRelease( vout_thread_t *p_vout )
642 if( p_vout->p_sys->p_d3dobj )
644 IDirect3D9_Release(p_vout->p_sys->p_d3dobj);
645 p_vout->p_sys->p_d3dobj = NULL;
647 if( NULL != p_vout->p_sys->hd3d9_dll )
649 FreeLibrary(p_vout->p_sys->hd3d9_dll);
650 p_vout->p_sys->hd3d9_dll = NULL;
654 static int Direct3DFillPresentationParameters(vout_thread_t *p_vout, D3DPRESENT_PARAMETERS *d3dpp)
656 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
657 D3DDISPLAYMODE d3ddm;
661 ** Get the current desktop display mode, so we can set up a back
662 ** buffer of the same format
664 hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
667 msg_Err( p_vout, "Could not read adapter display mode. (hr=0x%lX)", hr);
671 /* keep a copy of current desktop format */
672 p_vout->p_sys->bbFormat = d3ddm.Format;
674 /* Set up the structure used to create the D3DDevice. */
675 ZeroMemory( d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
676 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
677 d3dpp->Windowed = TRUE;
678 d3dpp->hDeviceWindow = p_vout->p_sys->hvideownd;
679 d3dpp->BackBufferWidth = p_vout->output.i_width;
680 d3dpp->BackBufferHeight = p_vout->output.i_height;
681 d3dpp->SwapEffect = D3DSWAPEFFECT_COPY;
682 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
683 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
684 d3dpp->BackBufferFormat = d3ddm.Format;
685 d3dpp->BackBufferCount = 1;
686 d3dpp->EnableAutoDepthStencil = FALSE;
691 /*****************************************************************************
692 * DirectD3DVoutOpen: Takes care of all the Direct3D9 initialisations
693 *****************************************************************************
694 * This function creates Direct3D device
695 * this must be called from the vout thread for performance reason, as
696 * all Direct3D Device APIs are used in a non multithread safe environment
697 *****************************************************************************/
698 static int Direct3DVoutOpen( vout_thread_t *p_vout )
700 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
701 LPDIRECT3DDEVICE9 p_d3ddev;
702 D3DPRESENT_PARAMETERS d3dpp;
705 if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
708 // Create the D3DDevice
709 hr = IDirect3D9_CreateDevice(p_d3dobj, D3DADAPTER_DEFAULT,
710 D3DDEVTYPE_HAL, p_vout->p_sys->hvideownd,
711 D3DCREATE_SOFTWARE_VERTEXPROCESSING|
712 D3DCREATE_MULTITHREADED,
716 msg_Err(p_vout, "Could not create the D3D device! (hr=0x%lX)", hr);
719 p_vout->p_sys->p_d3ddev = p_d3ddev;
721 msg_Dbg( p_vout, "Direct3D device adapter successfully initialized" );
725 /*****************************************************************************
726 * DirectD3DClose: release the Direct3D9 device
727 *****************************************************************************/
728 static void Direct3DVoutClose( vout_thread_t *p_vout )
730 if( p_vout->p_sys->p_d3ddev )
732 IDirect3DDevice9_Release(p_vout->p_sys->p_d3ddev);
733 p_vout->p_sys->p_d3ddev = NULL;
736 p_vout->p_sys->hmonitor = NULL;
739 /*****************************************************************************
740 * DirectD3DClose: reset the Direct3D9 device
741 *****************************************************************************
742 * All resources must be deallocated before the reset occur, they will be
743 * realllocated once the reset has been performed successfully
744 *****************************************************************************/
745 static int Direct3DVoutResetDevice( vout_thread_t *p_vout )
747 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
748 D3DPRESENT_PARAMETERS d3dpp;
751 if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
754 // release all D3D objects
755 Direct3DVoutReleaseScene( p_vout );
756 Direct3DVoutReleasePictures( p_vout );
758 hr = IDirect3DDevice9_Reset(p_d3ddev, &d3dpp);
762 if( (VLC_SUCCESS != Direct3DVoutCreatePictures(p_vout, 1))
763 || (VLC_SUCCESS != Direct3DVoutCreateScene(p_vout)) )
765 msg_Dbg(p_vout, "%s failed !", __FUNCTION__);
770 msg_Err(p_vout, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
776 static D3DFORMAT Direct3DVoutSelectFormat( vout_thread_t *p_vout, D3DFORMAT target,
777 const D3DFORMAT *formats, size_t count)
779 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
782 for( c=0; c<count; ++c )
785 D3DFORMAT format = formats[c];
786 /* test whether device can create a surface of that format */
787 hr = IDirect3D9_CheckDeviceFormat(p_d3dobj, D3DADAPTER_DEFAULT,
788 D3DDEVTYPE_HAL, target, 0, D3DRTYPE_SURFACE, format);
791 /* test whether device can perform color-conversion
792 ** from that format to target format
794 hr = IDirect3D9_CheckDeviceFormatConversion(p_d3dobj,
795 D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
800 // found a compatible format
804 msg_Dbg( p_vout, "selected surface pixel format is UYVY");
807 msg_Dbg( p_vout, "selected surface pixel format is YUY2");
809 case D3DFMT_X8R8G8B8:
810 msg_Dbg( p_vout, "selected surface pixel format is X8R8G8B8");
812 case D3DFMT_A8R8G8B8:
813 msg_Dbg( p_vout, "selected surface pixel format is A8R8G8B8");
816 msg_Dbg( p_vout, "selected surface pixel format is R8G8B8");
819 msg_Dbg( p_vout, "selected surface pixel format is R5G6B5");
821 case D3DFMT_X1R5G5B5:
822 msg_Dbg( p_vout, "selected surface pixel format is X1R5G5B5");
825 msg_Dbg( p_vout, "selected surface pixel format is 0x%0X", format);
830 else if( D3DERR_NOTAVAILABLE != hr )
832 msg_Err( p_vout, "Could not query adapter supported formats. (hr=0x%lX)", hr);
836 return D3DFMT_UNKNOWN;
839 static D3DFORMAT Direct3DVoutFindFormat(vout_thread_t *p_vout, int i_chroma, D3DFORMAT target)
841 //if( p_vout->p_sys->b_hw_yuv && ! _got_vista_or_above )
842 if( p_vout->p_sys->b_hw_yuv )
844 /* it sounds like vista does not support YUV surfaces at all */
847 case VLC_FOURCC('U','Y','V','Y'):
848 case VLC_FOURCC('U','Y','N','V'):
849 case VLC_FOURCC('Y','4','2','2'):
851 static const D3DFORMAT formats[] =
852 { D3DFMT_UYVY, D3DFMT_YUY2, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
853 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
855 case VLC_FOURCC('I','4','2','0'):
856 case VLC_FOURCC('I','4','2','2'):
857 case VLC_FOURCC('Y','V','1','2'):
859 /* typically 3D textures don't support planar format
860 ** fallback to packed version and use CPU for the conversion
862 static const D3DFORMAT formats[] =
863 { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
864 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
866 case VLC_FOURCC('Y','U','Y','2'):
867 case VLC_FOURCC('Y','U','N','V'):
869 static const D3DFORMAT formats[] =
870 { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
871 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
878 case VLC_FOURCC('R', 'V', '1', '5'):
880 static const D3DFORMAT formats[] =
882 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
884 case VLC_FOURCC('R', 'V', '1', '6'):
886 static const D3DFORMAT formats[] =
888 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
890 case VLC_FOURCC('R', 'V', '2', '4'):
892 static const D3DFORMAT formats[] =
893 { D3DFMT_R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8 };
894 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
896 case VLC_FOURCC('R', 'V', '3', '2'):
898 static const D3DFORMAT formats[] =
899 { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8 };
900 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
904 /* use display default format */
905 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
906 D3DDISPLAYMODE d3ddm;
908 HRESULT hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
912 ** some professional cards could use some advanced pixel format as default,
913 ** make sure we stick with chromas that we can handle internally
915 switch( d3ddm.Format )
918 case D3DFMT_X8R8G8B8:
919 case D3DFMT_A8R8G8B8:
921 case D3DFMT_X1R5G5B5:
922 msg_Dbg( p_vout, "defaulting to adapter pixel format");
923 return Direct3DVoutSelectFormat(p_vout, target, &d3ddm.Format, 1);
926 /* if we fall here, that probably means that we need to render some YUV format */
927 static const D3DFORMAT formats[] =
928 { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
929 msg_Dbg( p_vout, "defaulting to built-in pixel format");
930 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
936 return D3DFMT_UNKNOWN;
939 static int Direct3DVoutSetOutputFormat(vout_thread_t *p_vout, D3DFORMAT format)
944 p_vout->output.i_chroma = VLC_FOURCC('Y', 'U', 'Y', '2');
947 p_vout->output.i_chroma = VLC_FOURCC('U', 'Y', 'V', 'Y');
950 p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '2', '4');
951 p_vout->output.i_rmask = 0xff0000;
952 p_vout->output.i_gmask = 0x00ff00;
953 p_vout->output.i_bmask = 0x0000ff;
955 case D3DFMT_X8R8G8B8:
956 case D3DFMT_A8R8G8B8:
957 p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '3', '2');
958 p_vout->output.i_rmask = 0x00ff0000;
959 p_vout->output.i_gmask = 0x0000ff00;
960 p_vout->output.i_bmask = 0x000000ff;
963 p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '6');
964 p_vout->output.i_rmask = (0x1fL)<<11;
965 p_vout->output.i_gmask = (0x3fL)<<5;
966 p_vout->output.i_bmask = (0x1fL)<<0;
968 case D3DFMT_X1R5G5B5:
969 p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '5');
970 p_vout->output.i_rmask = (0x1fL)<<10;
971 p_vout->output.i_gmask = (0x1fL)<<5;
972 p_vout->output.i_bmask = (0x1fL)<<0;
980 /*****************************************************************************
981 * Direct3DVoutCreatePictures: allocate a vector of identical pictures
982 *****************************************************************************
983 * Each picture has an associated offscreen surface in video memory
984 * depending on hardware capabilities the picture chroma will be as close
985 * as possible to the orginal render chroma to reduce CPU conversion overhead
986 * and delegate this work to video card GPU
987 *****************************************************************************/
988 static int Direct3DVoutCreatePictures( vout_thread_t *p_vout, size_t i_num_pics )
990 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
994 // if vout is already running, use current chroma, otherwise choose from upstream
995 int i_chroma = p_vout->output.i_chroma ? : p_vout->render.i_chroma;
997 I_OUTPUTPICTURES = 0;
1000 ** find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
1001 ** the requested chroma which is usable by the hardware in an offscreen surface, as they
1002 ** typically support more formats than textures
1004 format = Direct3DVoutFindFormat(p_vout, i_chroma, p_vout->p_sys->bbFormat);
1005 if( VLC_SUCCESS != Direct3DVoutSetOutputFormat(p_vout, format) )
1007 msg_Err(p_vout, "surface pixel format is not supported.");
1008 return VLC_EGENERIC;
1011 for( c=0; c<i_num_pics; )
1014 LPDIRECT3DSURFACE9 p_d3dsurf;
1015 picture_t *p_pic = p_vout->p_picture+c;
1017 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(p_d3ddev,
1018 p_vout->render.i_width,
1019 p_vout->render.i_height,
1026 msg_Err(p_vout, "Failed to create picture surface. (hr=0x%lx)", hr);
1027 Direct3DVoutReleasePictures(p_vout);
1028 return VLC_EGENERIC;
1031 /* fill surface with black color */
1032 IDirect3DDevice9_ColorFill(p_d3ddev, p_d3dsurf, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) );
1034 /* assign surface to internal structure */
1035 p_pic->p_sys = (void *)p_d3dsurf;
1037 /* Now that we've got our direct-buffer, we can finish filling in the
1038 * picture_t structures */
1039 switch( p_vout->output.i_chroma )
1041 case VLC_FOURCC('R','G','B','2'):
1042 p_pic->p->i_lines = p_vout->output.i_height;
1043 p_pic->p->i_visible_lines = p_vout->output.i_height;
1044 p_pic->p->i_pixel_pitch = 1;
1045 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1046 p_pic->p->i_pixel_pitch;
1047 p_pic->i_planes = 1;
1049 case VLC_FOURCC('R','V','1','5'):
1050 case VLC_FOURCC('R','V','1','6'):
1051 p_pic->p->i_lines = p_vout->output.i_height;
1052 p_pic->p->i_visible_lines = p_vout->output.i_height;
1053 p_pic->p->i_pixel_pitch = 2;
1054 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1055 p_pic->p->i_pixel_pitch;
1056 p_pic->i_planes = 1;
1058 case VLC_FOURCC('R','V','2','4'):
1059 p_pic->p->i_lines = p_vout->output.i_height;
1060 p_pic->p->i_visible_lines = p_vout->output.i_height;
1061 p_pic->p->i_pixel_pitch = 3;
1062 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1063 p_pic->p->i_pixel_pitch;
1064 p_pic->i_planes = 1;
1066 case VLC_FOURCC('R','V','3','2'):
1067 p_pic->p->i_lines = p_vout->output.i_height;
1068 p_pic->p->i_visible_lines = p_vout->output.i_height;
1069 p_pic->p->i_pixel_pitch = 4;
1070 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1071 p_pic->p->i_pixel_pitch;
1072 p_pic->i_planes = 1;
1074 case VLC_FOURCC('U','Y','V','Y'):
1075 case VLC_FOURCC('Y','U','Y','2'):
1076 p_pic->p->i_lines = p_vout->output.i_height;
1077 p_pic->p->i_visible_lines = p_vout->output.i_height;
1078 p_pic->p->i_pixel_pitch = 2;
1079 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1080 p_pic->p->i_pixel_pitch;
1081 p_pic->i_planes = 1;
1084 Direct3DVoutReleasePictures(p_vout);
1085 return VLC_EGENERIC;
1087 p_pic->i_status = DESTROYED_PICTURE;
1088 p_pic->i_type = DIRECT_PICTURE;
1089 p_pic->b_slow = VLC_TRUE;
1090 p_pic->pf_lock = Direct3DVoutLockSurface;
1091 p_pic->pf_unlock = Direct3DVoutUnlockSurface;
1092 PP_OUTPUTPICTURE[c] = p_pic;
1094 I_OUTPUTPICTURES = ++c;
1097 msg_Dbg( p_vout, "%u Direct3D pictures created successfully", c );
1102 /*****************************************************************************
1103 * Direct3DVoutReleasePictures: destroy a picture vector
1104 *****************************************************************************
1105 * release all video resources used for pictures
1106 *****************************************************************************/
1107 static void Direct3DVoutReleasePictures( vout_thread_t *p_vout)
1109 size_t i_num_pics = I_OUTPUTPICTURES;
1111 for( c=0; c<i_num_pics; ++c )
1113 picture_t *p_pic = p_vout->p_picture+c;
1116 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1118 p_pic->p_sys = NULL;
1122 IDirect3DSurface9_Release(p_d3dsurf);
1126 msg_Dbg( p_vout, "%u Direct3D pictures released.", c);
1128 I_OUTPUTPICTURES = 0;
1131 /*****************************************************************************
1132 * Direct3DVoutLockSurface: Lock surface and get picture data pointer
1133 *****************************************************************************
1134 * This function locks a surface and get the surface descriptor which amongst
1135 * other things has the pointer to the picture data.
1136 *****************************************************************************/
1137 static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1140 D3DLOCKED_RECT d3drect;
1141 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1143 if( NULL == p_d3dsurf )
1144 return VLC_EGENERIC;
1146 /* Lock the surface to get a valid pointer to the picture buffer */
1147 hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0);
1150 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1151 return VLC_EGENERIC;
1154 /* fill in buffer info in first plane */
1155 p_pic->p->p_pixels = d3drect.pBits;
1156 p_pic->p->i_pitch = d3drect.Pitch;
1161 /*****************************************************************************
1162 * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface().
1163 *****************************************************************************/
1164 static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1167 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1169 if( NULL == p_d3dsurf )
1170 return VLC_EGENERIC;
1172 /* Unlock the Surface */
1173 hr = IDirect3DSurface9_UnlockRect(p_d3dsurf);
1176 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1177 return VLC_EGENERIC;
1182 /*****************************************************************************
1183 * Direct3DVoutCreateScene: allocate and initialize a 3D scene
1184 *****************************************************************************
1185 * for advanced blending/filtering a texture needs be used in a 3D scene.
1186 *****************************************************************************/
1188 static int Direct3DVoutCreateScene( vout_thread_t *p_vout )
1190 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
1191 LPDIRECT3DTEXTURE9 p_d3dtex;
1192 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1197 ** Create a texture for use when rendering a scene
1198 ** for performance reason, texture format is identical to backbuffer
1199 ** which would usually be a RGB format
1201 hr = IDirect3DDevice9_CreateTexture(p_d3ddev,
1202 p_vout->render.i_width,
1203 p_vout->render.i_height,
1205 D3DUSAGE_RENDERTARGET,
1206 p_vout->p_sys->bbFormat,
1212 msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr);
1213 return VLC_EGENERIC;
1217 ** Create a vertex buffer for use when rendering scene
1219 hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev,
1220 sizeof(CUSTOMVERTEX)*4,
1221 D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1222 D3DFVF_CUSTOMVERTEX,
1228 msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1229 IDirect3DTexture9_Release(p_d3dtex);
1230 return VLC_EGENERIC;
1233 p_vout->p_sys->p_d3dtex = p_d3dtex;
1234 p_vout->p_sys->p_d3dvtc = p_d3dvtc;
1236 // Texture coordinates outside the range [0.0, 1.0] are set
1237 // to the texture color at 0.0 or 1.0, respectively.
1238 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1239 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1241 // Set linear filtering quality
1242 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1243 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1245 // set maximum ambient light
1246 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1249 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1251 // Turn off the zbuffer
1252 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1255 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE);
1258 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE);
1261 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE);
1264 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1265 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1266 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1267 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1268 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10);
1269 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1271 // Set texture states
1272 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
1273 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1274 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1276 // turn off alpha operation
1277 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1279 msg_Dbg( p_vout, "Direct3D scene created successfully");
1284 /*****************************************************************************
1285 * Direct3DVoutReleaseScene
1286 *****************************************************************************/
1287 static void Direct3DVoutReleaseScene( vout_thread_t *p_vout )
1289 LPDIRECT3DTEXTURE9 p_d3dtex = p_vout->p_sys->p_d3dtex;
1290 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1294 IDirect3DVertexBuffer9_Release(p_d3dvtc);
1295 p_vout->p_sys->p_d3dvtc = NULL;
1300 IDirect3DTexture9_Release(p_d3dtex);
1301 p_vout->p_sys->p_d3dtex = NULL;
1303 msg_Dbg( p_vout, "Direct3D scene released successfully");
1306 /*****************************************************************************
1307 * Render: copy picture surface into a texture and render into a scene
1308 *****************************************************************************
1309 * This function is intented for higher end 3D cards, with pixel shader support
1310 * and at least 64 MB of video RAM.
1311 *****************************************************************************/
1312 static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic )
1314 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
1315 LPDIRECT3DTEXTURE9 p_d3dtex;
1316 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1317 LPDIRECT3DSURFACE9 p_d3dsrc, p_d3ddest;
1318 CUSTOMVERTEX *p_vertices;
1320 float f_width, f_height;
1322 // check if device is still available
1323 hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev);
1326 if( (D3DERR_DEVICENOTRESET != hr)
1327 || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) )
1329 // device is not usable at present (lost device, out of video mem ?)
1333 p_d3dtex = p_vout->p_sys->p_d3dtex;
1334 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1336 /* Clear the backbuffer and the zbuffer */
1337 hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET,
1338 D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
1341 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1345 /* retrieve picture surface */
1346 p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1347 if( NULL == p_d3dsrc )
1349 msg_Dbg( p_vout, "no surface to render ?");
1353 /* retrieve texture top-level surface */
1354 hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest);
1357 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1361 /* Copy picture surface into texture surface, color space conversion happens here */
1362 hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE);
1363 IDirect3DSurface9_Release(p_d3ddest);
1366 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1370 /* Update the vertex buffer */
1371 hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD);
1374 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1378 /* Setup vertices */
1379 f_width = (float)(p_vout->output.i_width);
1380 f_height = (float)(p_vout->output.i_height);
1382 p_vertices[0].x = 0.0f; // left
1383 p_vertices[0].y = 0.0f; // top
1384 p_vertices[0].z = 0.0f;
1385 p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1386 p_vertices[0].rhw = 1.0f;
1387 p_vertices[0].tu = 0.0f;
1388 p_vertices[0].tv = 0.0f;
1390 p_vertices[1].x = f_width; // right
1391 p_vertices[1].y = 0.0f; // top
1392 p_vertices[1].z = 0.0f;
1393 p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1394 p_vertices[1].rhw = 1.0f;
1395 p_vertices[1].tu = 1.0f;
1396 p_vertices[1].tv = 0.0f;
1398 p_vertices[2].x = f_width; // right
1399 p_vertices[2].y = f_height; // bottom
1400 p_vertices[2].z = 0.0f;
1401 p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1402 p_vertices[2].rhw = 1.0f;
1403 p_vertices[2].tu = 1.0f;
1404 p_vertices[2].tv = 1.0f;
1406 p_vertices[3].x = 0.0f; // left
1407 p_vertices[3].y = f_height; // bottom
1408 p_vertices[3].z = 0.0f;
1409 p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1410 p_vertices[3].rhw = 1.0f;
1411 p_vertices[3].tu = 0.0f;
1412 p_vertices[3].tv = 1.0f;
1414 hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc);
1417 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1422 hr = IDirect3DDevice9_BeginScene(p_d3ddev);
1425 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1429 // Setup our texture. Using textures introduces the texture stage states,
1430 // which govern how textures get blended together (in the case of multiple
1431 // textures) and lighting information. In this case, we are modulating
1432 // (blending) our texture with the diffuse color of the vertices.
1433 hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex);
1436 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1437 IDirect3DDevice9_EndScene(p_d3ddev);
1441 // Render the vertex buffer contents
1442 hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX));
1445 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1446 IDirect3DDevice9_EndScene(p_d3ddev);
1450 // we use FVF instead of vertex shader
1451 hr = IDirect3DDevice9_SetVertexShader(p_d3ddev, NULL);
1454 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1455 IDirect3DDevice9_EndScene(p_d3ddev);
1459 hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX);
1462 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1463 IDirect3DDevice9_EndScene(p_d3ddev);
1468 hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1471 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1472 IDirect3DDevice9_EndScene(p_d3ddev);
1477 hr = IDirect3DDevice9_EndScene(p_d3ddev);
1480 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);