Release 1.6-rc2.
[wine/testsucceed.git] / dlls / d3d9 / buffer.c
blobd543b2709b608a1a42b3312e3587fd23a9fd0f34
1 /*
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
21 #include "config.h"
22 #include "d3d9_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26 static inline struct d3d9_vertexbuffer *impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
28 return CONTAINING_RECORD(iface, struct d3d9_vertexbuffer, IDirect3DVertexBuffer9_iface);
31 static HRESULT WINAPI d3d9_vertexbuffer_QueryInterface(IDirect3DVertexBuffer9 *iface, REFIID riid, void **out)
33 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
35 if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)
36 || IsEqualGUID(riid, &IID_IDirect3DResource9)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DVertexBuffer9_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d9_vertexbuffer_AddRef(IDirect3DVertexBuffer9 *iface)
52 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
53 ULONG refcount = InterlockedIncrement(&buffer->refcount);
55 TRACE("%p increasing refcount to %u.\n", iface, refcount);
57 if (refcount == 1)
59 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
60 wined3d_mutex_lock();
61 wined3d_buffer_incref(buffer->wined3d_buffer);
62 wined3d_mutex_unlock();
65 return refcount;
68 static ULONG WINAPI d3d9_vertexbuffer_Release(IDirect3DVertexBuffer9 *iface)
70 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
71 ULONG refcount = InterlockedDecrement(&buffer->refcount);
73 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
75 if (!refcount)
77 IDirect3DDevice9Ex *device = buffer->parent_device;
79 wined3d_mutex_lock();
80 wined3d_buffer_decref(buffer->wined3d_buffer);
81 wined3d_mutex_unlock();
83 /* Release the device last, as it may cause the device to be destroyed. */
84 IDirect3DDevice9Ex_Release(device);
87 return refcount;
90 static HRESULT WINAPI d3d9_vertexbuffer_GetDevice(IDirect3DVertexBuffer9 *iface, IDirect3DDevice9 **device)
92 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
94 TRACE("iface %p, device %p.\n", iface, device);
96 *device = (IDirect3DDevice9 *)buffer->parent_device;
97 IDirect3DDevice9_AddRef(*device);
99 TRACE("Returning device %p.\n", *device);
101 return D3D_OK;
104 static HRESULT WINAPI d3d9_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer9 *iface,
105 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
107 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
108 struct wined3d_resource *resource;
109 HRESULT hr;
111 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
112 iface, debugstr_guid(guid), data, data_size, flags);
114 wined3d_mutex_lock();
115 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
116 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
117 wined3d_mutex_unlock();
119 return hr;
122 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
123 REFGUID guid, void *data, DWORD *data_size)
125 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
126 struct wined3d_resource *resource;
127 HRESULT hr;
129 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
130 iface, debugstr_guid(guid), data, data_size);
132 wined3d_mutex_lock();
133 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
134 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
135 wined3d_mutex_unlock();
137 return hr;
140 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
142 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
143 struct wined3d_resource *resource;
144 HRESULT hr;
146 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
148 wined3d_mutex_lock();
149 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
150 hr = wined3d_resource_free_private_data(resource, guid);
151 wined3d_mutex_unlock();
153 return hr;
156 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
158 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
159 DWORD previous;
161 TRACE("iface %p, priority %u.\n", iface, priority);
163 wined3d_mutex_lock();
164 previous = wined3d_buffer_set_priority(buffer->wined3d_buffer, priority);
165 wined3d_mutex_unlock();
167 return previous;
170 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
172 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
173 DWORD priority;
175 TRACE("iface %p.\n", iface);
177 wined3d_mutex_lock();
178 priority = wined3d_buffer_get_priority(buffer->wined3d_buffer);
179 wined3d_mutex_unlock();
181 return priority;
184 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
186 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
188 TRACE("iface %p.\n", iface);
190 wined3d_mutex_lock();
191 wined3d_buffer_preload(buffer->wined3d_buffer);
192 wined3d_mutex_unlock();
195 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
197 TRACE("iface %p.\n", iface);
199 return D3DRTYPE_VERTEXBUFFER;
202 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
203 void **data, DWORD flags)
205 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
206 HRESULT hr;
208 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
209 iface, offset, size, data, flags);
211 wined3d_mutex_lock();
212 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
213 wined3d_mutex_unlock();
215 return hr;
218 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
220 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
222 TRACE("iface %p.\n", iface);
224 wined3d_mutex_lock();
225 wined3d_buffer_unmap(buffer->wined3d_buffer);
226 wined3d_mutex_unlock();
228 return D3D_OK;
231 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
232 D3DVERTEXBUFFER_DESC *desc)
234 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
235 struct wined3d_resource_desc wined3d_desc;
236 struct wined3d_resource *wined3d_resource;
238 TRACE("iface %p, desc %p.\n", iface, desc);
240 wined3d_mutex_lock();
241 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
242 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
243 wined3d_mutex_unlock();
245 desc->Format = D3DFMT_VERTEXDATA;
246 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
247 desc->Pool = wined3d_desc.pool;
248 desc->Size = wined3d_desc.size;
249 desc->Type = D3DRTYPE_VERTEXBUFFER;
250 desc->FVF = buffer->fvf;
252 return D3D_OK;
255 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
257 /* IUnknown */
258 d3d9_vertexbuffer_QueryInterface,
259 d3d9_vertexbuffer_AddRef,
260 d3d9_vertexbuffer_Release,
261 /* IDirect3DResource9 */
262 d3d9_vertexbuffer_GetDevice,
263 d3d9_vertexbuffer_SetPrivateData,
264 d3d9_vertexbuffer_GetPrivateData,
265 d3d9_vertexbuffer_FreePrivateData,
266 d3d9_vertexbuffer_SetPriority,
267 d3d9_vertexbuffer_GetPriority,
268 d3d9_vertexbuffer_PreLoad,
269 d3d9_vertexbuffer_GetType,
270 /* IDirect3DVertexBuffer9 */
271 d3d9_vertexbuffer_Lock,
272 d3d9_vertexbuffer_Unlock,
273 d3d9_vertexbuffer_GetDesc,
276 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
278 HeapFree(GetProcessHeap(), 0, parent);
281 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
283 d3d9_vertexbuffer_wined3d_object_destroyed,
286 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
287 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
289 HRESULT hr;
291 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
292 buffer->refcount = 1;
293 buffer->fvf = fvf;
295 wined3d_mutex_lock();
296 hr = wined3d_buffer_create_vb(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
297 (enum wined3d_pool)pool, buffer, &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
298 wined3d_mutex_unlock();
299 if (FAILED(hr))
301 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
302 return hr;
305 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
306 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
308 return D3D_OK;
311 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
313 if (!iface)
314 return NULL;
315 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
317 return impl_from_IDirect3DVertexBuffer9(iface);
320 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
322 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
325 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
327 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
329 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
330 || IsEqualGUID(riid, &IID_IDirect3DResource9)
331 || IsEqualGUID(riid, &IID_IUnknown))
333 IDirect3DIndexBuffer9_AddRef(iface);
334 *out = iface;
335 return S_OK;
338 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
340 *out = NULL;
341 return E_NOINTERFACE;
344 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
346 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
347 ULONG refcount = InterlockedIncrement(&buffer->refcount);
349 TRACE("%p increasing refcount to %u.\n", iface, refcount);
351 if (refcount == 1)
353 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
354 wined3d_mutex_lock();
355 wined3d_buffer_incref(buffer->wined3d_buffer);
356 wined3d_mutex_unlock();
359 return refcount;
362 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
364 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
365 ULONG refcount = InterlockedDecrement(&buffer->refcount);
367 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
369 if (!refcount)
371 IDirect3DDevice9Ex *device = buffer->parent_device;
373 wined3d_mutex_lock();
374 wined3d_buffer_decref(buffer->wined3d_buffer);
375 wined3d_mutex_unlock();
377 /* Release the device last, as it may cause the device to be destroyed. */
378 IDirect3DDevice9Ex_Release(device);
381 return refcount;
384 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
386 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
388 TRACE("iface %p, device %p.\n", iface, device);
390 *device = (IDirect3DDevice9 *)buffer->parent_device;
391 IDirect3DDevice9_AddRef(*device);
393 TRACE("Returning device %p.\n", *device);
395 return D3D_OK;
398 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
399 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
401 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
402 struct wined3d_resource *resource;
403 HRESULT hr;
405 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
406 iface, debugstr_guid(guid), data, data_size, flags);
408 wined3d_mutex_lock();
409 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
410 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
411 wined3d_mutex_unlock();
413 return hr;
416 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
417 REFGUID guid, void *data, DWORD *data_size)
419 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
420 struct wined3d_resource *resource;
421 HRESULT hr;
423 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
424 iface, debugstr_guid(guid), data, data_size);
426 wined3d_mutex_lock();
427 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
428 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
429 wined3d_mutex_unlock();
431 return hr;
434 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
436 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
437 struct wined3d_resource *resource;
438 HRESULT hr;
440 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
442 wined3d_mutex_lock();
443 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
444 hr = wined3d_resource_free_private_data(resource, guid);
445 wined3d_mutex_unlock();
447 return hr;
450 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
452 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
453 DWORD previous;
455 TRACE("iface %p, priority %u.\n", iface, priority);
457 wined3d_mutex_lock();
458 previous = wined3d_buffer_set_priority(buffer->wined3d_buffer, priority);
459 wined3d_mutex_unlock();
461 return previous;
464 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
466 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
467 DWORD priority;
469 TRACE("iface %p.\n", iface);
471 wined3d_mutex_lock();
472 priority = wined3d_buffer_get_priority(buffer->wined3d_buffer);
473 wined3d_mutex_unlock();
475 return priority;
478 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
480 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
482 TRACE("iface %p.\n", iface);
484 wined3d_mutex_lock();
485 wined3d_buffer_preload(buffer->wined3d_buffer);
486 wined3d_mutex_unlock();
489 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
491 TRACE("iface %p.\n", iface);
493 return D3DRTYPE_INDEXBUFFER;
496 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
497 UINT offset, UINT size, void **data, DWORD flags)
499 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
500 HRESULT hr;
502 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
503 iface, offset, size, data, flags);
505 wined3d_mutex_lock();
506 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
507 wined3d_mutex_unlock();
509 return hr;
512 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
514 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
516 TRACE("iface %p.\n", iface);
518 wined3d_mutex_lock();
519 wined3d_buffer_unmap(buffer->wined3d_buffer);
520 wined3d_mutex_unlock();
522 return D3D_OK;
525 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
527 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
528 struct wined3d_resource_desc wined3d_desc;
529 struct wined3d_resource *wined3d_resource;
531 TRACE("iface %p, desc %p.\n", iface, desc);
533 wined3d_mutex_lock();
534 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
535 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
536 wined3d_mutex_unlock();
538 desc->Format = d3dformat_from_wined3dformat(buffer->format);
539 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
540 desc->Pool = wined3d_desc.pool;
541 desc->Size = wined3d_desc.size;
542 desc->Type = D3DRTYPE_INDEXBUFFER;
544 return D3D_OK;
547 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
549 /* IUnknown */
550 d3d9_indexbuffer_QueryInterface,
551 d3d9_indexbuffer_AddRef,
552 d3d9_indexbuffer_Release,
553 /* IDirect3DResource9 */
554 d3d9_indexbuffer_GetDevice,
555 d3d9_indexbuffer_SetPrivateData,
556 d3d9_indexbuffer_GetPrivateData,
557 d3d9_indexbuffer_FreePrivateData,
558 d3d9_indexbuffer_SetPriority,
559 d3d9_indexbuffer_GetPriority,
560 d3d9_indexbuffer_PreLoad,
561 d3d9_indexbuffer_GetType,
562 /* IDirect3DIndexBuffer9 */
563 d3d9_indexbuffer_Lock,
564 d3d9_indexbuffer_Unlock,
565 d3d9_indexbuffer_GetDesc,
568 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
570 HeapFree(GetProcessHeap(), 0, parent);
573 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
575 d3d9_indexbuffer_wined3d_object_destroyed,
578 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
579 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
581 HRESULT hr;
583 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
584 buffer->refcount = 1;
585 buffer->format = wined3dformat_from_d3dformat(format);
587 wined3d_mutex_lock();
588 hr = wined3d_buffer_create_ib(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
589 (enum wined3d_pool)pool, buffer, &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
590 wined3d_mutex_unlock();
591 if (FAILED(hr))
593 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
594 return hr;
597 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
598 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
600 return D3D_OK;
603 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
605 if (!iface)
606 return NULL;
607 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
609 return impl_from_IDirect3DIndexBuffer9(iface);