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 *****************************************************************************/
36 #include <errno.h> /* ENOMEM */
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_interface.h>
45 #include <vlc_playlist.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int OpenVideo ( vlc_object_t * );
57 static void CloseVideo ( vlc_object_t * );
59 static int Init ( vout_thread_t * );
60 static void End ( vout_thread_t * );
61 static int Manage ( vout_thread_t * );
62 static void Display ( vout_thread_t *, picture_t * );
63 static void FirstDisplay( vout_thread_t *, picture_t * );
65 static int Direct3DVoutCreate ( vout_thread_t * );
66 static void Direct3DVoutRelease ( vout_thread_t * );
68 static int Direct3DVoutOpen ( vout_thread_t * );
69 static void Direct3DVoutClose ( vout_thread_t * );
71 static int Direct3DVoutResetDevice( vout_thread_t * );
73 static int Direct3DVoutCreatePictures ( vout_thread_t *, size_t );
74 static void Direct3DVoutReleasePictures ( vout_thread_t * );
76 static int Direct3DVoutLockSurface ( vout_thread_t *, picture_t * );
77 static int Direct3DVoutUnlockSurface( vout_thread_t *, picture_t * );
79 static int Direct3DVoutCreateScene ( vout_thread_t * );
80 static void Direct3DVoutReleaseScene ( vout_thread_t * );
81 static void Direct3DVoutRenderScene ( vout_thread_t *, picture_t * );
83 /*****************************************************************************
85 *****************************************************************************/
87 static bool IsVistaOrAbove(void)
90 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
92 if( GetVersionEx(&winVer) )
94 if( winVer.dwMajorVersion > 5 )
96 /* Windows Vista or above, make this module the default */
100 /* Windows XP or lower, make sure this module isn't the default */
104 static int OpenVideoXP( vlc_object_t *obj )
106 return IsVistaOrAbove() ? VLC_EGENERIC : OpenVideo( obj );
109 static int OpenVideoVista( vlc_object_t *obj )
111 return IsVistaOrAbove() ? OpenVideo( obj ) : VLC_EGENERIC;
115 set_shortname( "Direct3D" )
116 set_category( CAT_VIDEO )
117 set_subcategory( SUBCAT_VIDEO_VOUT )
118 set_description( N_("DirectX 3D video output") )
119 set_capability( "video output", 50 )
120 add_shortcut( "direct3d" )
121 set_callbacks( OpenVideoXP, CloseVideo )
123 /* FIXME: Hack to avoid unregistering our window class */
124 linked_with_a_crap_library_which_uses_atexit ()
127 set_capability( "video output", 150 )
128 add_shortcut( "direct3d" )
129 set_callbacks( OpenVideoVista, CloseVideo )
133 /* check if we registered a window class because we need to
136 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
137 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
140 /*****************************************************************************
142 *****************************************************************************
143 *****************************************************************************/
146 FLOAT x,y,z; // vertex untransformed position
147 FLOAT rhw; // eye distance
148 D3DCOLOR diffuse; // diffuse color
149 FLOAT tu, tv; // texture relative coordinates
152 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
154 /*****************************************************************************
155 * OpenVideo: allocate Vout video thread output method
156 *****************************************************************************
157 * This function allocates and initialize the Direct3D vout method.
158 *****************************************************************************/
159 static int OpenVideo( vlc_object_t *p_this )
161 vout_thread_t * p_vout = (vout_thread_t *)p_this;
163 /* Allocate structure */
164 p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
165 if( p_vout->p_sys == NULL )
168 if( VLC_SUCCESS != Direct3DVoutCreate( p_vout ) )
170 msg_Err( p_vout, "Direct3D could not be initialized !");
171 Direct3DVoutRelease( p_vout );
172 free( p_vout->p_sys );
176 /* Initialisations */
177 p_vout->pf_init = Init;
178 p_vout->pf_end = End;
179 p_vout->pf_manage = Manage;
180 p_vout->pf_render = Direct3DVoutRenderScene;
181 p_vout->pf_display = FirstDisplay;
183 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
184 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
185 p_vout->p_sys->i_changes = 0;
186 vlc_mutex_init( &p_vout->p_sys->lock );
187 SetRectEmpty( &p_vout->p_sys->rect_display );
188 SetRectEmpty( &p_vout->p_sys->rect_parent );
190 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
191 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
193 p_vout->p_sys->b_cursor_hidden = 0;
194 p_vout->p_sys->i_lastmoved = mdate();
195 p_vout->p_sys->i_mouse_hide_timeout =
196 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
198 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
201 /* Set main window's size */
202 p_vout->p_sys->i_window_width = p_vout->i_window_width;
203 p_vout->p_sys->i_window_height = p_vout->i_window_height;
205 /* Create the Vout EventThread, this thread is created by us to isolate
206 * the Win32 PeekMessage function calls. We want to do this because
207 * Windows can stay blocked inside this call for a long time, and when
208 * this happens it thus blocks vlc's video_output thread.
209 * Vout EventThread will take care of the creation of the video
210 * window (because PeekMessage has to be called from the same thread which
211 * created the window). */
212 msg_Dbg( p_vout, "creating Vout EventThread" );
213 p_vout->p_sys->p_event =
214 vlc_object_create( p_vout, sizeof(event_thread_t) );
215 p_vout->p_sys->p_event->p_vout = p_vout;
216 p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
217 if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
220 msg_Err( p_vout, "cannot create Vout EventThread" );
221 CloseHandle( p_vout->p_sys->p_event->window_ready );
222 vlc_object_release( p_vout->p_sys->p_event );
223 p_vout->p_sys->p_event = NULL;
226 WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
227 CloseHandle( p_vout->p_sys->p_event->window_ready );
229 if( p_vout->p_sys->p_event->b_error )
231 msg_Err( p_vout, "Vout EventThread failed" );
235 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
237 msg_Dbg( p_vout, "Vout EventThread running" );
239 /* Variable to indicate if the window should be on top of others */
240 /* Trigger a callback right now */
241 var_TriggerCallback( p_vout, "video-on-top" );
243 /* disable screensaver by temporarily changing system settings */
244 p_vout->p_sys->i_spi_lowpowertimeout = 0;
245 p_vout->p_sys->i_spi_powerofftimeout = 0;
246 p_vout->p_sys->i_spi_screensavetimeout = 0;
247 if( var_GetBool( p_vout, "disable-screensaver" ) ) {
248 msg_Dbg(p_vout, "disabling screen saver");
249 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
250 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
251 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
252 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
254 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
255 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
256 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
257 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
259 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
260 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
261 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
262 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
268 CloseVideo( VLC_OBJECT(p_vout) );
272 /*****************************************************************************
273 * CloseVideo: destroy Sys video thread output method
274 *****************************************************************************
275 * Terminate an output method created by Create
276 *****************************************************************************/
277 static void CloseVideo( vlc_object_t *p_this )
279 vout_thread_t * p_vout = (vout_thread_t *)p_this;
281 Direct3DVoutRelease( p_vout );
283 if( p_vout->b_fullscreen )
285 msg_Dbg( p_vout, "Quitting fullscreen" );
286 Win32ToggleFullscreen( p_vout );
287 /* Force fullscreen in the core for the next video */
288 var_SetBool( p_vout, "fullscreen", true );
291 if( p_vout->p_sys->p_event )
293 vlc_object_detach( p_vout->p_sys->p_event );
295 /* Kill Vout EventThread */
296 vlc_object_kill( p_vout->p_sys->p_event );
298 /* we need to be sure Vout EventThread won't stay stuck in
299 * GetMessage, so we send a fake message */
300 if( p_vout->p_sys->hwnd )
302 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
305 vlc_thread_join( p_vout->p_sys->p_event );
306 vlc_object_release( p_vout->p_sys->p_event );
309 vlc_mutex_destroy( &p_vout->p_sys->lock );
311 /* restore screensaver system settings */
312 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
313 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
314 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
316 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
317 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
318 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
320 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
321 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
322 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
325 free( p_vout->p_sys );
326 p_vout->p_sys = NULL;
329 /*****************************************************************************
330 * Init: initialize Direct3D video thread output method
331 *****************************************************************************/
332 static int Init( vout_thread_t *p_vout )
336 p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
338 /* Initialise Direct3D */
339 if( VLC_SUCCESS != Direct3DVoutOpen( p_vout ) )
341 msg_Err( p_vout, "cannot initialize Direct3D" );
345 /* Initialize the output structure.
346 * Since Direct3D can do rescaling for us, stick to the default
347 * coordinates and aspect. */
348 p_vout->output.i_width = p_vout->render.i_width;
349 p_vout->output.i_height = p_vout->render.i_height;
350 p_vout->output.i_aspect = p_vout->render.i_aspect;
351 p_vout->fmt_out = p_vout->fmt_in;
352 UpdateRects( p_vout, true );
354 /* create picture pool */
355 p_vout->output.i_chroma = 0;
356 i_ret = Direct3DVoutCreatePictures(p_vout, 1);
357 if( VLC_SUCCESS != i_ret )
359 msg_Err(p_vout, "Direct3D picture pool initialization failed !");
364 i_ret = Direct3DVoutCreateScene(p_vout);
365 if( VLC_SUCCESS != i_ret )
367 msg_Err(p_vout, "Direct3D scene initialization failed !");
368 Direct3DVoutReleasePictures(p_vout);
372 /* Change the window title bar text */
373 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
375 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
379 /*****************************************************************************
380 * End: terminate Sys video thread output method
381 *****************************************************************************
382 * Terminate an output method created by Create.
383 * It is called at the end of the thread.
384 *****************************************************************************/
385 static void End( vout_thread_t *p_vout )
387 Direct3DVoutReleaseScene(p_vout);
388 Direct3DVoutReleasePictures(p_vout);
389 Direct3DVoutClose( p_vout );
392 /*****************************************************************************
393 * Manage: handle Sys events
394 *****************************************************************************
395 * This function should be called regularly by the video output thread.
396 * It returns a non null value if an error occurred.
397 *****************************************************************************/
398 static int Manage( vout_thread_t *p_vout )
400 /* If we do not control our window, we check for geometry changes
401 * ourselves because the parent might not send us its events. */
402 vlc_mutex_lock( &p_vout->p_sys->lock );
403 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
408 vlc_mutex_unlock( &p_vout->p_sys->lock );
410 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
411 point.x = point.y = 0;
412 ClientToScreen( p_vout->p_sys->hparent, &point );
413 OffsetRect( &rect_parent, point.x, point.y );
415 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
417 p_vout->p_sys->rect_parent = rect_parent;
419 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
420 rect_parent.right - rect_parent.left,
421 rect_parent.bottom - rect_parent.top,
427 vlc_mutex_unlock( &p_vout->p_sys->lock );
433 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
435 #if 0 /* need that when bicubic filter is available */
439 GetClientRect(p_vout->p_sys->hvideownd, &rect);
440 width = rect.right-rect.left;
441 height = rect.bottom-rect.top;
443 if( (width != p_vout->p_sys->d3dpp.BackBufferWidth)
444 || (height != p_vout->p_sys->d3dpp.BackBufferHeight) )
446 msg_Dbg(p_vout, "resizing device back buffers to (%lux%lu)", width, height);
447 // need to reset D3D device to resize back buffer
448 if( VLC_SUCCESS != Direct3DVoutResetDevice(p_vout, width, height) )
452 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
455 /* autoscale toggle */
456 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
458 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
460 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
461 p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
463 UpdateRects( p_vout, true );
467 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
469 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
471 p_vout->b_autoscale = false;
473 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
474 UpdateRects( p_vout, true );
477 /* Check for cropping / aspect changes */
478 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
479 p_vout->i_changes & VOUT_ASPECT_CHANGE )
481 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
482 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
484 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
485 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
486 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
487 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
488 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
489 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
490 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
491 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
492 UpdateRects( p_vout, true );
495 /* We used to call the Win32 PeekMessage function here to read the window
496 * messages. But since window can stay blocked into this function for a
497 * long time (for example when you move your window on the screen), I
498 * decided to isolate PeekMessage in another thread. */
503 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
504 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
506 Win32ToggleFullscreen( p_vout );
508 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
509 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
515 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
516 (mdate() - p_vout->p_sys->i_lastmoved) >
517 p_vout->p_sys->i_mouse_hide_timeout )
522 /* Hide the cursor only if it is inside our window */
523 GetCursorPos( &point );
524 hwnd = WindowFromPoint(point);
525 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
527 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
531 p_vout->p_sys->i_lastmoved = mdate();
536 * "Always on top" status change
538 if( p_vout->p_sys->b_on_top_change )
541 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
543 var_Get( p_vout, "video-on-top", &val );
545 /* Set the window on top if necessary */
546 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
549 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
550 MF_BYCOMMAND | MFS_CHECKED );
551 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
552 SWP_NOSIZE | SWP_NOMOVE );
555 /* The window shouldn't be on top */
556 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
559 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
560 MF_BYCOMMAND | MFS_UNCHECKED );
561 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
562 SWP_NOSIZE | SWP_NOMOVE );
565 p_vout->p_sys->b_on_top_change = false;
568 /* Check if the event thread is still running */
569 if( !vlc_object_alive (p_vout->p_sys->p_event) )
571 return VLC_EGENERIC; /* exit */
577 /*****************************************************************************
578 * Display: displays previously rendered output
579 *****************************************************************************
580 * This function sends the currently rendered image to the display, wait until
581 * it is displayed and switch the two rendering buffers, preparing next frame.
582 *****************************************************************************/
583 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
585 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
586 // Present the back buffer contents to the display
587 // stretching and filtering happens here
588 HRESULT hr = IDirect3DDevice9_Present(p_d3ddev,
589 &(p_vout->p_sys->rect_src_clipped),
592 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
596 ** this function is only used once when the first picture is received
597 ** this function will show the video window once a picture is ready
600 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
602 /* get initial picture presented through D3D */
603 Display(p_vout, p_pic);
606 ** Video window is initially hidden, show it now since we got a
609 SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
617 /* use and restores proper display function for further pictures */
618 p_vout->pf_display = Display;
621 /*****************************************************************************
622 * DirectD3DVoutCreate: Initialize and instance of Direct3D9
623 *****************************************************************************
624 * This function initialize Direct3D and analyze available resources from
626 *****************************************************************************/
627 static int Direct3DVoutCreate( vout_thread_t *p_vout )
630 LPDIRECT3D9 p_d3dobj;
633 LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
635 p_vout->p_sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
636 if( NULL == p_vout->p_sys->hd3d9_dll )
638 msg_Warn( p_vout, "cannot load d3d9.dll, aborting" );
643 (void *)GetProcAddress( p_vout->p_sys->hd3d9_dll,
644 TEXT("Direct3DCreate9") );
645 if( OurDirect3DCreate9 == NULL )
647 msg_Err( p_vout, "Cannot locate reference to Direct3DCreate9 ABI in DLL" );
651 /* Create the D3D object. */
652 p_d3dobj = OurDirect3DCreate9( D3D_SDK_VERSION );
653 if( NULL == p_d3dobj )
655 msg_Err( p_vout, "Could not create Direct3D9 instance.");
658 p_vout->p_sys->p_d3dobj = p_d3dobj;
661 ** Get device capabilities
663 ZeroMemory(&d3dCaps, sizeof(d3dCaps));
664 hr = IDirect3D9_GetDeviceCaps(p_d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
667 msg_Err( p_vout, "Could not read adapter capabilities. (hr=0x%lX)", hr);
670 /* TODO: need to test device capabilities and select the right render function */
675 /*****************************************************************************
676 * DirectD3DVoutRelease: release an instance of Direct3D9
677 *****************************************************************************/
679 static void Direct3DVoutRelease( vout_thread_t *p_vout )
681 if( p_vout->p_sys->p_d3dobj )
683 IDirect3D9_Release(p_vout->p_sys->p_d3dobj);
684 p_vout->p_sys->p_d3dobj = NULL;
686 if( NULL != p_vout->p_sys->hd3d9_dll )
688 FreeLibrary(p_vout->p_sys->hd3d9_dll);
689 p_vout->p_sys->hd3d9_dll = NULL;
693 static int Direct3DFillPresentationParameters(vout_thread_t *p_vout, D3DPRESENT_PARAMETERS *d3dpp)
695 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
696 D3DDISPLAYMODE d3ddm;
700 ** Get the current desktop display mode, so we can set up a back
701 ** buffer of the same format
703 hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
706 msg_Err( p_vout, "Could not read adapter display mode. (hr=0x%lX)", hr);
710 /* keep a copy of current desktop format */
711 p_vout->p_sys->bbFormat = d3ddm.Format;
713 /* Set up the structure used to create the D3DDevice. */
714 ZeroMemory( d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
715 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
716 d3dpp->Windowed = TRUE;
717 d3dpp->hDeviceWindow = p_vout->p_sys->hvideownd;
718 d3dpp->BackBufferWidth = p_vout->output.i_width;
719 d3dpp->BackBufferHeight = p_vout->output.i_height;
720 d3dpp->SwapEffect = D3DSWAPEFFECT_COPY;
721 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
722 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
723 d3dpp->BackBufferFormat = d3ddm.Format;
724 d3dpp->BackBufferCount = 1;
725 d3dpp->EnableAutoDepthStencil = FALSE;
730 /*****************************************************************************
731 * DirectD3DVoutOpen: Takes care of all the Direct3D9 initialisations
732 *****************************************************************************
733 * This function creates Direct3D device
734 * this must be called from the vout thread for performance reason, as
735 * all Direct3D Device APIs are used in a non multithread safe environment
736 *****************************************************************************/
737 static int Direct3DVoutOpen( vout_thread_t *p_vout )
739 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
740 LPDIRECT3DDEVICE9 p_d3ddev;
741 D3DPRESENT_PARAMETERS d3dpp;
744 if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
747 // Create the D3DDevice
748 hr = IDirect3D9_CreateDevice(p_d3dobj, D3DADAPTER_DEFAULT,
749 D3DDEVTYPE_HAL, p_vout->p_sys->hvideownd,
750 D3DCREATE_SOFTWARE_VERTEXPROCESSING|
751 D3DCREATE_MULTITHREADED,
755 msg_Err(p_vout, "Could not create the D3D device! (hr=0x%lX)", hr);
758 p_vout->p_sys->p_d3ddev = p_d3ddev;
760 msg_Dbg( p_vout, "Direct3D device adapter successfully initialized" );
764 /*****************************************************************************
765 * DirectD3DClose: release the Direct3D9 device
766 *****************************************************************************/
767 static void Direct3DVoutClose( vout_thread_t *p_vout )
769 if( p_vout->p_sys->p_d3ddev )
771 IDirect3DDevice9_Release(p_vout->p_sys->p_d3ddev);
772 p_vout->p_sys->p_d3ddev = NULL;
775 p_vout->p_sys->hmonitor = NULL;
778 /*****************************************************************************
779 * DirectD3DClose: reset the Direct3D9 device
780 *****************************************************************************
781 * All resources must be deallocated before the reset occur, they will be
782 * realllocated once the reset has been performed successfully
783 *****************************************************************************/
784 static int Direct3DVoutResetDevice( vout_thread_t *p_vout )
786 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
787 D3DPRESENT_PARAMETERS d3dpp;
790 if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
793 // release all D3D objects
794 Direct3DVoutReleaseScene( p_vout );
795 Direct3DVoutReleasePictures( p_vout );
797 hr = IDirect3DDevice9_Reset(p_d3ddev, &d3dpp);
801 if( (VLC_SUCCESS != Direct3DVoutCreatePictures(p_vout, 1))
802 || (VLC_SUCCESS != Direct3DVoutCreateScene(p_vout)) )
804 msg_Dbg(p_vout, "%s failed !", __FUNCTION__);
809 msg_Err(p_vout, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
815 static D3DFORMAT Direct3DVoutSelectFormat( vout_thread_t *p_vout, D3DFORMAT target,
816 const D3DFORMAT *formats, size_t count)
818 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
821 for( c=0; c<count; ++c )
824 D3DFORMAT format = formats[c];
825 /* test whether device can create a surface of that format */
826 hr = IDirect3D9_CheckDeviceFormat(p_d3dobj, D3DADAPTER_DEFAULT,
827 D3DDEVTYPE_HAL, target, 0, D3DRTYPE_SURFACE, format);
830 /* test whether device can perform color-conversion
831 ** from that format to target format
833 hr = IDirect3D9_CheckDeviceFormatConversion(p_d3dobj,
834 D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
839 // found a compatible format
843 msg_Dbg( p_vout, "selected surface pixel format is UYVY");
846 msg_Dbg( p_vout, "selected surface pixel format is YUY2");
848 case D3DFMT_X8R8G8B8:
849 msg_Dbg( p_vout, "selected surface pixel format is X8R8G8B8");
851 case D3DFMT_A8R8G8B8:
852 msg_Dbg( p_vout, "selected surface pixel format is A8R8G8B8");
855 msg_Dbg( p_vout, "selected surface pixel format is R8G8B8");
858 msg_Dbg( p_vout, "selected surface pixel format is R5G6B5");
860 case D3DFMT_X1R5G5B5:
861 msg_Dbg( p_vout, "selected surface pixel format is X1R5G5B5");
864 msg_Dbg( p_vout, "selected surface pixel format is 0x%0X", format);
869 else if( D3DERR_NOTAVAILABLE != hr )
871 msg_Err( p_vout, "Could not query adapter supported formats. (hr=0x%lX)", hr);
875 return D3DFMT_UNKNOWN;
878 static D3DFORMAT Direct3DVoutFindFormat(vout_thread_t *p_vout, int i_chroma, D3DFORMAT target)
880 //if( p_vout->p_sys->b_hw_yuv && ! _got_vista_or_above )
881 if( p_vout->p_sys->b_hw_yuv )
883 /* it sounds like vista does not support YUV surfaces at all */
888 static const D3DFORMAT formats[] =
889 { D3DFMT_UYVY, D3DFMT_YUY2, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
890 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
896 /* typically 3D textures don't support planar format
897 ** fallback to packed version and use CPU for the conversion
899 static const D3DFORMAT formats[] =
900 { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
901 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
905 static const D3DFORMAT formats[] =
906 { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
907 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
914 case VLC_CODEC_RGB15:
916 static const D3DFORMAT formats[] =
918 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
920 case VLC_CODEC_RGB16:
922 static const D3DFORMAT formats[] =
924 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
926 case VLC_CODEC_RGB24:
928 static const D3DFORMAT formats[] =
929 { D3DFMT_R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8 };
930 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
932 case VLC_CODEC_RGB32:
934 static const D3DFORMAT formats[] =
935 { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8 };
936 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
940 /* use display default format */
941 LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
942 D3DDISPLAYMODE d3ddm;
944 HRESULT hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
948 ** some professional cards could use some advanced pixel format as default,
949 ** make sure we stick with chromas that we can handle internally
951 switch( d3ddm.Format )
954 case D3DFMT_X8R8G8B8:
955 case D3DFMT_A8R8G8B8:
957 case D3DFMT_X1R5G5B5:
958 msg_Dbg( p_vout, "defaulting to adapter pixel format");
959 return Direct3DVoutSelectFormat(p_vout, target, &d3ddm.Format, 1);
962 /* if we fall here, that probably means that we need to render some YUV format */
963 static const D3DFORMAT formats[] =
964 { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
965 msg_Dbg( p_vout, "defaulting to built-in pixel format");
966 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
972 return D3DFMT_UNKNOWN;
975 static int Direct3DVoutSetOutputFormat(vout_thread_t *p_vout, D3DFORMAT format)
980 p_vout->output.i_chroma = VLC_CODEC_YUYV;
983 p_vout->output.i_chroma = VLC_CODEC_UYVY;
986 p_vout->output.i_chroma = VLC_CODEC_RGB24;
987 p_vout->output.i_rmask = 0xff0000;
988 p_vout->output.i_gmask = 0x00ff00;
989 p_vout->output.i_bmask = 0x0000ff;
991 case D3DFMT_X8R8G8B8:
992 case D3DFMT_A8R8G8B8:
993 p_vout->output.i_chroma = VLC_CODEC_RGB32;
994 p_vout->output.i_rmask = 0x00ff0000;
995 p_vout->output.i_gmask = 0x0000ff00;
996 p_vout->output.i_bmask = 0x000000ff;
999 p_vout->output.i_chroma = VLC_CODEC_RGB16;
1000 p_vout->output.i_rmask = (0x1fL)<<11;
1001 p_vout->output.i_gmask = (0x3fL)<<5;
1002 p_vout->output.i_bmask = (0x1fL)<<0;
1004 case D3DFMT_X1R5G5B5:
1005 p_vout->output.i_chroma = VLC_CODEC_RGB15;
1006 p_vout->output.i_rmask = (0x1fL)<<10;
1007 p_vout->output.i_gmask = (0x1fL)<<5;
1008 p_vout->output.i_bmask = (0x1fL)<<0;
1011 return VLC_EGENERIC;
1016 /*****************************************************************************
1017 * Direct3DVoutCreatePictures: allocate a vector of identical pictures
1018 *****************************************************************************
1019 * Each picture has an associated offscreen surface in video memory
1020 * depending on hardware capabilities the picture chroma will be as close
1021 * as possible to the orginal render chroma to reduce CPU conversion overhead
1022 * and delegate this work to video card GPU
1023 *****************************************************************************/
1024 static int Direct3DVoutCreatePictures( vout_thread_t *p_vout, size_t i_num_pics )
1026 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
1030 // if vout is already running, use current chroma, otherwise choose from upstream
1031 int i_chroma = p_vout->output.i_chroma ? : p_vout->render.i_chroma;
1033 I_OUTPUTPICTURES = 0;
1036 ** find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
1037 ** the requested chroma which is usable by the hardware in an offscreen surface, as they
1038 ** typically support more formats than textures
1040 format = Direct3DVoutFindFormat(p_vout, i_chroma, p_vout->p_sys->bbFormat);
1041 if( VLC_SUCCESS != Direct3DVoutSetOutputFormat(p_vout, format) )
1043 msg_Err(p_vout, "surface pixel format is not supported.");
1044 return VLC_EGENERIC;
1047 for( c=0; c<i_num_pics; )
1050 LPDIRECT3DSURFACE9 p_d3dsurf;
1051 picture_t *p_pic = p_vout->p_picture+c;
1053 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(p_d3ddev,
1054 p_vout->render.i_width,
1055 p_vout->render.i_height,
1062 msg_Err(p_vout, "Failed to create picture surface. (hr=0x%lx)", hr);
1063 Direct3DVoutReleasePictures(p_vout);
1064 return VLC_EGENERIC;
1067 /* fill surface with black color */
1068 IDirect3DDevice9_ColorFill(p_d3ddev, p_d3dsurf, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) );
1070 /* assign surface to internal structure */
1071 p_pic->p_sys = (void *)p_d3dsurf;
1073 /* Now that we've got our direct-buffer, we can finish filling in the
1074 * picture_t structures */
1075 switch( p_vout->output.i_chroma )
1077 case VLC_CODEC_RGB8:
1078 p_pic->p->i_lines = p_vout->output.i_height;
1079 p_pic->p->i_visible_lines = p_vout->output.i_height;
1080 p_pic->p->i_pixel_pitch = 1;
1081 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1082 p_pic->p->i_pixel_pitch;
1083 p_pic->i_planes = 1;
1085 case VLC_CODEC_RGB15:
1086 case VLC_CODEC_RGB16:
1087 p_pic->p->i_lines = p_vout->output.i_height;
1088 p_pic->p->i_visible_lines = p_vout->output.i_height;
1089 p_pic->p->i_pixel_pitch = 2;
1090 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1091 p_pic->p->i_pixel_pitch;
1092 p_pic->i_planes = 1;
1094 case VLC_CODEC_RGB24:
1095 p_pic->p->i_lines = p_vout->output.i_height;
1096 p_pic->p->i_visible_lines = p_vout->output.i_height;
1097 p_pic->p->i_pixel_pitch = 3;
1098 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1099 p_pic->p->i_pixel_pitch;
1100 p_pic->i_planes = 1;
1102 case VLC_CODEC_RGB32:
1103 p_pic->p->i_lines = p_vout->output.i_height;
1104 p_pic->p->i_visible_lines = p_vout->output.i_height;
1105 p_pic->p->i_pixel_pitch = 4;
1106 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1107 p_pic->p->i_pixel_pitch;
1108 p_pic->i_planes = 1;
1110 case VLC_CODEC_UYVY:
1111 case VLC_CODEC_YUYV:
1112 p_pic->p->i_lines = p_vout->output.i_height;
1113 p_pic->p->i_visible_lines = p_vout->output.i_height;
1114 p_pic->p->i_pixel_pitch = 2;
1115 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1116 p_pic->p->i_pixel_pitch;
1117 p_pic->i_planes = 1;
1120 Direct3DVoutReleasePictures(p_vout);
1121 return VLC_EGENERIC;
1123 p_pic->i_status = DESTROYED_PICTURE;
1124 p_pic->i_type = DIRECT_PICTURE;
1125 p_pic->b_slow = true;
1126 p_pic->pf_lock = Direct3DVoutLockSurface;
1127 p_pic->pf_unlock = Direct3DVoutUnlockSurface;
1128 PP_OUTPUTPICTURE[c] = p_pic;
1130 I_OUTPUTPICTURES = ++c;
1133 msg_Dbg( p_vout, "%u Direct3D pictures created successfully", c );
1138 /*****************************************************************************
1139 * Direct3DVoutReleasePictures: destroy a picture vector
1140 *****************************************************************************
1141 * release all video resources used for pictures
1142 *****************************************************************************/
1143 static void Direct3DVoutReleasePictures( vout_thread_t *p_vout)
1145 size_t i_num_pics = I_OUTPUTPICTURES;
1147 for( c=0; c<i_num_pics; ++c )
1149 picture_t *p_pic = p_vout->p_picture+c;
1152 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1154 p_pic->p_sys = NULL;
1158 IDirect3DSurface9_Release(p_d3dsurf);
1162 msg_Dbg( p_vout, "%u Direct3D pictures released.", c);
1164 I_OUTPUTPICTURES = 0;
1167 /*****************************************************************************
1168 * Direct3DVoutLockSurface: Lock surface and get picture data pointer
1169 *****************************************************************************
1170 * This function locks a surface and get the surface descriptor which amongst
1171 * other things has the pointer to the picture data.
1172 *****************************************************************************/
1173 static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1176 D3DLOCKED_RECT d3drect;
1177 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1179 if( NULL == p_d3dsurf )
1180 return VLC_EGENERIC;
1182 /* Lock the surface to get a valid pointer to the picture buffer */
1183 hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0);
1186 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1187 return VLC_EGENERIC;
1190 /* fill in buffer info in first plane */
1191 p_pic->p->p_pixels = d3drect.pBits;
1192 p_pic->p->i_pitch = d3drect.Pitch;
1197 /*****************************************************************************
1198 * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface().
1199 *****************************************************************************/
1200 static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1203 LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1205 if( NULL == p_d3dsurf )
1206 return VLC_EGENERIC;
1208 /* Unlock the Surface */
1209 hr = IDirect3DSurface9_UnlockRect(p_d3dsurf);
1212 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1213 return VLC_EGENERIC;
1218 /*****************************************************************************
1219 * Direct3DVoutCreateScene: allocate and initialize a 3D scene
1220 *****************************************************************************
1221 * for advanced blending/filtering a texture needs be used in a 3D scene.
1222 *****************************************************************************/
1224 static int Direct3DVoutCreateScene( vout_thread_t *p_vout )
1226 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
1227 LPDIRECT3DTEXTURE9 p_d3dtex;
1228 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1233 ** Create a texture for use when rendering a scene
1234 ** for performance reason, texture format is identical to backbuffer
1235 ** which would usually be a RGB format
1237 hr = IDirect3DDevice9_CreateTexture(p_d3ddev,
1238 p_vout->render.i_width,
1239 p_vout->render.i_height,
1241 D3DUSAGE_RENDERTARGET,
1242 p_vout->p_sys->bbFormat,
1248 msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr);
1249 return VLC_EGENERIC;
1253 ** Create a vertex buffer for use when rendering scene
1255 hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev,
1256 sizeof(CUSTOMVERTEX)*4,
1257 D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1258 D3DFVF_CUSTOMVERTEX,
1264 msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1265 IDirect3DTexture9_Release(p_d3dtex);
1266 return VLC_EGENERIC;
1269 p_vout->p_sys->p_d3dtex = p_d3dtex;
1270 p_vout->p_sys->p_d3dvtc = p_d3dvtc;
1272 // Texture coordinates outside the range [0.0, 1.0] are set
1273 // to the texture color at 0.0 or 1.0, respectively.
1274 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1275 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1277 // Set linear filtering quality
1278 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1279 IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1281 // set maximum ambient light
1282 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1285 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1287 // Turn off the zbuffer
1288 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1291 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE);
1294 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE);
1297 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE);
1300 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1301 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1302 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1303 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1304 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10);
1305 IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1307 // Set texture states
1308 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
1309 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1310 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1312 // turn off alpha operation
1313 IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1315 msg_Dbg( p_vout, "Direct3D scene created successfully");
1320 /*****************************************************************************
1321 * Direct3DVoutReleaseScene
1322 *****************************************************************************/
1323 static void Direct3DVoutReleaseScene( vout_thread_t *p_vout )
1325 LPDIRECT3DTEXTURE9 p_d3dtex = p_vout->p_sys->p_d3dtex;
1326 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1330 IDirect3DVertexBuffer9_Release(p_d3dvtc);
1331 p_vout->p_sys->p_d3dvtc = NULL;
1336 IDirect3DTexture9_Release(p_d3dtex);
1337 p_vout->p_sys->p_d3dtex = NULL;
1339 msg_Dbg( p_vout, "Direct3D scene released successfully");
1342 /*****************************************************************************
1343 * Render: copy picture surface into a texture and render into a scene
1344 *****************************************************************************
1345 * This function is intented for higher end 3D cards, with pixel shader support
1346 * and at least 64 MB of video RAM.
1347 *****************************************************************************/
1348 static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic )
1350 LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev;
1351 LPDIRECT3DTEXTURE9 p_d3dtex;
1352 LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1353 LPDIRECT3DSURFACE9 p_d3dsrc, p_d3ddest;
1354 CUSTOMVERTEX *p_vertices;
1356 float f_width, f_height;
1358 // check if device is still available
1359 hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev);
1362 if( (D3DERR_DEVICENOTRESET != hr)
1363 || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) )
1365 // device is not usable at present (lost device, out of video mem ?)
1369 p_d3dtex = p_vout->p_sys->p_d3dtex;
1370 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1372 /* Clear the backbuffer and the zbuffer */
1373 hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET,
1374 D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
1377 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1381 /* retrieve picture surface */
1382 p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1383 if( NULL == p_d3dsrc )
1385 msg_Dbg( p_vout, "no surface to render ?");
1389 /* retrieve texture top-level surface */
1390 hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest);
1393 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1397 /* Copy picture surface into texture surface, color space conversion happens here */
1398 hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE);
1399 IDirect3DSurface9_Release(p_d3ddest);
1402 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1406 /* Update the vertex buffer */
1407 hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD);
1410 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1414 /* Setup vertices */
1415 f_width = (float)(p_vout->output.i_width);
1416 f_height = (float)(p_vout->output.i_height);
1418 /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
1419 /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
1420 p_vertices[0].x = -0.5f; // left
1421 p_vertices[0].y = -0.5f; // top
1422 p_vertices[0].z = 0.0f;
1423 p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1424 p_vertices[0].rhw = 1.0f;
1425 p_vertices[0].tu = 0.0f;
1426 p_vertices[0].tv = 0.0f;
1428 p_vertices[1].x = f_width - 0.5f; // right
1429 p_vertices[1].y = -0.5f; // top
1430 p_vertices[1].z = 0.0f;
1431 p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1432 p_vertices[1].rhw = 1.0f;
1433 p_vertices[1].tu = 1.0f;
1434 p_vertices[1].tv = 0.0f;
1436 p_vertices[2].x = f_width - 0.5f; // right
1437 p_vertices[2].y = f_height - 0.5f; // bottom
1438 p_vertices[2].z = 0.0f;
1439 p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1440 p_vertices[2].rhw = 1.0f;
1441 p_vertices[2].tu = 1.0f;
1442 p_vertices[2].tv = 1.0f;
1444 p_vertices[3].x = -0.5f; // left
1445 p_vertices[3].y = f_height - 0.5f; // bottom
1446 p_vertices[3].z = 0.0f;
1447 p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1448 p_vertices[3].rhw = 1.0f;
1449 p_vertices[3].tu = 0.0f;
1450 p_vertices[3].tv = 1.0f;
1452 hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc);
1455 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1460 hr = IDirect3DDevice9_BeginScene(p_d3ddev);
1463 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1467 // Setup our texture. Using textures introduces the texture stage states,
1468 // which govern how textures get blended together (in the case of multiple
1469 // textures) and lighting information. In this case, we are modulating
1470 // (blending) our texture with the diffuse color of the vertices.
1471 hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex);
1474 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1475 IDirect3DDevice9_EndScene(p_d3ddev);
1479 // Render the vertex buffer contents
1480 hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX));
1483 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1484 IDirect3DDevice9_EndScene(p_d3ddev);
1488 // we use FVF instead of vertex shader
1489 hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX);
1492 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1493 IDirect3DDevice9_EndScene(p_d3ddev);
1498 hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1501 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1502 IDirect3DDevice9_EndScene(p_d3ddev);
1507 hr = IDirect3DDevice9_EndScene(p_d3ddev);
1510 msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);