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