comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / d3d9 / buffer.c
blobc7db176d73a360676549a2cd21b8ff8679babf74
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 struct wined3d_resource *resource;
136 DWORD previous;
138 TRACE("iface %p, priority %u.\n", iface, priority);
140 wined3d_mutex_lock();
141 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
142 previous = wined3d_resource_set_priority(resource, priority);
143 wined3d_mutex_unlock();
145 return previous;
148 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
150 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
151 const struct wined3d_resource *resource;
152 DWORD priority;
154 TRACE("iface %p.\n", iface);
156 wined3d_mutex_lock();
157 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
158 priority = wined3d_resource_get_priority(resource);
159 wined3d_mutex_unlock();
161 return priority;
164 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
166 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
168 TRACE("iface %p.\n", iface);
170 wined3d_mutex_lock();
171 wined3d_buffer_preload(buffer->wined3d_buffer);
172 wined3d_mutex_unlock();
175 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
177 TRACE("iface %p.\n", iface);
179 return D3DRTYPE_VERTEXBUFFER;
182 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
183 void **data, DWORD flags)
185 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
186 HRESULT hr;
188 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
189 iface, offset, size, data, flags);
191 wined3d_mutex_lock();
192 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
193 wined3d_mutex_unlock();
195 return hr;
198 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
200 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
202 TRACE("iface %p.\n", iface);
204 wined3d_mutex_lock();
205 wined3d_buffer_unmap(buffer->wined3d_buffer);
206 wined3d_mutex_unlock();
208 return D3D_OK;
211 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
212 D3DVERTEXBUFFER_DESC *desc)
214 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
215 struct wined3d_resource_desc wined3d_desc;
216 struct wined3d_resource *wined3d_resource;
218 TRACE("iface %p, desc %p.\n", iface, desc);
220 wined3d_mutex_lock();
221 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
222 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
223 wined3d_mutex_unlock();
225 desc->Format = D3DFMT_VERTEXDATA;
226 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
227 desc->Pool = wined3d_desc.pool;
228 desc->Size = wined3d_desc.size;
229 desc->Type = D3DRTYPE_VERTEXBUFFER;
230 desc->FVF = buffer->fvf;
232 return D3D_OK;
235 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
237 /* IUnknown */
238 d3d9_vertexbuffer_QueryInterface,
239 d3d9_vertexbuffer_AddRef,
240 d3d9_vertexbuffer_Release,
241 /* IDirect3DResource9 */
242 d3d9_vertexbuffer_GetDevice,
243 d3d9_vertexbuffer_SetPrivateData,
244 d3d9_vertexbuffer_GetPrivateData,
245 d3d9_vertexbuffer_FreePrivateData,
246 d3d9_vertexbuffer_SetPriority,
247 d3d9_vertexbuffer_GetPriority,
248 d3d9_vertexbuffer_PreLoad,
249 d3d9_vertexbuffer_GetType,
250 /* IDirect3DVertexBuffer9 */
251 d3d9_vertexbuffer_Lock,
252 d3d9_vertexbuffer_Unlock,
253 d3d9_vertexbuffer_GetDesc,
256 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
258 struct d3d9_vertexbuffer *buffer = parent;
259 d3d9_resource_cleanup(&buffer->resource);
260 HeapFree(GetProcessHeap(), 0, buffer);
263 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
265 d3d9_vertexbuffer_wined3d_object_destroyed,
268 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
269 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
271 HRESULT hr;
273 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
274 buffer->fvf = fvf;
275 d3d9_resource_init(&buffer->resource);
277 wined3d_mutex_lock();
278 hr = wined3d_buffer_create_vb(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
279 (enum wined3d_pool)pool, buffer, &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
280 wined3d_mutex_unlock();
281 if (FAILED(hr))
283 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
284 return hr;
287 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
288 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
290 return D3D_OK;
293 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
295 if (!iface)
296 return NULL;
297 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
299 return impl_from_IDirect3DVertexBuffer9(iface);
302 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
304 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
307 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
309 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
311 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
312 || IsEqualGUID(riid, &IID_IDirect3DResource9)
313 || IsEqualGUID(riid, &IID_IUnknown))
315 IDirect3DIndexBuffer9_AddRef(iface);
316 *out = iface;
317 return S_OK;
320 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
322 *out = NULL;
323 return E_NOINTERFACE;
326 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
328 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
329 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
331 TRACE("%p increasing refcount to %u.\n", iface, refcount);
333 if (refcount == 1)
335 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
336 wined3d_mutex_lock();
337 wined3d_buffer_incref(buffer->wined3d_buffer);
338 wined3d_mutex_unlock();
341 return refcount;
344 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
346 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
347 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
349 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
351 if (!refcount)
353 IDirect3DDevice9Ex *device = buffer->parent_device;
355 wined3d_mutex_lock();
356 wined3d_buffer_decref(buffer->wined3d_buffer);
357 wined3d_mutex_unlock();
359 /* Release the device last, as it may cause the device to be destroyed. */
360 IDirect3DDevice9Ex_Release(device);
363 return refcount;
366 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
368 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
370 TRACE("iface %p, device %p.\n", iface, device);
372 *device = (IDirect3DDevice9 *)buffer->parent_device;
373 IDirect3DDevice9_AddRef(*device);
375 TRACE("Returning device %p.\n", *device);
377 return D3D_OK;
380 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
381 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
383 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
384 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
385 iface, debugstr_guid(guid), data, data_size, flags);
387 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
390 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
391 REFGUID guid, void *data, DWORD *data_size)
393 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
394 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
395 iface, debugstr_guid(guid), data, data_size);
397 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
400 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
402 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
403 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
405 return d3d9_resource_free_private_data(&buffer->resource, guid);
408 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
410 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
411 struct wined3d_resource *resource;
412 DWORD previous;
414 TRACE("iface %p, priority %u.\n", iface, priority);
416 wined3d_mutex_lock();
417 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
418 previous = wined3d_resource_set_priority(resource, priority);
419 wined3d_mutex_unlock();
421 return previous;
424 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
426 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
427 const struct wined3d_resource *resource;
428 DWORD priority;
430 TRACE("iface %p.\n", iface);
432 wined3d_mutex_lock();
433 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
434 priority = wined3d_resource_get_priority(resource);
435 wined3d_mutex_unlock();
437 return priority;
440 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
442 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
444 TRACE("iface %p.\n", iface);
446 wined3d_mutex_lock();
447 wined3d_buffer_preload(buffer->wined3d_buffer);
448 wined3d_mutex_unlock();
451 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
453 TRACE("iface %p.\n", iface);
455 return D3DRTYPE_INDEXBUFFER;
458 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
459 UINT offset, UINT size, void **data, DWORD flags)
461 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
462 HRESULT hr;
464 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
465 iface, offset, size, data, flags);
467 wined3d_mutex_lock();
468 hr = wined3d_buffer_map(buffer->wined3d_buffer, offset, size, (BYTE **)data, flags);
469 wined3d_mutex_unlock();
471 return hr;
474 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
476 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
478 TRACE("iface %p.\n", iface);
480 wined3d_mutex_lock();
481 wined3d_buffer_unmap(buffer->wined3d_buffer);
482 wined3d_mutex_unlock();
484 return D3D_OK;
487 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
489 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
490 struct wined3d_resource_desc wined3d_desc;
491 struct wined3d_resource *wined3d_resource;
493 TRACE("iface %p, desc %p.\n", iface, desc);
495 wined3d_mutex_lock();
496 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
497 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
498 wined3d_mutex_unlock();
500 desc->Format = d3dformat_from_wined3dformat(buffer->format);
501 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
502 desc->Pool = wined3d_desc.pool;
503 desc->Size = wined3d_desc.size;
504 desc->Type = D3DRTYPE_INDEXBUFFER;
506 return D3D_OK;
509 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
511 /* IUnknown */
512 d3d9_indexbuffer_QueryInterface,
513 d3d9_indexbuffer_AddRef,
514 d3d9_indexbuffer_Release,
515 /* IDirect3DResource9 */
516 d3d9_indexbuffer_GetDevice,
517 d3d9_indexbuffer_SetPrivateData,
518 d3d9_indexbuffer_GetPrivateData,
519 d3d9_indexbuffer_FreePrivateData,
520 d3d9_indexbuffer_SetPriority,
521 d3d9_indexbuffer_GetPriority,
522 d3d9_indexbuffer_PreLoad,
523 d3d9_indexbuffer_GetType,
524 /* IDirect3DIndexBuffer9 */
525 d3d9_indexbuffer_Lock,
526 d3d9_indexbuffer_Unlock,
527 d3d9_indexbuffer_GetDesc,
530 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
532 struct d3d9_indexbuffer *buffer = parent;
533 d3d9_resource_cleanup(&buffer->resource);
534 HeapFree(GetProcessHeap(), 0, buffer);
537 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
539 d3d9_indexbuffer_wined3d_object_destroyed,
542 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
543 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
545 HRESULT hr;
547 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
548 buffer->format = wined3dformat_from_d3dformat(format);
549 d3d9_resource_init(&buffer->resource);
551 wined3d_mutex_lock();
552 hr = wined3d_buffer_create_ib(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
553 (enum wined3d_pool)pool, buffer, &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
554 wined3d_mutex_unlock();
555 if (FAILED(hr))
557 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
558 return hr;
561 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
562 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
564 return D3D_OK;
567 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
569 if (!iface)
570 return NULL;
571 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
573 return impl_from_IDirect3DIndexBuffer9(iface);