netapi32: Fix character count passed to GetUserNameW and GetComputerNameW in init_wks...
[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  *
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 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
28
29 /* *******************************************
30    IWineD3DBaseTexture IUnknown parts follow
31    ******************************************* */
32 HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DBase)
38         || IsEqualGUID(riid, &IID_IWineD3DResource)
39         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)) {
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return S_OK;
43     }
44     *ppobj = NULL;
45     return E_NOINTERFACE;
46 }
47
48 ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface) {
49     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
50     ULONG ref = InterlockedIncrement(&This->resource.ref);
51
52     TRACE("(%p) : AddRef increasing from %d\n", This,ref - 1);
53     return ref;
54 }
55
56 ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface) {
57     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
58     ULONG ref = InterlockedDecrement(&This->resource.ref);
59     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
60     if (ref == 0) {
61         IWineD3DBaseTextureImpl_CleanUp(iface);
62         HeapFree(GetProcessHeap(), 0, This);
63     }
64     return ref;
65 }
66
67 /* class static */
68 void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface) {
69     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
70     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
71
72     TRACE("(%p) : textureName(%d)\n", This, This->baseTexture.textureName);
73     if (This->baseTexture.textureName != 0) {
74         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
75         ENTER_GL();
76         TRACE("(%p) : Deleting texture %d\n", This, This->baseTexture.textureName);
77         glDeleteTextures(1, &This->baseTexture.textureName);
78         LEAVE_GL();
79     }
80     IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
81 }
82
83 /* ****************************************************
84    IWineD3DBaseTexture IWineD3DResource parts follow
85    **************************************************** */
86 HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice** ppDevice) {
87     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
88 }
89
90 HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
91     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
92 }
93
94 HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
96 }
97
98 HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid) {
99     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
100 }
101
102 DWORD    WINAPI        IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD PriorityNew) {
103     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
104 }
105
106 DWORD    WINAPI        IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface) {
107     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
108 }
109
110 void     WINAPI        IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface) {
111     IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
112 }
113
114 void     WINAPI        IWineD3DBaseTextureImpl_UnLoad(IWineD3DBaseTexture *iface) {
115     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
116     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
117
118     if(This->baseTexture.textureName) {
119         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
120         ENTER_GL();
121         glDeleteTextures(1, &This->baseTexture.textureName);
122         This->baseTexture.textureName = 0;
123         LEAVE_GL();
124     }
125     This->baseTexture.dirty = TRUE;
126 }
127
128 WINED3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface) {
129     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
130 }
131
132 HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent) {
133     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
134 }
135
136 /* ******************************************************
137    IWineD3DBaseTexture IWineD3DBaseTexture parts follow
138    ****************************************************** */
139
140 /* There is no OpenGL equivalent of setLOD, getLOD. All they do anyway is prioritize texture loading
141  * so just pretend that they work unless something really needs a failure. */
142 DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew) {
143     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
144
145     if (This->resource.pool != WINED3DPOOL_MANAGED) {
146         return  WINED3DERR_INVALIDCALL;
147     }
148
149     if(LODNew >= This->baseTexture.levels)
150         LODNew = This->baseTexture.levels - 1;
151      This->baseTexture.LOD = LODNew;
152
153     TRACE("(%p) : set bogus LOD to %d\n", This, This->baseTexture.LOD);
154
155     return This->baseTexture.LOD;
156 }
157
158 DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface) {
159     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
160
161     if (This->resource.pool != WINED3DPOOL_MANAGED) {
162         return  WINED3DERR_INVALIDCALL;
163     }
164
165     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
166
167     return This->baseTexture.LOD;
168 }
169
170 DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface) {
171     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
172     TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
173     return This->baseTexture.levels;
174 }
175
176 HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
177   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
178   IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
179   UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
180
181   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
182       TRACE("(%p) : returning invalid call\n", This);
183       return WINED3DERR_INVALIDCALL;
184   }
185   if(This->baseTexture.filterType != FilterType) {
186       /* What about multithreading? Do we want all the context overhead just to set this value?
187        * Or should we delay the applying until the texture is used for drawing? For now, apply
188        * immediately.
189        */
190       ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
191       ENTER_GL();
192       glBindTexture(textureDimensions, This->baseTexture.textureName);
193       checkGLcall("glBindTexture");
194       switch(FilterType) {
195           case WINED3DTEXF_NONE:
196           case WINED3DTEXF_POINT:
197               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
198               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
199
200           case WINED3DTEXF_LINEAR:
201               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
202               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
203
204           default:
205               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
206               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
207               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
208       }
209       LEAVE_GL();
210   }
211   This->baseTexture.filterType = FilterType;
212   TRACE("(%p) :\n", This);
213   return WINED3D_OK;
214 }
215
216 WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface) {
217   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
218   FIXME("(%p) : stub\n", This);
219   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
220      return WINED3DTEXF_NONE;
221   }
222   return This->baseTexture.filterType;
223 }
224
225 void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface) {
226   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
227   /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
228   FIXME("(%p) : stub\n", This);
229   return ;
230 }
231
232 /* Internal function, No d3d mapping */
233 BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL dirty) {
234     BOOL old;
235     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
236     old = This->baseTexture.dirty;
237     This->baseTexture.dirty = dirty;
238     return old;
239 }
240
241 BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface) {
242     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
243     return This->baseTexture.dirty;
244 }
245
246 HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface) {
247     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
248     HRESULT hr = WINED3D_OK;
249     UINT textureDimensions;
250     BOOL isNewTexture = FALSE;
251     TRACE("(%p) : About to bind texture\n", This);
252
253     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
254     ENTER_GL();
255     /* Generate a texture name if we don't already have one */
256     if (This->baseTexture.textureName == 0) {
257         glGenTextures(1, &This->baseTexture.textureName);
258         checkGLcall("glGenTextures");
259         TRACE("Generated texture %d\n", This->baseTexture.textureName);
260         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
261             /* Tell opengl to try and keep this texture in video ram (well mostly) */
262             GLclampf tmp;
263             tmp = 0.9f;
264             glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
265
266         }
267         /* Initialise the state of the texture object
268         to the openGL defaults, not the directx defaults */
269         This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
270         This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
271         This->baseTexture.states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
272         This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
273         This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
274         This->baseTexture.states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
275         This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
276         This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
277         This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = 0;
278         This->baseTexture.states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
279         This->baseTexture.states[WINED3DTEXSTA_ELEMENTINDEX]  = 0;
280         This->baseTexture.states[WINED3DTEXSTA_DMAPOFFSET]    = 0;
281         This->baseTexture.states[WINED3DTEXSTA_TSSADDRESSW]   = WINED3DTADDRESS_WRAP;
282         IWineD3DBaseTexture_SetDirty(iface, TRUE);
283         isNewTexture = TRUE;
284
285         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
286             /* This means double binding the texture at creation, but keeps the code simpler all
287              * in all, and the run-time path free from additional checks
288              */
289             glBindTexture(textureDimensions, This->baseTexture.textureName);
290             checkGLcall("glBindTexture");
291             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
292             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
293         }
294     }
295
296     /* Bind the texture */
297     if (This->baseTexture.textureName != 0) {
298         glBindTexture(textureDimensions, This->baseTexture.textureName);
299         checkGLcall("glBindTexture");
300         if (isNewTexture) {
301             /* For a new texture we have to set the textures levels after binding the texture.
302              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
303              * also need to set the texture dimensions before the texture is set
304              * Beware that texture rectangles do not support mipmapping.
305              */
306             if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
307                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
308                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
309                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
310             }
311             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
312                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
313                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
314                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
315                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
316             }
317         }
318                 
319     } else { /* this only happened if we've run out of openGL textures */
320         WARN("This texture doesn't have an openGL texture assigned to it\n");
321         hr =  WINED3DERR_INVALIDCALL;
322     }
323
324     LEAVE_GL();
325     return hr;
326 }
327
328 HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface) {
329     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
330     UINT textureDimensions;
331
332     TRACE("(%p) : About to bind texture\n", This);
333     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
334
335     ENTER_GL();
336
337     glBindTexture(textureDimensions, 0);
338 #if 0 /* TODO: context manager support */
339      IWineD3DContextManager_PopState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
340 #else
341     glDisable(textureDimensions);
342 #endif
343
344     LEAVE_GL();
345     return WINED3D_OK;
346 }
347
348 UINT WINAPI IWineD3DBaseTextureImpl_GetTextureDimensions(IWineD3DBaseTexture *iface){
349     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
350     FIXME("(%p) : This shouldn't be called\n", This);
351     return WINED3D_OK;
352 }
353
354 static inline GLenum warpLookupType(WINED3DSAMPLERSTATETYPE Type) {
355     switch(Type) {
356     case WINED3DSAMP_ADDRESSU:
357         return GL_TEXTURE_WRAP_S;
358     case WINED3DSAMP_ADDRESSV:
359         return GL_TEXTURE_WRAP_T;
360     case WINED3DSAMP_ADDRESSW:
361         return GL_TEXTURE_WRAP_R;
362     default:
363         FIXME("Unexpected warp type %d\n", Type);
364         return 0;
365     }
366 }
367
368 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type) {
369     GLint wrapParm;
370
371     if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
372         FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
373     } else {
374         if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
375             /* Cubemaps are always set to clamp, regardless of the sampler state. */
376             wrapParm = GL_CLAMP_TO_EDGE;
377         } else if(textureDimensions==GL_TEXTURE_RECTANGLE_ARB) {
378             if(state == WINED3DTADDRESS_WRAP) {
379                 wrapParm = GL_CLAMP_TO_EDGE;
380             } else {
381                 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
382             }
383         } else {
384             wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
385         }
386         TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
387         glTexParameteri(textureDimensions, type, wrapParm);
388         checkGLcall("glTexParameteri(..., type, wrapParm)");
389     }
390 }
391
392 void WINAPI IWineD3DBaseTextureImpl_ApplyStateChanges(IWineD3DBaseTexture *iface,
393                                     const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
394                                     const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
395     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
396     DWORD state;
397     GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
398
399     IWineD3DBaseTexture_PreLoad(iface);
400
401     if(samplerStates[WINED3DSAMP_ADDRESSU]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]) {
402         state = samplerStates[WINED3DSAMP_ADDRESSU];
403         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S);
404         This->baseTexture.states[WINED3DTEXSTA_ADDRESSU] = state;
405     }
406
407     if(samplerStates[WINED3DSAMP_ADDRESSV]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]) {
408         state = samplerStates[WINED3DSAMP_ADDRESSV];
409         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T);
410         This->baseTexture.states[WINED3DTEXSTA_ADDRESSV] = state;
411     }
412
413     if(samplerStates[WINED3DSAMP_ADDRESSW]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSW]) {
414         state = samplerStates[WINED3DSAMP_ADDRESSW];
415         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R);
416         This->baseTexture.states[WINED3DTEXSTA_ADDRESSW] = state;
417     }
418
419     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]) {
420         float col[4];
421
422         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
423         D3DCOLORTOGLFLOAT4(state, col);
424         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
425         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
426         checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
427         This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR] = state;
428     }
429
430     if(samplerStates[WINED3DSAMP_MAGFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]) {
431         GLint glValue;
432         state = samplerStates[WINED3DSAMP_MAGFILTER];
433         if (state < minLookup[WINELOOKUP_MAGFILTER] || state > maxLookup[WINELOOKUP_MAGFILTER]) {
434             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
435         }
436         glValue = stateLookup[WINELOOKUP_MAGFILTER][state - minLookup[WINELOOKUP_MAGFILTER]];
437         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
438         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
439         /* 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. */
440         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && WINED3DTEXF_ANISOTROPIC == state &&
441             textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
442             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
443         }
444         This->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = state;
445     }
446
447     if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB &&
448        (samplerStates[WINED3DSAMP_MINFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MINFILTER] ||
449         samplerStates[WINED3DSAMP_MIPFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] ||
450         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL])) {
451         GLint glValue;
452
453         This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
454         This->baseTexture.states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
455         This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
456
457         if (This->baseTexture.states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
458             This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
459         {
460
461             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
462                   This->baseTexture.states[WINED3DTEXSTA_MINFILTER],
463                   This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]);
464         }
465         glValue = minMipLookup[min(max(samplerStates[WINED3DSAMP_MINFILTER],WINED3DTEXF_NONE), WINED3DTEXF_ANISOTROPIC)]
466                 [min(max(samplerStates[WINED3DSAMP_MIPFILTER],WINED3DTEXF_NONE), WINED3DTEXF_LINEAR)];
467
468         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
469               samplerStates[WINED3DSAMP_MINFILTER],
470               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
471         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
472         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
473
474         if(This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
475             glValue = 0;
476         } else if(This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
477             glValue = This->baseTexture.levels - 1;
478         } else {
479             glValue = This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL];
480         }
481         glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
482     }
483
484     if(samplerStates[WINED3DSAMP_MAXANISOTROPY] != This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY]) {
485         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
486             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
487             checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
488         } else {
489             WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
490         }
491         This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];
492     }
493 }
494
495
496 static const IWineD3DBaseTextureVtbl IWineD3DBaseTexture_Vtbl =
497 {
498     /* IUnknown */
499     IWineD3DBaseTextureImpl_QueryInterface,
500     IWineD3DBaseTextureImpl_AddRef,
501     IWineD3DBaseTextureImpl_Release,
502     /* IWineD3DResource */
503     IWineD3DBaseTextureImpl_GetParent,
504     IWineD3DBaseTextureImpl_GetDevice,
505     IWineD3DBaseTextureImpl_SetPrivateData,
506     IWineD3DBaseTextureImpl_GetPrivateData,
507     IWineD3DBaseTextureImpl_FreePrivateData,
508     IWineD3DBaseTextureImpl_SetPriority,
509     IWineD3DBaseTextureImpl_GetPriority,
510     IWineD3DBaseTextureImpl_PreLoad,
511     IWineD3DBaseTextureImpl_UnLoad,
512     IWineD3DBaseTextureImpl_GetType,
513     /*IWineD3DBaseTexture*/
514     IWineD3DBaseTextureImpl_SetLOD,
515     IWineD3DBaseTextureImpl_GetLOD,
516     IWineD3DBaseTextureImpl_GetLevelCount,
517     IWineD3DBaseTextureImpl_SetAutoGenFilterType,
518     IWineD3DBaseTextureImpl_GetAutoGenFilterType,
519     IWineD3DBaseTextureImpl_GenerateMipSubLevels,
520     IWineD3DBaseTextureImpl_SetDirty,
521     IWineD3DBaseTextureImpl_GetDirty,
522     /* internal */
523     IWineD3DBaseTextureImpl_BindTexture,
524     IWineD3DBaseTextureImpl_UnBindTexture,
525     IWineD3DBaseTextureImpl_GetTextureDimensions,
526     IWineD3DBaseTextureImpl_ApplyStateChanges
527
528 };