2 * IWineD3DBaseTexture Implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29 /* *******************************************
30 IWineD3DBaseTexture IUnknown parts follow
31 ******************************************* */
32 HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
34 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
35 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36 /* FIXME: This needs to extend an IWineD3DBaseObject */
37 if (IsEqualGUID(riid, &IID_IUnknown)
38 || IsEqualGUID(riid, &IID_IWineD3DResource)
39 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)) {
40 IUnknown_AddRef(iface);
47 ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface) {
48 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
49 ULONG ref = InterlockedIncrement(&This->resource.ref);
51 TRACE("(%p) : AddRef increasing from %ld\n", This,ref - 1);
55 ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface) {
56 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
57 ULONG ref = InterlockedDecrement(&This->resource.ref);
58 TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
60 IWineD3DBaseTextureImpl_CleanUp(iface);
61 HeapFree(GetProcessHeap(), 0, This);
67 void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface) {
68 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
69 TRACE("(%p) : textureName(%d)\n", This, This->baseTexture.textureName);
70 if (This->baseTexture.textureName != 0) {
72 TRACE("(%p) : Deleting texture %d\n", This, This->baseTexture.textureName);
73 glDeleteTextures(1, &This->baseTexture.textureName);
76 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
79 /* ****************************************************
80 IWineD3DBaseTexture IWineD3DResource parts follow
81 **************************************************** */
82 HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice** ppDevice) {
83 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
86 HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
87 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
90 HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
91 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
94 HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid) {
95 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
98 DWORD WINAPI IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD PriorityNew) {
99 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
102 DWORD WINAPI IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface) {
103 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
106 void WINAPI IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface) {
107 return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
110 D3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface) {
111 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
114 HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent) {
115 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
118 /* ******************************************************
119 IWineD3DBaseTexture IWineD3DBaseTexture parts follow
120 ****************************************************** */
122 /* There is no OpenGL equivilent of setLOD, getLOD, all they do it priortise testure loading
123 * so just pretend that they work unless something really needs a failure. */
124 DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew) {
125 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
127 if (This->resource.pool != D3DPOOL_MANAGED) {
128 return D3DERR_INVALIDCALL;
131 if(LODNew >= This->baseTexture.levels)
132 LODNew = This->baseTexture.levels - 1;
133 This->baseTexture.LOD = LODNew;
135 TRACE("(%p) : set bogus LOD to %d \n", This, This->baseTexture.LOD);
137 return This->baseTexture.LOD;
140 DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface) {
141 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
143 if (This->resource.pool != D3DPOOL_MANAGED) {
144 return D3DERR_INVALIDCALL;
147 TRACE("(%p) : returning %d \n", This, This->baseTexture.LOD);
149 return This->baseTexture.LOD;
152 DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface) {
153 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
154 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
155 return This->baseTexture.levels;
158 HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
159 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
161 if (!(This->baseTexture.usage & D3DUSAGE_AUTOGENMIPMAP)) {
162 TRACE("(%p) : returning invalid call\n", This);
163 return D3DERR_INVALIDCALL;
165 This->baseTexture.filterType = FilterType;
166 TRACE("(%p) : \n", This);
170 D3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface) {
171 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
172 FIXME("(%p) : stub\n", This);
173 if (!(This->baseTexture.usage & D3DUSAGE_AUTOGENMIPMAP)) {
176 return This->baseTexture.filterType;
177 return D3DTEXF_LINEAR; /* default */
180 void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface) {
181 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
182 /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
183 FIXME("(%p) : stub\n", This);
187 /* Internal function, No d3d mapping */
188 BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL dirty) {
190 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
191 old = This->baseTexture.dirty;
192 This->baseTexture.dirty = dirty;
196 BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface) {
197 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
198 return This->baseTexture.dirty;
201 HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface) {
202 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
204 UINT textureDimensions;
205 BOOL isNewTexture = FALSE;
206 TRACE("(%p) : About to bind texture\n", This);
208 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
210 #if 0 /* TODO: context manager support */
211 IWineD3DContextManager_PushState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
213 glEnable(textureDimensions);
216 /* Generate a texture name if we don't already have one */
217 if (This->baseTexture.textureName == 0) {
218 glGenTextures(1, &This->baseTexture.textureName);
219 checkGLcall("glGenTextures");
220 TRACE("Generated texture %d\n", This->baseTexture.textureName);
221 if (This->resource.pool == D3DPOOL_DEFAULT) {
222 /* Tell opengl to try and keep this texture in video ram (well mostly) */
225 glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
227 IWineD3DBaseTexture_SetDirty(iface, TRUE);
231 /* Bind the texture */
232 if (This->baseTexture.textureName != 0) {
233 /* Always need to reset the number of mipmap levels when rebinding as it is
234 a property of the active texture unit, and another texture may have set it
235 to a different value */
236 if (This->baseTexture.levels > 0) {
237 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
238 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
239 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
241 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
243 glBindTexture(textureDimensions, This->baseTexture.textureName);
244 checkGLcall("glBindTexture");
246 /* For a new texture we have to set the textures levels after binding the texture,
247 * in theory this is all we should ever have to dom, but because ATI's drivers are broken we
248 * also need to set the texture dimensins before the texture is is set */
249 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
250 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
251 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
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 = D3DERR_INVALIDCALL;
262 HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface) {
263 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
264 UINT textureDimensions;
266 TRACE("(%p) : About to bind texture\n", This);
267 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
271 glBindTexture(textureDimensions, 0);
272 #if 0 /* TODO: context manager support */
273 IWineD3DContextManager_PopState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
275 glDisable(textureDimensions);
282 UINT WINAPI IWineD3DBaseTextureImpl_GetTextureDimensions(IWineD3DBaseTexture *iface){
283 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
284 FIXME("(%p) : This shouldn't be called\n", This);
288 static const IWineD3DBaseTextureVtbl IWineD3DBaseTexture_Vtbl =
291 IWineD3DBaseTextureImpl_QueryInterface,
292 IWineD3DBaseTextureImpl_AddRef,
293 IWineD3DBaseTextureImpl_Release,
294 /* IWineD3DResource */
295 IWineD3DBaseTextureImpl_GetParent,
296 IWineD3DBaseTextureImpl_GetDevice,
297 IWineD3DBaseTextureImpl_SetPrivateData,
298 IWineD3DBaseTextureImpl_GetPrivateData,
299 IWineD3DBaseTextureImpl_FreePrivateData,
300 IWineD3DBaseTextureImpl_SetPriority,
301 IWineD3DBaseTextureImpl_GetPriority,
302 IWineD3DBaseTextureImpl_PreLoad,
303 IWineD3DBaseTextureImpl_GetType,
304 /*IWineD3DBaseTexture*/
305 IWineD3DBaseTextureImpl_SetLOD,
306 IWineD3DBaseTextureImpl_GetLOD,
307 IWineD3DBaseTextureImpl_GetLevelCount,
308 IWineD3DBaseTextureImpl_SetAutoGenFilterType,
309 IWineD3DBaseTextureImpl_GetAutoGenFilterType,
310 IWineD3DBaseTextureImpl_GenerateMipSubLevels,
311 IWineD3DBaseTextureImpl_SetDirty,
312 IWineD3DBaseTextureImpl_GetDirty,
314 IWineD3DBaseTextureImpl_BindTexture,
315 IWineD3DBaseTextureImpl_UnBindTexture,
316 IWineD3DBaseTextureImpl_GetTextureDimensions