shdocvw: Support URLs passed by reference in WebBrowser_Navigate2.
[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  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25
26 #include "initguid.h"
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 int num_lock = 0;
32 void (*CDECL wine_tsx11_lock_ptr)(void) = NULL;
33 void (*CDECL wine_tsx11_unlock_ptr)(void) = NULL;
34
35
36 /* When updating default value here, make sure to update winecfg as well,
37  * where appropriate. */
38 wined3d_settings_t wined3d_settings = 
39 {
40     VS_HW,          /* Hardware by default */
41     PS_HW,          /* Hardware by default */
42     TRUE,           /* Use of GLSL enabled by default */
43     ORM_FBO,        /* Use FBOs to do offscreen rendering */
44     RTL_READTEX,    /* Default render target locking method */
45     PCI_VENDOR_NONE,/* PCI Vendor ID */
46     PCI_DEVICE_NONE,/* PCI Device ID */
47     0,              /* The default of memory is set in FillGLCaps */
48     NULL,           /* No wine logo by default */
49     FALSE           /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
50 };
51
52 IWineD3D* WINAPI WineDirect3DCreate(UINT dxVersion, IUnknown *parent) {
53     IWineD3DImpl* object;
54
55     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
56     object->lpVtbl = &IWineD3D_Vtbl;
57     object->dxVersion = dxVersion;
58     object->ref = 1;
59     object->parent = parent;
60
61     if (!InitAdapters(object))
62     {
63         WARN("Failed to initialize direct3d adapters, Direct3D will not be available\n");
64         if (dxVersion > 7)
65         {
66             ERR("Direct3D%d is not available without opengl\n", dxVersion);
67             HeapFree(GetProcessHeap(), 0, object);
68             return NULL;
69         }
70     }
71
72     TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
73
74     return (IWineD3D *)object;
75 }
76
77 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
78 {
79     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
80     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
81     return ERROR_FILE_NOT_FOUND;
82 }
83
84 static inline DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char* name, DWORD *data)
85 {
86     DWORD type;
87     DWORD size = sizeof(DWORD);
88     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
89     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
90     return ERROR_FILE_NOT_FOUND;
91 }
92
93 static void CDECL wined3d_do_nothing(void)
94 {
95 }
96
97 static BOOL wined3d_init(HINSTANCE hInstDLL)
98 {
99     DWORD wined3d_context_tls_idx;
100     HMODULE mod;
101     char buffer[MAX_PATH+10];
102     DWORD size = sizeof(buffer);
103     HKEY hkey = 0;
104     HKEY appkey = 0;
105     DWORD len, tmpvalue;
106     WNDCLASSA wc;
107
108     wined3d_context_tls_idx = TlsAlloc();
109     if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
110     {
111         DWORD err = GetLastError();
112         ERR("Failed to allocate context TLS index, err %#x.\n", err);
113         return FALSE;
114     }
115     context_set_tls_idx(wined3d_context_tls_idx);
116
117     /* We need our own window class for a fake window which we use to retrieve GL capabilities */
118     /* We might need CS_OWNDC in the future if we notice strange things on Windows.
119      * Various articles/posts about OpenGL problems on Windows recommend this. */
120     wc.style                = CS_HREDRAW | CS_VREDRAW;
121     wc.lpfnWndProc          = DefWindowProcA;
122     wc.cbClsExtra           = 0;
123     wc.cbWndExtra           = 0;
124     wc.hInstance            = hInstDLL;
125     wc.hIcon                = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
126     wc.hCursor              = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
127     wc.hbrBackground        = NULL;
128     wc.lpszMenuName         = NULL;
129     wc.lpszClassName        = WINED3D_OPENGL_WINDOW_CLASS_NAME;
130
131     if (!RegisterClassA(&wc))
132     {
133         ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
134         if (!TlsFree(wined3d_context_tls_idx))
135         {
136             DWORD err = GetLastError();
137             ERR("Failed to free context TLS index, err %#x.\n", err);
138         }
139         return FALSE;
140     }
141
142     DisableThreadLibraryCalls(hInstDLL);
143
144     mod = GetModuleHandleA( "winex11.drv" );
145     if (mod)
146     {
147         wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
148         wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
149     }
150     else /* We are most likely on Windows */
151     {
152         wine_tsx11_lock_ptr   = wined3d_do_nothing;
153         wine_tsx11_unlock_ptr = wined3d_do_nothing;
154     }
155     /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
156     if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
157
158     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
159     if (len && len < MAX_PATH)
160     {
161         HKEY tmpkey;
162         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
163         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
164         {
165             char *p, *appname = buffer;
166             if ((p = strrchr( appname, '/' ))) appname = p + 1;
167             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
168             strcat( appname, "\\Direct3D" );
169             TRACE("appname = [%s]\n", appname);
170             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
171             RegCloseKey( tmpkey );
172         }
173     }
174
175     if ( 0 != hkey || 0 != appkey )
176     {
177         if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
178         {
179             if (!strcmp(buffer,"none"))
180             {
181                 TRACE("Disable vertex shaders\n");
182                 wined3d_settings.vs_mode = VS_NONE;
183             }
184         }
185         if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
186         {
187             if (!strcmp(buffer,"enabled"))
188             {
189                 TRACE("Allow pixel shaders\n");
190                 wined3d_settings.ps_mode = PS_HW;
191             }
192             if (!strcmp(buffer,"disabled"))
193             {
194                 TRACE("Disable pixel shaders\n");
195                 wined3d_settings.ps_mode = PS_NONE;
196             }
197         }
198         if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
199         {
200             if (!strcmp(buffer,"disabled"))
201             {
202                 TRACE("Use of GL Shading Language disabled\n");
203                 wined3d_settings.glslRequested = FALSE;
204             }
205         }
206         if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
207         {
208             if (!strcmp(buffer,"backbuffer"))
209             {
210                 TRACE("Using the backbuffer for offscreen rendering\n");
211                 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
212             }
213             else if (!strcmp(buffer,"pbuffer"))
214             {
215                 TRACE("Using PBuffers for offscreen rendering\n");
216                 wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
217             }
218             else if (!strcmp(buffer,"fbo"))
219             {
220                 TRACE("Using FBOs for offscreen rendering\n");
221                 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
222             }
223         }
224         if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
225         {
226             if (!strcmp(buffer,"disabled"))
227             {
228                 TRACE("Disabling render target locking\n");
229                 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
230             }
231             else if (!strcmp(buffer,"readdraw"))
232             {
233                 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
234                 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
235             }
236             else if (!strcmp(buffer,"readtex"))
237             {
238                 TRACE("Using glReadPixels for render target reading and textures for writing\n");
239                 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
240             }
241         }
242         if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
243         {
244             int pci_device_id = tmpvalue;
245
246             /* A pci device id is 16-bit */
247             if(pci_device_id > 0xffff)
248             {
249                 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
250             }
251             else
252             {
253                 TRACE("Using PCI Device ID %04x\n", pci_device_id);
254                 wined3d_settings.pci_device_id = pci_device_id;
255             }
256         }
257         if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
258         {
259             int pci_vendor_id = tmpvalue;
260
261             /* A pci device id is 16-bit */
262             if(pci_vendor_id > 0xffff)
263             {
264                 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
265             }
266             else
267             {
268                 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
269                 wined3d_settings.pci_vendor_id = pci_vendor_id;
270             }
271         }
272         if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
273         {
274             int TmpVideoMemorySize = atoi(buffer);
275             if(TmpVideoMemorySize > 0)
276             {
277                 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
278                 TRACE("Use %iMB = %d byte for emulated_textureram\n",
279                         TmpVideoMemorySize,
280                         wined3d_settings.emulated_textureram);
281             }
282             else
283                 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
284         }
285         if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
286         {
287             wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, strlen(buffer) + 1);
288             if(wined3d_settings.logo) strcpy(wined3d_settings.logo, buffer);
289         }
290         if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
291         {
292             if (!strcmp(buffer,"enabled"))
293             {
294                 TRACE("Allow multisampling\n");
295                 wined3d_settings.allow_multisampling = TRUE;
296             }
297         }
298     }
299     if (wined3d_settings.vs_mode == VS_HW)
300         TRACE("Allow HW vertex shaders\n");
301     if (wined3d_settings.ps_mode == PS_NONE)
302         TRACE("Disable pixel shaders\n");
303     if (wined3d_settings.glslRequested)
304         TRACE("If supported by your system, GL Shading Language will be used\n");
305
306     if (appkey) RegCloseKey( appkey );
307     if (hkey) RegCloseKey( hkey );
308
309     return TRUE;
310 }
311
312 static BOOL wined3d_destroy(HINSTANCE hInstDLL)
313 {
314     DWORD wined3d_context_tls_idx = context_get_tls_idx();
315
316     if (!TlsFree(wined3d_context_tls_idx))
317     {
318         DWORD err = GetLastError();
319         ERR("Failed to free context TLS index, err %#x.\n", err);
320     }
321
322     HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
323     UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
324
325     return TRUE;
326 }
327
328 /* At process attach */
329 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
330 {
331     TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
332
333     switch (fdwReason)
334     {
335         case DLL_PROCESS_ATTACH:
336             return wined3d_init(hInstDLL);
337
338         case DLL_PROCESS_DETACH:
339             return wined3d_destroy(hInstDLL);
340
341         case DLL_THREAD_DETACH:
342         {
343             if (!context_set_current(NULL))
344             {
345                 ERR("Failed to clear current context.\n");
346             }
347             return TRUE;
348         }
349
350         default:
351             return TRUE;
352     }
353 }