wined3d: Keep track of shaders.
[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     IWineD3DResourceImpl_UnLoad((IWineD3DResource *)iface);
116 }
117
118 WINED3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface) {
119     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
120 }
121
122 HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent) {
123     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
124 }
125
126 /* ******************************************************
127    IWineD3DBaseTexture IWineD3DBaseTexture parts follow
128    ****************************************************** */
129
130 /* There is no OpenGL equivilent of setLOD, getLOD, all they do it priortise testure loading
131  * so just pretend that they work unless something really needs a failure. */
132 DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew) {
133     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
134
135     if (This->resource.pool != WINED3DPOOL_MANAGED) {
136         return  WINED3DERR_INVALIDCALL;
137     }
138
139     if(LODNew >= This->baseTexture.levels)
140         LODNew = This->baseTexture.levels - 1;
141      This->baseTexture.LOD = LODNew;
142
143     TRACE("(%p) : set bogus LOD to %d\n", This, This->baseTexture.LOD);
144
145     return This->baseTexture.LOD;
146 }
147
148 DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface) {
149     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
150
151     if (This->resource.pool != WINED3DPOOL_MANAGED) {
152         return  WINED3DERR_INVALIDCALL;
153     }
154
155     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
156
157     return This->baseTexture.LOD;
158 }
159
160 DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface) {
161     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
162     TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
163     return This->baseTexture.levels;
164 }
165
166 HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
167   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
168   IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
169   UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
170
171   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
172       TRACE("(%p) : returning invalid call\n", This);
173       return WINED3DERR_INVALIDCALL;
174   }
175   if(This->baseTexture.filterType != FilterType) {
176       /* What about multithreading? Do we want all the context overhead just to set this value?
177        * Or should we delay the applying until the texture is used for drawing? For now, apply
178        * immediately.
179        */
180       ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
181       ENTER_GL();
182       glBindTexture(textureDimensions, This->baseTexture.textureName);
183       checkGLcall("glBindTexture");
184       switch(FilterType) {
185           case WINED3DTEXF_NONE:
186           case WINED3DTEXF_POINT:
187               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
188               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
189
190           case WINED3DTEXF_LINEAR:
191               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
192               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
193
194           default:
195               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
196               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
197               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
198       }
199       LEAVE_GL();
200   }
201   This->baseTexture.filterType = FilterType;
202   TRACE("(%p) :\n", This);
203   return WINED3D_OK;
204 }
205
206 WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface) {
207   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
208   FIXME("(%p) : stub\n", This);
209   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
210      return WINED3DTEXF_NONE;
211   }
212   return This->baseTexture.filterType;
213 }
214
215 void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface) {
216   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
217   /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
218   FIXME("(%p) : stub\n", This);
219   return ;
220 }
221
222 /* Internal function, No d3d mapping */
223 BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL dirty) {
224     BOOL old;
225     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
226     old = This->baseTexture.dirty;
227     This->baseTexture.dirty = dirty;
228     return old;
229 }
230
231 BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface) {
232     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
233     return This->baseTexture.dirty;
234 }
235
236 HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface) {
237     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
238     HRESULT hr = WINED3D_OK;
239     UINT textureDimensions;
240     BOOL isNewTexture = FALSE;
241     TRACE("(%p) : About to bind texture\n", This);
242
243     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
244     ENTER_GL();
245     /* Generate a texture name if we don't already have one */
246     if (This->baseTexture.textureName == 0) {
247         glGenTextures(1, &This->baseTexture.textureName);
248         checkGLcall("glGenTextures");
249         TRACE("Generated texture %d\n", This->baseTexture.textureName);
250         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
251             /* Tell opengl to try and keep this texture in video ram (well mostly) */
252             GLclampf tmp;
253             tmp = 0.9f;
254             glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
255
256         }
257         /* Initialise the state of the texture object
258         to the openGL defaults, not the directx defaults */
259         This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
260         This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
261         This->baseTexture.states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
262         This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
263         This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
264         This->baseTexture.states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
265         This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
266         This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
267         This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = 0;
268         This->baseTexture.states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
269         This->baseTexture.states[WINED3DTEXSTA_ELEMENTINDEX]  = 0;
270         This->baseTexture.states[WINED3DTEXSTA_DMAPOFFSET]    = 0;
271         This->baseTexture.states[WINED3DTEXSTA_TSSADDRESSW]   = WINED3DTADDRESS_WRAP;
272         IWineD3DBaseTexture_SetDirty(iface, TRUE);
273         isNewTexture = TRUE;
274
275         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
276             /* This means double binding the texture at creation, but keeps the code simpler all
277              * in all, and the run-time path free from additional checks
278              */
279             glBindTexture(textureDimensions, This->baseTexture.textureName);
280             checkGLcall("glBindTexture");
281             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
282             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
283         }
284     }
285
286     /* Bind the texture */
287     if (This->baseTexture.textureName != 0) {
288         glBindTexture(textureDimensions, This->baseTexture.textureName);
289         checkGLcall("glBindTexture");
290         if (isNewTexture) {
291             /* For a new texture we have to set the textures levels after binding the texture.
292              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
293              * also need to set the texture dimensions before the texture is set
294              * Beware that texture rectangles do not support mipmapping.
295              */
296             if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
297                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
298                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
299                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
300             }
301             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
302                 /* Cubemaps are always set to clamp, regardeless of the sampler state. */
303                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
304                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
305                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
306             }
307         }
308                 
309     } else { /* this only happened if we've run out of openGL textures */
310         WARN("This texture doesn't have an openGL texture assigned to it\n");
311         hr =  WINED3DERR_INVALIDCALL;
312     }
313
314     LEAVE_GL();
315     return hr;
316 }
317
318 HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface) {
319     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
320     UINT textureDimensions;
321
322     TRACE("(%p) : About to bind texture\n", This);
323     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
324
325     ENTER_GL();
326
327     glBindTexture(textureDimensions, 0);
328 #if 0 /* TODO: context manager support */
329      IWineD3DContextManager_PopState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
330 #else
331     glDisable(textureDimensions);
332 #endif
333
334     LEAVE_GL();
335     return WINED3D_OK;
336 }
337
338 UINT WINAPI IWineD3DBaseTextureImpl_GetTextureDimensions(IWineD3DBaseTexture *iface){
339     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
340     FIXME("(%p) : This shouldn't be called\n", This);
341     return WINED3D_OK;
342 }
343
344 static inline GLenum warpLookupType(WINED3DSAMPLERSTATETYPE Type) {
345     switch(Type) {
346     case WINED3DSAMP_ADDRESSU:
347         return GL_TEXTURE_WRAP_S;
348     case WINED3DSAMP_ADDRESSV:
349         return GL_TEXTURE_WRAP_T;
350     case WINED3DSAMP_ADDRESSW:
351         return GL_TEXTURE_WRAP_R;
352     default:
353         FIXME("Unexpected warp type %d\n", Type);
354         return 0;
355     }
356 }
357
358 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type) {
359     GLint wrapParm;
360
361     if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
362         FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
363     } else {
364         if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
365             /* Cubemaps are always set to clamp, regardeless of the sampler state. */
366             wrapParm = GL_CLAMP_TO_EDGE;
367         } else {
368             wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
369         }
370         TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
371         glTexParameteri(textureDimensions, type, wrapParm);
372         checkGLcall("glTexParameteri(..., type, wrapParm)");
373     }
374 }
375
376 void WINAPI IWineD3DBaseTextureImpl_ApplyStateChanges(IWineD3DBaseTexture *iface,
377                                     const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
378                                     const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
379     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
380     DWORD state;
381     GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
382
383     IWineD3DBaseTexture_PreLoad(iface);
384
385     if(samplerStates[WINED3DSAMP_ADDRESSU]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]) {
386         state = samplerStates[WINED3DSAMP_ADDRESSU];
387         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S);
388         This->baseTexture.states[WINED3DTEXSTA_ADDRESSU] = state;
389     }
390
391     if(samplerStates[WINED3DSAMP_ADDRESSV]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]) {
392         state = samplerStates[WINED3DSAMP_ADDRESSV];
393         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T);
394         This->baseTexture.states[WINED3DTEXSTA_ADDRESSV] = state;
395     }
396
397     if(samplerStates[WINED3DSAMP_ADDRESSW]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSW]) {
398         state = samplerStates[WINED3DSAMP_ADDRESSW];
399         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R);
400         This->baseTexture.states[WINED3DTEXSTA_ADDRESSW] = state;
401     }
402
403     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]) {
404         float col[4];
405
406         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
407         D3DCOLORTOGLFLOAT4(state, col);
408         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
409         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
410         checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
411         This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR] = state;
412     }
413
414     if(samplerStates[WINED3DSAMP_MAGFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]) {
415         GLint glValue;
416         state = samplerStates[WINED3DSAMP_MAGFILTER];
417         if (state < minLookup[WINELOOKUP_MAGFILTER] || state > maxLookup[WINELOOKUP_MAGFILTER]) {
418             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
419         }
420         glValue = stateLookup[WINELOOKUP_MAGFILTER][state - minLookup[WINELOOKUP_MAGFILTER]];
421         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
422         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
423         /* We need to reset the Aniotropic filtering state when we change the mag filter to WINED3DTEXF_ANISOTROPIC (this seems a bit weird, check the documentataion to see how it should be switched off. */
424         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && WINED3DTEXF_ANISOTROPIC == state &&
425             textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
426             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
427         }
428         This->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = state;
429     }
430
431     if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB &&
432        (samplerStates[WINED3DSAMP_MINFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MINFILTER] ||
433         samplerStates[WINED3DSAMP_MIPFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] ||
434         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL])) {
435         GLint glValue;
436
437         This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
438         This->baseTexture.states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
439         This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
440
441         if (This->baseTexture.states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
442             This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
443         {
444
445             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
446                   This->baseTexture.states[WINED3DTEXSTA_MINFILTER],
447                   This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]);
448         }
449         glValue = minMipLookup[min(max(samplerStates[WINED3DSAMP_MINFILTER],WINED3DTEXF_NONE), WINED3DTEXF_ANISOTROPIC)]
450                 [min(max(samplerStates[WINED3DSAMP_MIPFILTER],WINED3DTEXF_NONE), WINED3DTEXF_LINEAR)];
451
452         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
453               samplerStates[WINED3DSAMP_MINFILTER],
454               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
455         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
456         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
457
458         if(This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
459             glValue = 0;
460         } else if(This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
461             glValue = This->baseTexture.levels - 1;
462         } else {
463             glValue = This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL];
464         }
465         glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
466     }
467
468     if(samplerStates[WINED3DSAMP_MAXANISOTROPY] != This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY]) {
469         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
470             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
471             checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
472         } else {
473             WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
474         }
475         This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];
476     }
477 }
478
479
480 static const IWineD3DBaseTextureVtbl IWineD3DBaseTexture_Vtbl =
481 {
482     /* IUnknown */
483     IWineD3DBaseTextureImpl_QueryInterface,
484     IWineD3DBaseTextureImpl_AddRef,
485     IWineD3DBaseTextureImpl_Release,
486     /* IWineD3DResource */
487     IWineD3DBaseTextureImpl_GetParent,
488     IWineD3DBaseTextureImpl_GetDevice,
489     IWineD3DBaseTextureImpl_SetPrivateData,
490     IWineD3DBaseTextureImpl_GetPrivateData,
491     IWineD3DBaseTextureImpl_FreePrivateData,
492     IWineD3DBaseTextureImpl_SetPriority,
493     IWineD3DBaseTextureImpl_GetPriority,
494     IWineD3DBaseTextureImpl_PreLoad,
495     IWineD3DBaseTextureImpl_UnLoad,
496     IWineD3DBaseTextureImpl_GetType,
497     /*IWineD3DBaseTexture*/
498     IWineD3DBaseTextureImpl_SetLOD,
499     IWineD3DBaseTextureImpl_GetLOD,
500     IWineD3DBaseTextureImpl_GetLevelCount,
501     IWineD3DBaseTextureImpl_SetAutoGenFilterType,
502     IWineD3DBaseTextureImpl_GetAutoGenFilterType,
503     IWineD3DBaseTextureImpl_GenerateMipSubLevels,
504     IWineD3DBaseTextureImpl_SetDirty,
505     IWineD3DBaseTextureImpl_GetDirty,
506     /* internal */
507     IWineD3DBaseTextureImpl_BindTexture,
508     IWineD3DBaseTextureImpl_UnBindTexture,
509     IWineD3DBaseTextureImpl_GetTextureDimensions,
510     IWineD3DBaseTextureImpl_ApplyStateChanges
511
512 };