2 * Direct3D wine internal interface main
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
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.
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.
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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 void (*CDECL wine_tsx11_lock_ptr)(void) = NULL;
33 void (*CDECL wine_tsx11_unlock_ptr)(void) = NULL;
36 /* When updating default value here, make sure to update winecfg as well,
37 * where appropriate. */
38 wined3d_settings_t wined3d_settings =
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 */
52 IWineD3D* WINAPI WineDirect3DCreate(UINT dxVersion, IUnknown *parent) {
55 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
56 object->lpVtbl = &IWineD3D_Vtbl;
57 object->dxVersion = dxVersion;
59 object->parent = parent;
61 if (!InitAdapters(object))
63 WARN("Failed to initialize direct3d adapters, Direct3D will not be available\n");
66 ERR("Direct3D%d is not available without opengl\n", dxVersion);
67 HeapFree(GetProcessHeap(), 0, object);
72 TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
74 return (IWineD3D *)object;
77 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
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;
84 static inline DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char* name, DWORD *data)
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;
93 static void CDECL wined3d_do_nothing(void)
97 static BOOL wined3d_init(HINSTANCE hInstDLL)
99 DWORD wined3d_context_tls_idx;
101 char buffer[MAX_PATH+10];
102 DWORD size = sizeof(buffer);
108 wined3d_context_tls_idx = TlsAlloc();
109 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
111 DWORD err = GetLastError();
112 ERR("Failed to allocate context TLS index, err %#x.\n", err);
115 context_set_tls_idx(wined3d_context_tls_idx);
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;
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;
131 if (!RegisterClassA(&wc))
133 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
134 if (!TlsFree(wined3d_context_tls_idx))
136 DWORD err = GetLastError();
137 ERR("Failed to free context TLS index, err %#x.\n", err);
142 DisableThreadLibraryCalls(hInstDLL);
144 mod = GetModuleHandleA( "winex11.drv" );
147 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
148 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
150 else /* We are most likely on Windows */
152 wine_tsx11_lock_ptr = wined3d_do_nothing;
153 wine_tsx11_unlock_ptr = wined3d_do_nothing;
155 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
156 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
158 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
159 if (len && len < MAX_PATH)
162 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
163 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
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 );
175 if ( 0 != hkey || 0 != appkey )
177 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
179 if (!strcmp(buffer,"none"))
181 TRACE("Disable vertex shaders\n");
182 wined3d_settings.vs_mode = VS_NONE;
185 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
187 if (!strcmp(buffer,"enabled"))
189 TRACE("Allow pixel shaders\n");
190 wined3d_settings.ps_mode = PS_HW;
192 if (!strcmp(buffer,"disabled"))
194 TRACE("Disable pixel shaders\n");
195 wined3d_settings.ps_mode = PS_NONE;
198 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
200 if (!strcmp(buffer,"disabled"))
202 TRACE("Use of GL Shading Language disabled\n");
203 wined3d_settings.glslRequested = FALSE;
206 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
208 if (!strcmp(buffer,"backbuffer"))
210 TRACE("Using the backbuffer for offscreen rendering\n");
211 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
213 else if (!strcmp(buffer,"pbuffer"))
215 TRACE("Using PBuffers for offscreen rendering\n");
216 wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
218 else if (!strcmp(buffer,"fbo"))
220 TRACE("Using FBOs for offscreen rendering\n");
221 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
224 if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
226 if (!strcmp(buffer,"disabled"))
228 TRACE("Disabling render target locking\n");
229 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
231 else if (!strcmp(buffer,"readdraw"))
233 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
234 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
236 else if (!strcmp(buffer,"readtex"))
238 TRACE("Using glReadPixels for render target reading and textures for writing\n");
239 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
242 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
244 int pci_device_id = tmpvalue;
246 /* A pci device id is 16-bit */
247 if(pci_device_id > 0xffff)
249 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
253 TRACE("Using PCI Device ID %04x\n", pci_device_id);
254 wined3d_settings.pci_device_id = pci_device_id;
257 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
259 int pci_vendor_id = tmpvalue;
261 /* A pci device id is 16-bit */
262 if(pci_vendor_id > 0xffff)
264 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
268 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
269 wined3d_settings.pci_vendor_id = pci_vendor_id;
272 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
274 int TmpVideoMemorySize = atoi(buffer);
275 if(TmpVideoMemorySize > 0)
277 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
278 TRACE("Use %iMB = %d byte for emulated_textureram\n",
280 wined3d_settings.emulated_textureram);
283 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
285 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
287 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, strlen(buffer) + 1);
288 if(wined3d_settings.logo) strcpy(wined3d_settings.logo, buffer);
290 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
292 if (!strcmp(buffer,"enabled"))
294 TRACE("Allow multisampling\n");
295 wined3d_settings.allow_multisampling = TRUE;
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");
306 if (appkey) RegCloseKey( appkey );
307 if (hkey) RegCloseKey( hkey );
312 static BOOL wined3d_destroy(HINSTANCE hInstDLL)
314 DWORD wined3d_context_tls_idx = context_get_tls_idx();
316 if (!TlsFree(wined3d_context_tls_idx))
318 DWORD err = GetLastError();
319 ERR("Failed to free context TLS index, err %#x.\n", err);
322 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
323 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
328 /* At process attach */
329 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
331 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
335 case DLL_PROCESS_ATTACH:
336 return wined3d_init(hInstDLL);
338 case DLL_PROCESS_DETACH:
339 return wined3d_destroy(hInstDLL);
341 case DLL_THREAD_DETACH:
343 if (!context_set_current(NULL))
345 ERR("Failed to clear current context.\n");