VirtualBox

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

最後變更 在這個檔案從35825是 34943,由 vboxsync 提交於 14 年 前

crOpenGL/wined3d: fix incorrect rendering for some d3d8/9 apps

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

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