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