2 * IWineD3DBaseTexture Implementation
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
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.
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.
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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
30 void basetexture_init(struct IWineD3DBaseTextureClass *texture, UINT levels, DWORD usage)
32 texture->levels = levels;
33 texture->filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
35 texture->dirty = TRUE;
36 texture->is_srgb = FALSE;
37 texture->srgb_mode_change_count = 0;
40 void basetexture_cleanup(IWineD3DBaseTexture *iface)
42 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
43 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
45 TRACE("(%p) : textureName(%d)\n", This, This->baseTexture.textureName);
46 if (This->baseTexture.textureName != 0) {
47 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
49 TRACE("(%p) : Deleting texture %d\n", This, This->baseTexture.textureName);
50 glDeleteTextures(1, &This->baseTexture.textureName);
54 resource_cleanup((IWineD3DResource *)iface);
57 void basetexture_unload(IWineD3DBaseTexture *iface)
59 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
60 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
62 if(This->baseTexture.textureName) {
63 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
65 glDeleteTextures(1, &This->baseTexture.textureName);
66 This->baseTexture.textureName = 0;
69 This->baseTexture.dirty = TRUE;
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)
76 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
78 if (This->resource.pool != WINED3DPOOL_MANAGED) {
79 return WINED3DERR_INVALIDCALL;
82 if(LODNew >= This->baseTexture.levels)
83 LODNew = This->baseTexture.levels - 1;
84 This->baseTexture.LOD = LODNew;
86 TRACE("(%p) : set bogus LOD to %d\n", This, This->baseTexture.LOD);
88 return This->baseTexture.LOD;
91 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
93 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
95 if (This->resource.pool != WINED3DPOOL_MANAGED) {
96 return WINED3DERR_INVALIDCALL;
99 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
101 return This->baseTexture.LOD;
104 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
106 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
107 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
108 return This->baseTexture.levels;
111 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
113 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
114 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
115 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
117 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
118 TRACE("(%p) : returning invalid call\n", This);
119 return WINED3DERR_INVALIDCALL;
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
126 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
128 glBindTexture(textureDimensions, This->baseTexture.textureName);
129 checkGLcall("glBindTexture");
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)");
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)");
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)");
149 This->baseTexture.filterType = FilterType;
150 TRACE("(%p) :\n", This);
154 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
156 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
157 FIXME("(%p) : stub\n", This);
158 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
159 return WINED3DTEXF_NONE;
161 return This->baseTexture.filterType;
164 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
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);
172 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
175 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
176 old = This->baseTexture.dirty;
177 This->baseTexture.dirty = dirty;
181 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
183 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
184 return This->baseTexture.dirty;
187 HRESULT basetexture_bind(IWineD3DBaseTexture *iface)
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);
195 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
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) */
206 glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
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);
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
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)");
238 /* Bind the texture */
239 if (This->baseTexture.textureName != 0) {
240 glBindTexture(textureDimensions, This->baseTexture.textureName);
241 checkGLcall("glBindTexture");
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)
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)");
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);
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;
271 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
275 if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
276 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
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;
285 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
288 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
290 TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
291 glTexParameteri(textureDimensions, type, wrapParm);
292 checkGLcall("glTexParameteri(..., type, wrapParm)");
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])
300 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
302 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
303 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
305 /* ApplyStateChanges relies on the correct texture being bound and loaded. */
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;
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;
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;
325 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != This->baseTexture.states[WINED3DTEXSTA_BORDERCOLOR]) {
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;
336 if(samplerStates[WINED3DSAMP_MAGFILTER] != This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]) {
338 state = samplerStates[WINED3DSAMP_MAGFILTER];
339 if (state > WINED3DTEXF_ANISOTROPIC) {
340 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
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 &&
348 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
350 This->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = state;
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])) {
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];
363 if (This->baseTexture.states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
364 This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
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]);
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)];
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, ...");
382 if(This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
384 } else if(This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
385 glValue = This->baseTexture.levels - 1;
387 glValue = This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL];
389 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
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 ...");
398 WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
400 This->baseTexture.states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];