7aaa410f3190b22f91ebe47ddab810d21d78980c
[vlc.git] / modules / video_output / msw / direct3d.c
1 /*****************************************************************************
2  * direct3d.c: Windows Direct3D video output module
3  *****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  *$Id$
6  *
7  * Authors: Damien Fouilleul <damienf@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
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.
30  *
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.
34  *
35  *****************************************************************************/
36 #include <errno.h>                                                 /* ENOMEM */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_interface.h>
45 #include <vlc_playlist.h>
46 #include <vlc_vout.h>
47
48 #include <windows.h>
49 #include <d3d9.h>
50
51 #include "vout.h"
52
53 /*****************************************************************************
54  * Local prototypes.
55  *****************************************************************************/
56 static int  OpenVideo  ( vlc_object_t * );
57 static void CloseVideo ( vlc_object_t * );
58
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 * );
64
65 static int Direct3DVoutCreate     ( vout_thread_t * );
66 static void Direct3DVoutRelease   ( vout_thread_t * );
67
68 static int  Direct3DVoutOpen      ( vout_thread_t * );
69 static void Direct3DVoutClose     ( vout_thread_t * );
70
71 static int Direct3DVoutResetDevice( vout_thread_t * );
72
73 static int Direct3DVoutCreatePictures   ( vout_thread_t *, size_t );
74 static void Direct3DVoutReleasePictures ( vout_thread_t * );
75
76 static int Direct3DVoutLockSurface  ( vout_thread_t *, picture_t * );
77 static int Direct3DVoutUnlockSurface( vout_thread_t *, picture_t * );
78
79 static int Direct3DVoutCreateScene      ( vout_thread_t * );
80 static void Direct3DVoutReleaseScene    ( vout_thread_t * );
81 static void Direct3DVoutRenderScene     ( vout_thread_t *, picture_t * );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86
87 static bool IsVistaOrAbove(void)
88 {
89     OSVERSIONINFO winVer;
90     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
91
92     if( GetVersionEx(&winVer) )
93     {
94         if( winVer.dwMajorVersion > 5 )
95         {
96             /* Windows Vista or above, make this module the default */
97             return true;
98         }
99     }
100     /* Windows XP or lower, make sure this module isn't the default */
101     return false;
102 }
103
104 static int OpenVideoXP( vlc_object_t *obj )
105 {
106     return IsVistaOrAbove() ? VLC_EGENERIC : OpenVideo( obj );
107 }
108
109 static int OpenVideoVista( vlc_object_t *obj )
110 {
111     return IsVistaOrAbove() ? OpenVideo( obj ) : VLC_EGENERIC;
112 }
113
114 vlc_module_begin ()
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 )
122
123     /* FIXME: Hack to avoid unregistering our window class */
124     linked_with_a_crap_library_which_uses_atexit ()
125
126     add_submodule()
127         set_capability( "video output", 150 )
128         add_shortcut( "direct3d" )
129         set_callbacks( OpenVideoVista, CloseVideo )
130 vlc_module_end ()
131
132 #if 0 /* FIXME */
133     /* check if we registered a window class because we need to
134      * unregister it */
135     WNDCLASS wndclass;
136     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
137         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
138 #endif
139
140 /*****************************************************************************
141  * CUSTOMVERTEX:
142  *****************************************************************************
143  *****************************************************************************/
144 typedef struct
145 {
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
150 } CUSTOMVERTEX;
151
152 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
153
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 )
160 {
161     vout_thread_t * p_vout = (vout_thread_t *)p_this;
162
163     /* Allocate structure */
164     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
165     if( p_vout->p_sys == NULL )
166         return VLC_ENOMEM;
167
168     if( VLC_SUCCESS != Direct3DVoutCreate( p_vout ) )
169     {
170         msg_Err( p_vout, "Direct3D could not be initialized !");
171         Direct3DVoutRelease( p_vout );
172         free( p_vout->p_sys );
173         return VLC_EGENERIC;
174     }
175
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;
182
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 );
189
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 );
192
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;
197
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 );
200
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;
204
205     if ( CreateEventThread( p_vout ) )
206     {
207         /* Variable to indicate if the window should be on top of others */
208         /* Trigger a callback right now */
209         var_TriggerCallback( p_vout, "video-on-top" );
210
211         DisableScreensaver ( p_vout );
212
213         return VLC_SUCCESS;
214     }
215     else
216     {
217         CloseVideo( VLC_OBJECT(p_vout) );
218         return VLC_EGENERIC;
219     }
220 }
221
222 /*****************************************************************************
223  * CloseVideo: destroy Sys video thread output method
224  *****************************************************************************
225  * Terminate an output method created by Create
226  *****************************************************************************/
227 static void CloseVideo( vlc_object_t *p_this )
228 {
229     vout_thread_t * p_vout = (vout_thread_t *)p_this;
230
231     Direct3DVoutRelease( p_vout );
232
233     StopEventThread( p_vout );
234
235     RestoreScreensaver( p_vout );
236
237     free( p_vout->p_sys );
238     p_vout->p_sys = NULL;
239 }
240
241 /*****************************************************************************
242  * Init: initialize Direct3D video thread output method
243  *****************************************************************************/
244 static int Init( vout_thread_t *p_vout )
245 {
246     int i_ret;
247
248     p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
249
250     /* Initialise Direct3D */
251     if( VLC_SUCCESS != Direct3DVoutOpen( p_vout ) )
252     {
253         msg_Err( p_vout, "cannot initialize Direct3D" );
254         return VLC_EGENERIC;
255     }
256
257     /* Initialize the output structure.
258      * Since Direct3D can do rescaling for us, stick to the default
259      * coordinates and aspect. */
260     p_vout->output.i_width  = p_vout->render.i_width;
261     p_vout->output.i_height = p_vout->render.i_height;
262     p_vout->output.i_aspect = p_vout->render.i_aspect;
263     p_vout->fmt_out = p_vout->fmt_in;
264     UpdateRects( p_vout, true );
265
266     /*  create picture pool */
267     p_vout->output.i_chroma = 0;
268     i_ret = Direct3DVoutCreatePictures(p_vout, 1);
269     if( VLC_SUCCESS != i_ret )
270     {
271         msg_Err(p_vout, "Direct3D picture pool initialization failed !");
272         return i_ret;
273     }
274
275     /* create scene */
276     i_ret = Direct3DVoutCreateScene(p_vout);
277     if( VLC_SUCCESS != i_ret )
278     {
279         msg_Err(p_vout, "Direct3D scene initialization failed !");
280         Direct3DVoutReleasePictures(p_vout);
281         return i_ret;
282     }
283
284     /* Change the window title bar text */
285     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
286
287     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
288     return VLC_SUCCESS;
289 }
290
291 /*****************************************************************************
292  * End: terminate Sys video thread output method
293  *****************************************************************************
294  * Terminate an output method created by Create.
295  * It is called at the end of the thread.
296  *****************************************************************************/
297 static void End( vout_thread_t *p_vout )
298 {
299     Direct3DVoutReleaseScene(p_vout);
300     Direct3DVoutReleasePictures(p_vout);
301     Direct3DVoutClose( p_vout );
302 }
303
304 /*****************************************************************************
305  * Manage: handle Sys events
306  *****************************************************************************
307  * This function should be called regularly by the video output thread.
308  * It returns a non null value if an error occurred.
309  *****************************************************************************/
310 static int Manage( vout_thread_t *p_vout )
311 {
312     /* If we do not control our window, we check for geometry changes
313      * ourselves because the parent might not send us its events. */
314     vlc_mutex_lock( &p_vout->p_sys->lock );
315     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
316     {
317         RECT rect_parent;
318         POINT point;
319
320         vlc_mutex_unlock( &p_vout->p_sys->lock );
321
322         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
323         point.x = point.y = 0;
324         ClientToScreen( p_vout->p_sys->hparent, &point );
325         OffsetRect( &rect_parent, point.x, point.y );
326
327         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
328         {
329             p_vout->p_sys->rect_parent = rect_parent;
330
331             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
332                           rect_parent.right - rect_parent.left,
333                           rect_parent.bottom - rect_parent.top,
334                           SWP_NOZORDER );
335         }
336     }
337     else
338     {
339         vlc_mutex_unlock( &p_vout->p_sys->lock );
340     }
341
342     /*
343      * Position Change
344      */
345     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
346     {
347 #if 0 /* need that when bicubic filter is available */
348         RECT rect;
349         UINT width, height;
350
351         GetClientRect(p_vout->p_sys->hvideownd, &rect);
352         width  = rect.right-rect.left;
353         height = rect.bottom-rect.top;
354
355         if( (width != p_vout->p_sys->d3dpp.BackBufferWidth)
356          || (height != p_vout->p_sys->d3dpp.BackBufferHeight) )
357         {
358             msg_Dbg(p_vout, "resizing device back buffers to (%lux%lu)", width, height);
359             // need to reset D3D device to resize back buffer
360             if( VLC_SUCCESS != Direct3DVoutResetDevice(p_vout, width, height) )
361                 return VLC_EGENERIC;
362         }
363 #endif
364         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
365     }
366
367     /* autoscale toggle */
368     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
369     {
370         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
371
372         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
373         p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
374
375         UpdateRects( p_vout, true );
376     }
377
378     /* scaling factor */
379     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
380     {
381         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
382
383         p_vout->b_autoscale = false;
384         p_vout->i_zoom =
385             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
386         UpdateRects( p_vout, true );
387     }
388
389     /* Check for cropping / aspect changes */
390     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
391         p_vout->i_changes & VOUT_ASPECT_CHANGE )
392     {
393         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
394         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
395
396         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
397         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
398         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
399         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
400         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
401         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
402         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
403         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
404         UpdateRects( p_vout, true );
405     }
406
407     /* We used to call the Win32 PeekMessage function here to read the window
408      * messages. But since window can stay blocked into this function for a
409      * long time (for example when you move your window on the screen), I
410      * decided to isolate PeekMessage in another thread. */
411
412     /*
413      * Fullscreen change
414      */
415     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
416         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
417     {
418         Win32ToggleFullscreen( p_vout );
419
420         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
421         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
422     }
423
424     /*
425      * Pointer change
426      */
427     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
428         (mdate() - p_vout->p_sys->i_lastmoved) >
429             p_vout->p_sys->i_mouse_hide_timeout )
430     {
431         POINT point;
432         HWND hwnd;
433
434         /* Hide the cursor only if it is inside our window */
435         GetCursorPos( &point );
436         hwnd = WindowFromPoint(point);
437         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
438         {
439             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
440         }
441         else
442         {
443             p_vout->p_sys->i_lastmoved = mdate();
444         }
445     }
446
447     /*
448      * "Always on top" status change
449      */
450     if( p_vout->p_sys->b_on_top_change )
451     {
452         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
453         bool b = var_GetBool( p_vout, "video-on-top" );
454
455         /* Set the window on top if necessary */
456         if( b && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
457                            & WS_EX_TOPMOST ) )
458         {
459             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
460                            MF_BYCOMMAND | MFS_CHECKED );
461             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
462                           SWP_NOSIZE | SWP_NOMOVE );
463         }
464         else
465         /* The window shouldn't be on top */
466         if( !b && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
467                            & WS_EX_TOPMOST ) )
468         {
469             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
470                            MF_BYCOMMAND | MFS_UNCHECKED );
471             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
472                           SWP_NOSIZE | SWP_NOMOVE );
473         }
474
475         p_vout->p_sys->b_on_top_change = false;
476     }
477
478     /* Check if the event thread is still running */
479     if( !vlc_object_alive (p_vout->p_sys->p_event) )
480     {
481         return VLC_EGENERIC; /* exit */
482     }
483
484     return VLC_SUCCESS;
485 }
486
487 /*****************************************************************************
488  * Display: displays previously rendered output
489  *****************************************************************************
490  * This function sends the currently rendered image to the display, wait until
491  * it is displayed and switch the two rendering buffers, preparing next frame.
492  *****************************************************************************/
493 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
494 {
495     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
496     // Present the back buffer contents to the display
497     // stretching and filtering happens here
498     HRESULT hr = IDirect3DDevice9_Present(p_d3ddev,
499                     &(p_vout->p_sys->rect_src_clipped),
500                     NULL, NULL, NULL);
501     if( FAILED(hr) )
502         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
503 }
504
505 /*
506 ** this function is only used once when the first picture is received
507 ** this function will show the video window once a picture is ready
508 */
509
510 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
511 {
512     /* get initial picture presented through D3D */
513     Display(p_vout, p_pic);
514
515     /*
516     ** Video window is initially hidden, show it now since we got a
517     ** picture to show.
518     */
519     SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
520         SWP_ASYNCWINDOWPOS|
521         SWP_FRAMECHANGED|
522         SWP_SHOWWINDOW|
523         SWP_NOMOVE|
524         SWP_NOSIZE|
525         SWP_NOZORDER );
526
527     /* use and restores proper display function for further pictures */
528     p_vout->pf_display = Display;
529 }
530
531 /*****************************************************************************
532  * DirectD3DVoutCreate: Initialize and instance of Direct3D9
533  *****************************************************************************
534  * This function initialize Direct3D and analyze available resources from
535  * default adapter.
536  *****************************************************************************/
537 static int Direct3DVoutCreate( vout_thread_t *p_vout )
538 {
539     HRESULT hr;
540     LPDIRECT3D9 p_d3dobj;
541     D3DCAPS9 d3dCaps;
542
543     LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
544
545     p_vout->p_sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
546     if( NULL == p_vout->p_sys->hd3d9_dll )
547     {
548         msg_Warn( p_vout, "cannot load d3d9.dll, aborting" );
549         return VLC_EGENERIC;
550     }
551
552     OurDirect3DCreate9 =
553       (void *)GetProcAddress( p_vout->p_sys->hd3d9_dll,
554                               TEXT("Direct3DCreate9") );
555     if( OurDirect3DCreate9 == NULL )
556     {
557         msg_Err( p_vout, "Cannot locate reference to Direct3DCreate9 ABI in DLL" );
558         return VLC_EGENERIC;
559     }
560
561     /* Create the D3D object. */
562     p_d3dobj = OurDirect3DCreate9( D3D_SDK_VERSION );
563     if( NULL == p_d3dobj )
564     {
565        msg_Err( p_vout, "Could not create Direct3D9 instance.");
566        return VLC_EGENERIC;
567     }
568     p_vout->p_sys->p_d3dobj = p_d3dobj;
569
570     /*
571     ** Get device capabilities
572     */
573     ZeroMemory(&d3dCaps, sizeof(d3dCaps));
574     hr = IDirect3D9_GetDeviceCaps(p_d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
575     if( FAILED(hr) )
576     {
577        msg_Err( p_vout, "Could not read adapter capabilities. (hr=0x%lX)", hr);
578        return VLC_EGENERIC;
579     }
580     /* TODO: need to test device capabilities and select the right render function */
581
582     return VLC_SUCCESS;
583 }
584
585 /*****************************************************************************
586  * DirectD3DVoutRelease: release an instance of Direct3D9
587  *****************************************************************************/
588
589 static void Direct3DVoutRelease( vout_thread_t *p_vout )
590 {
591     if( p_vout->p_sys->p_d3dobj )
592     {
593        IDirect3D9_Release(p_vout->p_sys->p_d3dobj);
594        p_vout->p_sys->p_d3dobj = NULL;
595     }
596     if( NULL != p_vout->p_sys->hd3d9_dll )
597     {
598         FreeLibrary(p_vout->p_sys->hd3d9_dll);
599         p_vout->p_sys->hd3d9_dll = NULL;
600     }
601 }
602
603 static int Direct3DFillPresentationParameters(vout_thread_t *p_vout, D3DPRESENT_PARAMETERS *d3dpp)
604 {
605     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
606     D3DDISPLAYMODE d3ddm;
607     HRESULT hr;
608
609     /*
610     ** Get the current desktop display mode, so we can set up a back
611     ** buffer of the same format
612     */
613     hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
614     if( FAILED(hr))
615     {
616        msg_Err( p_vout, "Could not read adapter display mode. (hr=0x%lX)", hr);
617        return VLC_EGENERIC;
618     }
619
620     /* keep a copy of current desktop format */
621     p_vout->p_sys->bbFormat = d3ddm.Format;
622
623     /* Set up the structure used to create the D3DDevice. */
624     ZeroMemory( d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
625     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
626     d3dpp->Windowed               = TRUE;
627     d3dpp->hDeviceWindow          = p_vout->p_sys->hvideownd;
628     d3dpp->BackBufferWidth        = p_vout->output.i_width;
629     d3dpp->BackBufferHeight       = p_vout->output.i_height;
630     d3dpp->SwapEffect             = D3DSWAPEFFECT_COPY;
631     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
632     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
633     d3dpp->BackBufferFormat       = d3ddm.Format;
634     d3dpp->BackBufferCount        = 1;
635     d3dpp->EnableAutoDepthStencil = FALSE;
636
637     return VLC_SUCCESS;
638 }
639
640 /*****************************************************************************
641  * DirectD3DVoutOpen: Takes care of all the Direct3D9 initialisations
642  *****************************************************************************
643  * This function creates Direct3D device
644  * this must be called from the vout thread for performance reason, as
645  * all Direct3D Device APIs are used in a non multithread safe environment
646  *****************************************************************************/
647 static int Direct3DVoutOpen( vout_thread_t *p_vout )
648 {
649     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
650     LPDIRECT3DDEVICE9 p_d3ddev;
651     D3DPRESENT_PARAMETERS d3dpp;
652     HRESULT hr;
653
654     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
655         return VLC_EGENERIC;
656
657     // Create the D3DDevice
658     hr = IDirect3D9_CreateDevice(p_d3dobj, D3DADAPTER_DEFAULT,
659                                  D3DDEVTYPE_HAL, p_vout->p_sys->hvideownd,
660                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING|
661                                  D3DCREATE_MULTITHREADED,
662                                  &d3dpp, &p_d3ddev );
663     if( FAILED(hr) )
664     {
665        msg_Err(p_vout, "Could not create the D3D device! (hr=0x%lX)", hr);
666        return VLC_EGENERIC;
667     }
668     p_vout->p_sys->p_d3ddev = p_d3ddev;
669
670     msg_Dbg( p_vout, "Direct3D device adapter successfully initialized" );
671     return VLC_SUCCESS;
672 }
673
674 /*****************************************************************************
675  * DirectD3DClose: release the Direct3D9 device
676  *****************************************************************************/
677 static void Direct3DVoutClose( vout_thread_t *p_vout )
678 {
679     if( p_vout->p_sys->p_d3ddev )
680     {
681        IDirect3DDevice9_Release(p_vout->p_sys->p_d3ddev);
682        p_vout->p_sys->p_d3ddev = NULL;
683     }
684
685     p_vout->p_sys->hmonitor = NULL;
686 }
687
688 /*****************************************************************************
689  * DirectD3DClose: reset the Direct3D9 device
690  *****************************************************************************
691  * All resources must be deallocated before the reset occur, they will be
692  * realllocated once the reset has been performed successfully
693  *****************************************************************************/
694 static int Direct3DVoutResetDevice( vout_thread_t *p_vout )
695 {
696     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
697     D3DPRESENT_PARAMETERS   d3dpp;
698     HRESULT hr;
699
700     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
701         return VLC_EGENERIC;
702
703     // release all D3D objects
704     Direct3DVoutReleaseScene( p_vout );
705     Direct3DVoutReleasePictures( p_vout );
706
707     hr = IDirect3DDevice9_Reset(p_d3ddev, &d3dpp);
708     if( SUCCEEDED(hr) )
709     {
710         // re-create them
711         if( (VLC_SUCCESS != Direct3DVoutCreatePictures(p_vout, 1))
712          || (VLC_SUCCESS != Direct3DVoutCreateScene(p_vout)) )
713         {
714             msg_Dbg(p_vout, "%s failed !", __FUNCTION__);
715             return VLC_EGENERIC;
716         }
717     }
718     else {
719         msg_Err(p_vout, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
720         return VLC_EGENERIC;
721     }
722     return VLC_SUCCESS;
723 }
724
725 static D3DFORMAT Direct3DVoutSelectFormat( vout_thread_t *p_vout, D3DFORMAT target,
726     const D3DFORMAT *formats, size_t count)
727 {
728     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
729     size_t c;
730
731     for( c=0; c<count; ++c )
732     {
733         HRESULT hr;
734         D3DFORMAT format = formats[c];
735         /* test whether device can create a surface of that format */
736         hr = IDirect3D9_CheckDeviceFormat(p_d3dobj, D3DADAPTER_DEFAULT,
737                 D3DDEVTYPE_HAL, target, 0, D3DRTYPE_SURFACE, format);
738         if( SUCCEEDED(hr) )
739         {
740             /* test whether device can perform color-conversion
741             ** from that format to target format
742             */
743             hr = IDirect3D9_CheckDeviceFormatConversion(p_d3dobj,
744                     D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
745                     format, target);
746         }
747         if( SUCCEEDED(hr) )
748         {
749             // found a compatible format
750             switch( format )
751             {
752                 case D3DFMT_UYVY:
753                     msg_Dbg( p_vout, "selected surface pixel format is UYVY");
754                     break;
755                 case D3DFMT_YUY2:
756                     msg_Dbg( p_vout, "selected surface pixel format is YUY2");
757                     break;
758                 case D3DFMT_X8R8G8B8:
759                     msg_Dbg( p_vout, "selected surface pixel format is X8R8G8B8");
760                     break;
761                 case D3DFMT_A8R8G8B8:
762                     msg_Dbg( p_vout, "selected surface pixel format is A8R8G8B8");
763                     break;
764                 case D3DFMT_R8G8B8:
765                     msg_Dbg( p_vout, "selected surface pixel format is R8G8B8");
766                     break;
767                 case D3DFMT_R5G6B5:
768                     msg_Dbg( p_vout, "selected surface pixel format is R5G6B5");
769                     break;
770                 case D3DFMT_X1R5G5B5:
771                     msg_Dbg( p_vout, "selected surface pixel format is X1R5G5B5");
772                     break;
773                 default:
774                     msg_Dbg( p_vout, "selected surface pixel format is 0x%0X", format);
775                     break;
776             }
777             return format;
778         }
779         else if( D3DERR_NOTAVAILABLE != hr )
780         {
781             msg_Err( p_vout, "Could not query adapter supported formats. (hr=0x%lX)", hr);
782             break;
783         }
784     }
785     return D3DFMT_UNKNOWN;
786 }
787
788 static D3DFORMAT Direct3DVoutFindFormat(vout_thread_t *p_vout, int i_chroma, D3DFORMAT target)
789 {
790     //if( p_vout->p_sys->b_hw_yuv && ! _got_vista_or_above )
791     if( p_vout->p_sys->b_hw_yuv )
792     {
793     /* it sounds like vista does not support YUV surfaces at all */
794         switch( i_chroma )
795         {
796             case VLC_CODEC_UYVY:
797             {
798                 static const D3DFORMAT formats[] =
799                     { D3DFMT_UYVY, D3DFMT_YUY2, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
800                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
801             }
802             case VLC_CODEC_I420:
803             case VLC_CODEC_I422:
804             case VLC_CODEC_YV12:
805             {
806                 /* typically 3D textures don't support planar format
807                 ** fallback to packed version and use CPU for the conversion
808                 */
809                 static const D3DFORMAT formats[] =
810                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
811                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
812             }
813             case VLC_CODEC_YUYV:
814             {
815                 static const D3DFORMAT formats[] =
816                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
817                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
818             }
819         }
820     }
821
822     switch( i_chroma )
823     {
824         case VLC_CODEC_RGB15:
825         {
826             static const D3DFORMAT formats[] =
827                 { D3DFMT_X1R5G5B5 };
828             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
829         }
830         case VLC_CODEC_RGB16:
831         {
832             static const D3DFORMAT formats[] =
833                 { D3DFMT_R5G6B5 };
834             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
835         }
836         case VLC_CODEC_RGB24:
837         {
838             static const D3DFORMAT formats[] =
839                 { D3DFMT_R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8 };
840             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
841         }
842         case VLC_CODEC_RGB32:
843         {
844             static const D3DFORMAT formats[] =
845                 { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8 };
846             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
847         }
848         default:
849         {
850             /* use display default format */
851             LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
852             D3DDISPLAYMODE d3ddm;
853
854             HRESULT hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
855             if( SUCCEEDED(hr))
856             {
857                 /*
858                 ** some professional cards could use some advanced pixel format as default,
859                 ** make sure we stick with chromas that we can handle internally
860                 */
861                 switch( d3ddm.Format )
862                 {
863                     case D3DFMT_R8G8B8:
864                     case D3DFMT_X8R8G8B8:
865                     case D3DFMT_A8R8G8B8:
866                     case D3DFMT_R5G6B5:
867                     case D3DFMT_X1R5G5B5:
868                         msg_Dbg( p_vout, "defaulting to adapter pixel format");
869                         return Direct3DVoutSelectFormat(p_vout, target, &d3ddm.Format, 1);
870                     default:
871                     {
872                         /* if we fall here, that probably means that we need to render some YUV format */
873                         static const D3DFORMAT formats[] =
874                             { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
875                         msg_Dbg( p_vout, "defaulting to built-in pixel format");
876                         return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
877                     }
878                 }
879             }
880         }
881     }
882     return D3DFMT_UNKNOWN;
883 }
884
885 static int Direct3DVoutSetOutputFormat(vout_thread_t *p_vout, D3DFORMAT format)
886 {
887     switch( format )
888     {
889         case D3DFMT_YUY2:
890             p_vout->output.i_chroma = VLC_CODEC_YUYV;
891             break;
892         case D3DFMT_UYVY:
893             p_vout->output.i_chroma = VLC_CODEC_UYVY;
894             break;
895         case D3DFMT_R8G8B8:
896             p_vout->output.i_chroma = VLC_CODEC_RGB24;
897             p_vout->output.i_rmask = 0xff0000;
898             p_vout->output.i_gmask = 0x00ff00;
899             p_vout->output.i_bmask = 0x0000ff;
900             break;
901         case D3DFMT_X8R8G8B8:
902         case D3DFMT_A8R8G8B8:
903             p_vout->output.i_chroma = VLC_CODEC_RGB32;
904             p_vout->output.i_rmask = 0x00ff0000;
905             p_vout->output.i_gmask = 0x0000ff00;
906             p_vout->output.i_bmask = 0x000000ff;
907             break;
908         case D3DFMT_R5G6B5:
909             p_vout->output.i_chroma = VLC_CODEC_RGB16;
910             p_vout->output.i_rmask = (0x1fL)<<11;
911             p_vout->output.i_gmask = (0x3fL)<<5;
912             p_vout->output.i_bmask = (0x1fL)<<0;
913             break;
914         case D3DFMT_X1R5G5B5:
915             p_vout->output.i_chroma = VLC_CODEC_RGB15;
916             p_vout->output.i_rmask = (0x1fL)<<10;
917             p_vout->output.i_gmask = (0x1fL)<<5;
918             p_vout->output.i_bmask = (0x1fL)<<0;
919             break;
920         default:
921             return VLC_EGENERIC;
922     }
923     return VLC_SUCCESS;
924 }
925
926 /*****************************************************************************
927  * Direct3DVoutCreatePictures: allocate a vector of identical pictures
928  *****************************************************************************
929  * Each picture has an associated offscreen surface in video memory
930  * depending on hardware capabilities the picture chroma will be as close
931  * as possible to the orginal render chroma to reduce CPU conversion overhead
932  * and delegate this work to video card GPU
933  *****************************************************************************/
934 static int Direct3DVoutCreatePictures( vout_thread_t *p_vout, size_t i_num_pics )
935 {
936     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
937     D3DFORMAT               format;
938     HRESULT hr;
939     size_t c;
940     // if vout is already running, use current chroma, otherwise choose from upstream
941     int i_chroma = p_vout->output.i_chroma ? p_vout->output.i_chroma
942                                            : p_vout->render.i_chroma;
943
944     I_OUTPUTPICTURES = 0;
945
946     /*
947     ** find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
948     ** the requested chroma which is usable by the hardware in an offscreen surface, as they
949     ** typically support more formats than textures
950     */
951     format = Direct3DVoutFindFormat(p_vout, i_chroma, p_vout->p_sys->bbFormat);
952     if( VLC_SUCCESS != Direct3DVoutSetOutputFormat(p_vout, format) )
953     {
954         msg_Err(p_vout, "surface pixel format is not supported.");
955         return VLC_EGENERIC;
956     }
957
958     for( c=0; c<i_num_pics; )
959     {
960
961         LPDIRECT3DSURFACE9 p_d3dsurf;
962         picture_t *p_pic = p_vout->p_picture+c;
963
964         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(p_d3ddev,
965                 p_vout->render.i_width,
966                 p_vout->render.i_height,
967                 format,
968                 D3DPOOL_DEFAULT,
969                 &p_d3dsurf,
970                 NULL);
971         if( FAILED(hr) )
972         {
973             msg_Err(p_vout, "Failed to create picture surface. (hr=0x%lx)", hr);
974             Direct3DVoutReleasePictures(p_vout);
975             return VLC_EGENERIC;
976         }
977
978         /* fill surface with black color */
979         IDirect3DDevice9_ColorFill(p_d3ddev, p_d3dsurf, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) );
980
981         /* assign surface to internal structure */
982         p_pic->p_sys = (void *)p_d3dsurf;
983
984         /* Now that we've got our direct-buffer, we can finish filling in the
985          * picture_t structures */
986         switch( p_vout->output.i_chroma )
987         {
988             case VLC_CODEC_RGB8:
989                 p_pic->p->i_lines = p_vout->output.i_height;
990                 p_pic->p->i_visible_lines = p_vout->output.i_height;
991                 p_pic->p->i_pixel_pitch = 1;
992                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
993                     p_pic->p->i_pixel_pitch;
994                 p_pic->i_planes = 1;
995             break;
996             case VLC_CODEC_RGB15:
997             case VLC_CODEC_RGB16:
998                 p_pic->p->i_lines = p_vout->output.i_height;
999                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1000                 p_pic->p->i_pixel_pitch = 2;
1001                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1002                     p_pic->p->i_pixel_pitch;
1003                 p_pic->i_planes = 1;
1004             break;
1005             case VLC_CODEC_RGB24:
1006                 p_pic->p->i_lines = p_vout->output.i_height;
1007                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1008                 p_pic->p->i_pixel_pitch = 3;
1009                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1010                     p_pic->p->i_pixel_pitch;
1011                 p_pic->i_planes = 1;
1012             break;
1013             case VLC_CODEC_RGB32:
1014                 p_pic->p->i_lines = p_vout->output.i_height;
1015                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1016                 p_pic->p->i_pixel_pitch = 4;
1017                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1018                     p_pic->p->i_pixel_pitch;
1019                 p_pic->i_planes = 1;
1020                 break;
1021             case VLC_CODEC_UYVY:
1022             case VLC_CODEC_YUYV:
1023                 p_pic->p->i_lines = p_vout->output.i_height;
1024                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1025                 p_pic->p->i_pixel_pitch = 2;
1026                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1027                     p_pic->p->i_pixel_pitch;
1028                 p_pic->i_planes = 1;
1029                 break;
1030             default:
1031                 Direct3DVoutReleasePictures(p_vout);
1032                 return VLC_EGENERIC;
1033         }
1034         p_pic->i_status = DESTROYED_PICTURE;
1035         p_pic->i_type   = DIRECT_PICTURE;
1036         p_pic->b_slow   = true;
1037         p_pic->pf_lock  = Direct3DVoutLockSurface;
1038         p_pic->pf_unlock = Direct3DVoutUnlockSurface;
1039         PP_OUTPUTPICTURE[c] = p_pic;
1040
1041         I_OUTPUTPICTURES = ++c;
1042     }
1043
1044     msg_Dbg( p_vout, "%u Direct3D pictures created successfully", c );
1045
1046     return VLC_SUCCESS;
1047 }
1048
1049 /*****************************************************************************
1050  * Direct3DVoutReleasePictures: destroy a picture vector
1051  *****************************************************************************
1052  * release all video resources used for pictures
1053  *****************************************************************************/
1054 static void Direct3DVoutReleasePictures( vout_thread_t *p_vout)
1055 {
1056     size_t i_num_pics = I_OUTPUTPICTURES;
1057     size_t c;
1058     for( c=0; c<i_num_pics; ++c )
1059     {
1060         picture_t *p_pic = p_vout->p_picture+c;
1061         if( p_pic->p_sys )
1062         {
1063             LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1064
1065             p_pic->p_sys = NULL;
1066
1067             if( p_d3dsurf )
1068             {
1069                 IDirect3DSurface9_Release(p_d3dsurf);
1070             }
1071         }
1072     }
1073     msg_Dbg( p_vout, "%u Direct3D pictures released.", c);
1074
1075     I_OUTPUTPICTURES = 0;
1076 }
1077
1078 /*****************************************************************************
1079  * Direct3DVoutLockSurface: Lock surface and get picture data pointer
1080  *****************************************************************************
1081  * This function locks a surface and get the surface descriptor which amongst
1082  * other things has the pointer to the picture data.
1083  *****************************************************************************/
1084 static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1085 {
1086     HRESULT hr;
1087     D3DLOCKED_RECT d3drect;
1088     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1089
1090     if( NULL == p_d3dsurf )
1091         return VLC_EGENERIC;
1092
1093     /* Lock the surface to get a valid pointer to the picture buffer */
1094     hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0);
1095     if( FAILED(hr) )
1096     {
1097         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1098         return VLC_EGENERIC;
1099     }
1100
1101     /* fill in buffer info in first plane */
1102     p_pic->p->p_pixels = d3drect.pBits;
1103     p_pic->p->i_pitch  = d3drect.Pitch;
1104
1105     return VLC_SUCCESS;
1106 }
1107
1108 /*****************************************************************************
1109  * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface().
1110  *****************************************************************************/
1111 static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1112 {
1113     HRESULT hr;
1114     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1115
1116     if( NULL == p_d3dsurf )
1117         return VLC_EGENERIC;
1118
1119     /* Unlock the Surface */
1120     hr = IDirect3DSurface9_UnlockRect(p_d3dsurf);
1121     if( FAILED(hr) )
1122     {
1123         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1124         return VLC_EGENERIC;
1125     }
1126     return VLC_SUCCESS;
1127 }
1128
1129 /*****************************************************************************
1130  * Direct3DVoutCreateScene: allocate and initialize a 3D scene
1131  *****************************************************************************
1132  * for advanced blending/filtering a texture needs be used in a 3D scene.
1133  *****************************************************************************/
1134
1135 static int Direct3DVoutCreateScene( vout_thread_t *p_vout )
1136 {
1137     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1138     LPDIRECT3DTEXTURE9      p_d3dtex;
1139     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1140
1141     HRESULT hr;
1142
1143     /*
1144     ** Create a texture for use when rendering a scene
1145     ** for performance reason, texture format is identical to backbuffer
1146     ** which would usually be a RGB format
1147     */
1148     hr = IDirect3DDevice9_CreateTexture(p_d3ddev,
1149             p_vout->render.i_width,
1150             p_vout->render.i_height,
1151             1,
1152             D3DUSAGE_RENDERTARGET,
1153             p_vout->p_sys->bbFormat,
1154             D3DPOOL_DEFAULT,
1155             &p_d3dtex,
1156             NULL);
1157     if( FAILED(hr))
1158     {
1159         msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr);
1160         return VLC_EGENERIC;
1161     }
1162
1163     /*
1164     ** Create a vertex buffer for use when rendering scene
1165     */
1166     hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev,
1167             sizeof(CUSTOMVERTEX)*4,
1168             D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1169             D3DFVF_CUSTOMVERTEX,
1170             D3DPOOL_DEFAULT,
1171             &p_d3dvtc,
1172             NULL);
1173     if( FAILED(hr) )
1174     {
1175         msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1176             IDirect3DTexture9_Release(p_d3dtex);
1177         return VLC_EGENERIC;
1178     }
1179
1180     p_vout->p_sys->p_d3dtex = p_d3dtex;
1181     p_vout->p_sys->p_d3dvtc = p_d3dvtc;
1182
1183     // Texture coordinates outside the range [0.0, 1.0] are set
1184     // to the texture color at 0.0 or 1.0, respectively.
1185     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1186     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1187
1188     // Set linear filtering quality
1189     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1190     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1191
1192     // set maximum ambient light
1193     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1194
1195     // Turn off culling
1196     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1197
1198     // Turn off the zbuffer
1199     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1200
1201     // Turn off lights
1202     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE);
1203
1204     // Enable dithering
1205     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE);
1206
1207     // disable stencil
1208     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE);
1209
1210     // manage blending
1211     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1212     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1213     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1214     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1215     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10);
1216     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1217
1218     // Set texture states
1219     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
1220     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1221     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1222
1223     // turn off alpha operation
1224     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1225
1226     msg_Dbg( p_vout, "Direct3D scene created successfully");
1227
1228     return VLC_SUCCESS;
1229 }
1230
1231 /*****************************************************************************
1232  * Direct3DVoutReleaseScene
1233  *****************************************************************************/
1234 static void Direct3DVoutReleaseScene( vout_thread_t *p_vout )
1235 {
1236     LPDIRECT3DTEXTURE9      p_d3dtex = p_vout->p_sys->p_d3dtex;
1237     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1238
1239     if( p_d3dvtc )
1240     {
1241         IDirect3DVertexBuffer9_Release(p_d3dvtc);
1242         p_vout->p_sys->p_d3dvtc = NULL;
1243     }
1244
1245     if( p_d3dtex )
1246     {
1247         IDirect3DTexture9_Release(p_d3dtex);
1248         p_vout->p_sys->p_d3dtex = NULL;
1249     }
1250     msg_Dbg( p_vout, "Direct3D scene released successfully");
1251 }
1252
1253 /*****************************************************************************
1254  * Render: copy picture surface into a texture and render into a scene
1255  *****************************************************************************
1256  * This function is intented for higher end 3D cards, with pixel shader support
1257  * and at least 64 MB of video RAM.
1258  *****************************************************************************/
1259 static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic )
1260 {
1261     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1262     LPDIRECT3DTEXTURE9      p_d3dtex;
1263     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1264     LPDIRECT3DSURFACE9      p_d3dsrc, p_d3ddest;
1265     CUSTOMVERTEX            *p_vertices;
1266     HRESULT hr;
1267     float f_width, f_height;
1268
1269     // check if device is still available
1270     hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev);
1271     if( FAILED(hr) )
1272     {
1273         if( (D3DERR_DEVICENOTRESET != hr)
1274          || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) )
1275         {
1276             // device is not usable at present (lost device, out of video mem ?)
1277             return;
1278         }
1279     }
1280     p_d3dtex  = p_vout->p_sys->p_d3dtex;
1281     p_d3dvtc  = p_vout->p_sys->p_d3dvtc;
1282
1283     /* Clear the backbuffer and the zbuffer */
1284     hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET,
1285                               D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
1286     if( FAILED(hr) )
1287     {
1288         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1289         return;
1290     }
1291
1292     /*  retrieve picture surface */
1293     p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1294     if( NULL == p_d3dsrc )
1295     {
1296         msg_Dbg( p_vout, "no surface to render ?");
1297         return;
1298     }
1299
1300     /* retrieve texture top-level surface */
1301     hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest);
1302     if( FAILED(hr) )
1303     {
1304         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1305         return;
1306     }
1307
1308     /* Copy picture surface into texture surface, color space conversion happens here */
1309     hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE);
1310     IDirect3DSurface9_Release(p_d3ddest);
1311     if( FAILED(hr) )
1312     {
1313         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1314         return;
1315     }
1316
1317     /* Update the vertex buffer */
1318     hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD);
1319     if( FAILED(hr) )
1320     {
1321         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1322         return;
1323     }
1324
1325     /* Setup vertices */
1326     f_width  = (float)(p_vout->output.i_width);
1327     f_height = (float)(p_vout->output.i_height);
1328
1329     /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
1330     /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
1331     p_vertices[0].x       = -0.5f;       // left
1332     p_vertices[0].y       = -0.5f;       // top
1333     p_vertices[0].z       = 0.0f;
1334     p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1335     p_vertices[0].rhw     = 1.0f;
1336     p_vertices[0].tu      = 0.0f;
1337     p_vertices[0].tv      = 0.0f;
1338
1339     p_vertices[1].x       = f_width - 0.5f;    // right
1340     p_vertices[1].y       = -0.5f;       // top
1341     p_vertices[1].z       = 0.0f;
1342     p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1343     p_vertices[1].rhw     = 1.0f;
1344     p_vertices[1].tu      = 1.0f;
1345     p_vertices[1].tv      = 0.0f;
1346
1347     p_vertices[2].x       = f_width - 0.5f;    // right
1348     p_vertices[2].y       = f_height - 0.5f;   // bottom
1349     p_vertices[2].z       = 0.0f;
1350     p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1351     p_vertices[2].rhw     = 1.0f;
1352     p_vertices[2].tu      = 1.0f;
1353     p_vertices[2].tv      = 1.0f;
1354
1355     p_vertices[3].x       = -0.5f;       // left
1356     p_vertices[3].y       = f_height - 0.5f;   // bottom
1357     p_vertices[3].z       = 0.0f;
1358     p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1359     p_vertices[3].rhw     = 1.0f;
1360     p_vertices[3].tu      = 0.0f;
1361     p_vertices[3].tv      = 1.0f;
1362
1363     hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc);
1364     if( FAILED(hr) )
1365     {
1366         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1367         return;
1368     }
1369
1370     // Begin the scene
1371     hr = IDirect3DDevice9_BeginScene(p_d3ddev);
1372     if( FAILED(hr) )
1373     {
1374         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1375         return;
1376     }
1377
1378     // Setup our texture. Using textures introduces the texture stage states,
1379     // which govern how textures get blended together (in the case of multiple
1380     // textures) and lighting information. In this case, we are modulating
1381     // (blending) our texture with the diffuse color of the vertices.
1382     hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex);
1383     if( FAILED(hr) )
1384     {
1385         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1386         IDirect3DDevice9_EndScene(p_d3ddev);
1387         return;
1388     }
1389
1390     // Render the vertex buffer contents
1391     hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX));
1392     if( FAILED(hr) )
1393     {
1394         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1395         IDirect3DDevice9_EndScene(p_d3ddev);
1396         return;
1397     }
1398
1399     // we use FVF instead of vertex shader
1400     hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX);
1401     if( FAILED(hr) )
1402     {
1403         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1404         IDirect3DDevice9_EndScene(p_d3ddev);
1405         return;
1406     }
1407
1408     // draw rectangle
1409     hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1410     if( FAILED(hr) )
1411     {
1412         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1413         IDirect3DDevice9_EndScene(p_d3ddev);
1414         return;
1415     }
1416
1417     // End the scene
1418     hr = IDirect3DDevice9_EndScene(p_d3ddev);
1419     if( FAILED(hr) )
1420     {
1421         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1422         return;
1423     }
1424 }
1425