2 * Copyright 2002-2004 Jason Edmeades
3 * Copyright 2002-2004 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d9_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26 static inline IDirect3DVertexBuffer9Impl *impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
28 return CONTAINING_RECORD(iface, IDirect3DVertexBuffer9Impl, IDirect3DVertexBuffer9_iface);
31 static HRESULT WINAPI d3d9_vertexbuffer_QueryInterface(IDirect3DVertexBuffer9 *iface, REFIID riid, void **object)
33 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
35 if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)
36 || IsEqualGUID(riid, &IID_IDirect3DResource9)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DVertexBuffer9_AddRef(iface);
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
50 static ULONG WINAPI d3d9_vertexbuffer_AddRef(IDirect3DVertexBuffer9 *iface)
52 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
53 ULONG refcount = InterlockedIncrement(&buffer->ref);
55 TRACE("%p increasing refcount to %u.\n", iface, refcount);
59 IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
61 wined3d_buffer_incref(buffer->wineD3DVertexBuffer);
62 wined3d_mutex_unlock();
68 static ULONG WINAPI d3d9_vertexbuffer_Release(IDirect3DVertexBuffer9 *iface)
70 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
71 ULONG refcount = InterlockedDecrement(&buffer->ref);
73 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
77 IDirect3DDevice9Ex *device = buffer->parentDevice;
80 wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
81 wined3d_mutex_unlock();
83 /* Release the device last, as it may cause the device to be destroyed. */
84 IDirect3DDevice9Ex_Release(device);
90 static HRESULT WINAPI d3d9_vertexbuffer_GetDevice(IDirect3DVertexBuffer9 *iface,
91 IDirect3DDevice9 **device)
93 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
95 TRACE("iface %p, device %p.\n", iface, device);
97 *device = (IDirect3DDevice9 *)buffer->parentDevice;
98 IDirect3DDevice9_AddRef(*device);
100 TRACE("Returning device %p.\n", *device);
105 static HRESULT WINAPI d3d9_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer9 *iface,
106 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
108 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
109 struct wined3d_resource *resource;
112 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
113 iface, debugstr_guid(guid), data, data_size, flags);
115 wined3d_mutex_lock();
116 resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
117 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
118 wined3d_mutex_unlock();
123 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
124 REFGUID guid, void *data, DWORD *data_size)
126 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
127 struct wined3d_resource *resource;
130 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
131 iface, debugstr_guid(guid), data, data_size);
133 wined3d_mutex_lock();
134 resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
135 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
136 wined3d_mutex_unlock();
141 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
143 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
144 struct wined3d_resource *resource;
147 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
149 wined3d_mutex_lock();
150 resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
151 hr = wined3d_resource_free_private_data(resource, guid);
152 wined3d_mutex_unlock();
157 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
159 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
162 TRACE("iface %p, priority %u.\n", iface, priority);
164 wined3d_mutex_lock();
165 previous = wined3d_buffer_set_priority(buffer->wineD3DVertexBuffer, priority);
166 wined3d_mutex_unlock();
171 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
173 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
176 TRACE("iface %p.\n", iface);
178 wined3d_mutex_lock();
179 priority = wined3d_buffer_get_priority(buffer->wineD3DVertexBuffer);
180 wined3d_mutex_unlock();
185 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
187 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
189 TRACE("iface %p.\n", iface);
191 wined3d_mutex_lock();
192 wined3d_buffer_preload(buffer->wineD3DVertexBuffer);
193 wined3d_mutex_unlock();
196 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
198 TRACE("iface %p.\n", iface);
200 return D3DRTYPE_VERTEXBUFFER;
203 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
204 void **data, DWORD flags)
206 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
209 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
210 iface, offset, size, data, flags);
212 wined3d_mutex_lock();
213 hr = wined3d_buffer_map(buffer->wineD3DVertexBuffer, offset, size, (BYTE **)data, flags);
214 wined3d_mutex_unlock();
219 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
221 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
223 TRACE("iface %p.\n", iface);
225 wined3d_mutex_lock();
226 wined3d_buffer_unmap(buffer->wineD3DVertexBuffer);
227 wined3d_mutex_unlock();
232 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
233 D3DVERTEXBUFFER_DESC *desc)
235 IDirect3DVertexBuffer9Impl *buffer = impl_from_IDirect3DVertexBuffer9(iface);
236 struct wined3d_resource_desc wined3d_desc;
237 struct wined3d_resource *wined3d_resource;
239 TRACE("iface %p, desc %p.\n", iface, desc);
241 wined3d_mutex_lock();
242 wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
243 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
244 wined3d_mutex_unlock();
246 desc->Format = D3DFMT_VERTEXDATA;
247 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
248 desc->Pool = wined3d_desc.pool;
249 desc->Size = wined3d_desc.size;
250 desc->Type = D3DRTYPE_VERTEXBUFFER;
251 desc->FVF = buffer->fvf;
256 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
259 d3d9_vertexbuffer_QueryInterface,
260 d3d9_vertexbuffer_AddRef,
261 d3d9_vertexbuffer_Release,
262 /* IDirect3DResource9 */
263 d3d9_vertexbuffer_GetDevice,
264 d3d9_vertexbuffer_SetPrivateData,
265 d3d9_vertexbuffer_GetPrivateData,
266 d3d9_vertexbuffer_FreePrivateData,
267 d3d9_vertexbuffer_SetPriority,
268 d3d9_vertexbuffer_GetPriority,
269 d3d9_vertexbuffer_PreLoad,
270 d3d9_vertexbuffer_GetType,
271 /* IDirect3DVertexBuffer9 */
272 d3d9_vertexbuffer_Lock,
273 d3d9_vertexbuffer_Unlock,
274 d3d9_vertexbuffer_GetDesc,
277 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
279 HeapFree(GetProcessHeap(), 0, parent);
282 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
284 d3d9_vertexbuffer_wined3d_object_destroyed,
287 HRESULT vertexbuffer_init(IDirect3DVertexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
288 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
292 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
296 wined3d_mutex_lock();
297 hr = wined3d_buffer_create_vb(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
298 (enum wined3d_pool)pool, buffer, &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->wineD3DVertexBuffer);
299 wined3d_mutex_unlock();
302 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
306 buffer->parentDevice = &device->IDirect3DDevice9Ex_iface;
307 IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
312 IDirect3DVertexBuffer9Impl *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
316 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
318 return impl_from_IDirect3DVertexBuffer9(iface);
321 static inline IDirect3DIndexBuffer9Impl *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
323 return CONTAINING_RECORD(iface, IDirect3DIndexBuffer9Impl, IDirect3DIndexBuffer9_iface);
326 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **object)
328 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
330 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
331 || IsEqualGUID(riid, &IID_IDirect3DResource9)
332 || IsEqualGUID(riid, &IID_IUnknown))
334 IDirect3DIndexBuffer9_AddRef(iface);
339 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
342 return E_NOINTERFACE;
345 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
347 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
348 ULONG refcount = InterlockedIncrement(&buffer->ref);
350 TRACE("%p increasing refcount to %u.\n", iface, refcount);
354 IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
355 wined3d_mutex_lock();
356 wined3d_buffer_incref(buffer->wineD3DIndexBuffer);
357 wined3d_mutex_unlock();
363 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
365 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
366 ULONG refcount = InterlockedDecrement(&buffer->ref);
368 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
372 IDirect3DDevice9Ex *device = buffer->parentDevice;
374 wined3d_mutex_lock();
375 wined3d_buffer_decref(buffer->wineD3DIndexBuffer);
376 wined3d_mutex_unlock();
378 /* Release the device last, as it may cause the device to be destroyed. */
379 IDirect3DDevice9Ex_Release(device);
385 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface,
386 IDirect3DDevice9 **device)
388 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
390 TRACE("iface %p, device %p.\n", iface, device);
392 *device = (IDirect3DDevice9 *)buffer->parentDevice;
393 IDirect3DDevice9_AddRef(*device);
395 TRACE("Returning device %p.\n", *device);
400 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
401 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
403 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
404 struct wined3d_resource *resource;
407 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
408 iface, debugstr_guid(guid), data, data_size, flags);
410 wined3d_mutex_lock();
411 resource = wined3d_buffer_get_resource(buffer->wineD3DIndexBuffer);
412 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
413 wined3d_mutex_unlock();
418 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
419 REFGUID guid, void *data, DWORD *data_size)
421 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
422 struct wined3d_resource *resource;
425 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
426 iface, debugstr_guid(guid), data, data_size);
428 wined3d_mutex_lock();
429 resource = wined3d_buffer_get_resource(buffer->wineD3DIndexBuffer);
430 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
431 wined3d_mutex_unlock();
436 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
438 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
439 struct wined3d_resource *resource;
442 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
444 wined3d_mutex_lock();
445 resource = wined3d_buffer_get_resource(buffer->wineD3DIndexBuffer);
446 hr = wined3d_resource_free_private_data(resource, guid);
447 wined3d_mutex_unlock();
452 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
454 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
457 TRACE("iface %p, priority %u.\n", iface, priority);
459 wined3d_mutex_lock();
460 previous = wined3d_buffer_set_priority(buffer->wineD3DIndexBuffer, priority);
461 wined3d_mutex_unlock();
466 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
468 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
471 TRACE("iface %p.\n", iface);
473 wined3d_mutex_lock();
474 priority = wined3d_buffer_get_priority(buffer->wineD3DIndexBuffer);
475 wined3d_mutex_unlock();
480 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
482 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
484 TRACE("iface %p.\n", iface);
486 wined3d_mutex_lock();
487 wined3d_buffer_preload(buffer->wineD3DIndexBuffer);
488 wined3d_mutex_unlock();
491 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
493 TRACE("iface %p.\n", iface);
495 return D3DRTYPE_INDEXBUFFER;
498 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
499 UINT offset, UINT size, void **data, DWORD flags)
501 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
504 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
505 iface, offset, size, data, flags);
507 wined3d_mutex_lock();
508 hr = wined3d_buffer_map(buffer->wineD3DIndexBuffer, offset, size, (BYTE **)data, flags);
509 wined3d_mutex_unlock();
514 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
516 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
518 TRACE("iface %p.\n", iface);
520 wined3d_mutex_lock();
521 wined3d_buffer_unmap(buffer->wineD3DIndexBuffer);
522 wined3d_mutex_unlock();
527 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface,
528 D3DINDEXBUFFER_DESC *desc)
530 IDirect3DIndexBuffer9Impl *buffer = impl_from_IDirect3DIndexBuffer9(iface);
531 struct wined3d_resource_desc wined3d_desc;
532 struct wined3d_resource *wined3d_resource;
534 TRACE("iface %p, desc %p.\n", iface, desc);
536 wined3d_mutex_lock();
537 wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DIndexBuffer);
538 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
539 wined3d_mutex_unlock();
541 desc->Format = d3dformat_from_wined3dformat(buffer->format);
542 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
543 desc->Pool = wined3d_desc.pool;
544 desc->Size = wined3d_desc.size;
545 desc->Type = D3DRTYPE_INDEXBUFFER;
550 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
553 d3d9_indexbuffer_QueryInterface,
554 d3d9_indexbuffer_AddRef,
555 d3d9_indexbuffer_Release,
556 /* IDirect3DResource9 */
557 d3d9_indexbuffer_GetDevice,
558 d3d9_indexbuffer_SetPrivateData,
559 d3d9_indexbuffer_GetPrivateData,
560 d3d9_indexbuffer_FreePrivateData,
561 d3d9_indexbuffer_SetPriority,
562 d3d9_indexbuffer_GetPriority,
563 d3d9_indexbuffer_PreLoad,
564 d3d9_indexbuffer_GetType,
565 /* IDirect3DIndexBuffer9 */
566 d3d9_indexbuffer_Lock,
567 d3d9_indexbuffer_Unlock,
568 d3d9_indexbuffer_GetDesc,
571 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
573 HeapFree(GetProcessHeap(), 0, parent);
576 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
578 d3d9_indexbuffer_wined3d_object_destroyed,
581 HRESULT indexbuffer_init(IDirect3DIndexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
582 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
586 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
588 buffer->format = wined3dformat_from_d3dformat(format);
590 wined3d_mutex_lock();
591 hr = wined3d_buffer_create_ib(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
592 (enum wined3d_pool)pool, buffer, &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wineD3DIndexBuffer);
593 wined3d_mutex_unlock();
596 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
600 buffer->parentDevice = &device->IDirect3DDevice9Ex_iface;
601 IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
606 IDirect3DIndexBuffer9Impl *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
610 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
612 return impl_from_IDirect3DIndexBuffer9(iface);