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