push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / wined3d / basetexture.c
blob9d64b63422f53d6d9f2e40366e71f4d06fc821ec
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 void basetexture_init(struct IWineD3DBaseTextureClass *texture, UINT levels, DWORD usage)
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;
41 void basetexture_cleanup(IWineD3DBaseTexture *iface)
43 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
44 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
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();
56 resource_cleanup((IWineD3DResource *)iface);
59 void basetexture_unload(IWineD3DBaseTexture *iface)
61 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
62 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
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();
73 This->baseTexture.dirty = TRUE;
74 This->baseTexture.srgbDirty = TRUE;
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)
81 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
83 if (This->resource.pool != WINED3DPOOL_MANAGED) {
84 return WINED3DERR_INVALIDCALL;
87 if(LODNew >= This->baseTexture.levels)
88 LODNew = This->baseTexture.levels - 1;
89 This->baseTexture.LOD = LODNew;
91 TRACE("(%p) : set bogus LOD to %d\n", This, This->baseTexture.LOD);
93 return This->baseTexture.LOD;
96 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
98 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
100 if (This->resource.pool != WINED3DPOOL_MANAGED) {
101 return WINED3DERR_INVALIDCALL;
104 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
106 return This->baseTexture.LOD;
109 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
111 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
112 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
113 return This->baseTexture.levels;
116 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
118 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
119 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
120 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
122 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
123 TRACE("(%p) : returning invalid call\n", This);
124 return WINED3DERR_INVALIDCALL;
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.
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)");
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)");
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)");
152 LEAVE_GL();
154 This->baseTexture.filterType = FilterType;
155 TRACE("(%p) :\n", This);
156 return WINED3D_OK;
159 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
161 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
162 FIXME("(%p) : stub\n", This);
163 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
164 return WINED3DTEXF_NONE;
166 return This->baseTexture.filterType;
169 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
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 ;
177 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
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;
187 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
189 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
190 return This->baseTexture.dirty || This->baseTexture.srgbDirty;
193 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
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);
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;
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);
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;
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
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)");
254 } else {
255 *set_surface_desc = FALSE;
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)
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)");
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);
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;
287 LEAVE_GL();
288 return hr;
291 /* GL locking is done by the caller */
292 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
293 BOOL cond_np2) {
294 GLint wrapParm;
296 if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
297 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
298 } else {
299 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
300 /* Cubemaps are always set to clamp, regardless of the sampler state. */
301 wrapParm = GL_CLAMP_TO_EDGE;
302 } else if(cond_np2) {
303 if(state == WINED3DTADDRESS_WRAP) {
304 wrapParm = GL_CLAMP_TO_EDGE;
305 } else {
306 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
308 } else {
309 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
311 TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
312 glTexParameteri(textureDimensions, type, wrapParm);
313 checkGLcall("glTexParameteri(..., type, wrapParm)");
317 /* GL locking is done by the caller (state handler) */
318 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
319 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
320 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
322 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
323 DWORD state, *states;
324 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
325 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
327 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
329 if(This->baseTexture.is_srgb) {
330 states = This->baseTexture.srgbstates;
331 } else {
332 states = This->baseTexture.states;
335 /* This function relies on the correct texture being bound and loaded. */
337 if(samplerStates[WINED3DSAMP_ADDRESSU] != states[WINED3DTEXSTA_ADDRESSU]) {
338 state = samplerStates[WINED3DSAMP_ADDRESSU];
339 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
340 states[WINED3DTEXSTA_ADDRESSU] = state;
343 if(samplerStates[WINED3DSAMP_ADDRESSV] != states[WINED3DTEXSTA_ADDRESSV]) {
344 state = samplerStates[WINED3DSAMP_ADDRESSV];
345 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
346 states[WINED3DTEXSTA_ADDRESSV] = state;
349 if(samplerStates[WINED3DSAMP_ADDRESSW] != states[WINED3DTEXSTA_ADDRESSW]) {
350 state = samplerStates[WINED3DSAMP_ADDRESSW];
351 apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
352 states[WINED3DTEXSTA_ADDRESSW] = state;
355 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != states[WINED3DTEXSTA_BORDERCOLOR]) {
356 float col[4];
358 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
359 D3DCOLORTOGLFLOAT4(state, col);
360 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
361 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
362 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
363 states[WINED3DTEXSTA_BORDERCOLOR] = state;
366 if(samplerStates[WINED3DSAMP_MAGFILTER] != states[WINED3DTEXSTA_MAGFILTER]) {
367 GLint glValue;
368 state = samplerStates[WINED3DSAMP_MAGFILTER];
369 if (state > WINED3DTEXF_ANISOTROPIC) {
370 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
371 } else {
372 glValue = This->baseTexture.magLookup[state - WINED3DTEXF_NONE];
373 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
374 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
375 /* 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. */
376 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && WINED3DTEXF_ANISOTROPIC == state &&
377 !cond_np2) {
378 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
380 states[WINED3DTEXSTA_MAGFILTER] = state;
384 if((samplerStates[WINED3DSAMP_MINFILTER] != states[WINED3DTEXSTA_MINFILTER] ||
385 samplerStates[WINED3DSAMP_MIPFILTER] != states[WINED3DTEXSTA_MIPFILTER] ||
386 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != states[WINED3DTEXSTA_MAXMIPLEVEL])) {
387 GLint glValue;
389 states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
390 states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
391 states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
393 if (states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC ||
394 states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_LINEAR)
397 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
398 states[WINED3DTEXSTA_MINFILTER],
399 states[WINED3DTEXSTA_MIPFILTER]);
401 glValue = This->baseTexture.minMipLookup
402 [min(max(samplerStates[WINED3DSAMP_MINFILTER],WINED3DTEXF_NONE), WINED3DTEXF_ANISOTROPIC)]
403 .mip[min(max(samplerStates[WINED3DSAMP_MIPFILTER],WINED3DTEXF_NONE), WINED3DTEXF_LINEAR)];
405 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
406 samplerStates[WINED3DSAMP_MINFILTER],
407 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
408 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
409 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
411 if(!cond_np2) {
412 if(states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
413 glValue = 0;
414 } else if(states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
415 glValue = This->baseTexture.levels - 1;
416 } else {
417 glValue = states[WINED3DTEXSTA_MAXMIPLEVEL];
419 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
423 if(samplerStates[WINED3DSAMP_MAXANISOTROPY] != states[WINED3DTEXSTA_MAXANISOTROPY]) {
424 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC) && !cond_np2) {
425 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerStates[WINED3DSAMP_MAXANISOTROPY]);
426 checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
427 } else {
428 WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
430 states[WINED3DTEXSTA_MAXANISOTROPY] = samplerStates[WINED3DSAMP_MAXANISOTROPY];