ddraw: Rewrite most of ddraw using WineD3D.
[wine] / dlls / ddraw / ddraw_private.h
1 /*
2  * Copyright 2006 Stefan Dösinger
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #ifndef __WINE_DLLS_DDRAW_DDRAW_PRIVATE_H
20 #define __WINE_DLLS_DDRAW_DDRAW_PRIVATE_H
21
22 /* MAY NOT CONTAIN X11 or DGA specific includes/defines/structs! */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wtypes.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "ddraw.h"
33 #include "ddrawi.h"
34 #include "d3d.h"
35
36 #include "ddcomimpl.h"
37
38 #include "wine/wined3d_interface.h"
39
40 /*****************************************************************************
41  * IParent - a helper interface
42  *****************************************************************************/
43 DEFINE_GUID(IID_IParent, 0xc20e4c88, 0x74e7, 0x4940, 0xba, 0x9f, 0x2e, 0x32, 0x3f, 0x9d, 0xc9, 0x81);
44 typedef struct IParent *LPPARENT, *PPARENT;
45
46 #define INTERFACE IParent
47 DECLARE_INTERFACE_(IParent,IUnknown)
48 {
49     /*** IUnknown methods ***/
50     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID riid, void** ppvObject) PURE;
51     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
52     STDMETHOD_(ULONG,Release)(THIS) PURE;
53 };
54 #undef INTERFACE
55
56 #if !defined(__cplusplus) || defined(CINTERFACE)
57 /*** IUnknown methods ***/
58 #define IParent_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)
59 #define IParent_AddRef(p)             (p)->lpVtbl->AddRef(p)
60 #define IParent_Release(p)            (p)->lpVtbl->Release(p)
61 #endif
62
63
64 /* Typdef the interfaces */
65 typedef struct IDirectDrawImpl            IDirectDrawImpl;
66 typedef struct IDirectDrawSurfaceImpl     IDirectDrawSurfaceImpl;
67 typedef struct IDirectDrawClipperImpl     IDirectDrawClipperImpl;
68 typedef struct IDirectDrawPaletteImpl     IDirectDrawPaletteImpl;
69 typedef struct IDirect3DDeviceImpl        IDirect3DDeviceImpl;
70 typedef struct IDirect3DLightImpl         IDirect3DLightImpl;
71 typedef struct IDirect3DViewportImpl      IDirect3DViewportImpl;
72 typedef struct IDirect3DMaterialImpl      IDirect3DMaterialImpl;
73 typedef struct IDirect3DExecuteBufferImpl IDirect3DExecuteBufferImpl;
74 typedef struct IDirect3DVertexBufferImpl  IDirect3DVertexBufferImpl;
75 typedef struct IParentImpl                IParentImpl;
76
77 /*****************************************************************************
78  * IDirectDraw implementation structure
79  *****************************************************************************/
80
81 struct IDirectDrawImpl
82 {
83     /* IUnknown fields */
84     ICOM_VFIELD_MULTI(IDirectDraw7);
85     ICOM_VFIELD_MULTI(IDirectDraw4);
86     ICOM_VFIELD_MULTI(IDirectDraw2);
87     ICOM_VFIELD_MULTI(IDirectDraw);
88     ICOM_VFIELD_MULTI(IDirect3D7);
89     ICOM_VFIELD_MULTI(IDirect3D3);
90     ICOM_VFIELD_MULTI(IDirect3D2);
91     ICOM_VFIELD_MULTI(IDirect3D);
92
93     LONG              ref;
94
95     /* WineD3D linkage */
96     IWineD3D                *wineD3D;
97     IWineD3DDevice          *wineD3DDevice;
98     IDirectDrawSurfaceImpl  *DepthStencilBuffer;
99     BOOL                    d3d_initialized;
100
101     /* misc ddraw fields */
102     UINT                    total_vidmem;
103     DWORD                   cur_scanline;
104     BOOL                    fake_vblank;
105     BOOL                    initialized;
106
107     /* DirectDraw things, which are not handled by WineD3D */
108     DWORD                   cooperative_level;
109
110     DWORD                   orig_width, orig_height;
111     DWORD                   orig_bpp;
112
113     DDCAPS                  caps;
114
115     LONG                    style;
116     LONG                    exStyle;
117
118     /* D3D things */
119     IDirectDrawSurfaceImpl  *d3d_target;
120     HWND                    d3d_window;
121     IDirect3DDeviceImpl     *d3ddevice;
122
123     /* Varios HWNDs */
124     HWND                    focuswindow;
125     HWND                    devicewindow;
126
127     /* The surface type to request */
128     WINED3DSURFTYPE ImplType;
129
130     /* The surface list - can't relay this to WineD3D
131      * because of IParent
132      */
133     IDirectDrawSurfaceImpl *surface_list;
134
135     /* Our private window class */
136     char classname[32];
137     WNDCLASSA wnd_class;
138
139     /* Helpers for surface creation */
140     IDirectDrawSurfaceImpl *tex_root;
141     BOOL                    depthstencil;
142
143     /* For the dll unload cleanup code */
144     IDirectDrawImpl *next;
145     BOOL DoNotDestroy;
146     LONG surfaces;
147 };
148
149 /* Declare the VTables. They can be found ddraw.c */
150 const IDirectDraw7Vtbl IDirectDraw7_Vtbl;
151 const IDirectDraw4Vtbl IDirectDraw4_Vtbl;
152 const IDirectDraw2Vtbl IDirectDraw2_Vtbl;
153 const IDirectDrawVtbl  IDirectDraw1_Vtbl;
154
155 /* Helper structures */
156 typedef struct EnumDisplayModesCBS
157 {
158     void *context;
159     LPDDENUMMODESCALLBACK2 callback;
160 } EnumDisplayModesCBS;
161
162 typedef struct EnumSurfacesCBS
163 {
164     void *context;
165     LPDDENUMSURFACESCALLBACK7 callback;
166     LPDDSURFACEDESC2 pDDSD;
167     DWORD Flags;
168 } EnumSurfacesCBS;
169
170 /* Utility functions */
171 void
172 DDRAW_Convert_DDSCAPS_1_To_2(const DDSCAPS* pIn,
173                              DDSCAPS2* pOut);
174 void
175 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(const DDDEVICEIDENTIFIER2* pIn,
176                                         DDDEVICEIDENTIFIER* pOut);
177 void
178 IDirectDrawImpl_Destroy(IDirectDrawImpl *This);
179
180 HRESULT WINAPI
181 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
182                                          DDSURFACEDESC2 *desc,
183                                          void *Context);
184 /* The cleanup list */
185 extern IDirectDrawImpl *ddraw_list;
186
187 /* The default surface type */
188 extern WINED3DSURFTYPE DefaultSurfaceType;
189
190 /*****************************************************************************
191  * IDirectDrawSurface implementation structure
192  *****************************************************************************/
193
194 struct IDirectDrawSurfaceImpl
195 {
196     /* IUnknown fields */
197     ICOM_VFIELD_MULTI(IDirectDrawSurface7);
198     ICOM_VFIELD_MULTI(IDirectDrawSurface3);
199     ICOM_VFIELD_MULTI(IDirectDrawGammaControl);
200     ICOM_VFIELD_MULTI(IDirect3DTexture2);
201     ICOM_VFIELD_MULTI(IDirect3DTexture);
202
203     LONG                     ref;
204
205     int                     version;
206
207     /* Connections to other Objects */
208     IDirectDrawImpl         *ddraw;
209     IWineD3DSurface         *WineD3DSurface;
210     IWineD3DTexture         *wineD3DTexture;
211
212     /* This implementation handles attaching surfaces to other surfaces */
213     IDirectDrawSurfaceImpl  *next_attached;
214     IDirectDrawSurfaceImpl  *first_attached;
215     IDirectDrawSurfaceImpl  *next_complex;
216     IDirectDrawSurfaceImpl  *first_complex;
217
218     /* Surface description, for GetAttachedSurface */
219     DDSURFACEDESC2          surface_desc;
220
221     /* Misc things */
222     DWORD                   uniqueness_value;
223     UINT                    mipmap_level;
224     WINED3DSURFTYPE         ImplType;
225
226     /* For D3DDevice creation */
227     BOOL                    isRenderTarget;
228
229     /* Clipper objects */
230     IDirectDrawClipperImpl  *clipper;
231
232     /* For the ddraw surface list */
233     IDirectDrawSurfaceImpl *next;
234     IDirectDrawSurfaceImpl *prev;
235 };
236
237 /* VTable declaration. It's located in surface.c / surface_thunks.c */
238 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl;
239 const IDirectDrawSurface3Vtbl IDirectDrawSurface3_Vtbl;
240 const IDirectDrawGammaControlVtbl IDirectDrawGammaControl_Vtbl;
241 const IDirect3DTexture2Vtbl IDirect3DTexture2_Vtbl;
242 const IDirect3DTextureVtbl IDirect3DTexture1_Vtbl;
243
244 /* Get the number of bytes per pixel for a given surface */
245 #define PFGET_BPP(pf) (pf.dwFlags&DDPF_PALETTEINDEXED8?1:((pf.dwRGBBitCount+7)/8))
246 #define GET_BPP(desc) PFGET_BPP(desc.ddpfPixelFormat)
247
248 /*****************************************************************************
249  * IParent Implementation
250  *****************************************************************************/
251 struct IParentImpl
252 {
253     /* IUnknown fields */
254     ICOM_VFIELD_MULTI(IParent);
255     LONG                    ref;
256
257     /* IParentImpl fields */
258     IUnknown      *child;
259
260 };
261
262 const IParentVtbl IParent_Vtbl;
263
264 /*****************************************************************************
265  * IDirect3DDevice implementation
266  *****************************************************************************/
267 struct IDirect3DDeviceImpl
268 {
269     /* IUnknown */
270     ICOM_VFIELD_MULTI(IDirect3DDevice7);
271     ICOM_VFIELD_MULTI(IDirect3DDevice3);
272     ICOM_VFIELD_MULTI(IDirect3DDevice2);
273     ICOM_VFIELD_MULTI(IDirect3DDevice);
274     LONG                    ref;
275
276     /* Other object connections */
277     IWineD3DDevice          *wineD3DDevice;
278     IDirectDrawImpl         *ddraw;
279     IWineD3DIndexBuffer     *indexbuffer;
280     IDirectDrawSurfaceImpl  *target;
281     BOOL                    OffScreenTarget;
282
283     /* Viewport management */
284     IDirect3DViewportImpl *viewport_list;
285     IDirect3DViewportImpl *current_viewport;
286     D3DVIEWPORT7 active_viewport;
287
288     /* Light state */
289     DWORD material;
290
291     /* Rendering functions to wrap D3D(1-3) to D3D7 */
292     D3DPRIMITIVETYPE primitive_type;
293     DWORD vertex_type;
294     DWORD render_flags;
295     DWORD nb_vertices;
296     LPBYTE vertex_buffer;
297     DWORD vertex_size;
298     DWORD buffer_size;
299 };
300
301 /* Vtables in various versions */
302 const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl;
303 const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl;
304 const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl;
305 const IDirect3DDeviceVtbl  IDirect3DDevice1_Vtbl;
306
307 /* The IID */
308 const GUID IID_D3DDEVICE_WineD3D;
309
310 /* Helper functions */
311 HRESULT IDirect3DImpl_GetCaps(IWineD3D *WineD3D, D3DDEVICEDESC *Desc123, D3DDEVICEDESC7 *Desc7);
312
313 /* Structures */
314 struct EnumTextureFormatsCBS
315 {
316     LPD3DENUMTEXTUREFORMATSCALLBACK cbv2;
317     LPD3DENUMPIXELFORMATSCALLBACK cbv7;
318     void *Context;
319 };
320
321 /*****************************************************************************
322  * IDirect3D implementation
323  *****************************************************************************/
324
325 /* No implementation structure as this is only another interface to DirectDraw */
326
327 /* the Vtables */
328 const IDirect3DVtbl  IDirect3D1_Vtbl;
329 const IDirect3D2Vtbl IDirect3D2_Vtbl;
330 const IDirect3D3Vtbl IDirect3D3_Vtbl;
331 const IDirect3D7Vtbl IDirect3D7_Vtbl;
332
333 /* Structure for EnumZBufferFormats */
334 struct EnumZBufferFormatsData
335 {
336     LPD3DENUMPIXELFORMATSCALLBACK Callback;
337     void *Context;
338 };
339
340 /*****************************************************************************
341  * IDirectDrawClipper implementation structure
342  *****************************************************************************/
343 struct IDirectDrawClipperImpl
344 {
345     /* IUnknown fields */
346     ICOM_VFIELD_MULTI(IDirectDrawClipper);
347     LONG ref;
348
349     /* IDirectDrawClipper fields */
350     HWND hWnd;
351
352     IDirectDrawImpl* ddraw_owner;
353     struct IDirectDrawClipperImpl* prev_ddraw;
354     struct IDirectDrawClipperImpl* next_ddraw;
355 };
356
357 const IDirectDrawClipperVtbl IDirectDrawClipper_Vtbl;
358
359 /*****************************************************************************
360  * IDirectDrawPalette implementation structure
361  *****************************************************************************/
362 struct IDirectDrawPaletteImpl
363 {
364     /* IUnknown fields */
365     ICOM_VFIELD_MULTI(IDirectDrawPalette);
366     LONG ref;
367
368     /* WineD3D uplink */
369     IWineD3DPalette           *wineD3DPalette;
370
371     /* IDirectDrawPalette fields */
372     IDirectDrawImpl           *ddraw_owner;
373 };
374 const IDirectDrawPaletteVtbl IDirectDrawPalette_Vtbl;
375
376 /******************************************************************************
377  * DirectDraw ClassFactory Implementation - incomplete
378  ******************************************************************************/
379 typedef struct
380 {
381     ICOM_VFIELD_MULTI(IClassFactory);
382
383     LONG ref;
384     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid, LPVOID *ppObj);
385 } IClassFactoryImpl;
386
387 /* Helper structures */
388 struct object_creation_info
389 {
390     const CLSID *clsid;
391     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
392                                  void **ppObj);
393 };
394
395 /******************************************************************************
396  * IDirect3DLight implementation structure - Wraps to D3D7
397  ******************************************************************************/
398 struct IDirect3DLightImpl
399 {
400     ICOM_VFIELD_MULTI(IDirect3DLight);
401     LONG ref;
402
403     /* IDirect3DLight fields */
404     IDirectDrawImpl           *ddraw;
405
406     /* If this light is active for one viewport, put the viewport here */
407     IDirect3DViewportImpl     *active_viewport;
408
409     D3DLIGHT2 light;
410     D3DLIGHT7 light7;
411
412     DWORD dwLightIndex;
413
414     /* Chained list used for adding / removing from viewports */
415     IDirect3DLightImpl        *next;
416
417     /* Activation function */
418     void                      (*activate)(IDirect3DLightImpl*);
419     void                      (*desactivate)(IDirect3DLightImpl*);
420     void                      (*update)(IDirect3DLightImpl*);
421 };
422
423 /* Vtable */
424 const IDirect3DLightVtbl IDirect3DLight_Vtbl;
425
426 /* Helper functions */
427 void light_update(IDirect3DLightImpl* This);
428 void light_activate(IDirect3DLightImpl* This);
429 void light_desactivate(IDirect3DLightImpl* This);
430
431 /******************************************************************************
432  * IDirect3DMaterial implementation structure - Wraps to D3D7
433  ******************************************************************************/
434 struct IDirect3DMaterialImpl
435 {
436     ICOM_VFIELD_MULTI(IDirect3DMaterial3);
437     ICOM_VFIELD_MULTI(IDirect3DMaterial2);
438     ICOM_VFIELD_MULTI(IDirect3DMaterial);
439     LONG  ref;
440
441     /* IDirect3DMaterial2 fields */
442     IDirectDrawImpl               *ddraw;
443     IDirect3DDeviceImpl           *active_device;
444
445     D3DMATERIAL mat;
446
447     void (*activate)(IDirect3DMaterialImpl* this);
448 };
449
450 /* VTables in varios versions */
451 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl;
452 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl;
453 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl;
454
455 /* Helper functions */
456 void material_activate(IDirect3DMaterialImpl* This);
457
458 /*****************************************************************************
459  * IDirect3DViewport - Wraps to D3D7
460  *****************************************************************************/
461 struct IDirect3DViewportImpl
462 {
463     ICOM_VFIELD_MULTI(IDirect3DViewport3);
464     LONG ref;
465
466     /* IDirect3DViewport fields */
467     IDirectDrawImpl           *ddraw;
468
469     /* If this viewport is active for one device, put the device here */
470     IDirect3DDeviceImpl       *active_device;
471
472     DWORD                     num_lights;
473     DWORD                     map_lights;
474
475     int                       use_vp2;
476
477     union
478     {
479         D3DVIEWPORT vp1;
480         D3DVIEWPORT2 vp2;
481     } viewports;
482
483     /* Activation function */
484     void                      (*activate)(IDirect3DViewportImpl*);
485
486     /* Field used to chain viewports together */
487     IDirect3DViewportImpl     *next;
488
489     /* Lights list */
490     IDirect3DLightImpl        *lights;
491
492     /* Background material */
493     IDirect3DMaterialImpl     *background;
494 };
495
496 /* Vtable */
497 const IDirect3DViewport3Vtbl IDirect3DViewport3_Vtbl;
498
499 /* Helper functions */
500 void viewport_activate(IDirect3DViewportImpl* This);
501
502 /*****************************************************************************
503  * IDirect3DExecuteBuffer - Wraps to D3D7
504  *****************************************************************************/
505 struct IDirect3DExecuteBufferImpl
506 {
507     /* IUnknown */
508     ICOM_VFIELD_MULTI(IDirect3DExecuteBuffer);
509     LONG                 ref;
510
511     /* IDirect3DExecuteBuffer fields */
512     IDirectDrawImpl      *ddraw;
513     IDirect3DDeviceImpl  *d3ddev;
514
515     D3DEXECUTEBUFFERDESC desc;
516     D3DEXECUTEDATA       data;
517
518     /* This buffer will store the transformed vertices */
519     void                 *vertex_data;
520     WORD                 *indices;
521     int                  nb_indices;
522
523     /* This flags is set to TRUE if we allocated ourselves the
524      * data buffer
525      */
526     BOOL                 need_free;
527 };
528
529 /* The VTable */
530 const IDirect3DExecuteBufferVtbl IDirect3DExecuteBuffer_Vtbl;
531
532 /* The execute function */
533 void
534 IDirect3DExecuteBufferImpl_Execute(IDirect3DExecuteBufferImpl *This,
535                                    IDirect3DDeviceImpl *Device,
536                                    IDirect3DViewportImpl *ViewportImpl);
537
538 /*****************************************************************************
539  * IDirect3DVertexBuffer
540  *****************************************************************************/
541 struct IDirect3DVertexBufferImpl
542 {
543     /*** IUnknown Methods ***/
544     ICOM_VFIELD_MULTI(IDirect3DVertexBuffer7);
545     ICOM_VFIELD_MULTI(IDirect3DVertexBuffer);
546     LONG                 ref;
547
548     /*** WineD3D link ***/
549     IWineD3DVertexBuffer *wineD3DVertexBuffer;
550
551     /*** Storage for D3D7 specific things ***/
552     DWORD                Caps;
553 };
554
555 /* The Vtables */
556 const IDirect3DVertexBuffer7Vtbl IDirect3DVertexBuffer7_Vtbl;
557 const IDirect3DVertexBufferVtbl IDirect3DVertexBuffer1_Vtbl;
558
559 /*****************************************************************************
560  * Helper functions from utils.c
561  *****************************************************************************/
562
563 #define GET_TEXCOUNT_FROM_FVF(d3dvtVertexType) \
564     (((d3dvtVertexType) & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT)
565
566 #define GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, tex_num) \
567     (((((d3dvtVertexType) >> (16 + (2 * (tex_num)))) + 1) & 0x03) + 1)
568
569 void PixelFormat_WineD3DtoDD(DDPIXELFORMAT *DDPixelFormat, WINED3DFORMAT WineD3DFormat);
570 WINED3DFORMAT PixelFormat_DD2WineD3D(DDPIXELFORMAT *DDPixelFormat);
571 void DDRAW_dump_surface_desc(const DDSURFACEDESC2 *lpddsd);
572 void DDRAW_dump_pixelformat(const DDPIXELFORMAT *PixelFormat);
573 void dump_D3DMATRIX(D3DMATRIX *mat);
574 void DDRAW_dump_DDCAPS(const DDCAPS *lpcaps);
575 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType);
576 void DDRAW_dump_DDSCAPS2(const DDSCAPS2 *in);
577 void DDRAW_dump_cooperativelevel(DWORD cooplevel);
578
579 /* This only needs to be here as long the processvertices functionality of
580  * IDirect3DExecuteBuffer isn't in WineD3D */
581 void multiply_matrix(LPD3DMATRIX dest, LPD3DMATRIX src1, LPD3DMATRIX src2);
582
583 /* Used for generic dumping */
584 typedef struct
585 {
586     DWORD val;
587     const char* name;
588 } flag_info;
589
590 #define FE(x) { x, #x }
591
592 typedef struct
593 {
594     DWORD val;
595     const char* name;
596     void (*func)(const void *);
597     ptrdiff_t offset;
598 } member_info;
599
600 /* Structure copy */
601 #define DDRAW_dump_flags(flags,names,num_names) DDRAW_dump_flags_(flags, names, num_names, 1)
602 #define ME(x,f,e) { x, #x, (void (*)(const void *))(f), offsetof(STRUCT, e) }
603
604 #define DD_STRUCT_COPY_BYSIZE(to,from)                  \
605         do {                                            \
606                 DWORD __size = (to)->dwSize;            \
607                 DWORD __copysize = __size;              \
608                 DWORD __resetsize = __size;             \
609                 assert(to != from);                     \
610                 if (__resetsize > sizeof(*to))          \
611                     __resetsize = sizeof(*to);          \
612                 memset(to,0,__resetsize);               \
613                 if ((from)->dwSize < __size)            \
614                     __copysize = (from)->dwSize;        \
615                 memcpy(to,from,__copysize);             \
616                 (to)->dwSize = __size;/*restore size*/  \
617         } while (0)
618
619
620 #endif
621
622 HRESULT hr_ddraw_from_wined3d(HRESULT hr);