Added support for vertex declarations to wined3d, it's the last patch
[wine/multimedia.git] / dlls / ddraw / direct3d_opengl.c
blob57c7c7bf71f9cbbecc2b4dc1775774270cd5f887
1 /*
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
20 #include "config.h"
22 #include <assert.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <fcntl.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
34 #define CONST_VTABLE
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "d3d.h"
40 #include "ddraw.h"
41 #include "winerror.h"
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);
51 HRESULT WINAPI
52 GL_IDirect3DImpl_1_EnumDevices(LPDIRECT3D iface,
53 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback,
54 LPVOID lpUserArg)
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)
61 return D3D_OK;
63 return D3D_OK;
66 HRESULT WINAPI
67 GL_IDirect3DImpl_2_EnumDevices(LPDIRECT3D2 iface,
68 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback,
69 LPVOID lpUserArg)
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)
76 return D3D_OK;
78 return D3D_OK;
81 HRESULT WINAPI
82 GL_IDirect3DImpl_3_EnumDevices(LPDIRECT3D3 iface,
83 LPD3DENUMDEVICESCALLBACK lpEnumDevicesCallback,
84 LPVOID lpUserArg)
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)
91 return D3D_OK;
93 return D3D_OK;
96 HRESULT WINAPI
97 GL_IDirect3DImpl_3_2T_1T_CreateLight(LPDIRECT3D3 iface,
98 LPDIRECT3DLIGHT* lplpDirect3DLight,
99 IUnknown* pUnkOuter)
101 ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
102 IDirect3DGLImpl *glThis = (IDirect3DGLImpl *) This->d3d_private;
103 int fl;
104 IDirect3DLightImpl *d3dlimpl;
105 HRESULT ret_value;
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);
111 break;
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);
120 return ret_value;
123 HRESULT WINAPI
124 GL_IDirect3DImpl_3_2T_1T_CreateMaterial(LPDIRECT3D3 iface,
125 LPDIRECT3DMATERIAL3* lplpDirect3DMaterial3,
126 IUnknown* pUnkOuter)
128 IDirect3DMaterialImpl *D3Dmat_impl;
129 HRESULT ret_value;
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);
137 return ret_value;
140 HRESULT WINAPI
141 GL_IDirect3DImpl_3_2T_1T_CreateViewport(LPDIRECT3D3 iface,
142 LPDIRECT3DVIEWPORT3* lplpD3DViewport3,
143 IUnknown* pUnkOuter)
145 IDirect3DViewportImpl *D3Dvp_impl;
146 HRESULT ret_value;
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);
154 return ret_value;
157 static HRESULT
158 create_device_helper(IDirectDrawImpl *This,
159 REFCLSID iid,
160 IDirectDrawSurfaceImpl *lpDDS,
161 void **obj,
162 int version) {
163 IDirect3DDeviceImpl *lpd3ddev;
164 HRESULT ret_value;
166 ret_value = d3ddevice_create(&lpd3ddev, This, lpDDS, version);
167 if (FAILED(ret_value)) return ret_value;
169 if ((iid == NULL) ||
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))) {
175 switch (version) {
176 case 1:
177 *obj = ICOM_INTERFACE(lpd3ddev, IDirect3DDevice);
178 TRACE(" returning OpenGL D3DDevice %p.\n", *obj);
179 return D3D_OK;
181 case 2:
182 *obj = ICOM_INTERFACE(lpd3ddev, IDirect3DDevice2);
183 TRACE(" returning OpenGL D3DDevice2 %p.\n", *obj);
184 return D3D_OK;
186 case 3:
187 *obj = ICOM_INTERFACE(lpd3ddev, IDirect3DDevice3);
188 TRACE(" returning OpenGL D3DDevice3 %p.\n", *obj);
189 return D3D_OK;
191 case 7:
192 *obj = ICOM_INTERFACE(lpd3ddev, IDirect3DDevice7);
193 TRACE(" returning OpenGL D3DDevice7 %p.\n", *obj);
194 return D3D_OK;
198 *obj = NULL;
199 ERR(" Interface unknown when creating D3DDevice (%s)\n", debugstr_guid(iid));
200 IDirect3DDevice7_Release(ICOM_INTERFACE(lpd3ddev, IDirect3DDevice7));
201 return DDERR_INVALIDPARAMS;
205 HRESULT WINAPI
206 GL_IDirect3DImpl_2_CreateDevice(LPDIRECT3D2 iface,
207 REFCLSID rclsid,
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);
217 HRESULT WINAPI
218 GL_IDirect3DImpl_3_CreateDevice(LPDIRECT3D3 iface,
219 REFCLSID rclsid,
220 LPDIRECTDRAWSURFACE4 lpDDS,
221 LPDIRECT3DDEVICE3* lplpD3DDevice3,
222 LPUNKNOWN lpUnk)
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);
230 HRESULT WINAPI
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);
240 HRESULT WINAPI
241 GL_IDirect3DImpl_7_3T_EnumZBufferFormats(LPDIRECT3D7 iface,
242 REFCLSID riidDevice,
243 LPD3DENUMPIXELFORMATSCALLBACK lpEnumCallback,
244 LPVOID lpContext)
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);
263 return D3D_OK;
266 HRESULT WINAPI
267 GL_IDirect3DImpl_7_EnumDevices(LPDIRECT3D7 iface,
268 LPD3DENUMDEVICESCALLBACK7 lpEnumDevicesCallback,
269 LPVOID lpUserArg)
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)
275 return D3D_OK;
277 return D3D_OK;
280 HRESULT WINAPI
281 GL_IDirect3DImpl_7_CreateDevice(LPDIRECT3D7 iface,
282 REFCLSID rclsid,
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);
292 HRESULT WINAPI
293 GL_IDirect3DImpl_7_3T_CreateVertexBuffer(LPDIRECT3D7 iface,
294 LPD3DVERTEXBUFFERDESC lpD3DVertBufDesc,
295 LPDIRECT3DVERTEXBUFFER7* lplpD3DVertBuf,
296 DWORD dwFlags)
298 ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
299 IDirect3DVertexBufferImpl *vbimpl;
300 HRESULT res;
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);
308 return res;
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))
319 #else
320 # define XCAST(fun) (void*)
321 #endif
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__)
336 #undef XCAST
337 #endif
340 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
341 # define XCAST(fun) (typeof(VTABLE_IDirect3D3.fun))
342 #else
343 # define XCAST(fun) (void*)
344 #endif
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__)
363 #undef XCAST
364 #endif
367 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
368 # define XCAST(fun) (typeof(VTABLE_IDirect3D2.fun))
369 #else
370 # define XCAST(fun) (void*)
371 #endif
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__)
387 #undef XCAST
388 #endif
391 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
392 # define XCAST(fun) (typeof(VTABLE_IDirect3D.fun))
393 #else
394 # define XCAST(fun) (void*)
395 #endif
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__)
411 #undef XCAST
412 #endif
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;
433 return DD_OK;
436 static HRESULT d3d_remove_device(IDirectDrawImpl *This, IDirect3DDeviceImpl *device)
438 This->current_device = NULL;
439 return DD_OK;
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);
465 return D3D_OK;