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