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_cleanup(IWineD3DBaseTexture
*iface
)
32 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
33 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
35 TRACE("(%p) : textureName(%d)\n", This
, This
->baseTexture
.textureName
);
36 if (This
->baseTexture
.textureName
!= 0) {
37 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
39 TRACE("(%p) : Deleting texture %d\n", This
, This
->baseTexture
.textureName
);
40 glDeleteTextures(1, &This
->baseTexture
.textureName
);
44 resource_cleanup((IWineD3DResource
*)iface
);
47 void basetexture_unload(IWineD3DBaseTexture
*iface
)
49 IWineD3DTextureImpl
*This
= (IWineD3DTextureImpl
*)iface
;
50 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
52 if(This
->baseTexture
.textureName
) {
53 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
55 glDeleteTextures(1, &This
->baseTexture
.textureName
);
56 This
->baseTexture
.textureName
= 0;
59 This
->baseTexture
.dirty
= TRUE
;
62 /* There is no OpenGL equivalent of setLOD, getLOD. All they do anyway is prioritize texture loading
63 * so just pretend that they work unless something really needs a failure. */
64 DWORD
basetexture_set_lod(IWineD3DBaseTexture
*iface
, DWORD LODNew
)
66 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
68 if (This
->resource
.pool
!= WINED3DPOOL_MANAGED
) {
69 return WINED3DERR_INVALIDCALL
;
72 if(LODNew
>= This
->baseTexture
.levels
)
73 LODNew
= This
->baseTexture
.levels
- 1;
74 This
->baseTexture
.LOD
= LODNew
;
76 TRACE("(%p) : set bogus LOD to %d\n", This
, This
->baseTexture
.LOD
);
78 return This
->baseTexture
.LOD
;
81 DWORD
basetexture_get_lod(IWineD3DBaseTexture
*iface
)
83 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
85 if (This
->resource
.pool
!= WINED3DPOOL_MANAGED
) {
86 return WINED3DERR_INVALIDCALL
;
89 TRACE("(%p) : returning %d\n", This
, This
->baseTexture
.LOD
);
91 return This
->baseTexture
.LOD
;
94 DWORD
basetexture_get_level_count(IWineD3DBaseTexture
*iface
)
96 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
97 TRACE("(%p) : returning %d\n", This
, This
->baseTexture
.levels
);
98 return This
->baseTexture
.levels
;
101 HRESULT
basetexture_set_autogen_filter_type(IWineD3DBaseTexture
*iface
, WINED3DTEXTUREFILTERTYPE FilterType
)
103 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
104 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
105 UINT textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
107 if (!(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)) {
108 TRACE("(%p) : returning invalid call\n", This
);
109 return WINED3DERR_INVALIDCALL
;
111 if(This
->baseTexture
.filterType
!= FilterType
) {
112 /* What about multithreading? Do we want all the context overhead just to set this value?
113 * Or should we delay the applying until the texture is used for drawing? For now, apply
116 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
118 glBindTexture(textureDimensions
, This
->baseTexture
.textureName
);
119 checkGLcall("glBindTexture");
121 case WINED3DTEXF_NONE
:
122 case WINED3DTEXF_POINT
:
123 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_FASTEST
);
124 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
127 case WINED3DTEXF_LINEAR
:
128 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
129 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
133 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType
);
134 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
135 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
139 This
->baseTexture
.filterType
= FilterType
;
140 TRACE("(%p) :\n", This
);
144 WINED3DTEXTUREFILTERTYPE
basetexture_get_autogen_filter_type(IWineD3DBaseTexture
*iface
)
146 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
147 FIXME("(%p) : stub\n", This
);
148 if (!(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)) {
149 return WINED3DTEXF_NONE
;
151 return This
->baseTexture
.filterType
;
154 void basetexture_generate_mipmaps(IWineD3DBaseTexture
*iface
)
156 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
157 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
158 FIXME("(%p) : stub\n", This
);
162 BOOL
basetexture_set_dirty(IWineD3DBaseTexture
*iface
, BOOL dirty
)
165 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
166 old
= This
->baseTexture
.dirty
;
167 This
->baseTexture
.dirty
= dirty
;
171 BOOL
basetexture_get_dirty(IWineD3DBaseTexture
*iface
)
173 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
174 return This
->baseTexture
.dirty
;
177 HRESULT
basetexture_bind(IWineD3DBaseTexture
*iface
)
179 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
180 HRESULT hr
= WINED3D_OK
;
181 UINT textureDimensions
;
182 BOOL isNewTexture
= FALSE
;
183 TRACE("(%p) : About to bind texture\n", This
);
185 textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
187 /* Generate a texture name if we don't already have one */
188 if (This
->baseTexture
.textureName
== 0) {
189 glGenTextures(1, &This
->baseTexture
.textureName
);
190 checkGLcall("glGenTextures");
191 TRACE("Generated texture %d\n", This
->baseTexture
.textureName
);
192 if (This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
193 /* Tell opengl to try and keep this texture in video ram (well mostly) */
196 glPrioritizeTextures(1, &This
->baseTexture
.textureName
, &tmp
);
199 /* Initialise the state of the texture object
200 to the openGL defaults, not the directx defaults */
201 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSU
] = WINED3DTADDRESS_WRAP
;
202 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSV
] = WINED3DTADDRESS_WRAP
;
203 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSW
] = WINED3DTADDRESS_WRAP
;
204 This
->baseTexture
.states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
205 This
->baseTexture
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_LINEAR
;
206 This
->baseTexture
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
207 This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
208 This
->baseTexture
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
209 This
->baseTexture
.states
[WINED3DTEXSTA_MAXANISOTROPY
] = 0;
210 This
->baseTexture
.states
[WINED3DTEXSTA_SRGBTEXTURE
] = 0;
211 This
->baseTexture
.states
[WINED3DTEXSTA_ELEMENTINDEX
] = 0;
212 This
->baseTexture
.states
[WINED3DTEXSTA_DMAPOFFSET
] = 0;
213 This
->baseTexture
.states
[WINED3DTEXSTA_TSSADDRESSW
] = WINED3DTADDRESS_WRAP
;
214 IWineD3DBaseTexture_SetDirty(iface
, TRUE
);
217 if(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
) {
218 /* This means double binding the texture at creation, but keeps the code simpler all
219 * in all, and the run-time path free from additional checks
221 glBindTexture(textureDimensions
, This
->baseTexture
.textureName
);
222 checkGLcall("glBindTexture");
223 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
224 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
228 /* Bind the texture */
229 if (This
->baseTexture
.textureName
!= 0) {
230 glBindTexture(textureDimensions
, This
->baseTexture
.textureName
);
231 checkGLcall("glBindTexture");
233 /* For a new texture we have to set the textures levels after binding the texture.
234 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
235 * also need to set the texture dimensions before the texture is set
236 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
237 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
238 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
240 if(textureDimensions
!= GL_TEXTURE_RECTANGLE_ARB
) {
241 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This
->baseTexture
.levels
- 1);
242 glTexParameteri(textureDimensions
, GL_TEXTURE_MAX_LEVEL
, This
->baseTexture
.levels
- 1);
243 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
245 if(textureDimensions
==GL_TEXTURE_CUBE_MAP_ARB
) {
246 /* Cubemaps are always set to clamp, regardless of the sampler state. */
247 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
248 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
249 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
253 } else { /* this only happened if we've run out of openGL textures */
254 WARN("This texture doesn't have an openGL texture assigned to it\n");
255 hr
= WINED3DERR_INVALIDCALL
;
262 static inline void apply_wrap(const GLint textureDimensions
, const DWORD state
, const GLint type
,
266 if (state
< minLookup
[WINELOOKUP_WARPPARAM
] || state
> maxLookup
[WINELOOKUP_WARPPARAM
]) {
267 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state
);
269 if(textureDimensions
==GL_TEXTURE_CUBE_MAP_ARB
) {
270 /* Cubemaps are always set to clamp, regardless of the sampler state. */
271 wrapParm
= GL_CLAMP_TO_EDGE
;
272 } else if(cond_np2
) {
273 if(state
== WINED3DTADDRESS_WRAP
) {
274 wrapParm
= GL_CLAMP_TO_EDGE
;
276 wrapParm
= stateLookup
[WINELOOKUP_WARPPARAM
][state
- minLookup
[WINELOOKUP_WARPPARAM
]];
279 wrapParm
= stateLookup
[WINELOOKUP_WARPPARAM
][state
- minLookup
[WINELOOKUP_WARPPARAM
]];
281 TRACE("Setting WRAP_S to %d for %x\n", wrapParm
, textureDimensions
);
282 glTexParameteri(textureDimensions
, type
, wrapParm
);
283 checkGLcall("glTexParameteri(..., type, wrapParm)");
287 void basetexture_apply_state_changes(IWineD3DBaseTexture
*iface
,
288 const DWORD textureStates
[WINED3D_HIGHEST_TEXTURE_STATE
+ 1],
289 const DWORD samplerStates
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1])
291 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
293 GLint textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
294 BOOL cond_np2
= IWineD3DBaseTexture_IsCondNP2(iface
);
296 /* ApplyStateChanges relies on the correct texture being bound and loaded. */
298 if(samplerStates
[WINED3DSAMP_ADDRESSU
] != This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSU
]) {
299 state
= samplerStates
[WINED3DSAMP_ADDRESSU
];
300 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
301 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSU
] = state
;
304 if(samplerStates
[WINED3DSAMP_ADDRESSV
] != This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSV
]) {
305 state
= samplerStates
[WINED3DSAMP_ADDRESSV
];
306 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
307 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSV
] = state
;
310 if(samplerStates
[WINED3DSAMP_ADDRESSW
] != This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSW
]) {
311 state
= samplerStates
[WINED3DSAMP_ADDRESSW
];
312 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
313 This
->baseTexture
.states
[WINED3DTEXSTA_ADDRESSW
] = state
;
316 if(samplerStates
[WINED3DSAMP_BORDERCOLOR
] != This
->baseTexture
.states
[WINED3DTEXSTA_BORDERCOLOR
]) {
319 state
= samplerStates
[WINED3DSAMP_BORDERCOLOR
];
320 D3DCOLORTOGLFLOAT4(state
, col
);
321 TRACE("Setting border color for %u to %x\n", textureDimensions
, state
);
322 glTexParameterfv(textureDimensions
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
323 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
324 This
->baseTexture
.states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
327 if(samplerStates
[WINED3DSAMP_MAGFILTER
] != This
->baseTexture
.states
[WINED3DTEXSTA_MAGFILTER
]) {
329 state
= samplerStates
[WINED3DSAMP_MAGFILTER
];
330 if (state
> WINED3DTEXF_ANISOTROPIC
) {
331 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state
);
333 glValue
= This
->baseTexture
.magLookup
[state
- WINED3DTEXF_NONE
];
334 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state
, glValue
);
335 glTexParameteri(textureDimensions
, GL_TEXTURE_MAG_FILTER
, glValue
);
336 /* 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. */
337 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC
) && WINED3DTEXF_ANISOTROPIC
== state
&&
339 glTexParameteri(textureDimensions
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, samplerStates
[WINED3DSAMP_MAXANISOTROPY
]);
341 This
->baseTexture
.states
[WINED3DTEXSTA_MAGFILTER
] = state
;
345 if((samplerStates
[WINED3DSAMP_MINFILTER
] != This
->baseTexture
.states
[WINED3DTEXSTA_MINFILTER
] ||
346 samplerStates
[WINED3DSAMP_MIPFILTER
] != This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
] ||
347 samplerStates
[WINED3DSAMP_MAXMIPLEVEL
] != This
->baseTexture
.states
[WINED3DTEXSTA_MAXMIPLEVEL
])) {
350 This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
] = samplerStates
[WINED3DSAMP_MIPFILTER
];
351 This
->baseTexture
.states
[WINED3DTEXSTA_MINFILTER
] = samplerStates
[WINED3DSAMP_MINFILTER
];
352 This
->baseTexture
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = samplerStates
[WINED3DSAMP_MAXMIPLEVEL
];
354 if (This
->baseTexture
.states
[WINED3DTEXSTA_MINFILTER
] > WINED3DTEXF_ANISOTROPIC
||
355 This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
] > WINED3DTEXF_LINEAR
)
358 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
359 This
->baseTexture
.states
[WINED3DTEXSTA_MINFILTER
],
360 This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
]);
362 glValue
= This
->baseTexture
.minMipLookup
363 [min(max(samplerStates
[WINED3DSAMP_MINFILTER
],WINED3DTEXF_NONE
), WINED3DTEXF_ANISOTROPIC
)]
364 .mip
[min(max(samplerStates
[WINED3DSAMP_MIPFILTER
],WINED3DTEXF_NONE
), WINED3DTEXF_LINEAR
)];
366 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
367 samplerStates
[WINED3DSAMP_MINFILTER
],
368 samplerStates
[WINED3DSAMP_MIPFILTER
], glValue
);
369 glTexParameteri(textureDimensions
, GL_TEXTURE_MIN_FILTER
, glValue
);
370 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
373 if(This
->baseTexture
.states
[WINED3DTEXSTA_MIPFILTER
] == WINED3DTEXF_NONE
) {
375 } else if(This
->baseTexture
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= This
->baseTexture
.levels
) {
376 glValue
= This
->baseTexture
.levels
- 1;
378 glValue
= This
->baseTexture
.states
[WINED3DTEXSTA_MAXMIPLEVEL
];
380 glTexParameteri(textureDimensions
, GL_TEXTURE_BASE_LEVEL
, glValue
);
384 if(samplerStates
[WINED3DSAMP_MAXANISOTROPY
] != This
->baseTexture
.states
[WINED3DTEXSTA_MAXANISOTROPY
]) {
385 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC
) && !cond_np2
) {
386 glTexParameteri(textureDimensions
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, samplerStates
[WINED3DSAMP_MAXANISOTROPY
]);
387 checkGLcall("glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT ...");
389 WARN("Unsupported in local OpenGL implementation: glTexParameteri GL_TEXTURE_MAX_ANISOTROPY_EXT\n");
391 This
->baseTexture
.states
[WINED3DTEXSTA_MAXANISOTROPY
] = samplerStates
[WINED3DSAMP_MAXANISOTROPY
];