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 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
)
36 hr
= resource_init((IWineD3DResource
*)texture
, resource_type
, device
, size
, usage
, format_desc
, pool
, parent
);
39 WARN("Failed to initialize resource, returning %#x\n", 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
;
54 void basetexture_cleanup(IWineD3DBaseTexture
*iface
)
56 basetexture_unload(iface
);
57 resource_cleanup((IWineD3DResource
*)iface
);
60 void basetexture_unload(IWineD3DBaseTexture
*iface
)
62 IWineD3DTextureImpl
*This
= (IWineD3DTextureImpl
*)iface
;
63 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
65 if(This
->baseTexture
.textureName
) {
66 ActivateContext(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
68 glDeleteTextures(1, &This
->baseTexture
.textureName
);
69 This
->baseTexture
.textureName
= 0;
73 if(This
->baseTexture
.srgbTextureName
) {
74 ActivateContext(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
76 glDeleteTextures(1, &This
->baseTexture
.srgbTextureName
);
77 This
->baseTexture
.srgbTextureName
= 0;
80 This
->baseTexture
.dirty
= TRUE
;
81 This
->baseTexture
.srgbDirty
= TRUE
;
84 /* There is no OpenGL equivalent of setLOD, getLOD. All they do anyway is prioritize texture loading
85 * so just pretend that they work unless something really needs a failure. */
86 DWORD
basetexture_set_lod(IWineD3DBaseTexture
*iface
, DWORD LODNew
)
88 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
90 if (This
->resource
.pool
!= WINED3DPOOL_MANAGED
) {
91 return WINED3DERR_INVALIDCALL
;
94 if(LODNew
>= This
->baseTexture
.levels
)
95 LODNew
= This
->baseTexture
.levels
- 1;
96 This
->baseTexture
.LOD
= LODNew
;
98 TRACE("(%p) : set bogus LOD to %d\n", This
, This
->baseTexture
.LOD
);
100 return This
->baseTexture
.LOD
;
103 DWORD
basetexture_get_lod(IWineD3DBaseTexture
*iface
)
105 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
107 if (This
->resource
.pool
!= WINED3DPOOL_MANAGED
) {
108 return WINED3DERR_INVALIDCALL
;
111 TRACE("(%p) : returning %d\n", This
, This
->baseTexture
.LOD
);
113 return This
->baseTexture
.LOD
;
116 DWORD
basetexture_get_level_count(IWineD3DBaseTexture
*iface
)
118 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
119 TRACE("(%p) : returning %d\n", This
, This
->baseTexture
.levels
);
120 return This
->baseTexture
.levels
;
123 HRESULT
basetexture_set_autogen_filter_type(IWineD3DBaseTexture
*iface
, WINED3DTEXTUREFILTERTYPE FilterType
)
125 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
126 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
127 UINT textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
129 if (!(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)) {
130 TRACE("(%p) : returning invalid call\n", This
);
131 return WINED3DERR_INVALIDCALL
;
133 if(This
->baseTexture
.filterType
!= FilterType
) {
134 /* What about multithreading? Do we want all the context overhead just to set this value?
135 * Or should we delay the applying until the texture is used for drawing? For now, apply
138 ActivateContext(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
140 glBindTexture(textureDimensions
, This
->baseTexture
.textureName
);
141 checkGLcall("glBindTexture");
143 case WINED3DTEXF_NONE
:
144 case WINED3DTEXF_POINT
:
145 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_FASTEST
);
146 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
149 case WINED3DTEXF_LINEAR
:
150 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
151 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
155 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType
);
156 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
157 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
161 This
->baseTexture
.filterType
= FilterType
;
162 TRACE("(%p) :\n", This
);
166 WINED3DTEXTUREFILTERTYPE
basetexture_get_autogen_filter_type(IWineD3DBaseTexture
*iface
)
168 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
169 FIXME("(%p) : stub\n", This
);
170 if (!(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)) {
171 return WINED3DTEXF_NONE
;
173 return This
->baseTexture
.filterType
;
176 void basetexture_generate_mipmaps(IWineD3DBaseTexture
*iface
)
178 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
179 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
180 FIXME("(%p) : stub\n", This
);
184 BOOL
basetexture_set_dirty(IWineD3DBaseTexture
*iface
, BOOL dirty
)
187 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
188 old
= This
->baseTexture
.dirty
|| This
->baseTexture
.srgbDirty
;
189 This
->baseTexture
.dirty
= dirty
;
190 This
->baseTexture
.srgbDirty
= dirty
;
194 BOOL
basetexture_get_dirty(IWineD3DBaseTexture
*iface
)
196 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
197 return This
->baseTexture
.dirty
|| This
->baseTexture
.srgbDirty
;
200 /* Context activation is done by the caller. */
201 HRESULT
basetexture_bind(IWineD3DBaseTexture
*iface
, BOOL srgb
, BOOL
*set_surface_desc
)
203 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
204 HRESULT hr
= WINED3D_OK
;
205 UINT textureDimensions
;
206 BOOL isNewTexture
= FALSE
;
209 TRACE("(%p) : About to bind texture\n", This
);
211 This
->baseTexture
.is_srgb
= srgb
; /* SRGB mode cache for PreLoad calls outside drawprim */
213 texture
= &This
->baseTexture
.srgbTextureName
;
214 states
= This
->baseTexture
.srgbstates
;
216 texture
= &This
->baseTexture
.textureName
;
217 states
= This
->baseTexture
.states
;
220 textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
222 /* Generate a texture name if we don't already have one */
224 *set_surface_desc
= TRUE
;
225 glGenTextures(1, texture
);
226 checkGLcall("glGenTextures");
227 TRACE("Generated texture %d\n", *texture
);
228 if (This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
229 /* Tell opengl to try and keep this texture in video ram (well mostly) */
232 glPrioritizeTextures(1, texture
, &tmp
);
235 /* Initialise the state of the texture object
236 to the openGL defaults, not the directx defaults */
237 states
[WINED3DTEXSTA_ADDRESSU
] = WINED3DTADDRESS_WRAP
;
238 states
[WINED3DTEXSTA_ADDRESSV
] = WINED3DTADDRESS_WRAP
;
239 states
[WINED3DTEXSTA_ADDRESSW
] = WINED3DTADDRESS_WRAP
;
240 states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
241 states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_LINEAR
;
242 states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
243 states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
244 states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
245 states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
246 states
[WINED3DTEXSTA_SRGBTEXTURE
] = 0;
247 states
[WINED3DTEXSTA_ELEMENTINDEX
] = 0;
248 states
[WINED3DTEXSTA_DMAPOFFSET
] = 0;
249 states
[WINED3DTEXSTA_TSSADDRESSW
] = WINED3DTADDRESS_WRAP
;
250 IWineD3DBaseTexture_SetDirty(iface
, TRUE
);
253 if(This
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
) {
254 /* This means double binding the texture at creation, but keeps the code simpler all
255 * in all, and the run-time path free from additional checks
257 glBindTexture(textureDimensions
, *texture
);
258 checkGLcall("glBindTexture");
259 glTexParameteri(textureDimensions
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
260 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
263 *set_surface_desc
= FALSE
;
266 /* Bind the texture */
268 glBindTexture(textureDimensions
, *texture
);
269 checkGLcall("glBindTexture");
271 /* For a new texture we have to set the textures levels after binding the texture.
272 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
273 * also need to set the texture dimensions before the texture is set
274 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
275 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
276 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
278 if(textureDimensions
!= GL_TEXTURE_RECTANGLE_ARB
) {
279 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This
->baseTexture
.levels
- 1);
280 glTexParameteri(textureDimensions
, GL_TEXTURE_MAX_LEVEL
, This
->baseTexture
.levels
- 1);
281 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
283 if(textureDimensions
==GL_TEXTURE_CUBE_MAP_ARB
) {
284 /* Cubemaps are always set to clamp, regardless of the sampler state. */
285 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
286 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
287 glTexParameteri(textureDimensions
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
290 } else { /* this only happened if we've run out of openGL textures */
291 WARN("This texture doesn't have an openGL texture assigned to it\n");
292 hr
= WINED3DERR_INVALIDCALL
;
299 /* GL locking is done by the caller */
300 static inline void apply_wrap(const GLint textureDimensions
, const DWORD state
, const GLint type
,
304 if (state
< minLookup
[WINELOOKUP_WARPPARAM
] || state
> maxLookup
[WINELOOKUP_WARPPARAM
]) {
305 FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state
);
307 if(textureDimensions
==GL_TEXTURE_CUBE_MAP_ARB
) {
308 /* Cubemaps are always set to clamp, regardless of the sampler state. */
309 wrapParm
= GL_CLAMP_TO_EDGE
;
310 } else if(cond_np2
) {
311 if(state
== WINED3DTADDRESS_WRAP
) {
312 wrapParm
= GL_CLAMP_TO_EDGE
;
314 wrapParm
= stateLookup
[WINELOOKUP_WARPPARAM
][state
- minLookup
[WINELOOKUP_WARPPARAM
]];
317 wrapParm
= stateLookup
[WINELOOKUP_WARPPARAM
][state
- minLookup
[WINELOOKUP_WARPPARAM
]];
319 TRACE("Setting WRAP_S to %d for %x\n", wrapParm
, textureDimensions
);
320 glTexParameteri(textureDimensions
, type
, wrapParm
);
321 checkGLcall("glTexParameteri(..., type, wrapParm)");
325 /* GL locking is done by the caller (state handler) */
326 void basetexture_apply_state_changes(IWineD3DBaseTexture
*iface
,
327 const DWORD textureStates
[WINED3D_HIGHEST_TEXTURE_STATE
+ 1],
328 const DWORD samplerStates
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1])
330 IWineD3DBaseTextureImpl
*This
= (IWineD3DBaseTextureImpl
*)iface
;
331 DWORD state
, *states
;
332 GLint textureDimensions
= IWineD3DBaseTexture_GetTextureDimensions(iface
);
333 BOOL cond_np2
= IWineD3DBaseTexture_IsCondNP2(iface
);
336 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface
, textureStates
, samplerStates
);
338 if(This
->baseTexture
.is_srgb
) {
339 states
= This
->baseTexture
.srgbstates
;
341 states
= This
->baseTexture
.states
;
344 /* This function relies on the correct texture being bound and loaded. */
346 if(samplerStates
[WINED3DSAMP_ADDRESSU
] != states
[WINED3DTEXSTA_ADDRESSU
]) {
347 state
= samplerStates
[WINED3DSAMP_ADDRESSU
];
348 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
349 states
[WINED3DTEXSTA_ADDRESSU
] = state
;
352 if(samplerStates
[WINED3DSAMP_ADDRESSV
] != states
[WINED3DTEXSTA_ADDRESSV
]) {
353 state
= samplerStates
[WINED3DSAMP_ADDRESSV
];
354 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
355 states
[WINED3DTEXSTA_ADDRESSV
] = state
;
358 if(samplerStates
[WINED3DSAMP_ADDRESSW
] != states
[WINED3DTEXSTA_ADDRESSW
]) {
359 state
= samplerStates
[WINED3DSAMP_ADDRESSW
];
360 apply_wrap(textureDimensions
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
361 states
[WINED3DTEXSTA_ADDRESSW
] = state
;
364 if(samplerStates
[WINED3DSAMP_BORDERCOLOR
] != states
[WINED3DTEXSTA_BORDERCOLOR
]) {
367 state
= samplerStates
[WINED3DSAMP_BORDERCOLOR
];
368 D3DCOLORTOGLFLOAT4(state
, col
);
369 TRACE("Setting border color for %u to %x\n", textureDimensions
, state
);
370 glTexParameterfv(textureDimensions
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
371 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
372 states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
375 if(samplerStates
[WINED3DSAMP_MAGFILTER
] != states
[WINED3DTEXSTA_MAGFILTER
]) {
377 state
= samplerStates
[WINED3DSAMP_MAGFILTER
];
378 if (state
> WINED3DTEXF_ANISOTROPIC
) {
379 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state
);
381 glValue
= This
->baseTexture
.magLookup
[state
- WINED3DTEXF_NONE
];
382 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state
, glValue
);
383 glTexParameteri(textureDimensions
, GL_TEXTURE_MAG_FILTER
, glValue
);
385 states
[WINED3DTEXSTA_MAGFILTER
] = state
;
388 if((samplerStates
[WINED3DSAMP_MINFILTER
] != states
[WINED3DTEXSTA_MINFILTER
] ||
389 samplerStates
[WINED3DSAMP_MIPFILTER
] != states
[WINED3DTEXSTA_MIPFILTER
] ||
390 samplerStates
[WINED3DSAMP_MAXMIPLEVEL
] != states
[WINED3DTEXSTA_MAXMIPLEVEL
])) {
393 states
[WINED3DTEXSTA_MIPFILTER
] = samplerStates
[WINED3DSAMP_MIPFILTER
];
394 states
[WINED3DTEXSTA_MINFILTER
] = samplerStates
[WINED3DSAMP_MINFILTER
];
395 states
[WINED3DTEXSTA_MAXMIPLEVEL
] = samplerStates
[WINED3DSAMP_MAXMIPLEVEL
];
397 if (states
[WINED3DTEXSTA_MINFILTER
] > WINED3DTEXF_ANISOTROPIC
||
398 states
[WINED3DTEXSTA_MIPFILTER
] > WINED3DTEXF_LINEAR
)
401 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
402 states
[WINED3DTEXSTA_MINFILTER
],
403 states
[WINED3DTEXSTA_MIPFILTER
]);
405 glValue
= This
->baseTexture
.minMipLookup
406 [min(max(samplerStates
[WINED3DSAMP_MINFILTER
],WINED3DTEXF_NONE
), WINED3DTEXF_ANISOTROPIC
)]
407 .mip
[min(max(samplerStates
[WINED3DSAMP_MIPFILTER
],WINED3DTEXF_NONE
), WINED3DTEXF_LINEAR
)];
409 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
410 samplerStates
[WINED3DSAMP_MINFILTER
],
411 samplerStates
[WINED3DSAMP_MIPFILTER
], glValue
);
412 glTexParameteri(textureDimensions
, GL_TEXTURE_MIN_FILTER
, glValue
);
413 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
416 if(states
[WINED3DTEXSTA_MIPFILTER
] == WINED3DTEXF_NONE
) {
418 } else if(states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= This
->baseTexture
.levels
) {
419 glValue
= This
->baseTexture
.levels
- 1;
421 glValue
= states
[WINED3DTEXSTA_MAXMIPLEVEL
];
423 glTexParameteri(textureDimensions
, GL_TEXTURE_BASE_LEVEL
, glValue
);
427 if ((states
[WINED3DSAMP_MAGFILTER
] != WINED3DTEXF_ANISOTROPIC
428 && states
[WINED3DSAMP_MINFILTER
] != WINED3DTEXF_ANISOTROPIC
429 && states
[WINED3DSAMP_MIPFILTER
] != WINED3DTEXF_ANISOTROPIC
)
436 aniso
= samplerStates
[WINED3DSAMP_MAXANISOTROPY
];
439 if (states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
441 if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC
))
443 glTexParameteri(textureDimensions
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
444 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
448 WARN("Anisotropic filtering not supported.\n");
450 states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;