VirtualBox

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

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

crOpenGL: update to wine 1.1.36 and disable unnecessary fbo state poll

  • 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_FBO, /* Use FBOs 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};
86
87IWineD3D* WINAPI WineDirect3DCreate(UINT dxVersion, IUnknown *parent) {
88 IWineD3DImpl* object;
89
90 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
91 object->lpVtbl = &IWineD3D_Vtbl;
92 object->dxVersion = dxVersion;
93 object->ref = 1;
94 object->parent = parent;
95
96 if (!InitAdapters(object))
97 {
98 WARN("Failed to initialize direct3d adapters, Direct3D will not be available\n");
99 if (dxVersion > 7)
100 {
101 ERR("Direct3D%d is not available without opengl\n", dxVersion);
102 HeapFree(GetProcessHeap(), 0, object);
103 return NULL;
104 }
105 }
106
107 TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
108
109 return (IWineD3D *)object;
110}
111
112static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
113{
114 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
115 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
116 return ERROR_FILE_NOT_FOUND;
117}
118
119static inline DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char* name, DWORD *data)
120{
121 DWORD type;
122 DWORD size = sizeof(DWORD);
123 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
124 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
125 return ERROR_FILE_NOT_FOUND;
126}
127
128static void CDECL wined3d_do_nothing(void)
129{
130}
131
132static BOOL wined3d_init(HINSTANCE hInstDLL)
133{
134 DWORD wined3d_context_tls_idx;
135 HMODULE mod;
136 char buffer[MAX_PATH+10];
137 DWORD size = sizeof(buffer);
138 HKEY hkey = 0;
139 HKEY appkey = 0;
140 DWORD len, tmpvalue;
141 WNDCLASSA wc;
142
143 wined3d_context_tls_idx = TlsAlloc();
144 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
145 {
146 DWORD err = GetLastError();
147 ERR("Failed to allocate context TLS index, err %#x.\n", err);
148 return FALSE;
149 }
150 context_set_tls_idx(wined3d_context_tls_idx);
151
152 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
153 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
154 * Various articles/posts about OpenGL problems on Windows recommend this. */
155 wc.style = CS_HREDRAW | CS_VREDRAW;
156 wc.lpfnWndProc = DefWindowProcA;
157 wc.cbClsExtra = 0;
158 wc.cbWndExtra = 0;
159 wc.hInstance = hInstDLL;
160 wc.hIcon = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
161 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
162 wc.hbrBackground = NULL;
163 wc.lpszMenuName = NULL;
164 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
165
166 if (!RegisterClassA(&wc))
167 {
168 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
169 if (!TlsFree(wined3d_context_tls_idx))
170 {
171 DWORD err = GetLastError();
172 ERR("Failed to free context TLS index, err %#x.\n", err);
173 }
174 return FALSE;
175 }
176
177 DisableThreadLibraryCalls(hInstDLL);
178
179 mod = GetModuleHandleA( "winex11.drv" );
180 if (mod)
181 {
182 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
183 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
184 }
185 else /* We are most likely on Windows */
186 {
187 wine_tsx11_lock_ptr = wined3d_do_nothing;
188 wine_tsx11_unlock_ptr = wined3d_do_nothing;
189 }
190 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
191 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
192
193 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
194 if (len && len < MAX_PATH)
195 {
196 HKEY tmpkey;
197 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
198 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
199 {
200 char *p, *appname = buffer;
201 if ((p = strrchr( appname, '/' ))) appname = p + 1;
202 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
203 strcat( appname, "\\Direct3D" );
204 TRACE("appname = [%s]\n", appname);
205 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
206 RegCloseKey( tmpkey );
207 }
208 }
209
210 if ( 0 != hkey || 0 != appkey )
211 {
212 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
213 {
214 if (!strcmp(buffer,"none"))
215 {
216 TRACE("Disable vertex shaders\n");
217 wined3d_settings.vs_mode = VS_NONE;
218 }
219 }
220 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
221 {
222 if (!strcmp(buffer,"enabled"))
223 {
224 TRACE("Allow pixel shaders\n");
225 wined3d_settings.ps_mode = PS_HW;
226 }
227 if (!strcmp(buffer,"disabled"))
228 {
229 TRACE("Disable pixel shaders\n");
230 wined3d_settings.ps_mode = PS_NONE;
231 }
232 }
233 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
234 {
235 if (!strcmp(buffer,"disabled"))
236 {
237 TRACE("Use of GL Shading Language disabled\n");
238 wined3d_settings.glslRequested = FALSE;
239 }
240 }
241 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
242 {
243 if (!strcmp(buffer,"backbuffer"))
244 {
245 TRACE("Using the backbuffer for offscreen rendering\n");
246 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
247 }
248 else if (!strcmp(buffer,"pbuffer"))
249 {
250 TRACE("Using PBuffers for offscreen rendering\n");
251 wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
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 }
337 if (wined3d_settings.vs_mode == VS_HW)
338 TRACE("Allow HW vertex shaders\n");
339 if (wined3d_settings.ps_mode == PS_NONE)
340 TRACE("Disable pixel shaders\n");
341 if (wined3d_settings.glslRequested)
342 TRACE("If supported by your system, GL Shading Language will be used\n");
343
344 if (appkey) RegCloseKey( appkey );
345 if (hkey) RegCloseKey( hkey );
346
347 return TRUE;
348}
349
350static BOOL wined3d_destroy(HINSTANCE hInstDLL)
351{
352 DWORD wined3d_context_tls_idx = context_get_tls_idx();
353 unsigned int i;
354
355 if (!TlsFree(wined3d_context_tls_idx))
356 {
357 DWORD err = GetLastError();
358 ERR("Failed to free context TLS index, err %#x.\n", err);
359 }
360
361 for (i = 0; i < wndproc_table.count; ++i)
362 {
363 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
364 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
365 }
366 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
367
368 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
369 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
370
371 return TRUE;
372}
373
374void WINAPI wined3d_mutex_lock(void)
375{
376 EnterCriticalSection(&wined3d_cs);
377}
378
379void WINAPI wined3d_mutex_unlock(void)
380{
381 LeaveCriticalSection(&wined3d_cs);
382}
383
384static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
385{
386 unsigned int i;
387
388 for (i = 0; i < wndproc_table.count; ++i)
389 {
390 if (wndproc_table.entries[i].window == window)
391 {
392 return &wndproc_table.entries[i];
393 }
394 }
395
396 return NULL;
397}
398
399static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
400{
401 struct wined3d_wndproc *entry;
402 IWineD3DDeviceImpl *device;
403 WNDPROC proc;
404
405 wined3d_mutex_lock();
406 entry = wined3d_find_wndproc(window);
407
408 if (!entry)
409 {
410 wined3d_mutex_unlock();
411 ERR("Window %p is not registered with wined3d.\n", window);
412 return DefWindowProcW(window, message, wparam, lparam);
413 }
414
415 device = entry->device;
416 proc = entry->proc;
417 wined3d_mutex_unlock();
418
419 return device_process_message(device, window, message, wparam, lparam, proc);
420}
421
422BOOL wined3d_register_window(HWND window, IWineD3DDeviceImpl *device)
423{
424 struct wined3d_wndproc *entry;
425
426 wined3d_mutex_lock();
427
428 if (wndproc_table.size == wndproc_table.count)
429 {
430 unsigned int new_size = max(1, wndproc_table.size * 2);
431 struct wined3d_wndproc *new_entries;
432
433 if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
434 else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
435
436 if (!new_entries)
437 {
438 wined3d_mutex_unlock();
439 ERR("Failed to grow table.\n");
440 return FALSE;
441 }
442
443 wndproc_table.entries = new_entries;
444 wndproc_table.size = new_size;
445 }
446
447 entry = &wndproc_table.entries[wndproc_table.count++];
448 entry->window = window;
449 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
450 entry->device = device;
451
452 wined3d_mutex_unlock();
453
454 return TRUE;
455}
456
457void wined3d_unregister_window(HWND window)
458{
459 unsigned int i;
460
461 wined3d_mutex_lock();
462 for (i = 0; i < wndproc_table.count; ++i)
463 {
464 if (wndproc_table.entries[i].window == window)
465 {
466 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
467 struct wined3d_wndproc *last = &wndproc_table.entries[--wndproc_table.count];
468
469 if (GetWindowLongPtrW(window, GWLP_WNDPROC) == (LONG_PTR)wined3d_wndproc)
470 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
471 if (entry != last) *entry = *last;
472 wined3d_mutex_unlock();
473
474 return;
475 }
476 }
477 wined3d_mutex_unlock();
478
479 ERR("Window %p is not registered with wined3d.\n", window);
480}
481
482/* At process attach */
483BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
484{
485 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
486
487 switch (fdwReason)
488 {
489 case DLL_PROCESS_ATTACH:
490 return wined3d_init(hInstDLL);
491
492 case DLL_PROCESS_DETACH:
493 return wined3d_destroy(hInstDLL);
494
495 case DLL_THREAD_DETACH:
496 {
497 if (!context_set_current(NULL))
498 {
499 ERR("Failed to clear current context.\n");
500 }
501 return TRUE;
502 }
503
504 default:
505 return TRUE;
506 }
507}
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