2 * IDirect3DTexture9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 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 IWineD3DTexture IUnknown parts follow
31 ******************************************* */
32 HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
34 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
35 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36 if (IsEqualGUID(riid, &IID_IUnknown)
37 || IsEqualGUID(riid, &IID_IWineD3DResource)
38 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
39 || IsEqualGUID(riid, &IID_IWineD3DTexture)){
40 IUnknown_AddRef(iface);
47 ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
48 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
49 TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
50 IUnknown_AddRef(This->resource.parent);
51 return InterlockedIncrement(&This->resource.ref);
54 ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
55 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
57 TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
58 ref = InterlockedDecrement(&This->resource.ref);
62 for (i = 0; i < This->baseTexture.levels; i++) {
63 if (This->surfaces[i] != NULL) {
64 /* Because the surfaces were created using a callback we need to release there parent otehrwise we leave the parent hanging */
65 IUnknown* surfaceParent;
66 IWineD3DSurface_GetParent(This->surfaces[i], &surfaceParent);
67 IUnknown_Release(surfaceParent);
68 IUnknown_Release(surfaceParent);
71 if (This->baseTexture.textureName != 0) {
73 TRACE("Deleting texture %d\n", This->baseTexture.textureName);
74 glDeleteTextures(1, &This->baseTexture.textureName);
77 IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
78 HeapFree(GetProcessHeap(), 0, This);
80 IUnknown_Release(This->resource.parent); /* Released the reference to the d3dx object */
85 /* ****************************************************
86 IWineD3DTexture IWineD3DResource parts follow
87 **************************************************** */
88 HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
89 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
92 HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
93 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
96 HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
97 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
100 HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
101 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
104 DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
105 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
108 DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
109 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
112 void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
113 /* Override the IWineD3DResource Preload method */
115 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
117 TRACE("(%p) : About to load texture\n", This);
121 #if 0 /* TODO: context manager support */
122 IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_2D, ENABLED, NOW /* make sure the state is applied now */);
125 /* Generate a texture name if we don't already have one */
126 if (This->baseTexture.textureName == 0) {
127 glGenTextures(1, &This->baseTexture.textureName);
128 checkGLcall("glGenTextures");
129 TRACE("Generated texture %d\n", This->baseTexture.textureName);
130 if (This->baseTexture.pool == D3DPOOL_DEFAULT) {
131 /* Tell opengl to try and keep this texture in video ram (well mostly) */
134 glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
138 /* Bind the texture */
139 if (This->baseTexture.textureName != 0) {
140 glBindTexture(GL_TEXTURE_2D, This->baseTexture.textureName);
141 checkGLcall("glBindTexture");
142 } else { /* this only happened if we've run out of openGL textures */
143 WARN("This texture doesn't have an openGL texture assigned to it\n");
147 /* If were dirty then reload the surfaces */
148 if(This->baseTexture.dirty != FALSE) {
149 for (i = 0; i < This->baseTexture.levels; i++) {
150 IWineD3DSurface_LoadTexture(This->surfaces[i], GL_TEXTURE_2D, i);
153 /* No longer dirty */
154 This->baseTexture.dirty = FALSE;
157 /* Always need to reset the number of mipmap levels when rebinding as it is
158 a property of the active texture unit, and another texture may have set it
159 to a different value */
160 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
162 checkGLcall("glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->levels)");
163 /* TODO: disable texture support, if it wastn't enabled when we entered. */
164 #if 0 /* TODO: context manager support */
165 IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_2D, DISABLED,DELAYED
166 /* we don't care when the state is disabled(if atall) */);
174 D3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
175 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
178 HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
179 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
182 /* ******************************************************
183 IWineD3DTexture IWineD3DBaseTexture parts follow
184 ****************************************************** */
185 DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
186 return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
189 DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
190 return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
193 DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
194 return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
197 HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
198 return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
201 D3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
202 return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
205 void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
206 return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
209 /* Internal function, No d3d mapping */
210 BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
211 return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
214 BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
215 return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
218 HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
219 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
220 TRACE("(%p) : %d \n", This, This->baseTexture.textureName);
221 #if 0 /* TODO: context manager support */
222 IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_2D, ENABLED, NOW /* make sure the state is applied now */);
225 IWineD3DTexture_PreLoad(iface); /* make sure the textures is preloaded */
226 #if 1 /* TODO: context manager support */
227 glEnable(GL_TEXTURE_2D); /* all this enable disable stuff is a bit of a mess */
229 glBindTexture(GL_TEXTURE_2D, This->baseTexture.textureName);
234 HRESULT WINAPI IWineD3DTextureImpl_UnBindTexture(IWineD3DTexture *iface) {
235 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
236 TRACE("(%p) \n", This);
237 #if 0 /* TODO: context manager support */
238 IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_2D, DISABLED, DELAYED);
241 #if 1 /* TODO: context manager support */
242 glBindTexture(GL_TEXTURE_2D, 0);
244 glDisable(GL_TEXTURE_2D); /* This should be queued! so that we don't keep enabling and disabling states. */
249 UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
250 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
251 TRACE("(%p) \n", This);
253 return GL_TEXTURE_2D;
257 /* *******************************************
258 IWineD3DTexture IWineD3DTexture parts follow
259 ******************************************* */
260 HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
261 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
263 if (Level < This->baseTexture.levels) {
264 TRACE("(%p) Level (%d)\n", This, Level);
265 return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[Level], pDesc);
267 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
268 return D3DERR_INVALIDCALL;
271 HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
272 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
273 *ppSurfaceLevel = (IWineD3DSurface *) This->surfaces[Level];
274 IWineD3DSurface_AddRef((IWineD3DSurface *) This->surfaces[Level]);
275 TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
279 HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, D3DLOCKED_RECT *pLockedRect,
280 CONST RECT *pRect, DWORD Flags) {
282 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
284 if (Level < This->baseTexture.levels) {
286 hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[Level], pLockedRect, pRect, Flags);
287 TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
289 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
290 return D3DERR_INVALIDCALL;
295 HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
297 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
299 if (Level < This->baseTexture.levels) {
300 hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[Level]);
301 TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
303 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
304 return D3DERR_INVALIDCALL;
309 HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
310 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
311 This->baseTexture.dirty = TRUE;
312 TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
313 return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *)This->surfaces[0], pDirtyRect);
316 IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
319 IWineD3DTextureImpl_QueryInterface,
320 IWineD3DTextureImpl_AddRef,
321 IWineD3DTextureImpl_Release,
322 /* IWineD3DResource */
323 IWineD3DTextureImpl_GetParent,
324 IWineD3DTextureImpl_GetDevice,
325 IWineD3DTextureImpl_SetPrivateData,
326 IWineD3DTextureImpl_GetPrivateData,
327 IWineD3DTextureImpl_FreePrivateData,
328 IWineD3DTextureImpl_SetPriority,
329 IWineD3DTextureImpl_GetPriority,
330 IWineD3DTextureImpl_PreLoad,
331 IWineD3DTextureImpl_GetType,
332 /* IWineD3DBaseTexture */
333 IWineD3DTextureImpl_SetLOD,
334 IWineD3DTextureImpl_GetLOD,
335 IWineD3DTextureImpl_GetLevelCount,
336 IWineD3DTextureImpl_SetAutoGenFilterType,
337 IWineD3DTextureImpl_GetAutoGenFilterType,
338 IWineD3DTextureImpl_GenerateMipSubLevels,
339 IWineD3DTextureImpl_SetDirty,
340 IWineD3DTextureImpl_GetDirty,
341 IWineD3DTextureImpl_BindTexture,
342 IWineD3DTextureImpl_UnBindTexture,
343 IWineD3DTextureImpl_GetTextureDimensions,
344 /* IWineD3DTexture */
345 IWineD3DTextureImpl_GetLevelDesc,
346 IWineD3DTextureImpl_GetSurfaceLevel,
347 IWineD3DTextureImpl_LockRect,
348 IWineD3DTextureImpl_UnlockRect,
349 IWineD3DTextureImpl_AddDirtyRect