wined3d: Added support for ARB_POINT_PARAMETERS.
[wine] / dlls / wined3d / resource.c
1 /*
2  * IWineD3DResource Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
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.
13  *
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.
18  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29
30 /* IWineD3DResource IUnknown parts follow: */
31 HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, LPVOID *ppobj)
32 {
33     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
34     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
35     if (IsEqualGUID(riid, &IID_IUnknown)
36         || IsEqualGUID(riid, &IID_IWineD3DResource)) {
37         IUnknown_AddRef(iface);
38         *ppobj = This;
39         return D3D_OK;
40     }
41     return E_NOINTERFACE;
42 }
43
44 ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface) {
45     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
46     ULONG ref = InterlockedIncrement(&This->resource.ref);
47     TRACE("(%p) : AddRef increasing from %ld\n", This, ref - 1);
48     return ref; 
49 }
50
51 ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface) {
52     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
53     ULONG ref = InterlockedDecrement(&This->resource.ref);
54     TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
55     if (ref == 0) {
56         IWineD3DResourceImpl_CleanUp(iface);
57         HeapFree(GetProcessHeap(), 0, This);
58     }
59     return ref;
60 }
61
62 /* class static (not in vtable) */
63 void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface){
64     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
65     TRACE("(%p) Cleaning up resource\n", This);
66     if (This->resource.pool == D3DPOOL_DEFAULT) {
67         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
68         globalChangeGlRam(-This->resource.size);
69     }
70
71     HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
72     This->resource.allocatedMemory = 0;
73
74     if (This->resource.wineD3DDevice != NULL) {
75         IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
76     }/* NOTE: this is not really an error for systemmem resoruces */
77     return;
78 }
79
80 /* IWineD3DResource Interface follow: */
81 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
82     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
83     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
84     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
85     IWineD3DDevice_AddRef(*ppDevice);
86     return D3D_OK;
87 }
88
89 static PrivateData** IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
90                     REFGUID tag)
91 {
92     PrivateData** data;
93     for (data = &This->resource.privateData; *data != NULL; data = &(*data)->next)
94     {
95         if (IsEqualGUID(&(*data)->tag, tag)) break;
96     }
97     return data;
98 }
99
100 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
101     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
102     PrivateData **data;
103
104     TRACE("(%p) : %p %p %ld %ld\n", This, refguid, pData, SizeOfData, Flags);
105     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
106     if (*data == NULL)
107     {
108         *data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**data));
109         if (NULL == *data) return E_OUTOFMEMORY;
110
111         (*data)->tag = *refguid;
112         (*data)->flags = Flags;
113 #if 0
114         (*data)->uniquenessValue = This->uniquenessValue;
115 #endif
116         if (Flags & D3DSPD_IUNKNOWN) {
117             (*data)->ptr.object = (LPUNKNOWN)pData;
118             (*data)->size = sizeof(LPUNKNOWN);
119             IUnknown_AddRef((*data)->ptr.object);
120         }
121         else
122         {
123             (*data)->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
124             if (NULL == (*data)->ptr.data) {
125                 HeapFree(GetProcessHeap(), 0, *data);
126                 return E_OUTOFMEMORY;
127             }
128         }
129         /* link it in */
130         (*data)->next = This->resource.privateData;
131         This->resource.privateData = *data;
132         return D3D_OK;
133
134     } else {
135         /* I don't actually know how windows handles this case. The only
136             * reason I don't just call FreePrivateData is because I want to
137             * guarantee SetPrivateData working when using LPUNKNOWN or data
138             * that is no larger than the old data. */
139         return E_FAIL;
140
141     }
142
143     return D3D_OK;
144 }
145
146 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
147     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
148     PrivateData **data;
149
150     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
151     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
152     if (*data == NULL) return D3DERR_NOTFOUND;
153
154
155 #if 0 /* This may not be right. */
156     if (((*data)->flags & D3DSPD_VOLATILE)
157         && (*data)->uniquenessValue != This->uniquenessValue)
158         return DDERR_EXPIRED;
159 #endif
160     if (*pSizeOfData < (*data)->size) {
161         *pSizeOfData = (*data)->size;
162         return D3DERR_MOREDATA;
163     }
164
165     if ((*data)->flags & D3DSPD_IUNKNOWN) {
166         *(LPUNKNOWN *)pData = (*data)->ptr.object;
167         IUnknown_AddRef((*data)->ptr.object);
168     }
169     else {
170         memcpy(pData, (*data)->ptr.data, (*data)->size);
171     }
172
173     return D3D_OK;
174 }
175 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
176     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
177     PrivateData **data;
178
179     TRACE("(%p) : %p\n", This, refguid);
180     /* TODO: move this code off into a linked list class */
181     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
182     if (*data == NULL) return D3DERR_NOTFOUND;
183
184     *data = (*data)->next;
185
186     if ((*data)->flags & D3DSPD_IUNKNOWN)
187     {
188         if ((*data)->ptr.object != NULL)
189             IUnknown_Release((*data)->ptr.object);
190     } else {
191         HeapFree(GetProcessHeap(), 0, (*data)->ptr.data);
192     }
193
194     HeapFree(GetProcessHeap(), 0, *data);
195
196     return D3D_OK;
197 }
198
199 /* Priority support is not implemented yet */
200 DWORD    WINAPI        IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
201     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
202     FIXME("(%p) : stub\n", This);
203     return 0;
204 }
205 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
206     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
207     FIXME("(%p) : stub\n", This);
208     return 0;
209 }
210
211 /* Preloading of resources is not supported yet */
212 void     WINAPI        IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
213     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
214     FIXME("(%p) : stub\n", This);
215 }
216
217 D3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
218     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
219     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
220     return This->resource.resourceType;
221 }
222
223 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
224     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
225     IUnknown_AddRef(This->resource.parent);
226     *pParent = This->resource.parent;
227     return D3D_OK;
228 }
229
230
231 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
232 {
233     /* IUnknown */
234     IWineD3DResourceImpl_QueryInterface,
235     IWineD3DResourceImpl_AddRef,
236     IWineD3DResourceImpl_Release,
237     /* IWineD3DResource */
238     IWineD3DResourceImpl_GetParent,
239     IWineD3DResourceImpl_GetDevice,
240     IWineD3DResourceImpl_SetPrivateData,
241     IWineD3DResourceImpl_GetPrivateData,
242     IWineD3DResourceImpl_FreePrivateData,
243     IWineD3DResourceImpl_SetPriority,
244     IWineD3DResourceImpl_GetPriority,
245     IWineD3DResourceImpl_PreLoad,
246     IWineD3DResourceImpl_GetType
247 };