dplayx: Adjust GetCaps behaviour to documentation
[wine/gsoc_dplay.git] / dlls / wined3d / basetexture.c
blobab38b625aae21f881511b1408b181a8c134a87a7
1 /*
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
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
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)
34 HRESULT hr;
36 hr = resource_init((IWineD3DResource *)texture, resource_type, device, size, usage, format_desc, pool, parent);
37 if (FAILED(hr))
39 WARN("Failed to initialize resource, returning %#x\n", hr);
40 return hr;
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;
51 if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
53 texture->baseTexture.minMipLookup = minMipLookup;
54 texture->baseTexture.magLookup = magLookup;
56 else
58 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
59 texture->baseTexture.magLookup = magLookup_noFilter;
62 return WINED3D_OK;
65 void basetexture_cleanup(IWineD3DBaseTexture *iface)
67 basetexture_unload(iface);
68 resource_cleanup((IWineD3DResource *)iface);
71 void basetexture_unload(IWineD3DBaseTexture *iface)
73 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
74 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
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();
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();
91 This->baseTexture.dirty = TRUE;
92 This->baseTexture.srgbDirty = TRUE;
95 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
97 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
98 DWORD old = This->baseTexture.LOD;
100 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
101 * textures. The call always returns 0, and GetLOD always returns 0
103 if (This->resource.pool != WINED3DPOOL_MANAGED) {
104 TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
105 return 0;
108 if(LODNew >= This->baseTexture.levels)
109 LODNew = This->baseTexture.levels - 1;
111 if(This->baseTexture.LOD != LODNew) {
112 This->baseTexture.LOD = LODNew;
114 This->baseTexture.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
115 This->baseTexture.srgbstates[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
116 if(This->baseTexture.bindCount) {
117 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(This->baseTexture.sampler));
121 TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
123 return old;
126 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
128 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
130 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
132 return This->baseTexture.LOD;
135 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
137 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
138 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
139 return This->baseTexture.levels;
142 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
144 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
145 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
146 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
148 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
149 TRACE("(%p) : returning invalid call\n", This);
150 return WINED3DERR_INVALIDCALL;
152 if(This->baseTexture.filterType != FilterType) {
153 /* What about multithreading? Do we want all the context overhead just to set this value?
154 * Or should we delay the applying until the texture is used for drawing? For now, apply
155 * immediately.
157 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
158 ENTER_GL();
159 glBindTexture(textureDimensions, This->baseTexture.textureName);
160 checkGLcall("glBindTexture");
161 switch(FilterType) {
162 case WINED3DTEXF_NONE:
163 case WINED3DTEXF_POINT:
164 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
165 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
167 break;
168 case WINED3DTEXF_LINEAR:
169 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
170 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
172 break;
173 default:
174 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
175 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
176 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
178 LEAVE_GL();
180 This->baseTexture.filterType = FilterType;
181 TRACE("(%p) :\n", This);
182 return WINED3D_OK;
185 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
187 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
189 FIXME("(%p) : stub\n", This);
191 return This->baseTexture.filterType;
194 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
196 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
197 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
198 FIXME("(%p) : stub\n", This);
199 return ;
202 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
204 BOOL old;
205 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
206 old = This->baseTexture.dirty || This->baseTexture.srgbDirty;
207 This->baseTexture.dirty = dirty;
208 This->baseTexture.srgbDirty = dirty;
209 return old;
212 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
214 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
215 return This->baseTexture.dirty || This->baseTexture.srgbDirty;
218 /* Context activation is done by the caller. */
219 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
221 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
222 HRESULT hr = WINED3D_OK;
223 UINT textureDimensions;
224 BOOL isNewTexture = FALSE;
225 GLuint *texture;
226 DWORD *states;
227 TRACE("(%p) : About to bind texture\n", This);
229 This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
230 if(srgb) {
231 texture = &This->baseTexture.srgbTextureName;
232 states = This->baseTexture.srgbstates;
233 } else {
234 texture = &This->baseTexture.textureName;
235 states = This->baseTexture.states;
238 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
239 ENTER_GL();
240 /* Generate a texture name if we don't already have one */
241 if (*texture == 0) {
242 *set_surface_desc = TRUE;
243 glGenTextures(1, texture);
244 checkGLcall("glGenTextures");
245 TRACE("Generated texture %d\n", *texture);
246 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
247 /* Tell opengl to try and keep this texture in video ram (well mostly) */
248 GLclampf tmp;
249 tmp = 0.9f;
250 glPrioritizeTextures(1, texture, &tmp);
253 /* Initialise the state of the texture object
254 to the openGL defaults, not the directx defaults */
255 states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
256 states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
257 states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
258 states[WINED3DTEXSTA_BORDERCOLOR] = 0;
259 states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
260 states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
261 states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
262 states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
263 states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
264 states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
265 states[WINED3DTEXSTA_ELEMENTINDEX] = 0;
266 states[WINED3DTEXSTA_DMAPOFFSET] = 0;
267 states[WINED3DTEXSTA_TSSADDRESSW] = WINED3DTADDRESS_WRAP;
268 IWineD3DBaseTexture_SetDirty(iface, TRUE);
269 isNewTexture = TRUE;
271 if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
272 /* This means double binding the texture at creation, but keeps the code simpler all
273 * in all, and the run-time path free from additional checks
275 glBindTexture(textureDimensions, *texture);
276 checkGLcall("glBindTexture");
277 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
278 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
280 } else {
281 *set_surface_desc = FALSE;
284 /* Bind the texture */
285 if (*texture != 0) {
286 glBindTexture(textureDimensions, *texture);
287 checkGLcall("glBindTexture");
288 if (isNewTexture) {
289 /* For a new texture we have to set the textures levels after binding the texture.
290 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
291 * also need to set the texture dimensions before the texture is set
292 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
293 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
294 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
296 if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
297 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
298 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
299 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
301 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
302 /* Cubemaps are always set to clamp, regardless of the sampler state. */
303 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
304 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
305 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
308 } else { /* this only happened if we've run out of openGL textures */
309 WARN("This texture doesn't have an openGL texture assigned to it\n");
310 hr = WINED3DERR_INVALIDCALL;
313 LEAVE_GL();
314 return hr;
317 /* GL locking is done by the caller */
318 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
319 BOOL cond_np2) {
320 GLint wrapParm;
322 if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
323 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
324 } else {
325 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
326 /* Cubemaps are always set to clamp, regardless of the sampler state. */
327 wrapParm = GL_CLAMP_TO_EDGE;
328 } else if(cond_np2) {
329 if(state == WINED3DTADDRESS_WRAP) {
330 wrapParm = GL_CLAMP_TO_EDGE;
331 } else {
332 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
334 } else {
335 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
337 TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
338 glTexParameteri(textureDimensions, type, wrapParm);
339 checkGLcall("glTexParameteri(..., type, wrapParm)");
343 /* GL locking is done by the caller (state handler) */
344 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
345 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
346 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
348 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
349 DWORD state, *states;
350 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
351 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
352 DWORD aniso;
354 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
356 if(This->baseTexture.is_srgb) {
357 states = This->baseTexture.srgbstates;
358 } else {
359 states = This->baseTexture.states;
362 /* This function relies on the correct texture being bound and loaded. */
364 if(samplerStates[WINED3DSAMP_ADDRESSU] != states[WINED3DTEXSTA_ADDRESSU]) {
365 state = samplerStates[WINED3DSAMP_ADDRESSU];
366 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
367 states[WINED3DTEXSTA_ADDRESSU] = state;
370 if(samplerStates[WINED3DSAMP_ADDRESSV] != states[WINED3DTEXSTA_ADDRESSV]) {
371 state = samplerStates[WINED3DSAMP_ADDRESSV];
372 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
373 states[WINED3DTEXSTA_ADDRESSV] = state;
376 if(samplerStates[WINED3DSAMP_ADDRESSW] != states[WINED3DTEXSTA_ADDRESSW]) {
377 state = samplerStates[WINED3DSAMP_ADDRESSW];
378 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
379 states[WINED3DTEXSTA_ADDRESSW] = state;
382 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != states[WINED3DTEXSTA_BORDERCOLOR]) {
383 float col[4];
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 states[WINED3DTEXSTA_BORDERCOLOR] = state;
393 if(samplerStates[WINED3DSAMP_MAGFILTER] != states[WINED3DTEXSTA_MAGFILTER]) {
394 GLint glValue;
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 states[WINED3DTEXSTA_MAGFILTER] = state;
408 if((samplerStates[WINED3DSAMP_MINFILTER] != states[WINED3DTEXSTA_MINFILTER] ||
409 samplerStates[WINED3DSAMP_MIPFILTER] != states[WINED3DTEXSTA_MIPFILTER] ||
410 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != states[WINED3DTEXSTA_MAXMIPLEVEL])) {
411 GLint glValue;
413 states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
414 states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
415 states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
417 if (states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
418 || states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
421 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
422 states[WINED3DTEXSTA_MINFILTER],
423 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, ...");
435 if(!cond_np2) {
436 if(states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
437 glValue = This->baseTexture.LOD;
438 } else if(states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
439 glValue = This->baseTexture.levels - 1;
440 } else if(states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD) {
441 /* baseTexture.LOD is already clamped in the setter */
442 glValue = This->baseTexture.LOD;
443 } else {
444 glValue = 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 ((states[WINED3DSAMP_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
455 && states[WINED3DSAMP_MINFILTER] != WINED3DTEXF_ANISOTROPIC
456 && states[WINED3DSAMP_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
457 || cond_np2)
459 aniso = 1;
461 else
463 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
466 if (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)");
473 else
475 WARN("Anisotropic filtering not supported.\n");
477 states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;