501db41439b1647ea6d29f222fe0de0097339990
[reactos.git] / reactos / dll / directx / wine / d3d9 / vertexbuffer.c
1 /*
2 * IDirect3DVertexBuffer9 implementation
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 *
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.
12 *
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.
17 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DVertexBuffer9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_QueryInterface(LPDIRECT3DVERTEXBUFFER9 iface, REFIID riid, LPVOID* ppobj) {
30 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
31
32 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3DResource9)
34 || IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)) {
35 IDirect3DVertexBuffer9_AddRef(iface);
36 *ppobj = This;
37 return S_OK;
38 }
39
40 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41 *ppobj = NULL;
42 return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DVertexBuffer9Impl_AddRef(LPDIRECT3DVERTEXBUFFER9 iface) {
46 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
47 ULONG ref = InterlockedIncrement(&This->ref);
48
49 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50
51 return ref;
52 }
53
54 static ULONG WINAPI IDirect3DVertexBuffer9Impl_Release(LPDIRECT3DVERTEXBUFFER9 iface) {
55 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
56 ULONG ref = InterlockedDecrement(&This->ref);
57
58 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59
60 if (ref == 0) {
61 EnterCriticalSection(&d3d9_cs);
62 IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
63 LeaveCriticalSection(&d3d9_cs);
64 IDirect3DDevice9Ex_Release(This->parentDevice);
65 HeapFree(GetProcessHeap(), 0, This);
66 }
67 return ref;
68 }
69
70 /* IDirect3DVertexBuffer9 IDirect3DResource9 Interface follow: */
71 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDevice(LPDIRECT3DVERTEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
72 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
73 IWineD3DDevice *wined3d_device;
74 HRESULT hr;
75 TRACE("(%p) Relay\n", This);
76
77 EnterCriticalSection(&d3d9_cs);
78 hr = IWineD3DBuffer_GetDevice(This->wineD3DVertexBuffer, &wined3d_device);
79 if (SUCCEEDED(hr))
80 {
81 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
82 IWineD3DDevice_Release(wined3d_device);
83 }
84 LeaveCriticalSection(&d3d9_cs);
85 return hr;
86 }
87
88 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
89 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
90 HRESULT hr;
91
92 TRACE("(%p) Relay\n", This);
93
94 EnterCriticalSection(&d3d9_cs);
95 hr = IWineD3DBuffer_SetPrivateData(This->wineD3DVertexBuffer, refguid, pData, SizeOfData, Flags);
96 LeaveCriticalSection(&d3d9_cs);
97
98 return hr;
99 }
100
101 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
102 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
103 HRESULT hr;
104 TRACE("(%p) Relay\n", This);
105
106 EnterCriticalSection(&d3d9_cs);
107 hr = IWineD3DBuffer_GetPrivateData(This->wineD3DVertexBuffer, refguid, pData, pSizeOfData);
108 LeaveCriticalSection(&d3d9_cs);
109 return hr;
110 }
111
112 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid) {
113 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
114 HRESULT hr;
115 TRACE("(%p) Relay\n", This);
116
117 EnterCriticalSection(&d3d9_cs);
118 hr = IWineD3DBuffer_FreePrivateData(This->wineD3DVertexBuffer, refguid);
119 LeaveCriticalSection(&d3d9_cs);
120 return hr;
121 }
122
123 static DWORD WINAPI IDirect3DVertexBuffer9Impl_SetPriority(LPDIRECT3DVERTEXBUFFER9 iface, DWORD PriorityNew) {
124 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
125 HRESULT hr;
126 TRACE("(%p) Relay\n", This);
127
128 EnterCriticalSection(&d3d9_cs);
129 hr = IWineD3DBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
130 LeaveCriticalSection(&d3d9_cs);
131 return hr;
132 }
133
134 static DWORD WINAPI IDirect3DVertexBuffer9Impl_GetPriority(LPDIRECT3DVERTEXBUFFER9 iface) {
135 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
136 HRESULT hr;
137 TRACE("(%p) Relay\n", This);
138
139 EnterCriticalSection(&d3d9_cs);
140 hr = IWineD3DBuffer_GetPriority(This->wineD3DVertexBuffer);
141 LeaveCriticalSection(&d3d9_cs);
142 return hr;
143 }
144
145 static void WINAPI IDirect3DVertexBuffer9Impl_PreLoad(LPDIRECT3DVERTEXBUFFER9 iface) {
146 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
147 TRACE("(%p) Relay\n", This);
148
149 EnterCriticalSection(&d3d9_cs);
150 IWineD3DBuffer_PreLoad(This->wineD3DVertexBuffer);
151 LeaveCriticalSection(&d3d9_cs);
152 return ;
153 }
154
155 static D3DRESOURCETYPE WINAPI IDirect3DVertexBuffer9Impl_GetType(LPDIRECT3DVERTEXBUFFER9 iface) {
156 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
157 TRACE("(%p)\n", This);
158
159 return D3DRTYPE_VERTEXBUFFER;
160 }
161
162 /* IDirect3DVertexBuffer9 Interface follow: */
163 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Lock(LPDIRECT3DVERTEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
164 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
165 HRESULT hr;
166 TRACE("(%p) Relay\n", This);
167
168 EnterCriticalSection(&d3d9_cs);
169 hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
170 LeaveCriticalSection(&d3d9_cs);
171 return hr;
172 }
173
174 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Unlock(LPDIRECT3DVERTEXBUFFER9 iface) {
175 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
176 HRESULT hr;
177 TRACE("(%p) Relay\n", This);
178
179 EnterCriticalSection(&d3d9_cs);
180 hr = IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
181 LeaveCriticalSection(&d3d9_cs);
182 return hr;
183 }
184
185 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDesc(LPDIRECT3DVERTEXBUFFER9 iface, D3DVERTEXBUFFER_DESC* pDesc) {
186 IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
187 HRESULT hr;
188 WINED3DBUFFER_DESC desc;
189 TRACE("(%p) Relay\n", This);
190
191 EnterCriticalSection(&d3d9_cs);
192 hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &desc);
193 LeaveCriticalSection(&d3d9_cs);
194
195 if (SUCCEEDED(hr)) {
196 pDesc->Format = D3DFMT_VERTEXDATA;
197 pDesc->Usage = desc.Usage;
198 pDesc->Pool = desc.Pool;
199 pDesc->Size = desc.Size;
200 pDesc->Type = D3DRTYPE_VERTEXBUFFER;
201 pDesc->FVF = This->fvf;
202 }
203
204
205 return hr;
206 }
207
208 static const IDirect3DVertexBuffer9Vtbl Direct3DVertexBuffer9_Vtbl =
209 {
210 /* IUnknown */
211 IDirect3DVertexBuffer9Impl_QueryInterface,
212 IDirect3DVertexBuffer9Impl_AddRef,
213 IDirect3DVertexBuffer9Impl_Release,
214 /* IDirect3DResource9 */
215 IDirect3DVertexBuffer9Impl_GetDevice,
216 IDirect3DVertexBuffer9Impl_SetPrivateData,
217 IDirect3DVertexBuffer9Impl_GetPrivateData,
218 IDirect3DVertexBuffer9Impl_FreePrivateData,
219 IDirect3DVertexBuffer9Impl_SetPriority,
220 IDirect3DVertexBuffer9Impl_GetPriority,
221 IDirect3DVertexBuffer9Impl_PreLoad,
222 IDirect3DVertexBuffer9Impl_GetType,
223 /* IDirect3DVertexBuffer9 */
224 IDirect3DVertexBuffer9Impl_Lock,
225 IDirect3DVertexBuffer9Impl_Unlock,
226 IDirect3DVertexBuffer9Impl_GetDesc
227 };
228
229
230 /* IDirect3DDevice9 IDirect3DVertexBuffer9 Methods follow: */
231 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(LPDIRECT3DDEVICE9EX iface,
232 UINT Size, DWORD Usage, DWORD FVF, D3DPOOL Pool,
233 IDirect3DVertexBuffer9** ppVertexBuffer, HANDLE* pSharedHandle) {
234
235 IDirect3DVertexBuffer9Impl *object;
236 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
237 HRESULT hrc = D3D_OK;
238
239 /* Allocate the storage for the device */
240 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexBuffer9Impl));
241 if (NULL == object) {
242 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
243 return D3DERR_OUTOFVIDEOMEMORY;
244 }
245
246 object->lpVtbl = &Direct3DVertexBuffer9_Vtbl;
247 object->ref = 1;
248 object->fvf = FVF;
249 EnterCriticalSection(&d3d9_cs);
250 hrc = IWineD3DDevice_CreateVertexBuffer(This->WineD3DDevice, Size, Usage & WINED3DUSAGE_MASK,
251 0 /* fvf for ddraw only */, (WINED3DPOOL) Pool, &(object->wineD3DVertexBuffer), (IUnknown *)object);
252 LeaveCriticalSection(&d3d9_cs);
253
254 if (hrc != D3D_OK) {
255
256 /* free up object */
257 WARN("(%p) call to IWineD3DDevice_CreateVertexBuffer failed\n", This);
258 HeapFree(GetProcessHeap(), 0, object);
259 } else {
260 IDirect3DDevice9Ex_AddRef(iface);
261 object->parentDevice = iface;
262 TRACE("(%p) : Created vertex buffer %p\n", This, object);
263 *ppVertexBuffer = (LPDIRECT3DVERTEXBUFFER9) object;
264 }
265 return hrc;
266 }