wined3d: Cleanup GL extension detection.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include "initguid.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(wine_d3d);
29
30 int num_lock = 0;
31 void (*wine_tsx11_lock_ptr)(void) = NULL;
32 void (*wine_tsx11_unlock_ptr)(void) = NULL;
33
34
35 /* When updating default value here, make sure to update winecfg as well,
36  * where appropriate. */
37 wined3d_settings_t wined3d_settings = 
38 {
39     VS_HW,          /* Hardware by default */
40     PS_HW,          /* Hardware by default */
41     VBO_HW,         /* Hardware by default */
42     FALSE,          /* Use of GLSL disabled by default */
43     ORM_BACKBUFFER, /* Use the backbuffer to do offscreen rendering */
44     RTL_AUTO,       /* Automatically determine best locking method */
45     64*1024*1024    /* 64MB texture memory by default */
46 };
47
48 WineD3DGlobalStatistics *wineD3DGlobalStatistics = NULL;
49
50 long globalChangeGlRam(long glram){
51     /* FIXME: replace this function with object tracking */
52     int result;
53
54     wineD3DGlobalStatistics->glsurfaceram     += glram;
55     TRACE("Adjusted gl ram by %ld to %d\n", glram, wineD3DGlobalStatistics->glsurfaceram);
56     result = wineD3DGlobalStatistics->glsurfaceram;
57     return result;
58
59 }
60
61 IWineD3D* WINAPI WineDirect3DCreate(UINT SDKVersion, UINT dxVersion, IUnknown *parent) {
62     IWineD3DImpl* object;
63
64     if (!InitAdapters()) {
65         ERR("Failed to initialize direct3d adapters\n");
66         return NULL;
67     }
68
69     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
70     object->lpVtbl = &IWineD3D_Vtbl;
71     object->dxVersion = dxVersion;
72     object->ref = 1;
73     object->parent = parent;
74
75     /*Create a structure for storing global data in*/
76     if(wineD3DGlobalStatistics == NULL){
77         TRACE("Creating global statistics store\n");
78         wineD3DGlobalStatistics = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wineD3DGlobalStatistics));
79
80     }
81
82     TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
83
84     return (IWineD3D *)object;
85 }
86
87 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
88 {
89     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
90     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
91     return ERROR_FILE_NOT_FOUND;
92 }
93
94 /* At process attach */
95 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
96 {
97     TRACE("WineD3D DLLMain Reason=%d\n", fdwReason);
98     if (fdwReason == DLL_PROCESS_ATTACH)
99     {
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;
106        wined3d_settings.emulated_textureram = 64*1024*1024;
107
108        DisableThreadLibraryCalls(hInstDLL);
109
110        mod = GetModuleHandleA( "winex11.drv" );
111        if (mod)
112        {
113            wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
114            wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
115        }
116        /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
117        if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
118
119        len = GetModuleFileNameA( 0, buffer, MAX_PATH );
120        if (len && len < MAX_PATH)
121        {
122             HKEY tmpkey;
123             /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
124             if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
125             {
126                 char *p, *appname = buffer;
127                 if ((p = strrchr( appname, '/' ))) appname = p + 1;
128                 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
129                 strcat( appname, "\\Direct3D" );
130                 TRACE("appname = [%s]\n", appname);
131                 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
132                 RegCloseKey( tmpkey );
133             }
134        }
135
136        if ( 0 != hkey || 0 != appkey )
137        {
138             if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
139             {
140                 if (!strcmp(buffer,"none"))
141                 {
142                     TRACE("Disable vertex shaders\n");
143                     wined3d_settings.vs_mode = VS_NONE;
144                 }
145             }
146             if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
147             {
148                 if (!strcmp(buffer,"enabled"))
149                 {
150                     TRACE("Allow pixel shaders\n");
151                     wined3d_settings.ps_mode = PS_HW;
152                 }
153                 if (!strcmp(buffer,"disabled"))
154                 {
155                     TRACE("Disable pixel shaders\n");
156                     wined3d_settings.ps_mode = PS_NONE;
157                 }
158             }
159             if ( !get_config_key( hkey, appkey, "VertexBufferMode", buffer, size) )
160             {
161                 if (!strcmp(buffer,"none"))
162                 {
163                     TRACE("Disable Vertex Buffer Hardware support\n");
164                     wined3d_settings.vbo_mode = VBO_NONE;
165                 }
166                 else if (!strcmp(buffer,"hardware"))
167                 {
168                     TRACE("Allow Vertex Buffer Hardware support\n");
169                     wined3d_settings.vbo_mode = VBO_HW;
170                 }
171             }
172             if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
173             {
174                 if (!strcmp(buffer,"enabled"))
175                 {
176                     TRACE("Use of GL Shading Language enabled for systems that support it\n");
177                     wined3d_settings.glslRequested = TRUE;
178                 }
179                 else
180                 {
181                     TRACE("Use of GL Shading Language disabled\n");
182                 }
183             }
184             if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
185             {
186                 if (!strcmp(buffer,"backbuffer"))
187                 {
188                     TRACE("Using the backbuffer for offscreen rendering\n");
189                     wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
190                 }
191                 else if (!strcmp(buffer,"pbuffer"))
192                 {
193                     TRACE("Using PBuffers for offscreen rendering\n");
194                     wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
195                 }
196                 else if (!strcmp(buffer,"fbo"))
197                 {
198                     TRACE("Using FBOs for offscreen rendering\n");
199                     wined3d_settings.offscreen_rendering_mode = ORM_FBO;
200                 }
201             }
202             if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
203             {
204                 if (!strcmp(buffer,"disabled"))
205                 {
206                     TRACE("Disabling render target locking\n");
207                     wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
208                 }
209                 else if (!strcmp(buffer,"readdraw"))
210                 {
211                     TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
212                     wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
213                 }
214                 else if (!strcmp(buffer,"readtex"))
215                 {
216                     TRACE("Using glReadPixels for render target reading and textures for writing\n");
217                     wined3d_settings.rendertargetlock_mode = RTL_READTEX;
218                 }
219                 else if (!strcmp(buffer,"texdraw"))
220                 {
221                     TRACE("Using textures for render target reading and glDrawPixels for writing\n");
222                     wined3d_settings.rendertargetlock_mode = RTL_TEXDRAW;
223                 }
224                 else if (!strcmp(buffer,"textex"))
225                 {
226                     TRACE("Reading render targets via textures and writing via textures\n");
227                     wined3d_settings.rendertargetlock_mode = RTL_TEXTEX;
228                 }
229             }
230             if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
231             {
232                 int TmpVideoMemorySize = atoi(buffer);
233                 if(TmpVideoMemorySize > 0)
234                 {
235                     wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
236                     TRACE("Use %iMB = %d byte for emulated_textureram\n",
237                             TmpVideoMemorySize,
238                             wined3d_settings.emulated_textureram);
239                 }
240                 else
241                     ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
242             }
243        }
244        if (wined3d_settings.vs_mode == VS_HW)
245            TRACE("Allow HW vertex shaders\n");
246        if (wined3d_settings.ps_mode == PS_NONE)
247            TRACE("Disable pixel shaders\n");
248        if (wined3d_settings.vbo_mode == VBO_NONE)
249            TRACE("Disable Vertex Buffer Hardware support\n");
250        if (wined3d_settings.glslRequested)
251            TRACE("If supported by your system, GL Shading Language will be used\n");
252
253        if (appkey) RegCloseKey( appkey );
254        if (hkey) RegCloseKey( hkey );
255     }
256     return TRUE;
257 }