winejoystick.drv: Use CP_UNIXCP instead of CP_ACP when converting a string that comes...
[wine] / dlls / wined3d / basetexture.c
1 /*
2  * IWineD3DBaseTexture Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2002-2004 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
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 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 void basetexture_init(struct IWineD3DBaseTextureClass *texture, UINT levels, DWORD usage)
31 {
32     texture->levels = levels;
33     texture->filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
34     texture->LOD = 0;
35     texture->dirty = TRUE;
36     texture->srgbDirty = TRUE;
37     texture->is_srgb = FALSE;
38 }
39
40 void basetexture_cleanup(IWineD3DBaseTexture *iface)
41 {
42     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
43     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
44
45     TRACE("(%p) : textureName(%d)\n", This, This->baseTexture.textureName);
46     if (This->baseTexture.textureName != 0) {
47         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
48         ENTER_GL();
49         TRACE("(%p) : Deleting texture %d\n", This, This->baseTexture.textureName);
50         glDeleteTextures(1, &This->baseTexture.textureName);
51         glDeleteTextures(1, &This->baseTexture.srgbTextureName);
52         LEAVE_GL();
53     }
54
55     resource_cleanup((IWineD3DResource *)iface);
56 }
57
58 void basetexture_unload(IWineD3DBaseTexture *iface)
59 {
60     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
61     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
62
63     if(This->baseTexture.textureName) {
64         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
65         ENTER_GL();
66         glDeleteTextures(1, &This->baseTexture.textureName);
67         glDeleteTextures(1, &This->baseTexture.srgbTextureName);
68         This->baseTexture.textureName = 0;
69         This->baseTexture.srgbTextureName = 0;
70         LEAVE_GL();
71     }
72     This->baseTexture.dirty = TRUE;
73     This->baseTexture.srgbDirty = TRUE;
74 }
75
76 /* There is no OpenGL equivalent of setLOD, getLOD. All they do anyway is prioritize texture loading
77  * so just pretend that they work unless something really needs a failure. */
78 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
79 {
80     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
81
82     if (This->resource.pool != WINED3DPOOL_MANAGED) {
83         return  WINED3DERR_INVALIDCALL;
84     }
85
86     if(LODNew >= This->baseTexture.levels)
87         LODNew = This->baseTexture.levels - 1;
88      This->baseTexture.LOD = LODNew;
89
90     TRACE("(%p) : set bogus LOD to %d\n", This, This->baseTexture.LOD);
91
92     return This->baseTexture.LOD;
93 }
94
95 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
96 {
97     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
98
99     if (This->resource.pool != WINED3DPOOL_MANAGED) {
100         return  WINED3DERR_INVALIDCALL;
101     }
102
103     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
104
105     return This->baseTexture.LOD;
106 }
107
108 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
109 {
110     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
111     TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
112     return This->baseTexture.levels;
113 }
114
115 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
116 {
117   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
118   IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
119   UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
120
121   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
122       TRACE("(%p) : returning invalid call\n", This);
123       return WINED3DERR_INVALIDCALL;
124   }
125   if(This->baseTexture.filterType != FilterType) {
126       /* What about multithreading? Do we want all the context overhead just to set this value?
127        * Or should we delay the applying until the texture is used for drawing? For now, apply
128        * immediately.
129        */
130       ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
131       ENTER_GL();
132       glBindTexture(textureDimensions, This->baseTexture.textureName);
133       checkGLcall("glBindTexture");
134       switch(FilterType) {
135           case WINED3DTEXF_NONE:
136           case WINED3DTEXF_POINT:
137               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
138               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
139
140               break;
141           case WINED3DTEXF_LINEAR:
142               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
143               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
144
145               break;
146           default:
147               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
148               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
149               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
150       }
151       LEAVE_GL();
152   }
153   This->baseTexture.filterType = FilterType;
154   TRACE("(%p) :\n", This);
155   return WINED3D_OK;
156 }
157
158 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
159 {
160   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
161   FIXME("(%p) : stub\n", This);
162   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
163      return WINED3DTEXF_NONE;
164   }
165   return This->baseTexture.filterType;
166 }
167
168 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
169 {
170   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
171   /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
172   FIXME("(%p) : stub\n", This);
173   return ;
174 }
175
176 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
177 {
178     BOOL old;
179     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
180     old = This->baseTexture.dirty || This->baseTexture.srgbDirty;
181     This->baseTexture.dirty = dirty;
182     This->baseTexture.srgbDirty = dirty;
183     return old;
184 }
185
186 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
187 {
188     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
189     return This->baseTexture.dirty || This->baseTexture.srgbDirty;
190 }
191
192 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
193 {
194     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
195     HRESULT hr = WINED3D_OK;
196     UINT textureDimensions;
197     BOOL isNewTexture = FALSE;
198     GLuint *texture;
199     DWORD *states;
200     TRACE("(%p) : About to bind texture\n", This);
201
202     This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
203     if(srgb) {
204         texture = &This->baseTexture.srgbTextureName;
205         states = This->baseTexture.srgbstates;
206     } else {
207         texture = &This->baseTexture.textureName;
208         states = This->baseTexture.states;
209     }
210
211     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
212     ENTER_GL();
213     /* Generate a texture name if we don't already have one */
214     if (*texture == 0) {
215         *set_surface_desc = TRUE;
216         glGenTextures(1, texture);
217         checkGLcall("glGenTextures");
218         TRACE("Generated texture %d\n", *texture);
219         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
220             /* Tell opengl to try and keep this texture in video ram (well mostly) */
221             GLclampf tmp;
222             tmp = 0.9f;
223             glPrioritizeTextures(1, texture, &tmp);
224
225         }
226         /* Initialise the state of the texture object
227         to the openGL defaults, not the directx defaults */
228         states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
229         states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
230         states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
231         states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
232         states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
233         states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
234         states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
235         states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
236         states[WINED3DTEXSTA_MAXANISOTROPY] = 0;
237         states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
238         states[WINED3DTEXSTA_ELEMENTINDEX]  = 0;
239         states[WINED3DTEXSTA_DMAPOFFSET]    = 0;
240         states[WINED3DTEXSTA_TSSADDRESSW]   = WINED3DTADDRESS_WRAP;
241         IWineD3DBaseTexture_SetDirty(iface, TRUE);
242         isNewTexture = TRUE;
243
244         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
245             /* This means double binding the texture at creation, but keeps the code simpler all
246              * in all, and the run-time path free from additional checks
247              */
248             glBindTexture(textureDimensions, *texture);
249             checkGLcall("glBindTexture");
250             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
251             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
252         }
253     } else {
254         *set_surface_desc = FALSE;
255     }
256
257     /* Bind the texture */
258     if (*texture != 0) {
259         glBindTexture(textureDimensions, *texture);
260         checkGLcall("glBindTexture");
261         if (isNewTexture) {
262             /* For a new texture we have to set the textures levels after binding the texture.
263              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
264              * also need to set the texture dimensions before the texture is set
265              * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
266              * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
267              * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
268              */
269             if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
270                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
271                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
272                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
273             }
274             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
275                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
276                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
277                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
278                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
279             }
280         }
281     } else { /* this only happened if we've run out of openGL textures */
282         WARN("This texture doesn't have an openGL texture assigned to it\n");
283         hr =  WINED3DERR_INVALIDCALL;
284     }
285
286     LEAVE_GL();
287     return hr;
288 }
289
290 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
291                               BOOL cond_np2) {
292     GLint wrapParm;
293
294     if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
295         FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
296     } else {
297         if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
298             /* Cubemaps are always set to clamp, regardless of the sampler state. */
299             wrapParm = GL_CLAMP_TO_EDGE;
300         } else if(cond_np2) {
301             if(state == WINED3DTADDRESS_WRAP) {
302                 wrapParm = GL_CLAMP_TO_EDGE;
303             } else {
304                 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
305             }
306         } else {
307             wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
308         }
309         TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
310         glTexParameteri(textureDimensions, type, wrapParm);
311         checkGLcall("glTexParameteri(..., type, wrapParm)");
312     }
313 }
314
315 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
316         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
317         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
318 {
319     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
320     DWORD state, *states;
321     GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
322     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
323
324     if(This->baseTexture.is_srgb) {
325         states = This->baseTexture.srgbstates;
326     } else {
327         states = This->baseTexture.states;
328     }
329
330     /* ApplyStateChanges relies on the correct texture being bound and loaded. */
331
332     if(samplerStates[WINED3DSAMP_ADDRESSU]      != states[WINED3DTEXSTA_ADDRESSU]) {
333         state = samplerStates[WINED3DSAMP_ADDRESSU];
334         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
335         states[WINED3DTEXSTA_ADDRESSU] = state;
336     }
337
338     if(samplerStates[WINED3DSAMP_ADDRESSV]      != states[WINED3DTEXSTA_ADDRESSV]) {
339         state = samplerStates[WINED3DSAMP_ADDRESSV];
340         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
341         states[WINED3DTEXSTA_ADDRESSV] = state;
342     }
343
344     if(samplerStates[WINED3DSAMP_ADDRESSW]      != states[WINED3DTEXSTA_ADDRESSW]) {
345         state = samplerStates[WINED3DSAMP_ADDRESSW];
346         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
347         states[WINED3DTEXSTA_ADDRESSW] = state;
348     }
349
350     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != states[WINED3DTEXSTA_BORDERCOLOR]) {
351         float col[4];
352
353         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
354         D3DCOLORTOGLFLOAT4(state, col);
355         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
356         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
357         checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
358         states[WINED3DTEXSTA_BORDERCOLOR] = state;
359     }
360
361     if(samplerStates[WINED3DSAMP_MAGFILTER]     != states[WINED3DTEXSTA_MAGFILTER]) {
362         GLint glValue;
363         state = samplerStates[WINED3DSAMP_MAGFILTER];
364         if (state > WINED3DTEXF_ANISOTROPIC) {
365             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
366         } else {
367             glValue = This->baseTexture.magLookup[state - WINED3DTEXF_NONE];
368             TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
369             glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
370             /* We need to reset the Anisotropic filtering state when we change the mag filter to WINED3DTEXF_ANISOTROPIC (this seems a bit weird, check the documentation to see how it should be switched off. */
371             if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && WINED3DTEXF_ANISOTROPIC == state &&
372                 !cond_np2) {
373                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
374             }
375             states[WINED3DTEXSTA_MAGFILTER] = state;
376         }
377     }
378
379     if((samplerStates[WINED3DSAMP_MINFILTER]     != states[WINED3DTEXSTA_MINFILTER] ||
380         samplerStates[WINED3DSAMP_MIPFILTER]     != states[WINED3DTEXSTA_MIPFILTER] ||
381         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != states[WINED3DTEXSTA_MAXMIPLEVEL])) {
382         GLint glValue;
383
384         states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
385         states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
386         states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
387
388         if (states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
389             states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
390         {
391
392             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
393                   states[WINED3DTEXSTA_MINFILTER],
394                   states[WINED3DTEXSTA_MIPFILTER]);
395         }
396         glValue = This->baseTexture.minMipLookup
397                 [min(max(samplerStates[WINED3DSAMP_MINFILTER],WINED3DTEXF_NONE), WINED3DTEXF_ANISOTROPIC)]
398                 .mip[min(max(samplerStates[WINED3DSAMP_MIPFILTER],WINED3DTEXF_NONE), WINED3DTEXF_LINEAR)];
399
400         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
401               samplerStates[WINED3DSAMP_MINFILTER],
402               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
403         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
404         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
405
406         if(!cond_np2) {
407             if(states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
408                 glValue = 0;
409             } else if(states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
410                 glValue = This->baseTexture.levels - 1;
411             } else {
412                 glValue = states[WINED3DTEXSTA_MAXMIPLEVEL];
413             }
414             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
415         }
416     }
417
418     if(samplerStates[WINED3DSAMP_MAXANISOTROPY] != states[WINED3DTEXSTA_MAXANISOTROPY]) {
419         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && !cond_np2) {
420             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
421             checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
422         } else {
423             WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
424         }
425         states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];
426     }
427 }