2 * Direct3D wine internal interface main
4 * Copyright 2002-2003 The wine-d3d team
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Jason Edmeades
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
32 struct wined3d_wndproc
37 IWineD3DDeviceImpl
*device
;
40 struct wined3d_wndproc_table
42 struct wined3d_wndproc
*entries
;
47 static struct wined3d_wndproc_table wndproc_table
;
50 void (CDECL
*wine_tsx11_lock_ptr
)(void) = NULL
;
51 void (CDECL
*wine_tsx11_unlock_ptr
)(void) = NULL
;
53 static CRITICAL_SECTION wined3d_cs
;
54 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
57 {&wined3d_cs_debug
.ProcessLocksList
,
58 &wined3d_cs_debug
.ProcessLocksList
},
59 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
61 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
63 /* When updating default value here, make sure to update winecfg as well,
64 * where appropriate. */
65 wined3d_settings_t wined3d_settings
=
67 VS_HW
, /* Hardware by default */
68 PS_HW
, /* Hardware by default */
69 TRUE
, /* Use of GLSL enabled by default */
70 ORM_FBO
, /* Use FBOs to do offscreen rendering */
71 RTL_READTEX
, /* Default render target locking method */
72 PCI_VENDOR_NONE
,/* PCI Vendor ID */
73 PCI_DEVICE_NONE
,/* PCI Device ID */
74 0, /* The default of memory is set in init_driver_info */
75 NULL
, /* No wine logo by default */
76 FALSE
, /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
77 FALSE
, /* No strict draw ordering. */
80 /* Do not call while under the GL lock. */
81 IWineD3D
* WINAPI
WineDirect3DCreate(UINT version
, void *parent
)
86 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
89 ERR("Failed to allocate wined3d object memory.\n");
93 hr
= wined3d_init(object
, version
, parent
);
96 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
97 HeapFree(GetProcessHeap(), 0, object
);
101 TRACE("Created wined3d object %p for d3d%d support.\n", object
, version
);
103 return (IWineD3D
*)object
;
106 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
108 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
109 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
110 return ERROR_FILE_NOT_FOUND
;
113 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
116 DWORD size
= sizeof(DWORD
);
117 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
118 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
119 return ERROR_FILE_NOT_FOUND
;
122 static void CDECL
wined3d_do_nothing(void)
126 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
128 DWORD wined3d_context_tls_idx
;
130 char buffer
[MAX_PATH
+10];
131 DWORD size
= sizeof(buffer
);
137 wined3d_context_tls_idx
= TlsAlloc();
138 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
140 DWORD err
= GetLastError();
141 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
144 context_set_tls_idx(wined3d_context_tls_idx
);
146 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
147 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
148 * Various articles/posts about OpenGL problems on Windows recommend this. */
149 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
150 wc
.lpfnWndProc
= DefWindowProcA
;
153 wc
.hInstance
= hInstDLL
;
154 wc
.hIcon
= LoadIconA(NULL
, (LPCSTR
)IDI_WINLOGO
);
155 wc
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)IDC_ARROW
);
156 wc
.hbrBackground
= NULL
;
157 wc
.lpszMenuName
= NULL
;
158 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
160 if (!RegisterClassA(&wc
))
162 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
163 if (!TlsFree(wined3d_context_tls_idx
))
165 DWORD err
= GetLastError();
166 ERR("Failed to free context TLS index, err %#x.\n", err
);
171 DisableThreadLibraryCalls(hInstDLL
);
173 mod
= GetModuleHandleA( "winex11.drv" );
176 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
177 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
179 else /* We are most likely on Windows */
181 wine_tsx11_lock_ptr
= wined3d_do_nothing
;
182 wine_tsx11_unlock_ptr
= wined3d_do_nothing
;
184 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
185 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
187 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
188 if (len
&& len
< MAX_PATH
)
191 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
192 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
194 char *p
, *appname
= buffer
;
195 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
196 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
197 strcat( appname
, "\\Direct3D" );
198 TRACE("appname = [%s]\n", appname
);
199 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
200 RegCloseKey( tmpkey
);
206 if ( !get_config_key( hkey
, appkey
, "VertexShaderMode", buffer
, size
) )
208 if (!strcmp(buffer
,"none"))
210 TRACE("Disable vertex shaders\n");
211 wined3d_settings
.vs_mode
= VS_NONE
;
214 if ( !get_config_key( hkey
, appkey
, "PixelShaderMode", buffer
, size
) )
216 if (!strcmp(buffer
,"enabled"))
218 TRACE("Allow pixel shaders\n");
219 wined3d_settings
.ps_mode
= PS_HW
;
221 if (!strcmp(buffer
,"disabled"))
223 TRACE("Disable pixel shaders\n");
224 wined3d_settings
.ps_mode
= PS_NONE
;
227 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
229 if (!strcmp(buffer
,"disabled"))
231 TRACE("Use of GL Shading Language disabled\n");
232 wined3d_settings
.glslRequested
= FALSE
;
235 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
237 if (!strcmp(buffer
,"backbuffer"))
239 TRACE("Using the backbuffer for offscreen rendering\n");
240 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
242 else if (!strcmp(buffer
,"fbo"))
244 TRACE("Using FBOs for offscreen rendering\n");
245 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
248 if ( !get_config_key( hkey
, appkey
, "RenderTargetLockMode", buffer
, size
) )
250 if (!strcmp(buffer
,"disabled"))
252 TRACE("Disabling render target locking\n");
253 wined3d_settings
.rendertargetlock_mode
= RTL_DISABLE
;
255 else if (!strcmp(buffer
,"readdraw"))
257 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
258 wined3d_settings
.rendertargetlock_mode
= RTL_READDRAW
;
260 else if (!strcmp(buffer
,"readtex"))
262 TRACE("Using glReadPixels for render target reading and textures for writing\n");
263 wined3d_settings
.rendertargetlock_mode
= RTL_READTEX
;
266 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
268 int pci_device_id
= tmpvalue
;
270 /* A pci device id is 16-bit */
271 if(pci_device_id
> 0xffff)
273 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
277 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
278 wined3d_settings
.pci_device_id
= pci_device_id
;
281 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
283 int pci_vendor_id
= tmpvalue
;
285 /* A pci device id is 16-bit */
286 if(pci_vendor_id
> 0xffff)
288 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
292 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
293 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
296 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
298 int TmpVideoMemorySize
= atoi(buffer
);
299 if(TmpVideoMemorySize
> 0)
301 wined3d_settings
.emulated_textureram
= TmpVideoMemorySize
*1024*1024;
302 TRACE("Use %iMB = %d byte for emulated_textureram\n",
304 wined3d_settings
.emulated_textureram
);
307 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
309 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
311 size_t len
= strlen(buffer
) + 1;
313 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
314 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
315 else memcpy(wined3d_settings
.logo
, buffer
, len
);
317 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
319 if (!strcmp(buffer
,"enabled"))
321 TRACE("Allow multisampling\n");
322 wined3d_settings
.allow_multisampling
= TRUE
;
325 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
326 && !strcmp(buffer
,"enabled"))
328 TRACE("Enforcing strict draw ordering.\n");
329 wined3d_settings
.strict_draw_ordering
= TRUE
;
332 if (wined3d_settings
.vs_mode
== VS_HW
)
333 TRACE("Allow HW vertex shaders\n");
334 if (wined3d_settings
.ps_mode
== PS_NONE
)
335 TRACE("Disable pixel shaders\n");
336 if (wined3d_settings
.glslRequested
)
337 TRACE("If supported by your system, GL Shading Language will be used\n");
339 if (appkey
) RegCloseKey( appkey
);
340 if (hkey
) RegCloseKey( hkey
);
345 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
347 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
350 if (!TlsFree(wined3d_context_tls_idx
))
352 DWORD err
= GetLastError();
353 ERR("Failed to free context TLS index, err %#x.\n", err
);
356 for (i
= 0; i
< wndproc_table
.count
; ++i
)
358 struct wined3d_wndproc
*entry
= &wndproc_table
.entries
[i
];
359 SetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
361 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
363 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
364 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
369 void WINAPI
wined3d_mutex_lock(void)
371 EnterCriticalSection(&wined3d_cs
);
374 void WINAPI
wined3d_mutex_unlock(void)
376 LeaveCriticalSection(&wined3d_cs
);
379 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
383 for (i
= 0; i
< wndproc_table
.count
; ++i
)
385 if (wndproc_table
.entries
[i
].window
== window
)
387 return &wndproc_table
.entries
[i
];
394 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
396 struct wined3d_wndproc
*entry
;
397 IWineD3DDeviceImpl
*device
;
401 wined3d_mutex_lock();
402 entry
= wined3d_find_wndproc(window
);
406 wined3d_mutex_unlock();
407 ERR("Window %p is not registered with wined3d.\n", window
);
408 return DefWindowProcW(window
, message
, wparam
, lparam
);
411 device
= entry
->device
;
412 unicode
= entry
->unicode
;
414 wined3d_mutex_unlock();
416 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
419 BOOL
wined3d_register_window(HWND window
, IWineD3DDeviceImpl
*device
)
421 struct wined3d_wndproc
*entry
;
423 wined3d_mutex_lock();
425 if (wined3d_find_wndproc(window
))
427 wined3d_mutex_unlock();
428 WARN("Window %p is already registered with wined3d.\n", window
);
432 if (wndproc_table
.size
== wndproc_table
.count
)
434 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
435 struct wined3d_wndproc
*new_entries
;
437 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
438 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
442 wined3d_mutex_unlock();
443 ERR("Failed to grow table.\n");
447 wndproc_table
.entries
= new_entries
;
448 wndproc_table
.size
= new_size
;
451 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
452 entry
->window
= window
;
453 entry
->unicode
= IsWindowUnicode(window
);
454 /* Set a window proc that matches the window. Some applications (e.g. NoX)
455 * replace the window proc after we've set ours, and expect to be able to
456 * call the previous one (ours) directly, without using CallWindowProc(). */
458 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
460 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
461 entry
->device
= device
;
463 wined3d_mutex_unlock();
468 void wined3d_unregister_window(HWND window
)
470 struct wined3d_wndproc
*entry
, *last
;
473 wined3d_mutex_lock();
475 if (!(entry
= wined3d_find_wndproc(window
)))
477 wined3d_mutex_unlock();
478 ERR("Window %p is not registered with wined3d.\n", window
);
484 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
485 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
487 wined3d_mutex_unlock();
488 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
489 window
, proc
, wined3d_wndproc
);
493 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
497 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
498 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
500 wined3d_mutex_unlock();
501 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
502 window
, proc
, wined3d_wndproc
);
506 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
509 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
510 if (entry
!= last
) *entry
= *last
;
512 wined3d_mutex_unlock();
515 /* At process attach */
516 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
518 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason
);
522 case DLL_PROCESS_ATTACH
:
523 return wined3d_dll_init(hInstDLL
);
525 case DLL_PROCESS_DETACH
:
526 return wined3d_dll_destroy(hInstDLL
);
528 case DLL_THREAD_DETACH
:
530 if (!context_set_current(NULL
))
532 ERR("Failed to clear current context.\n");