wined3d: Surface pitch fix.
[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 wined3d_settings_t wined3d_settings = 
36 {
37   VS_HW,   /* Hardware by default */
38   PS_NONE, /* Disabled by default */
39   VBO_HW,  /* Hardware by default */
40   FALSE,   /* Use of GLSL disabled by default */
41   RTL_AUTO /* Automatically determine best locking method */
42 };
43
44 WineD3DGlobalStatistics *wineD3DGlobalStatistics = NULL;
45 CRITICAL_SECTION resourceStoreCriticalSection;
46
47 long globalChangeGlRam(long glram){
48     /* FIXME: replace this function with object tracking */
49     int result;
50
51     EnterCriticalSection(&resourceStoreCriticalSection); /* this is overkill really, but I suppose it should be thread safe */
52     wineD3DGlobalStatistics->glsurfaceram     += glram;
53     TRACE("Adjusted gl ram by %ld to %d\n", glram, wineD3DGlobalStatistics->glsurfaceram);
54     result = wineD3DGlobalStatistics->glsurfaceram;
55     LeaveCriticalSection(&resourceStoreCriticalSection);
56     return result;
57
58 }
59
60 IWineD3D* WINAPI WineDirect3DCreate(UINT SDKVersion, UINT dxVersion, IUnknown *parent) {
61     IWineD3DImpl* object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
62     object->lpVtbl = &IWineD3D_Vtbl;
63     object->dxVersion = dxVersion;
64     object->ref = 1;
65     object->parent = parent;
66
67     /* TODO: Move this off to device and possibly x11drv */
68     /* Create a critical section for a dll global data store */
69     InitializeCriticalSectionAndSpinCount(&resourceStoreCriticalSection, 0x80000400);
70
71     /*Create a structure for storing global data in*/
72     if(wineD3DGlobalStatistics == NULL){
73         TRACE("Createing global statistics store\n");
74         wineD3DGlobalStatistics = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wineD3DGlobalStatistics));
75
76     }
77
78
79     TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
80
81     return (IWineD3D *)object;
82 }
83
84 inline static DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
85 {
86     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
87     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
88     return ERROR_FILE_NOT_FOUND;
89 }
90
91 /* At process attach */
92 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
93 {
94     TRACE("WineD3D DLLMain Reason=%ld\n", fdwReason);
95     if (fdwReason == DLL_PROCESS_ATTACH)
96     {
97        HMODULE mod;
98        char buffer[MAX_PATH+10];
99        DWORD size = sizeof(buffer);
100        HKEY hkey = 0;
101        HKEY appkey = 0;
102        DWORD len;
103        wined3d_settings.emulated_textureram = 64*1024*1024;
104
105        DisableThreadLibraryCalls(hInstDLL);
106
107        mod = GetModuleHandleA( "winex11.drv" );
108        if (mod)
109        {
110            wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
111            wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
112        }
113        /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
114        if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
115
116        len = GetModuleFileNameA( 0, buffer, MAX_PATH );
117        if (len && len < MAX_PATH)
118        {
119             HKEY tmpkey;
120             /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
121             if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
122             {
123                 char *p, *appname = buffer;
124                 if ((p = strrchr( appname, '/' ))) appname = p + 1;
125                 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
126                 strcat( appname, "\\Direct3D" );
127                 TRACE("appname = [%s]\n", appname);
128                 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
129                 RegCloseKey( tmpkey );
130             }
131        }
132
133        if ( 0 != hkey || 0 != appkey )
134        {
135             if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
136             {
137                 if (!strcmp(buffer,"none"))
138                 {
139                     TRACE("Disable vertex shaders\n");
140                     wined3d_settings.vs_mode = VS_NONE;
141                 }
142                 else if (!strcmp(buffer,"emulation"))
143                 {
144                     TRACE("Force SW vertex shaders\n");
145                     wined3d_settings.vs_mode = VS_SW;
146                 }
147             }
148             if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
149             {
150                 if (!strcmp(buffer,"enabled"))
151                 {
152                     TRACE("Allow pixel shaders\n");
153                     wined3d_settings.ps_mode = PS_HW;
154                 }
155                 if (!strcmp(buffer,"disabled"))
156                 {
157                     TRACE("Disable pixel shaders\n");
158                     wined3d_settings.ps_mode = PS_NONE;
159                 }
160             }
161             if ( !get_config_key( hkey, appkey, "VertexBufferMode", buffer, size) )
162             {
163                 if (!strcmp(buffer,"none"))
164                 {
165                     TRACE("Disable Vertex Buffer Hardware support\n");
166                     wined3d_settings.vbo_mode = VBO_NONE;
167                 }
168                 else if (!strcmp(buffer,"hardware"))
169                 {
170                     TRACE("Allow Vertex Buffer Hardware support\n");
171                     wined3d_settings.vbo_mode = VBO_HW;
172                 }
173             }
174             if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
175             {
176                 if (!strcmp(buffer,"enabled"))
177                 {
178                     TRACE("Use of GL Shading Language enabled for systems that support it\n");
179                     wined3d_settings.glslRequested = TRUE;
180                 }
181                 else
182                 {
183                     TRACE("Use of GL Shading Language disabled\n");
184                 }
185             }
186             if ( !get_config_key( hkey, appkey, "Nonpower2Mode", buffer, size) )
187             {
188                 if (!strcmp(buffer,"none"))
189                 {
190                     TRACE("Using default non-power2 textures\n");
191                     wined3d_settings.nonpower2_mode = NP2_NONE;
192
193                 }
194                 else if (!strcmp(buffer,"repack"))
195                 {
196                     TRACE("Repacking non-power2 textures\n");
197                     wined3d_settings.nonpower2_mode = NP2_REPACK;
198                 }
199                 /* There will be a couple of other choices for nonpow2, they are: TextureRecrangle and OpenGL 2 */
200             }
201             if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
202             {
203                 if (!strcmp(buffer,"disabled"))
204                 {
205                     TRACE("Disabling render target locking\n");
206                     wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
207                 }
208                 else if (!strcmp(buffer,"readdraw"))
209                 {
210                     TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
211                     wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
212                 }
213                 else if (!strcmp(buffer,"readtex"))
214                 {
215                     TRACE("Using glReadPixels for render target reading and textures for writing\n");
216                     wined3d_settings.rendertargetlock_mode = RTL_READTEX;
217                 }
218                 else if (!strcmp(buffer,"texdraw"))
219                 {
220                     TRACE("Using textures for render target reading and glDrawPixels for writing\n");
221                     wined3d_settings.rendertargetlock_mode = RTL_TEXDRAW;
222                 }
223                 else if (!strcmp(buffer,"textex"))
224                 {
225                     TRACE("Reading render targets via textures and writing via textures\n");
226                     wined3d_settings.rendertargetlock_mode = RTL_TEXTEX;
227                 }
228             }
229             if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
230             {
231                 int TmpVideoMemorySize = atoi(buffer);
232                 if(TmpVideoMemorySize > 0)
233                 {
234                     wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
235                     TRACE("Use %iMB = %d byte for emulated_textureram\n",
236                             TmpVideoMemorySize,
237                             wined3d_settings.emulated_textureram);
238                 }
239                 else
240                     ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
241             }
242        }
243        if (wined3d_settings.vs_mode == VS_HW)
244            TRACE("Allow HW vertex shaders\n");
245        if (wined3d_settings.ps_mode == PS_NONE)
246            TRACE("Disable pixel shaders\n");
247        if (wined3d_settings.vbo_mode == VBO_NONE)
248            TRACE("Disable Vertex Buffer Hardware support\n");
249        if (wined3d_settings.glslRequested)
250            TRACE("If supported by your system, GL Shading Language will be used\n");
251        if (wined3d_settings.nonpower2_mode == NP2_REPACK)
252            TRACE("Repacking non-power2 textures\n");
253
254        if (appkey) RegCloseKey( appkey );
255        if (hkey) RegCloseKey( hkey );
256     }
257     return TRUE;
258 }