d3d8/tests: Use a separate device for texop_test().
[wine/multimedia.git] / dlls / d3d9 / buffer.c
blobcc93b1874de3feddbe143e2c9c92127deb8c4c83
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->resource.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->resource.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 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
109 iface, debugstr_guid(guid), data, data_size, flags);
111 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
114 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
115 REFGUID guid, void *data, DWORD *data_size)
117 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
118 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
119 iface, debugstr_guid(guid), data, data_size);
121 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
124 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
126 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
127 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
129 return d3d9_resource_free_private_data(&buffer->resource, guid);
132 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
134 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
135 DWORD previous;
137 TRACE("iface %p, priority %u.\n", iface, priority);
139 wined3d_mutex_lock();
140 previous = wined3d_buffer_set_priority(buffer->wined3d_buffer, priority);
141 wined3d_mutex_unlock();
143 return previous;
146 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
148 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
149 DWORD priority;
151 TRACE("iface %p.\n", iface);
153 wined3d_mutex_lock();
154 priority = wined3d_buffer_get_priority(buffer->wined3d_buffer);
155 wined3d_mutex_unlock();
157 return priority;
160 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
162 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
164 TRACE("iface %p.\n", iface);
166 wined3d_mutex_lock();
167 wined3d_buffer_preload(buffer->wined3d_buffer);
168 wined3d_mutex_unlock();
171 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
173 TRACE("iface %p.\n", iface);
175 return D3DRTYPE_VERTEXBUFFER;
178 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
179 void **data, DWORD flags)
181 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
182 HRESULT hr;
184 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
185 iface, offset, size, data, flags);
187 wined3d_mutex_lock();
188 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
189 wined3d_mutex_unlock();
191 return hr;
194 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
196 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
198 TRACE("iface %p.\n", iface);
200 wined3d_mutex_lock();
201 wined3d_buffer_unmap(buffer->wined3d_buffer);
202 wined3d_mutex_unlock();
204 return D3D_OK;
207 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
208 D3DVERTEXBUFFER_DESC *desc)
210 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
211 struct wined3d_resource_desc wined3d_desc;
212 struct wined3d_resource *wined3d_resource;
214 TRACE("iface %p, desc %p.\n", iface, desc);
216 wined3d_mutex_lock();
217 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
218 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
219 wined3d_mutex_unlock();
221 desc->Format = D3DFMT_VERTEXDATA;
222 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
223 desc->Pool = wined3d_desc.pool;
224 desc->Size = wined3d_desc.size;
225 desc->Type = D3DRTYPE_VERTEXBUFFER;
226 desc->FVF = buffer->fvf;
228 return D3D_OK;
231 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
233 /* IUnknown */
234 d3d9_vertexbuffer_QueryInterface,
235 d3d9_vertexbuffer_AddRef,
236 d3d9_vertexbuffer_Release,
237 /* IDirect3DResource9 */
238 d3d9_vertexbuffer_GetDevice,
239 d3d9_vertexbuffer_SetPrivateData,
240 d3d9_vertexbuffer_GetPrivateData,
241 d3d9_vertexbuffer_FreePrivateData,
242 d3d9_vertexbuffer_SetPriority,
243 d3d9_vertexbuffer_GetPriority,
244 d3d9_vertexbuffer_PreLoad,
245 d3d9_vertexbuffer_GetType,
246 /* IDirect3DVertexBuffer9 */
247 d3d9_vertexbuffer_Lock,
248 d3d9_vertexbuffer_Unlock,
249 d3d9_vertexbuffer_GetDesc,
252 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
254 struct d3d9_vertexbuffer *buffer = parent;
255 d3d9_resource_cleanup(&buffer->resource);
256 HeapFree(GetProcessHeap(), 0, buffer);
259 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
261 d3d9_vertexbuffer_wined3d_object_destroyed,
264 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
265 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
267 HRESULT hr;
269 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
270 buffer->fvf = fvf;
271 d3d9_resource_init(&buffer->resource);
273 wined3d_mutex_lock();
274 hr = wined3d_buffer_create_vb(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
275 (enum wined3d_pool)pool, buffer, &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
276 wined3d_mutex_unlock();
277 if (FAILED(hr))
279 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
280 return hr;
283 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
284 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
286 return D3D_OK;
289 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
291 if (!iface)
292 return NULL;
293 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
295 return impl_from_IDirect3DVertexBuffer9(iface);
298 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
300 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
303 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
305 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
307 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
308 || IsEqualGUID(riid, &IID_IDirect3DResource9)
309 || IsEqualGUID(riid, &IID_IUnknown))
311 IDirect3DIndexBuffer9_AddRef(iface);
312 *out = iface;
313 return S_OK;
316 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
318 *out = NULL;
319 return E_NOINTERFACE;
322 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
324 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
325 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
327 TRACE("%p increasing refcount to %u.\n", iface, refcount);
329 if (refcount == 1)
331 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
332 wined3d_mutex_lock();
333 wined3d_buffer_incref(buffer->wined3d_buffer);
334 wined3d_mutex_unlock();
337 return refcount;
340 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
342 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
343 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
345 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
347 if (!refcount)
349 IDirect3DDevice9Ex *device = buffer->parent_device;
351 wined3d_mutex_lock();
352 wined3d_buffer_decref(buffer->wined3d_buffer);
353 wined3d_mutex_unlock();
355 /* Release the device last, as it may cause the device to be destroyed. */
356 IDirect3DDevice9Ex_Release(device);
359 return refcount;
362 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
364 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
366 TRACE("iface %p, device %p.\n", iface, device);
368 *device = (IDirect3DDevice9 *)buffer->parent_device;
369 IDirect3DDevice9_AddRef(*device);
371 TRACE("Returning device %p.\n", *device);
373 return D3D_OK;
376 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
377 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
379 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
380 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
381 iface, debugstr_guid(guid), data, data_size, flags);
383 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
386 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
387 REFGUID guid, void *data, DWORD *data_size)
389 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
390 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
391 iface, debugstr_guid(guid), data, data_size);
393 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
396 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
398 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
399 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
401 return d3d9_resource_free_private_data(&buffer->resource, guid);
404 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
406 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
407 DWORD previous;
409 TRACE("iface %p, priority %u.\n", iface, priority);
411 wined3d_mutex_lock();
412 previous = wined3d_buffer_set_priority(buffer->wined3d_buffer, priority);
413 wined3d_mutex_unlock();
415 return previous;
418 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
420 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
421 DWORD priority;
423 TRACE("iface %p.\n", iface);
425 wined3d_mutex_lock();
426 priority = wined3d_buffer_get_priority(buffer->wined3d_buffer);
427 wined3d_mutex_unlock();
429 return priority;
432 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
434 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
436 TRACE("iface %p.\n", iface);
438 wined3d_mutex_lock();
439 wined3d_buffer_preload(buffer->wined3d_buffer);
440 wined3d_mutex_unlock();
443 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
445 TRACE("iface %p.\n", iface);
447 return D3DRTYPE_INDEXBUFFER;
450 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
451 UINT offset, UINT size, void **data, DWORD flags)
453 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
454 HRESULT hr;
456 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
457 iface, offset, size, data, flags);
459 wined3d_mutex_lock();
460 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
461 wined3d_mutex_unlock();
463 return hr;
466 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
468 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
470 TRACE("iface %p.\n", iface);
472 wined3d_mutex_lock();
473 wined3d_buffer_unmap(buffer->wined3d_buffer);
474 wined3d_mutex_unlock();
476 return D3D_OK;
479 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
481 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
482 struct wined3d_resource_desc wined3d_desc;
483 struct wined3d_resource *wined3d_resource;
485 TRACE("iface %p, desc %p.\n", iface, desc);
487 wined3d_mutex_lock();
488 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
489 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
490 wined3d_mutex_unlock();
492 desc->Format = d3dformat_from_wined3dformat(buffer->format);
493 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
494 desc->Pool = wined3d_desc.pool;
495 desc->Size = wined3d_desc.size;
496 desc->Type = D3DRTYPE_INDEXBUFFER;
498 return D3D_OK;
501 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
503 /* IUnknown */
504 d3d9_indexbuffer_QueryInterface,
505 d3d9_indexbuffer_AddRef,
506 d3d9_indexbuffer_Release,
507 /* IDirect3DResource9 */
508 d3d9_indexbuffer_GetDevice,
509 d3d9_indexbuffer_SetPrivateData,
510 d3d9_indexbuffer_GetPrivateData,
511 d3d9_indexbuffer_FreePrivateData,
512 d3d9_indexbuffer_SetPriority,
513 d3d9_indexbuffer_GetPriority,
514 d3d9_indexbuffer_PreLoad,
515 d3d9_indexbuffer_GetType,
516 /* IDirect3DIndexBuffer9 */
517 d3d9_indexbuffer_Lock,
518 d3d9_indexbuffer_Unlock,
519 d3d9_indexbuffer_GetDesc,
522 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
524 struct d3d9_indexbuffer *buffer = parent;
525 d3d9_resource_cleanup(&buffer->resource);
526 HeapFree(GetProcessHeap(), 0, buffer);
529 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
531 d3d9_indexbuffer_wined3d_object_destroyed,
534 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
535 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
537 HRESULT hr;
539 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
540 buffer->format = wined3dformat_from_d3dformat(format);
541 d3d9_resource_init(&buffer->resource);
543 wined3d_mutex_lock();
544 hr = wined3d_buffer_create_ib(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
545 (enum wined3d_pool)pool, buffer, &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
546 wined3d_mutex_unlock();
547 if (FAILED(hr))
549 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
550 return hr;
553 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
554 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
556 return D3D_OK;
559 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
561 if (!iface)
562 return NULL;
563 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
565 return impl_from_IDirect3DIndexBuffer9(iface);