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
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
31 HRESULT basetexture_init(IWineD3DBaseTextureImpl *texture, UINT levels, WINED3DRESOURCETYPE resource_type,
32 IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
33 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
37 hr = resource_init((IWineD3DResource *)texture, resource_type, device,
38 size, usage, format_desc, pool, parent, parent_ops);
41 WARN("Failed to initialize resource, returning %#x\n", hr);
45 texture->baseTexture.levels = levels;
46 texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
47 texture->baseTexture.LOD = 0;
48 texture->baseTexture.texture_rgb.dirty = TRUE;
49 texture->baseTexture.texture_srgb.dirty = TRUE;
50 texture->baseTexture.is_srgb = FALSE;
51 texture->baseTexture.pow2Matrix_identity = TRUE;
53 if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
55 texture->baseTexture.minMipLookup = minMipLookup;
56 texture->baseTexture.magLookup = magLookup;
60 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
61 texture->baseTexture.magLookup = magLookup_noFilter;
67 void basetexture_cleanup(IWineD3DBaseTexture *iface)
69 basetexture_unload(iface);
70 resource_cleanup((IWineD3DResource *)iface);
73 /* A GL context is provided by the caller */
74 static void gltexture_delete(struct gl_texture *tex)
77 glDeleteTextures(1, &tex->name);
82 void basetexture_unload(IWineD3DBaseTexture *iface)
84 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
85 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
87 if(This->baseTexture.texture_rgb.name ||
88 This->baseTexture.texture_srgb.name) {
89 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
92 if(This->baseTexture.texture_rgb.name) {
93 gltexture_delete(&This->baseTexture.texture_rgb);
95 if(This->baseTexture.texture_srgb.name) {
96 gltexture_delete(&This->baseTexture.texture_srgb);
98 This->baseTexture.texture_rgb.dirty = TRUE;
99 This->baseTexture.texture_srgb.dirty = TRUE;
102 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
104 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
105 DWORD old = This->baseTexture.LOD;
107 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
108 * textures. The call always returns 0, and GetLOD always returns 0
110 if (This->resource.pool != WINED3DPOOL_MANAGED) {
111 TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
115 if(LODNew >= This->baseTexture.levels)
116 LODNew = This->baseTexture.levels - 1;
118 if(This->baseTexture.LOD != LODNew) {
119 This->baseTexture.LOD = LODNew;
121 This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
122 This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
123 if(This->baseTexture.bindCount) {
124 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(This->baseTexture.sampler));
128 TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
133 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
135 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
137 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
139 return This->baseTexture.LOD;
142 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
144 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
145 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
146 return This->baseTexture.levels;
149 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
151 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
152 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
153 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
155 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
156 TRACE("(%p) : returning invalid call\n", This);
157 return WINED3DERR_INVALIDCALL;
159 if(This->baseTexture.filterType != FilterType) {
160 /* What about multithreading? Do we want all the context overhead just to set this value?
161 * Or should we delay the applying until the texture is used for drawing? For now, apply
164 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
166 glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
167 checkGLcall("glBindTexture");
169 case WINED3DTEXF_NONE:
170 case WINED3DTEXF_POINT:
171 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
172 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
175 case WINED3DTEXF_LINEAR:
176 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
177 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
181 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
182 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
183 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
187 This->baseTexture.filterType = FilterType;
188 TRACE("(%p) :\n", This);
192 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
194 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
196 FIXME("(%p) : stub\n", This);
198 return This->baseTexture.filterType;
201 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
203 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
204 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
205 FIXME("(%p) : stub\n", This);
209 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
212 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
213 old = This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
214 This->baseTexture.texture_rgb.dirty = dirty;
215 This->baseTexture.texture_srgb.dirty = dirty;
219 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
221 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
222 return This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
225 /* Context activation is done by the caller. */
226 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
228 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
229 HRESULT hr = WINED3D_OK;
230 UINT textureDimensions;
231 BOOL isNewTexture = FALSE;
232 struct gl_texture *gl_tex;
233 TRACE("(%p) : About to bind texture\n", This);
235 This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
237 gl_tex = &This->baseTexture.texture_srgb;
239 gl_tex = &This->baseTexture.texture_rgb;
242 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
244 /* Generate a texture name if we don't already have one */
245 if (gl_tex->name == 0) {
246 *set_surface_desc = TRUE;
247 glGenTextures(1, &gl_tex->name);
248 checkGLcall("glGenTextures");
249 TRACE("Generated texture %d\n", gl_tex->name);
250 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
251 /* Tell opengl to try and keep this texture in video ram (well mostly) */
254 glPrioritizeTextures(1, &gl_tex->name, &tmp);
257 /* Initialise the state of the texture object
258 to the openGL defaults, not the directx defaults */
259 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
260 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
261 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
262 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
263 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
264 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
265 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
266 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
267 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
268 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
269 gl_tex->states[WINED3DTEXSTA_ELEMENTINDEX] = 0;
270 gl_tex->states[WINED3DTEXSTA_DMAPOFFSET] = 0;
271 gl_tex->states[WINED3DTEXSTA_TSSADDRESSW] = WINED3DTADDRESS_WRAP;
272 IWineD3DBaseTexture_SetDirty(iface, TRUE);
275 if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
276 /* This means double binding the texture at creation, but keeps the code simpler all
277 * in all, and the run-time path free from additional checks
279 glBindTexture(textureDimensions, gl_tex->name);
280 checkGLcall("glBindTexture");
281 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
282 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
285 *set_surface_desc = FALSE;
288 /* Bind the texture */
289 if (gl_tex->name != 0) {
290 glBindTexture(textureDimensions, gl_tex->name);
291 checkGLcall("glBindTexture");
293 /* For a new texture we have to set the textures levels after binding the texture.
294 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
295 * also need to set the texture dimensions before the texture is set
296 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
297 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
298 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
300 if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
301 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
302 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
303 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
305 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
306 /* Cubemaps are always set to clamp, regardless of the sampler state. */
307 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
308 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
309 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
312 } else { /* this only happened if we've run out of openGL textures */
313 WARN("This texture doesn't have an openGL texture assigned to it\n");
314 hr = WINED3DERR_INVALIDCALL;
321 /* GL locking is done by the caller */
322 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
326 if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
327 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
329 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
330 /* Cubemaps are always set to clamp, regardless of the sampler state. */
331 wrapParm = GL_CLAMP_TO_EDGE;
332 } else if(cond_np2) {
333 if(state == WINED3DTADDRESS_WRAP) {
334 wrapParm = GL_CLAMP_TO_EDGE;
336 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
339 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
341 TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
342 glTexParameteri(textureDimensions, type, wrapParm);
343 checkGLcall("glTexParameteri(..., type, wrapParm)");
347 /* GL locking is done by the caller (state handler) */
348 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
349 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
350 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
352 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
354 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
355 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
357 struct gl_texture *gl_tex;
359 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
361 if(This->baseTexture.is_srgb) {
362 gl_tex = &This->baseTexture.texture_srgb;
364 gl_tex = &This->baseTexture.texture_rgb;
367 /* This function relies on the correct texture being bound and loaded. */
369 if(samplerStates[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
370 state = samplerStates[WINED3DSAMP_ADDRESSU];
371 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
372 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
375 if(samplerStates[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
376 state = samplerStates[WINED3DSAMP_ADDRESSV];
377 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
378 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
381 if(samplerStates[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
382 state = samplerStates[WINED3DSAMP_ADDRESSW];
383 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
384 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
387 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
390 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
391 D3DCOLORTOGLFLOAT4(state, col);
392 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
393 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
394 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
395 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
398 if(samplerStates[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
400 state = samplerStates[WINED3DSAMP_MAGFILTER];
401 if (state > WINED3DTEXF_ANISOTROPIC) {
402 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
405 glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
406 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
407 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
408 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
410 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
413 if((samplerStates[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
414 samplerStates[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
415 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
418 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
419 gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
420 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
422 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
423 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
426 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
427 gl_tex->states[WINED3DTEXSTA_MINFILTER],
428 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
430 glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
431 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
432 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
434 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
435 samplerStates[WINED3DSAMP_MINFILTER],
436 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
437 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
438 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
441 if(gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
442 glValue = This->baseTexture.LOD;
443 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
444 glValue = This->baseTexture.levels - 1;
445 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD) {
446 /* baseTexture.LOD is already clamped in the setter */
447 glValue = This->baseTexture.LOD;
449 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
451 /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
452 * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
453 * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
455 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
459 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
460 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
461 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
468 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
471 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
473 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC))
475 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
476 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
480 WARN("Anisotropic filtering not supported.\n");
482 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;