wined3d: Get rid of vcheckGLcall.
[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                 
253     } else { /* this only happened if we've run out of openGL textures */
254         WARN("This texture doesn't have an openGL texture assigned to it\n");
255         hr =  WINED3DERR_INVALIDCALL;
256     }
257
258     LEAVE_GL();
259     return hr;
260 }
261
262 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
263                               BOOL cond_np2) {
264     GLint wrapParm;
265
266     if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
267         FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
268     } else {
269         if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
270             /* Cubemaps are always set to clamp, regardless of the sampler state. */
271             wrapParm = GL_CLAMP_TO_EDGE;
272         } else if(cond_np2) {
273             if(state == WINED3DTADDRESS_WRAP) {
274                 wrapParm = GL_CLAMP_TO_EDGE;
275             } else {
276                 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
277             }
278         } else {
279             wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
280         }
281         TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
282         glTexParameteri(textureDimensions, type, wrapParm);
283         checkGLcall("glTexParameteri(..., type, wrapParm)");
284     }
285 }
286
287 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
288         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
289         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
290 {
291     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
292     DWORD state;
293     GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
294     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
295
296     /* ApplyStateChanges relies on the correct texture being bound and loaded. */
297
298     if(samplerStates[WINED3DSAMP_ADDRESSU]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]) {
299         state = samplerStates[WINED3DSAMP_ADDRESSU];
300         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
301         This->baseTexture.states[WINED3DTEXSTA_ADDRESSU] = state;
302     }
303
304     if(samplerStates[WINED3DSAMP_ADDRESSV]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]) {
305         state = samplerStates[WINED3DSAMP_ADDRESSV];
306         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
307         This->baseTexture.states[WINED3DTEXSTA_ADDRESSV] = state;
308     }
309
310     if(samplerStates[WINED3DSAMP_ADDRESSW]      != This->baseTexture.states[WINED3DTEXSTA_ADDRESSW]) {
311         state = samplerStates[WINED3DSAMP_ADDRESSW];
312         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
313         This->baseTexture.states[WINED3DTEXSTA_ADDRESSW] = state;
314     }
315
316     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]) {
317         float col[4];
318
319         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
320         D3DCOLORTOGLFLOAT4(state, col);
321         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
322         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
323         checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
324         This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR] = state;
325     }
326
327     if(samplerStates[WINED3DSAMP_MAGFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]) {
328         GLint glValue;
329         state = samplerStates[WINED3DSAMP_MAGFILTER];
330         if (state > WINED3DTEXF_ANISOTROPIC) {
331             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
332         } else {
333             glValue = This->baseTexture.magLookup[state - WINED3DTEXF_NONE];
334             TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
335             glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
336             /* 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. */
337             if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && WINED3DTEXF_ANISOTROPIC == state &&
338                 !cond_np2) {
339                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
340             }
341             This->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = state;
342         }
343     }
344
345     if((samplerStates[WINED3DSAMP_MINFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MINFILTER] ||
346         samplerStates[WINED3DSAMP_MIPFILTER]     != This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] ||
347         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL])) {
348         GLint glValue;
349
350         This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
351         This->baseTexture.states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
352         This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
353
354         if (This->baseTexture.states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
355             This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
356         {
357
358             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
359                   This->baseTexture.states[WINED3DTEXSTA_MINFILTER],
360                   This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]);
361         }
362         glValue = This->baseTexture.minMipLookup
363                 [min(max(samplerStates[WINED3DSAMP_MINFILTER],WINED3DTEXF_NONE), WINED3DTEXF_ANISOTROPIC)]
364                 .mip[min(max(samplerStates[WINED3DSAMP_MIPFILTER],WINED3DTEXF_NONE), WINED3DTEXF_LINEAR)];
365
366         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
367               samplerStates[WINED3DSAMP_MINFILTER],
368               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
369         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
370         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
371
372         if(!cond_np2) {
373             if(This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
374                 glValue = 0;
375             } else if(This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
376                 glValue = This->baseTexture.levels - 1;
377             } else {
378                 glValue = This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL];
379             }
380             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
381         }
382     }
383
384     if(samplerStates[WINED3DSAMP_MAXANISOTROPY] != This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY]) {
385         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && !cond_np2) {
386             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
387             checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
388         } else {
389             WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
390         }
391         This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];
392     }
393 }