maintainers: Move WineHQ URLs to https.
[wine.git] / dlls / d3d8 / buffer.c
blob8116c1b87b895fb3a72e53f34fc37cd67e8e98f7
1 /*
2 * Copyright 2005 Oliver Stieber
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "d3d8_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
24 static inline struct d3d8_vertexbuffer *impl_from_IDirect3DVertexBuffer8(IDirect3DVertexBuffer8 *iface)
26 return CONTAINING_RECORD(iface, struct d3d8_vertexbuffer, IDirect3DVertexBuffer8_iface);
29 static HRESULT WINAPI d3d8_vertexbuffer_QueryInterface(IDirect3DVertexBuffer8 *iface, REFIID riid, void **object)
31 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
33 if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer8)
34 || IsEqualGUID(riid, &IID_IDirect3DResource8)
35 || IsEqualGUID(riid, &IID_IUnknown))
37 IDirect3DVertexBuffer8_AddRef(iface);
38 *object = iface;
39 return S_OK;
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
44 *object = NULL;
45 return E_NOINTERFACE;
48 static ULONG WINAPI d3d8_vertexbuffer_AddRef(IDirect3DVertexBuffer8 *iface)
50 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
51 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
53 TRACE("%p increasing refcount to %u.\n", iface, refcount);
55 if (refcount == 1)
57 IDirect3DDevice8_AddRef(buffer->parent_device);
58 wined3d_mutex_lock();
59 wined3d_buffer_incref(buffer->wined3d_buffer);
60 wined3d_mutex_unlock();
63 return refcount;
66 static ULONG WINAPI d3d8_vertexbuffer_Release(IDirect3DVertexBuffer8 *iface)
68 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
69 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
71 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
73 if (!refcount)
75 IDirect3DDevice8 *device = buffer->parent_device;
77 wined3d_mutex_lock();
78 wined3d_buffer_decref(buffer->wined3d_buffer);
79 wined3d_mutex_unlock();
81 /* Release the device last, as it may cause the device to be destroyed. */
82 IDirect3DDevice8_Release(device);
85 return refcount;
88 static HRESULT WINAPI d3d8_vertexbuffer_GetDevice(IDirect3DVertexBuffer8 *iface,
89 IDirect3DDevice8 **device)
91 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
93 TRACE("iface %p, device %p.\n", iface, device);
95 *device = buffer->parent_device;
96 IDirect3DDevice8_AddRef(*device);
98 TRACE("Returning device %p.\n", *device);
100 return D3D_OK;
103 static HRESULT WINAPI d3d8_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer8 *iface,
104 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
106 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
107 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
108 iface, debugstr_guid(guid), data, data_size, flags);
110 return d3d8_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
113 static HRESULT WINAPI d3d8_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer8 *iface,
114 REFGUID guid, void *data, DWORD *data_size)
116 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
117 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
118 iface, debugstr_guid(guid), data, data_size);
120 return d3d8_resource_get_private_data(&buffer->resource, guid, data, data_size);
123 static HRESULT WINAPI d3d8_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer8 *iface, REFGUID guid)
125 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
126 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
128 return d3d8_resource_free_private_data(&buffer->resource, guid);
131 static DWORD WINAPI d3d8_vertexbuffer_SetPriority(IDirect3DVertexBuffer8 *iface, DWORD priority)
133 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
134 struct wined3d_resource *resource;
135 DWORD previous;
137 TRACE("iface %p, priority %u.\n", iface, priority);
139 wined3d_mutex_lock();
140 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
141 previous = wined3d_resource_set_priority(resource, priority);
142 wined3d_mutex_unlock();
144 return previous;
147 static DWORD WINAPI d3d8_vertexbuffer_GetPriority(IDirect3DVertexBuffer8 *iface)
149 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
150 const struct wined3d_resource *resource;
151 DWORD priority;
153 TRACE("iface %p.\n", iface);
155 wined3d_mutex_lock();
156 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
157 priority = wined3d_resource_get_priority(resource);
158 wined3d_mutex_unlock();
160 return priority;
163 static void WINAPI d3d8_vertexbuffer_PreLoad(IDirect3DVertexBuffer8 *iface)
165 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
167 TRACE("iface %p.\n", iface);
169 wined3d_mutex_lock();
170 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
171 wined3d_mutex_unlock();
174 static D3DRESOURCETYPE WINAPI d3d8_vertexbuffer_GetType(IDirect3DVertexBuffer8 *iface)
176 TRACE("iface %p.\n", iface);
178 return D3DRTYPE_VERTEXBUFFER;
181 static HRESULT WINAPI d3d8_vertexbuffer_Lock(IDirect3DVertexBuffer8 *iface, UINT offset, UINT size,
182 BYTE **data, DWORD flags)
184 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
185 struct wined3d_map_desc wined3d_map_desc;
186 struct wined3d_box wined3d_box = {0};
187 HRESULT hr;
189 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
190 iface, offset, size, data, flags);
192 wined3d_box.left = offset;
193 wined3d_box.right = offset + size;
194 wined3d_mutex_lock();
195 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
196 0, &wined3d_map_desc, &wined3d_box, flags);
197 wined3d_mutex_unlock();
198 *data = wined3d_map_desc.data;
200 return hr;
203 static HRESULT WINAPI d3d8_vertexbuffer_Unlock(IDirect3DVertexBuffer8 *iface)
205 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
207 TRACE("iface %p.\n", iface);
209 wined3d_mutex_lock();
210 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
211 wined3d_mutex_unlock();
213 return D3D_OK;
216 static HRESULT WINAPI d3d8_vertexbuffer_GetDesc(IDirect3DVertexBuffer8 *iface,
217 D3DVERTEXBUFFER_DESC *desc)
219 struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
220 struct wined3d_resource_desc wined3d_desc;
221 struct wined3d_resource *wined3d_resource;
223 TRACE("iface %p, desc %p.\n", iface, desc);
225 wined3d_mutex_lock();
226 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
227 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
228 wined3d_mutex_unlock();
230 desc->Type = D3DRTYPE_VERTEXBUFFER;
231 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
232 desc->Pool = wined3d_desc.pool;
233 desc->Size = wined3d_desc.size;
234 desc->FVF = buffer->fvf;
235 desc->Format = D3DFMT_VERTEXDATA;
237 return D3D_OK;
240 static const IDirect3DVertexBuffer8Vtbl Direct3DVertexBuffer8_Vtbl =
242 /* IUnknown */
243 d3d8_vertexbuffer_QueryInterface,
244 d3d8_vertexbuffer_AddRef,
245 d3d8_vertexbuffer_Release,
246 /* IDirect3DResource8 */
247 d3d8_vertexbuffer_GetDevice,
248 d3d8_vertexbuffer_SetPrivateData,
249 d3d8_vertexbuffer_GetPrivateData,
250 d3d8_vertexbuffer_FreePrivateData,
251 d3d8_vertexbuffer_SetPriority,
252 d3d8_vertexbuffer_GetPriority,
253 d3d8_vertexbuffer_PreLoad,
254 d3d8_vertexbuffer_GetType,
255 /* IDirect3DVertexBuffer8 */
256 d3d8_vertexbuffer_Lock,
257 d3d8_vertexbuffer_Unlock,
258 d3d8_vertexbuffer_GetDesc,
261 static void STDMETHODCALLTYPE d3d8_vertexbuffer_wined3d_object_destroyed(void *parent)
263 struct d3d8_vertexbuffer *buffer = parent;
264 d3d8_resource_cleanup(&buffer->resource);
265 HeapFree(GetProcessHeap(), 0, buffer);
268 static const struct wined3d_parent_ops d3d8_vertexbuffer_wined3d_parent_ops =
270 d3d8_vertexbuffer_wined3d_object_destroyed,
273 HRESULT vertexbuffer_init(struct d3d8_vertexbuffer *buffer, struct d3d8_device *device,
274 UINT size, DWORD usage, DWORD fvf, D3DPOOL pool)
276 HRESULT hr;
278 buffer->IDirect3DVertexBuffer8_iface.lpVtbl = &Direct3DVertexBuffer8_Vtbl;
279 d3d8_resource_init(&buffer->resource);
280 buffer->fvf = fvf;
282 wined3d_mutex_lock();
283 hr = wined3d_buffer_create_vb(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
284 (enum wined3d_pool)pool, buffer, &d3d8_vertexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
285 wined3d_mutex_unlock();
286 if (FAILED(hr))
288 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
289 return hr;
292 buffer->parent_device = &device->IDirect3DDevice8_iface;
293 IDirect3DDevice8_AddRef(buffer->parent_device);
295 return D3D_OK;
298 struct d3d8_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer8(IDirect3DVertexBuffer8 *iface)
300 if (!iface)
301 return NULL;
302 assert(iface->lpVtbl == &Direct3DVertexBuffer8_Vtbl);
304 return impl_from_IDirect3DVertexBuffer8(iface);
307 static inline struct d3d8_indexbuffer *impl_from_IDirect3DIndexBuffer8(IDirect3DIndexBuffer8 *iface)
309 return CONTAINING_RECORD(iface, struct d3d8_indexbuffer, IDirect3DIndexBuffer8_iface);
312 static HRESULT WINAPI d3d8_indexbuffer_QueryInterface(IDirect3DIndexBuffer8 *iface, REFIID riid, void **object)
314 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
316 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer8)
317 || IsEqualGUID(riid, &IID_IDirect3DResource8)
318 || IsEqualGUID(riid, &IID_IUnknown))
320 IDirect3DIndexBuffer8_AddRef(iface);
321 *object = iface;
322 return S_OK;
325 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
327 *object = NULL;
328 return E_NOINTERFACE;
331 static ULONG WINAPI d3d8_indexbuffer_AddRef(IDirect3DIndexBuffer8 *iface)
333 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
334 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
336 TRACE("%p increasing refcount to %u.\n", iface, refcount);
338 if (refcount == 1)
340 IDirect3DDevice8_AddRef(buffer->parent_device);
341 wined3d_mutex_lock();
342 wined3d_buffer_incref(buffer->wined3d_buffer);
343 wined3d_mutex_unlock();
346 return refcount;
349 static ULONG WINAPI d3d8_indexbuffer_Release(IDirect3DIndexBuffer8 *iface)
351 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
352 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
354 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
356 if (!refcount)
358 IDirect3DDevice8 *device = buffer->parent_device;
360 wined3d_mutex_lock();
361 wined3d_buffer_decref(buffer->wined3d_buffer);
362 wined3d_mutex_unlock();
364 /* Release the device last, as it may cause the device to be destroyed. */
365 IDirect3DDevice8_Release(device);
368 return refcount;
371 static HRESULT WINAPI d3d8_indexbuffer_GetDevice(IDirect3DIndexBuffer8 *iface,
372 IDirect3DDevice8 **device)
374 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
376 TRACE("iface %p, device %p.\n", iface, device);
378 *device = buffer->parent_device;
379 IDirect3DDevice8_AddRef(*device);
381 TRACE("Returning device %p.\n", *device);
383 return D3D_OK;
386 static HRESULT WINAPI d3d8_indexbuffer_SetPrivateData(IDirect3DIndexBuffer8 *iface,
387 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
389 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
390 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
391 iface, debugstr_guid(guid), data, data_size, flags);
393 return d3d8_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
396 static HRESULT WINAPI d3d8_indexbuffer_GetPrivateData(IDirect3DIndexBuffer8 *iface,
397 REFGUID guid, void *data, DWORD *data_size)
399 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
400 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
401 iface, debugstr_guid(guid), data, data_size);
403 return d3d8_resource_get_private_data(&buffer->resource, guid, data, data_size);
406 static HRESULT WINAPI d3d8_indexbuffer_FreePrivateData(IDirect3DIndexBuffer8 *iface, REFGUID guid)
408 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
409 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
411 return d3d8_resource_free_private_data(&buffer->resource, guid);
414 static DWORD WINAPI d3d8_indexbuffer_SetPriority(IDirect3DIndexBuffer8 *iface, DWORD priority)
416 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
417 struct wined3d_resource *resource;
418 DWORD previous;
420 TRACE("iface %p, priority %u.\n", iface, priority);
422 wined3d_mutex_lock();
423 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
424 previous = wined3d_resource_set_priority(resource, priority);
425 wined3d_mutex_unlock();
427 return previous;
430 static DWORD WINAPI d3d8_indexbuffer_GetPriority(IDirect3DIndexBuffer8 *iface)
432 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
433 const struct wined3d_resource *resource;
434 DWORD priority;
436 TRACE("iface %p.\n", iface);
438 wined3d_mutex_lock();
439 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
440 priority = wined3d_resource_get_priority(resource);
441 wined3d_mutex_unlock();
443 return priority;
446 static void WINAPI d3d8_indexbuffer_PreLoad(IDirect3DIndexBuffer8 *iface)
448 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
450 TRACE("iface %p.\n", iface);
452 wined3d_mutex_lock();
453 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
454 wined3d_mutex_unlock();
457 static D3DRESOURCETYPE WINAPI d3d8_indexbuffer_GetType(IDirect3DIndexBuffer8 *iface)
459 TRACE("iface %p.\n", iface);
461 return D3DRTYPE_INDEXBUFFER;
464 static HRESULT WINAPI d3d8_indexbuffer_Lock(IDirect3DIndexBuffer8 *iface, UINT offset, UINT size,
465 BYTE **data, DWORD flags)
467 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
468 struct wined3d_map_desc wined3d_map_desc;
469 struct wined3d_box wined3d_box = {0};
470 HRESULT hr;
472 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
473 iface, offset, size, data, flags);
475 wined3d_box.left = offset;
476 wined3d_box.right = offset + size;
477 wined3d_mutex_lock();
478 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
479 0, &wined3d_map_desc, &wined3d_box, flags);
480 wined3d_mutex_unlock();
481 *data = wined3d_map_desc.data;
483 return hr;
486 static HRESULT WINAPI d3d8_indexbuffer_Unlock(IDirect3DIndexBuffer8 *iface)
488 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
490 TRACE("iface %p.\n", iface);
492 wined3d_mutex_lock();
493 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
494 wined3d_mutex_unlock();
496 return D3D_OK;
499 static HRESULT WINAPI d3d8_indexbuffer_GetDesc(IDirect3DIndexBuffer8 *iface,
500 D3DINDEXBUFFER_DESC *desc)
502 struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
503 struct wined3d_resource_desc wined3d_desc;
504 struct wined3d_resource *wined3d_resource;
506 TRACE("iface %p, desc %p.\n", iface, desc);
508 wined3d_mutex_lock();
509 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
510 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
511 wined3d_mutex_unlock();
513 desc->Format = d3dformat_from_wined3dformat(buffer->format);
514 desc->Type = D3DRTYPE_INDEXBUFFER;
515 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
516 desc->Pool = wined3d_desc.pool;
517 desc->Size = wined3d_desc.size;
519 return D3D_OK;
522 static const IDirect3DIndexBuffer8Vtbl d3d8_indexbuffer_vtbl =
524 /* IUnknown */
525 d3d8_indexbuffer_QueryInterface,
526 d3d8_indexbuffer_AddRef,
527 d3d8_indexbuffer_Release,
528 /* IDirect3DResource8 */
529 d3d8_indexbuffer_GetDevice,
530 d3d8_indexbuffer_SetPrivateData,
531 d3d8_indexbuffer_GetPrivateData,
532 d3d8_indexbuffer_FreePrivateData,
533 d3d8_indexbuffer_SetPriority,
534 d3d8_indexbuffer_GetPriority,
535 d3d8_indexbuffer_PreLoad,
536 d3d8_indexbuffer_GetType,
537 /* IDirect3DIndexBuffer8 */
538 d3d8_indexbuffer_Lock,
539 d3d8_indexbuffer_Unlock,
540 d3d8_indexbuffer_GetDesc,
543 static void STDMETHODCALLTYPE d3d8_indexbuffer_wined3d_object_destroyed(void *parent)
545 struct d3d8_indexbuffer *buffer = parent;
546 d3d8_resource_cleanup(&buffer->resource);
547 HeapFree(GetProcessHeap(), 0, buffer);
550 static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
552 d3d8_indexbuffer_wined3d_object_destroyed,
555 HRESULT indexbuffer_init(struct d3d8_indexbuffer *buffer, struct d3d8_device *device,
556 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
558 HRESULT hr;
560 buffer->IDirect3DIndexBuffer8_iface.lpVtbl = &d3d8_indexbuffer_vtbl;
561 d3d8_resource_init(&buffer->resource);
562 buffer->format = wined3dformat_from_d3dformat(format);
564 wined3d_mutex_lock();
565 hr = wined3d_buffer_create_ib(device->wined3d_device, size, usage & WINED3DUSAGE_MASK,
566 (enum wined3d_pool)pool, buffer, &d3d8_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
567 wined3d_mutex_unlock();
568 if (FAILED(hr))
570 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
571 return hr;
574 buffer->parent_device = &device->IDirect3DDevice8_iface;
575 IDirect3DDevice8_AddRef(buffer->parent_device);
577 return D3D_OK;
580 struct d3d8_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer8(IDirect3DIndexBuffer8 *iface)
582 if (!iface)
583 return NULL;
584 assert(iface->lpVtbl == &d3d8_indexbuffer_vtbl);
586 return impl_from_IDirect3DIndexBuffer8(iface);