- Merge from trunk up to r45543
[reactos.git] / dll / directx / wine / d3d9 / indexbuffer.c
1 /*
2 * IDirect3DIndexBuffer9 implementation
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Raphael Junqueira
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DIndexBuffer9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_QueryInterface(LPDIRECT3DINDEXBUFFER9 iface, REFIID riid, LPVOID* ppobj) {
29 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
30
31 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
32
33 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDirect3DResource9)
35 || IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)) {
36 IDirect3DIndexBuffer9_AddRef(iface);
37 *ppobj = This;
38 return S_OK;
39 }
40
41 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
42 *ppobj = NULL;
43 return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DIndexBuffer9Impl_AddRef(LPDIRECT3DINDEXBUFFER9 iface) {
47 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
48 ULONG ref = InterlockedIncrement(&This->ref);
49
50 TRACE("%p increasing refcount to %u.\n", iface, ref);
51
52 if (ref == 1)
53 {
54 IDirect3DDevice9Ex_AddRef(This->parentDevice);
55 wined3d_mutex_lock();
56 IWineD3DBuffer_AddRef(This->wineD3DIndexBuffer);
57 wined3d_mutex_unlock();
58 }
59
60 return ref;
61 }
62
63 static ULONG WINAPI IDirect3DIndexBuffer9Impl_Release(LPDIRECT3DINDEXBUFFER9 iface) {
64 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
65 ULONG ref = InterlockedDecrement(&This->ref);
66
67 TRACE("%p decreasing refcount to %u.\n", iface, ref);
68
69 if (ref == 0) {
70 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
71
72 wined3d_mutex_lock();
73 IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
74 wined3d_mutex_unlock();
75
76 /* Release the device last, as it may cause the device to be destroyed. */
77 IDirect3DDevice9Ex_Release(parentDevice);
78 }
79 return ref;
80 }
81
82 /* IDirect3DIndexBuffer9 IDirect3DResource9 Interface follow: */
83 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
84 {
85 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
86
87 TRACE("iface %p, device %p.\n", iface, device);
88
89 *device = (IDirect3DDevice9 *)This->parentDevice;
90 IDirect3DDevice9_AddRef(*device);
91
92 TRACE("Returning device %p.\n", *device);
93
94 return D3D_OK;
95 }
96
97 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
98 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
99 HRESULT hr;
100
101 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
102 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
103
104 wined3d_mutex_lock();
105 hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
106 wined3d_mutex_unlock();
107
108 return hr;
109 }
110
111 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
112 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
113 HRESULT hr;
114
115 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
116 iface, debugstr_guid(refguid), pData, pSizeOfData);
117
118 wined3d_mutex_lock();
119 hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
120 wined3d_mutex_unlock();
121
122 return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid) {
126 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
127 HRESULT hr;
128
129 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
130
131 wined3d_mutex_lock();
132 hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
133 wined3d_mutex_unlock();
134
135 return hr;
136 }
137
138 static DWORD WINAPI IDirect3DIndexBuffer9Impl_SetPriority(LPDIRECT3DINDEXBUFFER9 iface, DWORD PriorityNew) {
139 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
140 DWORD ret;
141
142 TRACE("iface %p, priority %u.\n", iface, PriorityNew);
143
144 wined3d_mutex_lock();
145 ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
146 wined3d_mutex_unlock();
147
148 return ret;
149 }
150
151 static DWORD WINAPI IDirect3DIndexBuffer9Impl_GetPriority(LPDIRECT3DINDEXBUFFER9 iface) {
152 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
153 DWORD ret;
154
155 TRACE("iface %p.\n", iface);
156
157 wined3d_mutex_lock();
158 ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
159 wined3d_mutex_unlock();
160
161 return ret;
162 }
163
164 static void WINAPI IDirect3DIndexBuffer9Impl_PreLoad(LPDIRECT3DINDEXBUFFER9 iface) {
165 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
166
167 TRACE("iface %p.\n", iface);
168
169 wined3d_mutex_lock();
170 IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
171 wined3d_mutex_unlock();
172 }
173
174 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer9Impl_GetType(IDirect3DIndexBuffer9 *iface)
175 {
176 TRACE("iface %p.\n", iface);
177
178 return D3DRTYPE_INDEXBUFFER;
179 }
180
181 /* IDirect3DIndexBuffer9 Interface follow: */
182 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Lock(LPDIRECT3DINDEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
183 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
184 HRESULT hr;
185
186 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
187 iface, OffsetToLock, SizeToLock, ppbData, Flags);
188
189 wined3d_mutex_lock();
190 hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
191 wined3d_mutex_unlock();
192
193 return hr;
194 }
195
196 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Unlock(LPDIRECT3DINDEXBUFFER9 iface) {
197 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
198 HRESULT hr;
199
200 TRACE("iface %p.\n", iface);
201
202 wined3d_mutex_lock();
203 hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
204 wined3d_mutex_unlock();
205
206 return hr;
207 }
208
209 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetDesc(LPDIRECT3DINDEXBUFFER9 iface, D3DINDEXBUFFER_DESC *pDesc) {
210 IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
211 HRESULT hr;
212 WINED3DBUFFER_DESC desc;
213
214 TRACE("iface %p, desc %p.\n", iface, pDesc);
215
216 wined3d_mutex_lock();
217 hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
218 wined3d_mutex_unlock();
219
220 if (SUCCEEDED(hr)) {
221 pDesc->Format = d3dformat_from_wined3dformat(This->format);
222 pDesc->Usage = desc.Usage;
223 pDesc->Pool = desc.Pool;
224 pDesc->Size = desc.Size;
225 pDesc->Type = D3DRTYPE_INDEXBUFFER;
226 }
227
228 return hr;
229 }
230
231
232 static const IDirect3DIndexBuffer9Vtbl Direct3DIndexBuffer9_Vtbl =
233 {
234 /* IUnknown */
235 IDirect3DIndexBuffer9Impl_QueryInterface,
236 IDirect3DIndexBuffer9Impl_AddRef,
237 IDirect3DIndexBuffer9Impl_Release,
238 /* IDirect3DResource9 */
239 IDirect3DIndexBuffer9Impl_GetDevice,
240 IDirect3DIndexBuffer9Impl_SetPrivateData,
241 IDirect3DIndexBuffer9Impl_GetPrivateData,
242 IDirect3DIndexBuffer9Impl_FreePrivateData,
243 IDirect3DIndexBuffer9Impl_SetPriority,
244 IDirect3DIndexBuffer9Impl_GetPriority,
245 IDirect3DIndexBuffer9Impl_PreLoad,
246 IDirect3DIndexBuffer9Impl_GetType,
247 /* IDirect3DIndexBuffer9 */
248 IDirect3DIndexBuffer9Impl_Lock,
249 IDirect3DIndexBuffer9Impl_Unlock,
250 IDirect3DIndexBuffer9Impl_GetDesc
251 };
252
253 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
254 {
255 HeapFree(GetProcessHeap(), 0, parent);
256 }
257
258 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
259 {
260 d3d9_indexbuffer_wined3d_object_destroyed,
261 };
262
263 HRESULT indexbuffer_init(IDirect3DIndexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
264 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
265 {
266 HRESULT hr;
267
268 buffer->lpVtbl = &Direct3DIndexBuffer9_Vtbl;
269 buffer->ref = 1;
270 buffer->format = wined3dformat_from_d3dformat(format);
271
272 wined3d_mutex_lock();
273 hr = IWineD3DDevice_CreateIndexBuffer(device->WineD3DDevice, size,
274 usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DIndexBuffer,
275 (IUnknown *)buffer, &d3d9_indexbuffer_wined3d_parent_ops);
276 wined3d_mutex_unlock();
277 if (FAILED(hr))
278 {
279 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
280 return hr;
281 }
282
283 buffer->parentDevice = (IDirect3DDevice9Ex *)device;
284 IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
285
286 return D3D_OK;
287 }