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