comctl32/tests: Use CRT allocation functions.
[wine.git] / dlls / d3d9 / buffer.c
blob73190cb9dd6630542dedf2a9240cf153f4764a18
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 "d3d9_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
25 static inline struct d3d9_vertexbuffer *impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
27 return CONTAINING_RECORD(iface, struct d3d9_vertexbuffer, IDirect3DVertexBuffer9_iface);
30 static HRESULT WINAPI d3d9_vertexbuffer_QueryInterface(IDirect3DVertexBuffer9 *iface, REFIID riid, void **out)
32 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
34 if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)
35 || IsEqualGUID(riid, &IID_IDirect3DResource9)
36 || IsEqualGUID(riid, &IID_IUnknown))
38 IDirect3DVertexBuffer9_AddRef(iface);
39 *out = iface;
40 return S_OK;
43 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
45 *out = NULL;
46 return E_NOINTERFACE;
49 static ULONG WINAPI d3d9_vertexbuffer_AddRef(IDirect3DVertexBuffer9 *iface)
51 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
52 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
54 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
56 if (refcount == 1)
58 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
59 if (buffer->draw_buffer)
60 wined3d_buffer_incref(buffer->draw_buffer);
61 else
62 wined3d_buffer_incref(buffer->wined3d_buffer);
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 %lu.\n", iface, refcount);
75 if (!refcount)
77 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
78 IDirect3DDevice9Ex *device = buffer->parent_device;
80 if (draw_buffer)
81 wined3d_buffer_decref(draw_buffer);
82 else
83 wined3d_buffer_decref(buffer->wined3d_buffer);
85 /* Release the device last, as it may cause the device to be destroyed. */
86 IDirect3DDevice9Ex_Release(device);
89 return refcount;
92 static HRESULT WINAPI d3d9_vertexbuffer_GetDevice(IDirect3DVertexBuffer9 *iface, IDirect3DDevice9 **device)
94 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
96 TRACE("iface %p, device %p.\n", iface, device);
98 *device = (IDirect3DDevice9 *)buffer->parent_device;
99 IDirect3DDevice9_AddRef(*device);
101 TRACE("Returning device %p.\n", *device);
103 return D3D_OK;
106 static HRESULT WINAPI d3d9_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer9 *iface,
107 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
109 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
110 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
111 iface, debugstr_guid(guid), data, data_size, flags);
113 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
116 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
117 REFGUID guid, void *data, DWORD *data_size)
119 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
120 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
121 iface, debugstr_guid(guid), data, data_size);
123 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
126 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
128 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
129 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
131 return d3d9_resource_free_private_data(&buffer->resource, guid);
134 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
136 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
137 struct wined3d_resource *resource;
138 DWORD previous;
140 TRACE("iface %p, priority %lu.\n", iface, priority);
142 wined3d_mutex_lock();
143 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
144 previous = wined3d_resource_set_priority(resource, priority);
145 wined3d_mutex_unlock();
147 return previous;
150 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
152 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
153 const struct wined3d_resource *resource;
154 DWORD priority;
156 TRACE("iface %p.\n", iface);
158 wined3d_mutex_lock();
159 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
160 priority = wined3d_resource_get_priority(resource);
161 wined3d_mutex_unlock();
163 return priority;
166 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
168 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
170 TRACE("iface %p.\n", iface);
172 wined3d_mutex_lock();
173 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
174 wined3d_mutex_unlock();
177 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
179 TRACE("iface %p.\n", iface);
181 return D3DRTYPE_VERTEXBUFFER;
184 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
185 void **data, DWORD flags)
187 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
188 struct wined3d_resource *wined3d_resource;
189 struct wined3d_map_desc wined3d_map_desc;
190 struct wined3d_box wined3d_box;
191 HRESULT hr;
193 TRACE("iface %p, offset %u, size %u, data %p, flags %#lx.\n",
194 iface, offset, size, data, flags);
196 wined3d_box_set(&wined3d_box, offset, 0, offset + size, 1, 0, 1);
198 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
199 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
200 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
201 *data = wined3d_map_desc.data;
203 return hr;
206 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
208 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
210 TRACE("iface %p.\n", iface);
212 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
214 return D3D_OK;
217 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
218 D3DVERTEXBUFFER_DESC *desc)
220 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
221 struct wined3d_resource_desc wined3d_desc;
222 struct wined3d_resource *wined3d_resource;
224 TRACE("iface %p, desc %p.\n", iface, desc);
226 wined3d_mutex_lock();
227 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
228 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
229 wined3d_mutex_unlock();
231 desc->Format = D3DFMT_VERTEXDATA;
232 desc->Type = D3DRTYPE_VERTEXBUFFER;
233 desc->Usage = buffer->usage;
234 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
235 desc->Size = wined3d_desc.size;
236 desc->FVF = buffer->fvf;
238 return D3D_OK;
241 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
243 /* IUnknown */
244 d3d9_vertexbuffer_QueryInterface,
245 d3d9_vertexbuffer_AddRef,
246 d3d9_vertexbuffer_Release,
247 /* IDirect3DResource9 */
248 d3d9_vertexbuffer_GetDevice,
249 d3d9_vertexbuffer_SetPrivateData,
250 d3d9_vertexbuffer_GetPrivateData,
251 d3d9_vertexbuffer_FreePrivateData,
252 d3d9_vertexbuffer_SetPriority,
253 d3d9_vertexbuffer_GetPriority,
254 d3d9_vertexbuffer_PreLoad,
255 d3d9_vertexbuffer_GetType,
256 /* IDirect3DVertexBuffer9 */
257 d3d9_vertexbuffer_Lock,
258 d3d9_vertexbuffer_Unlock,
259 d3d9_vertexbuffer_GetDesc,
262 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
264 struct d3d9_vertexbuffer *buffer = parent;
266 if (buffer->draw_buffer)
267 wined3d_buffer_decref(buffer->wined3d_buffer);
268 d3d9_resource_cleanup(&buffer->resource);
269 free(buffer);
272 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
274 d3d9_vertexbuffer_wined3d_object_destroyed,
277 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
278 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
280 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
281 struct wined3d_buffer_desc desc;
282 HRESULT hr;
284 if (pool == D3DPOOL_SCRATCH)
286 WARN("Vertex buffer with D3DPOOL_SCRATCH requested.\n");
287 return D3DERR_INVALIDCALL;
290 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
292 WARN("Managed resources are not supported by d3d9ex devices.\n");
293 return D3DERR_INVALIDCALL;
296 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
297 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
298 return D3DERR_INVALIDCALL;
300 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
301 buffer->fvf = fvf;
302 buffer->usage = usage;
303 d3d9_resource_init(&buffer->resource);
305 desc.byte_width = size;
306 desc.usage = wined3d_usage_from_d3d(pool, usage);
307 desc.bind_flags = 0;
308 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
309 /* Buffers are always readable. */
310 if (pool != D3DPOOL_DEFAULT)
311 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
312 desc.misc_flags = 0;
313 desc.structure_byte_stride = 0;
315 if (!device->d3d_parent->extended)
316 desc.usage |= WINED3DUSAGE_VIDMEM_ACCOUNTING;
318 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
320 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
321 parent_ops = &d3d9_vertexbuffer_wined3d_parent_ops;
324 wined3d_mutex_lock();
325 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
326 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
328 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
329 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
330 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
331 &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
332 wined3d_buffer_decref(buffer->wined3d_buffer);
334 wined3d_mutex_unlock();
335 if (FAILED(hr))
337 WARN("Failed to create wined3d buffer, hr %#lx.\n", hr);
338 return hr;
341 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
342 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
344 return D3D_OK;
347 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
349 if (!iface)
350 return NULL;
351 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
353 return impl_from_IDirect3DVertexBuffer9(iface);
356 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
358 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
361 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
363 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
365 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
366 || IsEqualGUID(riid, &IID_IDirect3DResource9)
367 || IsEqualGUID(riid, &IID_IUnknown))
369 IDirect3DIndexBuffer9_AddRef(iface);
370 *out = iface;
371 return S_OK;
374 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
376 *out = NULL;
377 return E_NOINTERFACE;
380 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
382 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
383 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
385 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
387 if (refcount == 1)
389 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
390 wined3d_buffer_incref(buffer->wined3d_buffer);
393 return refcount;
396 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
398 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
399 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
401 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
403 if (!refcount)
405 IDirect3DDevice9Ex *device = buffer->parent_device;
407 wined3d_buffer_decref(buffer->wined3d_buffer);
409 /* Release the device last, as it may cause the device to be destroyed. */
410 IDirect3DDevice9Ex_Release(device);
413 return refcount;
416 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
418 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
420 TRACE("iface %p, device %p.\n", iface, device);
422 *device = (IDirect3DDevice9 *)buffer->parent_device;
423 IDirect3DDevice9_AddRef(*device);
425 TRACE("Returning device %p.\n", *device);
427 return D3D_OK;
430 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
431 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
433 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
434 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
435 iface, debugstr_guid(guid), data, data_size, flags);
437 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
440 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
441 REFGUID guid, void *data, DWORD *data_size)
443 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
444 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
445 iface, debugstr_guid(guid), data, data_size);
447 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
450 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
452 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
453 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
455 return d3d9_resource_free_private_data(&buffer->resource, guid);
458 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
460 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
461 struct wined3d_resource *resource;
462 DWORD previous;
464 TRACE("iface %p, priority %lu.\n", iface, priority);
466 wined3d_mutex_lock();
467 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
468 previous = wined3d_resource_set_priority(resource, priority);
469 wined3d_mutex_unlock();
471 return previous;
474 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
476 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
477 const struct wined3d_resource *resource;
478 DWORD priority;
480 TRACE("iface %p.\n", iface);
482 wined3d_mutex_lock();
483 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
484 priority = wined3d_resource_get_priority(resource);
485 wined3d_mutex_unlock();
487 return priority;
490 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
492 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
494 TRACE("iface %p.\n", iface);
496 wined3d_mutex_lock();
497 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
498 wined3d_mutex_unlock();
501 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
503 TRACE("iface %p.\n", iface);
505 return D3DRTYPE_INDEXBUFFER;
508 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
509 UINT offset, UINT size, void **data, DWORD flags)
511 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
512 struct wined3d_resource *wined3d_resource;
513 struct wined3d_map_desc wined3d_map_desc;
514 struct wined3d_box wined3d_box;
515 HRESULT hr;
517 TRACE("iface %p, offset %u, size %u, data %p, flags %#lx.\n",
518 iface, offset, size, data, flags);
520 wined3d_box_set(&wined3d_box, offset, 0, offset + size, 1, 0, 1);
522 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
523 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
524 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
525 *data = wined3d_map_desc.data;
527 return hr;
530 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
532 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
534 TRACE("iface %p.\n", iface);
536 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
538 return D3D_OK;
541 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
543 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
544 struct wined3d_resource_desc wined3d_desc;
545 struct wined3d_resource *wined3d_resource;
547 TRACE("iface %p, desc %p.\n", iface, desc);
549 wined3d_mutex_lock();
550 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
551 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
552 wined3d_mutex_unlock();
554 desc->Format = d3dformat_from_wined3dformat(buffer->format);
555 desc->Type = D3DRTYPE_INDEXBUFFER;
556 desc->Usage = buffer->usage;
557 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
558 desc->Size = wined3d_desc.size;
560 return D3D_OK;
563 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
565 /* IUnknown */
566 d3d9_indexbuffer_QueryInterface,
567 d3d9_indexbuffer_AddRef,
568 d3d9_indexbuffer_Release,
569 /* IDirect3DResource9 */
570 d3d9_indexbuffer_GetDevice,
571 d3d9_indexbuffer_SetPrivateData,
572 d3d9_indexbuffer_GetPrivateData,
573 d3d9_indexbuffer_FreePrivateData,
574 d3d9_indexbuffer_SetPriority,
575 d3d9_indexbuffer_GetPriority,
576 d3d9_indexbuffer_PreLoad,
577 d3d9_indexbuffer_GetType,
578 /* IDirect3DIndexBuffer9 */
579 d3d9_indexbuffer_Lock,
580 d3d9_indexbuffer_Unlock,
581 d3d9_indexbuffer_GetDesc,
584 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
586 struct d3d9_indexbuffer *buffer = parent;
588 d3d9_resource_cleanup(&buffer->resource);
589 free(buffer);
592 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
594 d3d9_indexbuffer_wined3d_object_destroyed,
597 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
598 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
600 struct wined3d_buffer_desc desc;
601 HRESULT hr;
603 if (pool == D3DPOOL_SCRATCH)
604 return D3DERR_INVALIDCALL;
606 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
608 WARN("Managed resources are not supported by d3d9ex devices.\n");
609 return D3DERR_INVALIDCALL;
612 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
613 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
614 return D3DERR_INVALIDCALL;
616 desc.byte_width = size;
617 desc.usage = wined3d_usage_from_d3d(pool, usage) | WINED3DUSAGE_STATICDECL;
618 desc.bind_flags = 0;
619 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
620 /* Buffers are always readable. */
621 if (pool != D3DPOOL_DEFAULT)
622 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
623 desc.misc_flags = 0;
624 desc.structure_byte_stride = 0;
626 if (!device->d3d_parent->extended)
627 desc.usage |= WINED3DUSAGE_VIDMEM_ACCOUNTING;
629 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
630 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
632 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
633 buffer->format = wined3dformat_from_d3dformat(format);
634 buffer->usage = usage;
635 buffer->sysmem = !(desc.access & WINED3D_RESOURCE_ACCESS_GPU);
636 d3d9_resource_init(&buffer->resource);
638 wined3d_mutex_lock();
639 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
640 &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
641 wined3d_mutex_unlock();
642 if (FAILED(hr))
644 WARN("Failed to create wined3d buffer, hr %#lx.\n", hr);
645 return hr;
648 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
649 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
651 return D3D_OK;
654 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
656 if (!iface)
657 return NULL;
658 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
660 return impl_from_IDirect3DIndexBuffer9(iface);