kernelbase: Check pointer validity in unsafe_ptr_from_HLOCAL.
[wine.git] / dlls / d3d9 / buffer.c
blobbcb97011fe55942af1e18d41d4d9686d1660b51a
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 %u.\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 %u.\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 %u, flags %#x.\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 %u.\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 %#x.\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 heap_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 = usage & WINED3DUSAGE_MASK;
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 (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
317 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
318 parent_ops = &d3d9_vertexbuffer_wined3d_parent_ops;
321 wined3d_mutex_lock();
322 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
323 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
325 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
326 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
327 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
328 &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
329 wined3d_buffer_decref(buffer->wined3d_buffer);
331 wined3d_mutex_unlock();
332 if (FAILED(hr))
334 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
335 return hr;
338 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
339 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
341 return D3D_OK;
344 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
346 if (!iface)
347 return NULL;
348 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
350 return impl_from_IDirect3DVertexBuffer9(iface);
353 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
355 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
358 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
360 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
362 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
363 || IsEqualGUID(riid, &IID_IDirect3DResource9)
364 || IsEqualGUID(riid, &IID_IUnknown))
366 IDirect3DIndexBuffer9_AddRef(iface);
367 *out = iface;
368 return S_OK;
371 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
373 *out = NULL;
374 return E_NOINTERFACE;
377 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
379 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
380 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
382 TRACE("%p increasing refcount to %u.\n", iface, refcount);
384 if (refcount == 1)
386 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
387 if (buffer->draw_buffer)
388 wined3d_buffer_incref(buffer->draw_buffer);
389 else
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 %u.\n", iface, refcount);
403 if (!refcount)
405 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
406 IDirect3DDevice9Ex *device = buffer->parent_device;
408 if (draw_buffer)
409 wined3d_buffer_decref(draw_buffer);
410 else
411 wined3d_buffer_decref(buffer->wined3d_buffer);
413 /* Release the device last, as it may cause the device to be destroyed. */
414 IDirect3DDevice9Ex_Release(device);
417 return refcount;
420 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
422 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
424 TRACE("iface %p, device %p.\n", iface, device);
426 *device = (IDirect3DDevice9 *)buffer->parent_device;
427 IDirect3DDevice9_AddRef(*device);
429 TRACE("Returning device %p.\n", *device);
431 return D3D_OK;
434 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
435 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
437 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
438 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
439 iface, debugstr_guid(guid), data, data_size, flags);
441 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
444 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
445 REFGUID guid, void *data, DWORD *data_size)
447 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
448 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
449 iface, debugstr_guid(guid), data, data_size);
451 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
454 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
456 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
457 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
459 return d3d9_resource_free_private_data(&buffer->resource, guid);
462 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
464 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
465 struct wined3d_resource *resource;
466 DWORD previous;
468 TRACE("iface %p, priority %u.\n", iface, priority);
470 wined3d_mutex_lock();
471 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
472 previous = wined3d_resource_set_priority(resource, priority);
473 wined3d_mutex_unlock();
475 return previous;
478 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
480 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
481 const struct wined3d_resource *resource;
482 DWORD priority;
484 TRACE("iface %p.\n", iface);
486 wined3d_mutex_lock();
487 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
488 priority = wined3d_resource_get_priority(resource);
489 wined3d_mutex_unlock();
491 return priority;
494 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
496 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
498 TRACE("iface %p.\n", iface);
500 wined3d_mutex_lock();
501 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
502 wined3d_mutex_unlock();
505 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
507 TRACE("iface %p.\n", iface);
509 return D3DRTYPE_INDEXBUFFER;
512 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
513 UINT offset, UINT size, void **data, DWORD flags)
515 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
516 struct wined3d_resource *wined3d_resource;
517 struct wined3d_map_desc wined3d_map_desc;
518 struct wined3d_box wined3d_box;
519 HRESULT hr;
521 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
522 iface, offset, size, data, flags);
524 wined3d_box_set(&wined3d_box, offset, 0, offset + size, 1, 0, 1);
526 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
527 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
528 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
529 *data = wined3d_map_desc.data;
531 return hr;
534 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
536 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
538 TRACE("iface %p.\n", iface);
540 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
542 return D3D_OK;
545 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
547 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
548 struct wined3d_resource_desc wined3d_desc;
549 struct wined3d_resource *wined3d_resource;
551 TRACE("iface %p, desc %p.\n", iface, desc);
553 wined3d_mutex_lock();
554 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
555 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
556 wined3d_mutex_unlock();
558 desc->Format = d3dformat_from_wined3dformat(buffer->format);
559 desc->Type = D3DRTYPE_INDEXBUFFER;
560 desc->Usage = buffer->usage;
561 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
562 desc->Size = wined3d_desc.size;
564 return D3D_OK;
567 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
569 /* IUnknown */
570 d3d9_indexbuffer_QueryInterface,
571 d3d9_indexbuffer_AddRef,
572 d3d9_indexbuffer_Release,
573 /* IDirect3DResource9 */
574 d3d9_indexbuffer_GetDevice,
575 d3d9_indexbuffer_SetPrivateData,
576 d3d9_indexbuffer_GetPrivateData,
577 d3d9_indexbuffer_FreePrivateData,
578 d3d9_indexbuffer_SetPriority,
579 d3d9_indexbuffer_GetPriority,
580 d3d9_indexbuffer_PreLoad,
581 d3d9_indexbuffer_GetType,
582 /* IDirect3DIndexBuffer9 */
583 d3d9_indexbuffer_Lock,
584 d3d9_indexbuffer_Unlock,
585 d3d9_indexbuffer_GetDesc,
588 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
590 struct d3d9_indexbuffer *buffer = parent;
592 if (buffer->draw_buffer)
593 wined3d_buffer_decref(buffer->wined3d_buffer);
594 d3d9_resource_cleanup(&buffer->resource);
595 heap_free(buffer);
598 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
600 d3d9_indexbuffer_wined3d_object_destroyed,
603 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
604 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
606 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
607 struct wined3d_buffer_desc desc;
608 HRESULT hr;
610 if (pool == D3DPOOL_SCRATCH)
611 return D3DERR_INVALIDCALL;
613 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
615 WARN("Managed resources are not supported by d3d9ex devices.\n");
616 return D3DERR_INVALIDCALL;
619 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
620 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
621 return D3DERR_INVALIDCALL;
623 desc.byte_width = size;
624 desc.usage = (usage & WINED3DUSAGE_MASK) | WINED3DUSAGE_STATICDECL;
625 desc.bind_flags = 0;
626 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
627 /* Buffers are always readable. */
628 if (pool != D3DPOOL_DEFAULT)
629 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
630 desc.misc_flags = 0;
631 desc.structure_byte_stride = 0;
633 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
635 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
636 parent_ops = &d3d9_indexbuffer_wined3d_parent_ops;
639 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
640 buffer->format = wined3dformat_from_d3dformat(format);
641 buffer->usage = usage;
642 d3d9_resource_init(&buffer->resource);
644 wined3d_mutex_lock();
645 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
646 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
648 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
649 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
650 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
651 &d3d9_indexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
652 wined3d_buffer_decref(buffer->wined3d_buffer);
654 wined3d_mutex_unlock();
655 if (FAILED(hr))
657 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
658 return hr;
661 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
662 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
664 return D3D_OK;
667 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
669 if (!iface)
670 return NULL;
671 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
673 return impl_from_IDirect3DIndexBuffer9(iface);