2 * Copyright 2000 Marcus Meissner
3 * Copyright 2000 Peter Hunnisett
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
43 #include "ddraw_private.h"
44 #include "d3d_private.h"
45 #include "opengl_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
52 GL_IDirect3DImpl_1_EnumDevices(LPDIRECT3D iface
,
53 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback
,
56 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D
, iface
);
57 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lpEnumDevicesCallback
, lpUserArg
);
59 /* Call functions defined in d3ddevices.c */
60 if (d3ddevice_enumerate(lpEnumDevicesCallback
, lpUserArg
, 1) != D3DENUMRET_OK
)
67 GL_IDirect3DImpl_2_EnumDevices(LPDIRECT3D2 iface
,
68 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback
,
71 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D2
, iface
);
72 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lpEnumDevicesCallback
, lpUserArg
);
74 /* Call functions defined in d3ddevices.c */
75 if (d3ddevice_enumerate(lpEnumDevicesCallback
, lpUserArg
, 2) != D3DENUMRET_OK
)
82 GL_IDirect3DImpl_3_EnumDevices(LPDIRECT3D3 iface
,
83 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback
,
86 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
87 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lpEnumDevicesCallback
, lpUserArg
);
89 /* Call functions defined in d3ddevices.c */
90 if (d3ddevice_enumerate(lpEnumDevicesCallback
, lpUserArg
, 3) != D3DENUMRET_OK
)
97 GL_IDirect3DImpl_3_2T_1T_CreateLight(LPDIRECT3D3 iface
,
98 LPDIRECT3DLIGHT
* lplpDirect3DLight
,
101 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
102 IDirect3DGLImpl
*glThis
= (IDirect3DGLImpl
*) This
->d3d_private
;
104 IDirect3DLightImpl
*d3dlimpl
;
107 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lplpDirect3DLight
, pUnkOuter
);
108 for (fl
= 0; fl
< MAX_LIGHTS
; fl
++) {
109 if ((glThis
->free_lights
& (0x01 << fl
)) != 0) {
110 glThis
->free_lights
&= ~(0x01 << fl
);
114 if (fl
== MAX_LIGHTS
) {
115 return DDERR_INVALIDPARAMS
; /* No way to say 'max lights reached' ... */
117 ret_value
= d3dlight_create(&d3dlimpl
, This
, GL_LIGHT0
+ fl
);
118 *lplpDirect3DLight
= ICOM_INTERFACE(d3dlimpl
, IDirect3DLight
);
124 GL_IDirect3DImpl_3_2T_1T_CreateMaterial(LPDIRECT3D3 iface
,
125 LPDIRECT3DMATERIAL3
* lplpDirect3DMaterial3
,
128 IDirect3DMaterialImpl
*D3Dmat_impl
;
130 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
132 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lplpDirect3DMaterial3
, pUnkOuter
);
133 ret_value
= d3dmaterial_create(&D3Dmat_impl
, This
);
135 *lplpDirect3DMaterial3
= ICOM_INTERFACE(D3Dmat_impl
, IDirect3DMaterial3
);
141 GL_IDirect3DImpl_3_2T_1T_CreateViewport(LPDIRECT3D3 iface
,
142 LPDIRECT3DVIEWPORT3
* lplpD3DViewport3
,
145 IDirect3DViewportImpl
*D3Dvp_impl
;
147 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
149 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lplpD3DViewport3
, pUnkOuter
);
150 ret_value
= d3dviewport_create(&D3Dvp_impl
, This
);
152 *lplpD3DViewport3
= ICOM_INTERFACE(D3Dvp_impl
, IDirect3DViewport3
);
158 create_device_helper(IDirectDrawImpl
*This
,
160 IDirectDrawSurfaceImpl
*lpDDS
,
163 IDirect3DDeviceImpl
*lpd3ddev
;
166 ret_value
= d3ddevice_create(&lpd3ddev
, This
, lpDDS
, version
);
167 if (FAILED(ret_value
)) return ret_value
;
170 (IsEqualGUID(&IID_D3DDEVICE_OpenGL
, iid
)) ||
171 (IsEqualGUID(&IID_IDirect3DHALDevice
, iid
)) ||
172 (IsEqualGUID(&IID_IDirect3DTnLHalDevice
, iid
)) ||
173 (IsEqualGUID(&IID_IDirect3DRGBDevice
, iid
)) ||
174 (IsEqualGUID(&IID_IDirect3DRefDevice
, iid
))) {
177 *obj
= ICOM_INTERFACE(lpd3ddev
, IDirect3DDevice
);
178 TRACE(" returning OpenGL D3DDevice %p.\n", *obj
);
182 *obj
= ICOM_INTERFACE(lpd3ddev
, IDirect3DDevice2
);
183 TRACE(" returning OpenGL D3DDevice2 %p.\n", *obj
);
187 *obj
= ICOM_INTERFACE(lpd3ddev
, IDirect3DDevice3
);
188 TRACE(" returning OpenGL D3DDevice3 %p.\n", *obj
);
192 *obj
= ICOM_INTERFACE(lpd3ddev
, IDirect3DDevice7
);
193 TRACE(" returning OpenGL D3DDevice7 %p.\n", *obj
);
199 ERR(" Interface unknown when creating D3DDevice (%s)\n", debugstr_guid(iid
));
200 IDirect3DDevice7_Release(ICOM_INTERFACE(lpd3ddev
, IDirect3DDevice7
));
201 return DDERR_INVALIDPARAMS
;
206 GL_IDirect3DImpl_2_CreateDevice(LPDIRECT3D2 iface
,
208 LPDIRECTDRAWSURFACE lpDDS
,
209 LPDIRECT3DDEVICE2
* lplpD3DDevice2
)
211 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D2
, iface
);
212 IDirectDrawSurfaceImpl
*ddsurfaceimpl
= ICOM_OBJECT(IDirectDrawSurfaceImpl
, IDirectDrawSurface3
, lpDDS
);
213 TRACE("(%p/%p)->(%s,%p,%p)\n", This
, iface
, debugstr_guid(rclsid
), lpDDS
, lplpD3DDevice2
);
214 return create_device_helper(This
, rclsid
, ddsurfaceimpl
, (void **) lplpD3DDevice2
, 2);
218 GL_IDirect3DImpl_3_CreateDevice(LPDIRECT3D3 iface
,
220 LPDIRECTDRAWSURFACE4 lpDDS
,
221 LPDIRECT3DDEVICE3
* lplpD3DDevice3
,
224 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
225 IDirectDrawSurfaceImpl
*ddsurfaceimpl
= ICOM_OBJECT(IDirectDrawSurfaceImpl
, IDirectDrawSurface7
, lpDDS
);
226 TRACE("(%p/%p)->(%s,%p,%p)\n", This
, iface
, debugstr_guid(rclsid
), lpDDS
, lplpD3DDevice3
);
227 return create_device_helper(This
, rclsid
, ddsurfaceimpl
, (void **) lplpD3DDevice3
, 3);
231 GL_IDirect3DImpl_3_2T_1T_FindDevice(LPDIRECT3D3 iface
,
232 LPD3DFINDDEVICESEARCH lpD3DDFS
,
233 LPD3DFINDDEVICERESULT lpD3DFDR
)
235 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D3
, iface
);
236 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lpD3DDFS
, lpD3DFDR
);
237 return d3ddevice_find(This
, lpD3DDFS
, lpD3DFDR
);
241 GL_IDirect3DImpl_7_3T_EnumZBufferFormats(LPDIRECT3D7 iface
,
243 LPD3DENUMPIXELFORMATSCALLBACK lpEnumCallback
,
246 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D7
, iface
);
247 DDPIXELFORMAT pformat
;
249 TRACE("(%p/%p)->(%s,%p,%p)\n", This
, iface
, debugstr_guid(riidDevice
), lpEnumCallback
, lpContext
);
251 memset(&pformat
, 0, sizeof(pformat
));
252 pformat
.dwSize
= sizeof(DDPIXELFORMAT
);
253 pformat
.dwFourCC
= 0;
254 TRACE("Enumerating dummy ZBuffer format (16 bits)\n");
255 pformat
.dwFlags
= DDPF_ZBUFFER
;
256 pformat
.u1
.dwZBufferBitDepth
= 16;
257 pformat
.u3
.dwZBitMask
= 0x0000FFFF;
258 pformat
.u5
.dwRGBZBitMask
= 0x0000FFFF;
260 /* Whatever the return value, stop here.. */
261 lpEnumCallback(&pformat
, lpContext
);
267 GL_IDirect3DImpl_7_EnumDevices(LPDIRECT3D7 iface
,
268 LPD3DENUMDEVICESCALLBACK7 lpEnumDevicesCallback
,
271 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D7
, iface
);
272 TRACE("(%p/%p)->(%p,%p)\n", This
, iface
, lpEnumDevicesCallback
, lpUserArg
);
274 if (d3ddevice_enumerate7(lpEnumDevicesCallback
, lpUserArg
) != D3DENUMRET_OK
)
281 GL_IDirect3DImpl_7_CreateDevice(LPDIRECT3D7 iface
,
283 LPDIRECTDRAWSURFACE7 lpDDS
,
284 LPDIRECT3DDEVICE7
* lplpD3DDevice
)
286 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D7
, iface
);
287 IDirectDrawSurfaceImpl
*ddsurfaceimpl
= ICOM_OBJECT(IDirectDrawSurfaceImpl
, IDirectDrawSurface7
, lpDDS
);
288 TRACE("(%p/%p)->(%s,%p,%p)\n", This
, iface
, debugstr_guid(rclsid
), lpDDS
, lplpD3DDevice
);
289 return create_device_helper(This
, rclsid
, ddsurfaceimpl
, (void **) lplpD3DDevice
, 7);
293 GL_IDirect3DImpl_7_3T_CreateVertexBuffer(LPDIRECT3D7 iface
,
294 LPD3DVERTEXBUFFERDESC lpD3DVertBufDesc
,
295 LPDIRECT3DVERTEXBUFFER7
* lplpD3DVertBuf
,
298 ICOM_THIS_FROM(IDirectDrawImpl
, IDirect3D7
, iface
);
299 IDirect3DVertexBufferImpl
*vbimpl
;
302 TRACE("(%p/%p)->(%p,%p,%08lx)\n", This
, iface
, lpD3DVertBufDesc
, lplpD3DVertBuf
, dwFlags
);
304 res
= d3dvertexbuffer_create(&vbimpl
, This
, lpD3DVertBufDesc
, dwFlags
);
306 *lplpD3DVertBuf
= ICOM_INTERFACE(vbimpl
, IDirect3DVertexBuffer7
);
311 static void light_released(IDirectDrawImpl
*This
, GLenum light_num
)
313 IDirect3DGLImpl
*glThis
= (IDirect3DGLImpl
*) This
->d3d_private
;
314 glThis
->free_lights
|= (light_num
- GL_LIGHT0
);
317 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
318 # define XCAST(fun) (typeof(VTABLE_IDirect3D7.fun))
320 # define XCAST(fun) (void*)
323 static const IDirect3D7Vtbl VTABLE_IDirect3D7
=
325 XCAST(QueryInterface
) Thunk_IDirect3DImpl_7_QueryInterface
,
326 XCAST(AddRef
) Thunk_IDirect3DImpl_7_AddRef
,
327 XCAST(Release
) Thunk_IDirect3DImpl_7_Release
,
328 XCAST(EnumDevices
) GL_IDirect3DImpl_7_EnumDevices
,
329 XCAST(CreateDevice
) GL_IDirect3DImpl_7_CreateDevice
,
330 XCAST(CreateVertexBuffer
) GL_IDirect3DImpl_7_3T_CreateVertexBuffer
,
331 XCAST(EnumZBufferFormats
) GL_IDirect3DImpl_7_3T_EnumZBufferFormats
,
332 XCAST(EvictManagedTextures
) Main_IDirect3DImpl_7_3T_EvictManagedTextures
,
335 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
340 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
341 # define XCAST(fun) (typeof(VTABLE_IDirect3D3.fun))
343 # define XCAST(fun) (void*)
346 static const IDirect3D3Vtbl VTABLE_IDirect3D3
=
348 XCAST(QueryInterface
) Thunk_IDirect3DImpl_3_QueryInterface
,
349 XCAST(AddRef
) Thunk_IDirect3DImpl_3_AddRef
,
350 XCAST(Release
) Thunk_IDirect3DImpl_3_Release
,
351 XCAST(EnumDevices
) GL_IDirect3DImpl_3_EnumDevices
,
352 XCAST(CreateLight
) GL_IDirect3DImpl_3_2T_1T_CreateLight
,
353 XCAST(CreateMaterial
) GL_IDirect3DImpl_3_2T_1T_CreateMaterial
,
354 XCAST(CreateViewport
) GL_IDirect3DImpl_3_2T_1T_CreateViewport
,
355 XCAST(FindDevice
) GL_IDirect3DImpl_3_2T_1T_FindDevice
,
356 XCAST(CreateDevice
) GL_IDirect3DImpl_3_CreateDevice
,
357 XCAST(CreateVertexBuffer
) Thunk_IDirect3DImpl_3_CreateVertexBuffer
,
358 XCAST(EnumZBufferFormats
) Thunk_IDirect3DImpl_3_EnumZBufferFormats
,
359 XCAST(EvictManagedTextures
) Thunk_IDirect3DImpl_3_EvictManagedTextures
,
362 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
367 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
368 # define XCAST(fun) (typeof(VTABLE_IDirect3D2.fun))
370 # define XCAST(fun) (void*)
373 static const IDirect3D2Vtbl VTABLE_IDirect3D2
=
375 XCAST(QueryInterface
) Thunk_IDirect3DImpl_2_QueryInterface
,
376 XCAST(AddRef
) Thunk_IDirect3DImpl_2_AddRef
,
377 XCAST(Release
) Thunk_IDirect3DImpl_2_Release
,
378 XCAST(EnumDevices
) GL_IDirect3DImpl_2_EnumDevices
,
379 XCAST(CreateLight
) Thunk_IDirect3DImpl_2_CreateLight
,
380 XCAST(CreateMaterial
) Thunk_IDirect3DImpl_2_CreateMaterial
,
381 XCAST(CreateViewport
) Thunk_IDirect3DImpl_2_CreateViewport
,
382 XCAST(FindDevice
) Thunk_IDirect3DImpl_2_FindDevice
,
383 XCAST(CreateDevice
) GL_IDirect3DImpl_2_CreateDevice
,
386 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
391 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
392 # define XCAST(fun) (typeof(VTABLE_IDirect3D.fun))
394 # define XCAST(fun) (void*)
397 static const IDirect3DVtbl VTABLE_IDirect3D
=
399 XCAST(QueryInterface
) Thunk_IDirect3DImpl_1_QueryInterface
,
400 XCAST(AddRef
) Thunk_IDirect3DImpl_1_AddRef
,
401 XCAST(Release
) Thunk_IDirect3DImpl_1_Release
,
402 XCAST(Initialize
) Main_IDirect3DImpl_1_Initialize
,
403 XCAST(EnumDevices
) GL_IDirect3DImpl_1_EnumDevices
,
404 XCAST(CreateLight
) Thunk_IDirect3DImpl_1_CreateLight
,
405 XCAST(CreateMaterial
) Thunk_IDirect3DImpl_1_CreateMaterial
,
406 XCAST(CreateViewport
) Thunk_IDirect3DImpl_1_CreateViewport
,
407 XCAST(FindDevice
) Thunk_IDirect3DImpl_1_FindDevice
,
410 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
414 static HRESULT
d3d_add_device(IDirectDrawImpl
*This
, IDirect3DDeviceImpl
*device
)
416 if (This
->current_device
== NULL
) {
417 /* Create delayed textures now that we have an OpenGL context...
418 For that, go through all surface attached to our DDraw object and create
419 OpenGL textures for all textures.. */
420 IDirectDrawSurfaceImpl
*surf
= This
->surfaces
;
422 while (surf
!= NULL
) {
423 if (surf
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_TEXTURE
) {
424 /* Found a texture.. Now create the OpenGL part */
425 d3dtexture_create(This
, surf
, FALSE
, surf
->mip_main
);
427 surf
= surf
->next_ddraw
;
430 /* For the moment, only one device 'supported'... */
431 This
->current_device
= device
;
436 static HRESULT
d3d_remove_device(IDirectDrawImpl
*This
, IDirect3DDeviceImpl
*device
)
438 This
->current_device
= NULL
;
442 HRESULT
direct3d_create(IDirectDrawImpl
*This
)
444 IDirect3DGLImpl
*globject
;
446 globject
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DGLImpl
));
447 if (globject
== NULL
) return DDERR_OUTOFMEMORY
;
449 This
->d3d_create_texture
= d3dtexture_create
;
450 This
->d3d_added_device
= d3d_add_device
;
451 This
->d3d_removed_device
= d3d_remove_device
;
453 ICOM_INIT_INTERFACE(This
, IDirect3D
, VTABLE_IDirect3D
);
454 ICOM_INIT_INTERFACE(This
, IDirect3D2
, VTABLE_IDirect3D2
);
455 ICOM_INIT_INTERFACE(This
, IDirect3D3
, VTABLE_IDirect3D3
);
456 ICOM_INIT_INTERFACE(This
, IDirect3D7
, VTABLE_IDirect3D7
);
458 globject
->free_lights
= (0x01 << MAX_LIGHTS
) - 1; /* There are, in total, 8 lights in OpenGL */
459 globject
->light_released
= light_released
;
461 This
->d3d_private
= globject
;
463 TRACE(" creating OpenGL private storage at %p.\n", globject
);