VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c@ 30585

Last change on this file since 30585 was 28747, checked in by vboxsync, 15 years ago

crOpenGL: workaround for googlearth in d3d mode

  • Property svn:eol-style set to native
File size: 16.5 KB
Line 
1/*
2 * Direct3D wine internal interface main
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25/*
26 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35
36#include "initguid.h"
37#include "wined3d_private.h"
38
39WINE_DEFAULT_DEBUG_CHANNEL(d3d);
40
41struct wined3d_wndproc
42{
43 HWND window;
44 WNDPROC proc;
45 IWineD3DDeviceImpl *device;
46};
47
48struct wined3d_wndproc_table
49{
50 struct wined3d_wndproc *entries;
51 unsigned int count;
52 unsigned int size;
53};
54
55static struct wined3d_wndproc_table wndproc_table;
56
57int num_lock = 0;
58void (*CDECL wine_tsx11_lock_ptr)(void) = NULL;
59void (*CDECL wine_tsx11_unlock_ptr)(void) = NULL;
60
61static CRITICAL_SECTION wined3d_cs;
62static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
63{
64 0, 0, &wined3d_cs,
65 {&wined3d_cs_debug.ProcessLocksList,
66 &wined3d_cs_debug.ProcessLocksList},
67 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
68};
69static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
70
71/* When updating default value here, make sure to update winecfg as well,
72 * where appropriate. */
73wined3d_settings_t wined3d_settings =
74{
75 VS_HW, /* Hardware by default */
76 PS_HW, /* Hardware by default */
77 TRUE, /* Use of GLSL enabled by default */
78 ORM_BACKBUFFER, /* Use Backbuffer to do offscreen rendering */
79 RTL_READTEX, /* Default render target locking method */
80 PCI_VENDOR_NONE,/* PCI Vendor ID */
81 PCI_DEVICE_NONE,/* PCI Device ID */
82 0, /* The default of memory is set in FillGLCaps */
83 NULL, /* No wine logo by default */
84 FALSE, /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
85 FALSE, /* No strict draw ordering. */
86};
87
88IWineD3D * WINAPI WineDirect3DCreate(UINT version, IUnknown *parent)
89{
90 IWineD3DImpl *object;
91 HRESULT hr;
92
93 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
94 if (!object)
95 {
96 ERR("Failed to allocate wined3d object memory.\n");
97 return NULL;
98 }
99
100 hr = wined3d_init(object, version, parent);
101 if (FAILED(hr))
102 {
103 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
104 HeapFree(GetProcessHeap(), 0, object);
105 return NULL;
106 }
107
108 TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
109
110 return (IWineD3D *)object;
111}
112
113static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
114{
115 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
116 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
117 return ERROR_FILE_NOT_FOUND;
118}
119
120static inline DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char* name, DWORD *data)
121{
122 DWORD type;
123 DWORD size = sizeof(DWORD);
124 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
125 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
126 return ERROR_FILE_NOT_FOUND;
127}
128
129static void CDECL wined3d_do_nothing(void)
130{
131}
132
133static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
134{
135 DWORD wined3d_context_tls_idx;
136 HMODULE mod;
137 char buffer[MAX_PATH+10];
138 DWORD size = sizeof(buffer);
139 HKEY hkey = 0;
140 HKEY appkey = 0;
141 DWORD len, tmpvalue;
142 WNDCLASSA wc;
143
144 wined3d_context_tls_idx = TlsAlloc();
145 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
146 {
147 DWORD err = GetLastError();
148 ERR("Failed to allocate context TLS index, err %#x.\n", err);
149 return FALSE;
150 }
151 context_set_tls_idx(wined3d_context_tls_idx);
152
153 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
154 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
155 * Various articles/posts about OpenGL problems on Windows recommend this. */
156 wc.style = CS_HREDRAW | CS_VREDRAW;
157 wc.lpfnWndProc = DefWindowProcA;
158 wc.cbClsExtra = 0;
159 wc.cbWndExtra = 0;
160 wc.hInstance = hInstDLL;
161 wc.hIcon = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
162 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
163 wc.hbrBackground = NULL;
164 wc.lpszMenuName = NULL;
165 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
166
167 if (!RegisterClassA(&wc))
168 {
169 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
170 if (!TlsFree(wined3d_context_tls_idx))
171 {
172 DWORD err = GetLastError();
173 ERR("Failed to free context TLS index, err %#x.\n", err);
174 }
175 return FALSE;
176 }
177
178 DisableThreadLibraryCalls(hInstDLL);
179
180 mod = GetModuleHandleA( "winex11.drv" );
181 if (mod)
182 {
183 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
184 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
185 }
186 else /* We are most likely on Windows */
187 {
188 wine_tsx11_lock_ptr = wined3d_do_nothing;
189 wine_tsx11_unlock_ptr = wined3d_do_nothing;
190 }
191 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
192 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
193
194 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
195 if (len && len < MAX_PATH)
196 {
197 HKEY tmpkey;
198 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
199 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
200 {
201 char *p, *appname = buffer;
202 if ((p = strrchr( appname, '/' ))) appname = p + 1;
203 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
204 strcat( appname, "\\Direct3D" );
205 TRACE("appname = [%s]\n", appname);
206 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
207 RegCloseKey( tmpkey );
208 }
209 }
210
211 if ( 0 != hkey || 0 != appkey )
212 {
213 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
214 {
215 if (!strcmp(buffer,"none"))
216 {
217 TRACE("Disable vertex shaders\n");
218 wined3d_settings.vs_mode = VS_NONE;
219 }
220 }
221 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
222 {
223 if (!strcmp(buffer,"enabled"))
224 {
225 TRACE("Allow pixel shaders\n");
226 wined3d_settings.ps_mode = PS_HW;
227 }
228 if (!strcmp(buffer,"disabled"))
229 {
230 TRACE("Disable pixel shaders\n");
231 wined3d_settings.ps_mode = PS_NONE;
232 }
233 }
234 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
235 {
236 if (!strcmp(buffer,"disabled"))
237 {
238 TRACE("Use of GL Shading Language disabled\n");
239 wined3d_settings.glslRequested = FALSE;
240 }
241 }
242 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
243 {
244 if (!strcmp(buffer,"backbuffer"))
245 {
246 TRACE("Using the backbuffer for offscreen rendering\n");
247 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
248 }
249 else if (!strcmp(buffer,"fbo"))
250 {
251 TRACE("Using FBOs for offscreen rendering\n");
252 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
253 }
254 }
255 if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
256 {
257 if (!strcmp(buffer,"disabled"))
258 {
259 TRACE("Disabling render target locking\n");
260 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
261 }
262 else if (!strcmp(buffer,"readdraw"))
263 {
264 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
265 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
266 }
267 else if (!strcmp(buffer,"readtex"))
268 {
269 TRACE("Using glReadPixels for render target reading and textures for writing\n");
270 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
271 }
272 }
273 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
274 {
275 int pci_device_id = tmpvalue;
276
277 /* A pci device id is 16-bit */
278 if(pci_device_id > 0xffff)
279 {
280 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
281 }
282 else
283 {
284 TRACE("Using PCI Device ID %04x\n", pci_device_id);
285 wined3d_settings.pci_device_id = pci_device_id;
286 }
287 }
288 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
289 {
290 int pci_vendor_id = tmpvalue;
291
292 /* A pci device id is 16-bit */
293 if(pci_vendor_id > 0xffff)
294 {
295 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
296 }
297 else
298 {
299 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
300 wined3d_settings.pci_vendor_id = pci_vendor_id;
301 }
302 }
303 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
304 {
305 int TmpVideoMemorySize = atoi(buffer);
306 if(TmpVideoMemorySize > 0)
307 {
308 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
309 TRACE("Use %iMB = %d byte for emulated_textureram\n",
310 TmpVideoMemorySize,
311 wined3d_settings.emulated_textureram);
312 }
313 else
314 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
315 }
316 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
317 {
318 size_t len = strlen(buffer) + 1;
319
320 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
321 if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
322 else memcpy(wined3d_settings.logo, buffer, len);
323 }
324 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
325 {
326 if (!strcmp(buffer,"enabled"))
327 {
328 TRACE("Allow multisampling\n");
329 wined3d_settings.allow_multisampling = TRUE;
330 }
331 }
332 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
333 && !strcmp(buffer,"enabled"))
334 {
335 TRACE("Enforcing strict draw ordering.\n");
336 wined3d_settings.strict_draw_ordering = TRUE;
337 }
338 }
339 if (wined3d_settings.vs_mode == VS_HW)
340 TRACE("Allow HW vertex shaders\n");
341 if (wined3d_settings.ps_mode == PS_NONE)
342 TRACE("Disable pixel shaders\n");
343 if (wined3d_settings.glslRequested)
344 TRACE("If supported by your system, GL Shading Language will be used\n");
345
346 if (appkey) RegCloseKey( appkey );
347 if (hkey) RegCloseKey( hkey );
348
349 return TRUE;
350}
351
352static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
353{
354 DWORD wined3d_context_tls_idx = context_get_tls_idx();
355 unsigned int i;
356
357 if (!TlsFree(wined3d_context_tls_idx))
358 {
359 DWORD err = GetLastError();
360 ERR("Failed to free context TLS index, err %#x.\n", err);
361 }
362
363 for (i = 0; i < wndproc_table.count; ++i)
364 {
365 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
366 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
367 }
368 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
369
370 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
371 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
372
373 return TRUE;
374}
375
376void WINAPI wined3d_mutex_lock(void)
377{
378 EnterCriticalSection(&wined3d_cs);
379}
380
381void WINAPI wined3d_mutex_unlock(void)
382{
383 LeaveCriticalSection(&wined3d_cs);
384}
385
386static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
387{
388 unsigned int i;
389
390 for (i = 0; i < wndproc_table.count; ++i)
391 {
392 if (wndproc_table.entries[i].window == window)
393 {
394 return &wndproc_table.entries[i];
395 }
396 }
397
398 return NULL;
399}
400
401static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
402{
403 struct wined3d_wndproc *entry;
404 IWineD3DDeviceImpl *device;
405 WNDPROC proc;
406
407 wined3d_mutex_lock();
408 entry = wined3d_find_wndproc(window);
409
410 if (!entry)
411 {
412 wined3d_mutex_unlock();
413 ERR("Window %p is not registered with wined3d.\n", window);
414 return DefWindowProcW(window, message, wparam, lparam);
415 }
416
417 device = entry->device;
418 proc = entry->proc;
419 wined3d_mutex_unlock();
420
421 return device_process_message(device, window, message, wparam, lparam, proc);
422}
423
424BOOL wined3d_register_window(HWND window, IWineD3DDeviceImpl *device)
425{
426 struct wined3d_wndproc *entry;
427
428 wined3d_mutex_lock();
429
430 if (wndproc_table.size == wndproc_table.count)
431 {
432 unsigned int new_size = max(1, wndproc_table.size * 2);
433 struct wined3d_wndproc *new_entries;
434
435 if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
436 else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
437
438 if (!new_entries)
439 {
440 wined3d_mutex_unlock();
441 ERR("Failed to grow table.\n");
442 return FALSE;
443 }
444
445 wndproc_table.entries = new_entries;
446 wndproc_table.size = new_size;
447 }
448
449 entry = &wndproc_table.entries[wndproc_table.count++];
450 entry->window = window;
451 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
452 entry->device = device;
453
454 wined3d_mutex_unlock();
455
456 return TRUE;
457}
458
459void wined3d_unregister_window(HWND window)
460{
461 unsigned int i;
462
463 wined3d_mutex_lock();
464 for (i = 0; i < wndproc_table.count; ++i)
465 {
466 if (wndproc_table.entries[i].window == window)
467 {
468 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
469 struct wined3d_wndproc *last = &wndproc_table.entries[--wndproc_table.count];
470
471 if (GetWindowLongPtrW(window, GWLP_WNDPROC) == (LONG_PTR)wined3d_wndproc)
472 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
473 if (entry != last) *entry = *last;
474 wined3d_mutex_unlock();
475
476 return;
477 }
478 }
479 wined3d_mutex_unlock();
480
481 ERR("Window %p is not registered with wined3d.\n", window);
482}
483
484/* At process attach */
485BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
486{
487 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
488
489 switch (fdwReason)
490 {
491 case DLL_PROCESS_ATTACH:
492 return wined3d_dll_init(hInstDLL);
493
494 case DLL_PROCESS_DETACH:
495 return wined3d_dll_destroy(hInstDLL);
496
497 case DLL_THREAD_DETACH:
498 {
499 if (!context_set_current(NULL))
500 {
501 ERR("Failed to clear current context.\n");
502 }
503 return TRUE;
504 }
505
506 default:
507 return TRUE;
508 }
509}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette