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 void basetexture_unload(IWineD3DBaseTexture *iface)
75 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
76 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
78 if(This->baseTexture.texture_rgb.name) {
79 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
81 glDeleteTextures(1, &This->baseTexture.texture_rgb.name);
82 This->baseTexture.texture_rgb.name = 0;
86 if(This->baseTexture.texture_srgb.name) {
87 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
89 glDeleteTextures(1, &This->baseTexture.texture_srgb.name);
90 This->baseTexture.texture_srgb.name = 0;
93 This->baseTexture.texture_rgb.dirty = TRUE;
94 This->baseTexture.texture_srgb.dirty = TRUE;
97 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
99 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
100 DWORD old = This->baseTexture.LOD;
102 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
103 * textures. The call always returns 0, and GetLOD always returns 0
105 if (This->resource.pool != WINED3DPOOL_MANAGED) {
106 TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
110 if(LODNew >= This->baseTexture.levels)
111 LODNew = This->baseTexture.levels - 1;
113 if(This->baseTexture.LOD != LODNew) {
114 This->baseTexture.LOD = LODNew;
116 This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
117 This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
118 if(This->baseTexture.bindCount) {
119 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(This->baseTexture.sampler));
123 TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
128 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
130 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
132 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
134 return This->baseTexture.LOD;
137 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
139 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
140 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
141 return This->baseTexture.levels;
144 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
146 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
147 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
148 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
150 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
151 TRACE("(%p) : returning invalid call\n", This);
152 return WINED3DERR_INVALIDCALL;
154 if(This->baseTexture.filterType != FilterType) {
155 /* What about multithreading? Do we want all the context overhead just to set this value?
156 * Or should we delay the applying until the texture is used for drawing? For now, apply
159 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
161 glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
162 checkGLcall("glBindTexture");
164 case WINED3DTEXF_NONE:
165 case WINED3DTEXF_POINT:
166 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
167 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
170 case WINED3DTEXF_LINEAR:
171 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
172 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
176 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
177 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
178 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
182 This->baseTexture.filterType = FilterType;
183 TRACE("(%p) :\n", This);
187 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
189 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
191 FIXME("(%p) : stub\n", This);
193 return This->baseTexture.filterType;
196 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
198 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
199 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
200 FIXME("(%p) : stub\n", This);
204 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
207 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
208 old = This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
209 This->baseTexture.texture_rgb.dirty = dirty;
210 This->baseTexture.texture_srgb.dirty = dirty;
214 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
216 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
217 return This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
220 /* Context activation is done by the caller. */
221 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
223 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
224 HRESULT hr = WINED3D_OK;
225 UINT textureDimensions;
226 BOOL isNewTexture = FALSE;
227 struct gl_texture *gl_tex;
228 TRACE("(%p) : About to bind texture\n", This);
230 This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
232 gl_tex = &This->baseTexture.texture_srgb;
234 gl_tex = &This->baseTexture.texture_rgb;
237 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
239 /* Generate a texture name if we don't already have one */
240 if (gl_tex->name == 0) {
241 *set_surface_desc = TRUE;
242 glGenTextures(1, &gl_tex->name);
243 checkGLcall("glGenTextures");
244 TRACE("Generated texture %d\n", gl_tex->name);
245 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
246 /* Tell opengl to try and keep this texture in video ram (well mostly) */
249 glPrioritizeTextures(1, &gl_tex->name, &tmp);
252 /* Initialise the state of the texture object
253 to the openGL defaults, not the directx defaults */
254 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
255 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
256 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
257 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
258 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
259 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
260 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
261 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
262 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
263 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
264 gl_tex->states[WINED3DTEXSTA_ELEMENTINDEX] = 0;
265 gl_tex->states[WINED3DTEXSTA_DMAPOFFSET] = 0;
266 gl_tex->states[WINED3DTEXSTA_TSSADDRESSW] = WINED3DTADDRESS_WRAP;
267 IWineD3DBaseTexture_SetDirty(iface, TRUE);
270 if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
271 /* This means double binding the texture at creation, but keeps the code simpler all
272 * in all, and the run-time path free from additional checks
274 glBindTexture(textureDimensions, gl_tex->name);
275 checkGLcall("glBindTexture");
276 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
277 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
280 *set_surface_desc = FALSE;
283 /* Bind the texture */
284 if (gl_tex->name != 0) {
285 glBindTexture(textureDimensions, gl_tex->name);
286 checkGLcall("glBindTexture");
288 /* For a new texture we have to set the textures levels after binding the texture.
289 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
290 * also need to set the texture dimensions before the texture is set
291 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
292 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
293 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
295 if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
296 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
297 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
298 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
300 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
301 /* Cubemaps are always set to clamp, regardless of the sampler state. */
302 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
303 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
304 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
307 } else { /* this only happened if we've run out of openGL textures */
308 WARN("This texture doesn't have an openGL texture assigned to it\n");
309 hr = WINED3DERR_INVALIDCALL;
316 /* GL locking is done by the caller */
317 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
321 if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
322 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
324 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
325 /* Cubemaps are always set to clamp, regardless of the sampler state. */
326 wrapParm = GL_CLAMP_TO_EDGE;
327 } else if(cond_np2) {
328 if(state == WINED3DTADDRESS_WRAP) {
329 wrapParm = GL_CLAMP_TO_EDGE;
331 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
334 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
336 TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
337 glTexParameteri(textureDimensions, type, wrapParm);
338 checkGLcall("glTexParameteri(..., type, wrapParm)");
342 /* GL locking is done by the caller (state handler) */
343 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
344 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
345 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
347 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
349 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
350 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
352 struct gl_texture *gl_tex;
354 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
356 if(This->baseTexture.is_srgb) {
357 gl_tex = &This->baseTexture.texture_srgb;
359 gl_tex = &This->baseTexture.texture_rgb;
362 /* This function relies on the correct texture being bound and loaded. */
364 if(samplerStates[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
365 state = samplerStates[WINED3DSAMP_ADDRESSU];
366 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
367 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
370 if(samplerStates[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
371 state = samplerStates[WINED3DSAMP_ADDRESSV];
372 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
373 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
376 if(samplerStates[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
377 state = samplerStates[WINED3DSAMP_ADDRESSW];
378 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
379 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
382 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
385 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
386 D3DCOLORTOGLFLOAT4(state, col);
387 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
388 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
389 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
390 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
393 if(samplerStates[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
395 state = samplerStates[WINED3DSAMP_MAGFILTER];
396 if (state > WINED3DTEXF_ANISOTROPIC) {
397 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
400 glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
401 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
402 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
403 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
405 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
408 if((samplerStates[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
409 samplerStates[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
410 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
413 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
414 gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
415 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
417 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
418 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
421 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
422 gl_tex->states[WINED3DTEXSTA_MINFILTER],
423 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
425 glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
426 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
427 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
429 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
430 samplerStates[WINED3DSAMP_MINFILTER],
431 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
432 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
433 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
436 if(gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
437 glValue = This->baseTexture.LOD;
438 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
439 glValue = This->baseTexture.levels - 1;
440 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD) {
441 /* baseTexture.LOD is already clamped in the setter */
442 glValue = This->baseTexture.LOD;
444 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
446 /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
447 * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
448 * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
450 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
454 if ((gl_tex->states[WINED3DSAMP_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
455 && gl_tex->states[WINED3DSAMP_MINFILTER] != WINED3DTEXF_ANISOTROPIC
456 && gl_tex->states[WINED3DSAMP_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
463 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
466 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
468 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC))
470 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
471 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
475 WARN("Anisotropic filtering not supported.\n");
477 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;