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