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
26 #include "wine/port.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
34 struct wined3d_wndproc
39 struct wined3d_device
*device
;
42 struct wined3d_wndproc_table
44 struct wined3d_wndproc
*entries
;
49 static struct wined3d_wndproc_table wndproc_table
;
52 void (CDECL
*wine_tsx11_lock_ptr
)(void) = NULL
;
53 void (CDECL
*wine_tsx11_unlock_ptr
)(void) = NULL
;
55 static CRITICAL_SECTION wined3d_cs
;
56 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
59 {&wined3d_cs_debug
.ProcessLocksList
,
60 &wined3d_cs_debug
.ProcessLocksList
},
61 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
63 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
65 static CRITICAL_SECTION wined3d_wndproc_cs
;
66 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug
=
68 0, 0, &wined3d_wndproc_cs
,
69 {&wined3d_wndproc_cs_debug
.ProcessLocksList
,
70 &wined3d_wndproc_cs_debug
.ProcessLocksList
},
71 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_wndproc_cs")}
73 static CRITICAL_SECTION wined3d_wndproc_cs
= {&wined3d_wndproc_cs_debug
, -1, 0, 0, 0, 0};
75 /* When updating default value here, make sure to update winecfg as well,
76 * where appropriate. */
77 struct wined3d_settings wined3d_settings
=
79 VS_HW
, /* Hardware by default */
80 PS_HW
, /* Hardware by default */
81 TRUE
, /* Use of GLSL enabled by default */
82 ORM_FBO
, /* Use FBOs to do offscreen rendering */
83 RTL_READTEX
, /* Default render target locking method */
84 PCI_VENDOR_NONE
,/* PCI Vendor ID */
85 PCI_DEVICE_NONE
,/* PCI Device ID */
86 0, /* The default of memory is set in init_driver_info */
87 NULL
, /* No wine logo by default */
88 TRUE
, /* Multisampling enabled by default. */
89 FALSE
, /* No strict draw ordering. */
90 FALSE
, /* Try to render onscreen by default. */
93 /* Do not call while under the GL lock. */
94 struct wined3d
* CDECL
wined3d_create(UINT version
, DWORD flags
)
96 struct wined3d
*object
;
99 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
102 ERR("Failed to allocate wined3d object memory.\n");
106 hr
= wined3d_init(object
, version
, flags
);
109 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
110 HeapFree(GetProcessHeap(), 0, object
);
114 TRACE("Created wined3d object %p for d3d%d support.\n", object
, version
);
119 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
121 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
122 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
123 return ERROR_FILE_NOT_FOUND
;
126 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
129 DWORD size
= sizeof(DWORD
);
130 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
131 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
132 return ERROR_FILE_NOT_FOUND
;
135 static void CDECL
wined3d_do_nothing(void)
139 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
141 DWORD wined3d_context_tls_idx
;
143 char buffer
[MAX_PATH
+10];
144 DWORD size
= sizeof(buffer
);
150 wined3d_context_tls_idx
= TlsAlloc();
151 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
153 DWORD err
= GetLastError();
154 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
157 context_set_tls_idx(wined3d_context_tls_idx
);
159 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
160 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
161 * Various articles/posts about OpenGL problems on Windows recommend this. */
162 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
163 wc
.lpfnWndProc
= DefWindowProcA
;
166 wc
.hInstance
= hInstDLL
;
167 wc
.hIcon
= LoadIconA(NULL
, (LPCSTR
)IDI_WINLOGO
);
168 wc
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)IDC_ARROW
);
169 wc
.hbrBackground
= NULL
;
170 wc
.lpszMenuName
= NULL
;
171 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
173 if (!RegisterClassA(&wc
))
175 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
176 if (!TlsFree(wined3d_context_tls_idx
))
178 DWORD err
= GetLastError();
179 ERR("Failed to free context TLS index, err %#x.\n", err
);
184 DisableThreadLibraryCalls(hInstDLL
);
186 mod
= GetModuleHandleA( "winex11.drv" );
189 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
190 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
192 else /* We are most likely on Windows */
194 wine_tsx11_lock_ptr
= wined3d_do_nothing
;
195 wine_tsx11_unlock_ptr
= wined3d_do_nothing
;
197 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
198 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
200 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
201 if (len
&& len
< MAX_PATH
)
204 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
205 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
207 char *p
, *appname
= buffer
;
208 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
209 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
210 strcat( appname
, "\\Direct3D" );
211 TRACE("appname = [%s]\n", appname
);
212 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
213 RegCloseKey( tmpkey
);
219 if ( !get_config_key( hkey
, appkey
, "VertexShaderMode", buffer
, size
) )
221 if (!strcmp(buffer
,"none"))
223 TRACE("Disable vertex shaders\n");
224 wined3d_settings
.vs_mode
= VS_NONE
;
227 if ( !get_config_key( hkey
, appkey
, "PixelShaderMode", buffer
, size
) )
229 if (!strcmp(buffer
,"enabled"))
231 TRACE("Allow pixel shaders\n");
232 wined3d_settings
.ps_mode
= PS_HW
;
234 if (!strcmp(buffer
,"disabled"))
236 TRACE("Disable pixel shaders\n");
237 wined3d_settings
.ps_mode
= PS_NONE
;
240 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
242 if (!strcmp(buffer
,"disabled"))
244 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
245 TRACE("Use of GL Shading Language disabled\n");
246 wined3d_settings
.glslRequested
= FALSE
;
249 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
251 if (!strcmp(buffer
,"backbuffer"))
253 TRACE("Using the backbuffer for offscreen rendering\n");
254 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
256 else if (!strcmp(buffer
,"fbo"))
258 TRACE("Using FBOs for offscreen rendering\n");
259 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
262 if ( !get_config_key( hkey
, appkey
, "RenderTargetLockMode", buffer
, size
) )
264 if (!strcmp(buffer
,"readdraw"))
266 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
267 wined3d_settings
.rendertargetlock_mode
= RTL_READDRAW
;
269 else if (!strcmp(buffer
,"readtex"))
271 TRACE("Using glReadPixels for render target reading and textures for writing\n");
272 wined3d_settings
.rendertargetlock_mode
= RTL_READTEX
;
275 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
277 int pci_device_id
= tmpvalue
;
279 /* A pci device id is 16-bit */
280 if(pci_device_id
> 0xffff)
282 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
286 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
287 wined3d_settings
.pci_device_id
= pci_device_id
;
290 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
292 int pci_vendor_id
= tmpvalue
;
294 /* A pci device id is 16-bit */
295 if(pci_vendor_id
> 0xffff)
297 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
301 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
302 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
305 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
307 int TmpVideoMemorySize
= atoi(buffer
);
308 if(TmpVideoMemorySize
> 0)
310 wined3d_settings
.emulated_textureram
= TmpVideoMemorySize
*1024*1024;
311 TRACE("Use %iMB = %d byte for emulated_textureram\n",
313 wined3d_settings
.emulated_textureram
);
316 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
318 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
320 size_t len
= strlen(buffer
) + 1;
322 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
323 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
324 else memcpy(wined3d_settings
.logo
, buffer
, len
);
326 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
328 if (!strcmp(buffer
, "disabled"))
330 TRACE("Multisampling disabled.\n");
331 wined3d_settings
.allow_multisampling
= FALSE
;
334 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
335 && !strcmp(buffer
,"enabled"))
337 TRACE("Enforcing strict draw ordering.\n");
338 wined3d_settings
.strict_draw_ordering
= TRUE
;
340 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
341 && !strcmp(buffer
,"enabled"))
343 TRACE("Always rendering backbuffers offscreen.\n");
344 wined3d_settings
.always_offscreen
= TRUE
;
347 if (wined3d_settings
.vs_mode
== VS_HW
)
348 TRACE("Allow HW vertex shaders\n");
349 if (wined3d_settings
.ps_mode
== PS_NONE
)
350 TRACE("Disable pixel shaders\n");
351 if (wined3d_settings
.glslRequested
)
352 TRACE("If supported by your system, GL Shading Language will be used\n");
354 if (appkey
) RegCloseKey( appkey
);
355 if (hkey
) RegCloseKey( hkey
);
360 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
362 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
365 if (!TlsFree(wined3d_context_tls_idx
))
367 DWORD err
= GetLastError();
368 ERR("Failed to free context TLS index, err %#x.\n", err
);
371 for (i
= 0; i
< wndproc_table
.count
; ++i
)
373 /* Trying to unregister these would be futile. These entries can only
374 * exist if either we skipped them in wined3d_unregister_window() due
375 * to the application replacing the wndproc after the entry was
376 * registered, or if the application still has an active wined3d
377 * device. In the latter case the application has bigger problems than
379 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
381 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
383 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
384 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
386 DeleteCriticalSection(&wined3d_wndproc_cs
);
387 DeleteCriticalSection(&wined3d_cs
);
391 void WINAPI
wined3d_mutex_lock(void)
393 EnterCriticalSection(&wined3d_cs
);
396 void WINAPI
wined3d_mutex_unlock(void)
398 LeaveCriticalSection(&wined3d_cs
);
401 static void wined3d_wndproc_mutex_lock(void)
403 EnterCriticalSection(&wined3d_wndproc_cs
);
406 static void wined3d_wndproc_mutex_unlock(void)
408 LeaveCriticalSection(&wined3d_wndproc_cs
);
411 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
415 for (i
= 0; i
< wndproc_table
.count
; ++i
)
417 if (wndproc_table
.entries
[i
].window
== window
)
419 return &wndproc_table
.entries
[i
];
426 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
428 struct wined3d_wndproc
*entry
;
429 struct wined3d_device
*device
;
433 wined3d_wndproc_mutex_lock();
434 entry
= wined3d_find_wndproc(window
);
438 wined3d_wndproc_mutex_unlock();
439 ERR("Window %p is not registered with wined3d.\n", window
);
440 return DefWindowProcW(window
, message
, wparam
, lparam
);
443 device
= entry
->device
;
444 unicode
= entry
->unicode
;
446 wined3d_wndproc_mutex_unlock();
449 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
451 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
452 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
455 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
457 struct wined3d_wndproc
*entry
;
459 wined3d_wndproc_mutex_lock();
461 if (wined3d_find_wndproc(window
))
463 wined3d_wndproc_mutex_unlock();
464 WARN("Window %p is already registered with wined3d.\n", window
);
468 if (wndproc_table
.size
== wndproc_table
.count
)
470 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
471 struct wined3d_wndproc
*new_entries
;
473 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
474 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
478 wined3d_wndproc_mutex_unlock();
479 ERR("Failed to grow table.\n");
483 wndproc_table
.entries
= new_entries
;
484 wndproc_table
.size
= new_size
;
487 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
488 entry
->window
= window
;
489 entry
->unicode
= IsWindowUnicode(window
);
490 /* Set a window proc that matches the window. Some applications (e.g. NoX)
491 * replace the window proc after we've set ours, and expect to be able to
492 * call the previous one (ours) directly, without using CallWindowProc(). */
494 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
496 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
497 entry
->device
= device
;
499 wined3d_wndproc_mutex_unlock();
504 void wined3d_unregister_window(HWND window
)
506 struct wined3d_wndproc
*entry
, *last
;
509 wined3d_wndproc_mutex_lock();
511 if (!(entry
= wined3d_find_wndproc(window
)))
513 wined3d_wndproc_mutex_unlock();
514 ERR("Window %p is not registered with wined3d.\n", window
);
520 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
521 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
523 entry
->device
= NULL
;
524 wined3d_wndproc_mutex_unlock();
525 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
526 window
, proc
, wined3d_wndproc
);
530 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
534 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
535 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
537 entry
->device
= NULL
;
538 wined3d_wndproc_mutex_unlock();
539 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
540 window
, proc
, wined3d_wndproc
);
544 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
547 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
548 if (entry
!= last
) *entry
= *last
;
550 wined3d_wndproc_mutex_unlock();
553 /* At process attach */
554 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
556 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason
);
560 case DLL_PROCESS_ATTACH
:
561 return wined3d_dll_init(hInstDLL
);
563 case DLL_PROCESS_DETACH
:
564 return wined3d_dll_destroy(hInstDLL
);
566 case DLL_THREAD_DETACH
:
568 if (!context_set_current(NULL
))
570 ERR("Failed to clear current context.\n");