ntdll: Translate signal to trap when trap code is 0 on ARM.
[wine.git] / dlls / d3d9 / buffer.c
blob43321b3202bc015d88340f45be4725682be75ab0
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 "wine/port.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline struct d3d9_vertexbuffer *impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
29 return CONTAINING_RECORD(iface, struct d3d9_vertexbuffer, IDirect3DVertexBuffer9_iface);
32 static HRESULT WINAPI d3d9_vertexbuffer_QueryInterface(IDirect3DVertexBuffer9 *iface, REFIID riid, void **out)
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
36 if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)
37 || IsEqualGUID(riid, &IID_IDirect3DResource9)
38 || IsEqualGUID(riid, &IID_IUnknown))
40 IDirect3DVertexBuffer9_AddRef(iface);
41 *out = iface;
42 return S_OK;
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
47 *out = NULL;
48 return E_NOINTERFACE;
51 static ULONG WINAPI d3d9_vertexbuffer_AddRef(IDirect3DVertexBuffer9 *iface)
53 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
54 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
56 TRACE("%p increasing refcount to %u.\n", iface, refcount);
58 if (refcount == 1)
60 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
61 wined3d_mutex_lock();
62 if (buffer->draw_buffer)
63 wined3d_buffer_incref(buffer->draw_buffer);
64 else
65 wined3d_buffer_incref(buffer->wined3d_buffer);
66 wined3d_mutex_unlock();
69 return refcount;
72 static ULONG WINAPI d3d9_vertexbuffer_Release(IDirect3DVertexBuffer9 *iface)
74 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
75 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
77 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
79 if (!refcount)
81 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
82 IDirect3DDevice9Ex *device = buffer->parent_device;
84 wined3d_mutex_lock();
85 if (draw_buffer)
86 wined3d_buffer_decref(draw_buffer);
87 else
88 wined3d_buffer_decref(buffer->wined3d_buffer);
89 wined3d_mutex_unlock();
91 /* Release the device last, as it may cause the device to be destroyed. */
92 IDirect3DDevice9Ex_Release(device);
95 return refcount;
98 static HRESULT WINAPI d3d9_vertexbuffer_GetDevice(IDirect3DVertexBuffer9 *iface, IDirect3DDevice9 **device)
100 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
102 TRACE("iface %p, device %p.\n", iface, device);
104 *device = (IDirect3DDevice9 *)buffer->parent_device;
105 IDirect3DDevice9_AddRef(*device);
107 TRACE("Returning device %p.\n", *device);
109 return D3D_OK;
112 static HRESULT WINAPI d3d9_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer9 *iface,
113 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
115 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
116 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
117 iface, debugstr_guid(guid), data, data_size, flags);
119 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
122 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
123 REFGUID guid, void *data, DWORD *data_size)
125 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
126 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
127 iface, debugstr_guid(guid), data, data_size);
129 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
132 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
134 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
135 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
137 return d3d9_resource_free_private_data(&buffer->resource, guid);
140 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
142 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
143 struct wined3d_resource *resource;
144 DWORD previous;
146 TRACE("iface %p, priority %u.\n", iface, priority);
148 wined3d_mutex_lock();
149 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
150 previous = wined3d_resource_set_priority(resource, priority);
151 wined3d_mutex_unlock();
153 return previous;
156 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
158 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
159 const struct wined3d_resource *resource;
160 DWORD priority;
162 TRACE("iface %p.\n", iface);
164 wined3d_mutex_lock();
165 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
166 priority = wined3d_resource_get_priority(resource);
167 wined3d_mutex_unlock();
169 return priority;
172 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
174 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
176 TRACE("iface %p.\n", iface);
178 wined3d_mutex_lock();
179 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
180 wined3d_mutex_unlock();
183 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
185 TRACE("iface %p.\n", iface);
187 return D3DRTYPE_VERTEXBUFFER;
190 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
191 void **data, DWORD flags)
193 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
194 struct wined3d_resource *wined3d_resource;
195 struct wined3d_map_desc wined3d_map_desc;
196 struct wined3d_box wined3d_box = {0};
197 HRESULT hr;
199 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
200 iface, offset, size, data, flags);
202 wined3d_box.left = offset;
203 wined3d_box.right = offset + size;
204 wined3d_mutex_lock();
205 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
206 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
207 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
208 wined3d_mutex_unlock();
209 *data = wined3d_map_desc.data;
211 return hr;
214 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
216 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
218 TRACE("iface %p.\n", iface);
220 wined3d_mutex_lock();
221 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
222 wined3d_mutex_unlock();
224 return D3D_OK;
227 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
228 D3DVERTEXBUFFER_DESC *desc)
230 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
231 struct wined3d_resource_desc wined3d_desc;
232 struct wined3d_resource *wined3d_resource;
234 TRACE("iface %p, desc %p.\n", iface, desc);
236 wined3d_mutex_lock();
237 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
238 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
239 wined3d_mutex_unlock();
241 desc->Format = D3DFMT_VERTEXDATA;
242 desc->Type = D3DRTYPE_VERTEXBUFFER;
243 desc->Usage = buffer->usage;
244 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
245 desc->Size = wined3d_desc.size;
246 desc->FVF = buffer->fvf;
248 return D3D_OK;
251 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
253 /* IUnknown */
254 d3d9_vertexbuffer_QueryInterface,
255 d3d9_vertexbuffer_AddRef,
256 d3d9_vertexbuffer_Release,
257 /* IDirect3DResource9 */
258 d3d9_vertexbuffer_GetDevice,
259 d3d9_vertexbuffer_SetPrivateData,
260 d3d9_vertexbuffer_GetPrivateData,
261 d3d9_vertexbuffer_FreePrivateData,
262 d3d9_vertexbuffer_SetPriority,
263 d3d9_vertexbuffer_GetPriority,
264 d3d9_vertexbuffer_PreLoad,
265 d3d9_vertexbuffer_GetType,
266 /* IDirect3DVertexBuffer9 */
267 d3d9_vertexbuffer_Lock,
268 d3d9_vertexbuffer_Unlock,
269 d3d9_vertexbuffer_GetDesc,
272 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
274 struct d3d9_vertexbuffer *buffer = parent;
276 if (buffer->draw_buffer)
277 wined3d_buffer_decref(buffer->wined3d_buffer);
278 d3d9_resource_cleanup(&buffer->resource);
279 heap_free(buffer);
282 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
284 d3d9_vertexbuffer_wined3d_object_destroyed,
287 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
288 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
290 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
291 struct wined3d_buffer_desc desc;
292 HRESULT hr;
294 if (pool == D3DPOOL_SCRATCH)
296 WARN("Vertex buffer with D3DPOOL_SCRATCH requested.\n");
297 return D3DERR_INVALIDCALL;
300 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
302 WARN("Managed resources are not supported by d3d9ex devices.\n");
303 return D3DERR_INVALIDCALL;
306 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
307 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
308 return D3DERR_INVALIDCALL;
310 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
311 buffer->fvf = fvf;
312 buffer->usage = usage;
313 d3d9_resource_init(&buffer->resource);
315 desc.byte_width = size;
316 desc.usage = usage & WINED3DUSAGE_MASK;
317 desc.bind_flags = 0;
318 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
319 desc.misc_flags = 0;
320 desc.structure_byte_stride = 0;
322 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
324 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
325 parent_ops = &d3d9_vertexbuffer_wined3d_parent_ops;
328 wined3d_mutex_lock();
329 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
330 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
332 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
333 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
334 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
335 &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
336 wined3d_buffer_decref(buffer->wined3d_buffer);
338 wined3d_mutex_unlock();
339 if (FAILED(hr))
341 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
342 return hr;
345 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
346 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
348 return D3D_OK;
351 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
353 if (!iface)
354 return NULL;
355 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
357 return impl_from_IDirect3DVertexBuffer9(iface);
360 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
362 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
365 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
367 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
369 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
370 || IsEqualGUID(riid, &IID_IDirect3DResource9)
371 || IsEqualGUID(riid, &IID_IUnknown))
373 IDirect3DIndexBuffer9_AddRef(iface);
374 *out = iface;
375 return S_OK;
378 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
380 *out = NULL;
381 return E_NOINTERFACE;
384 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
386 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
387 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
389 TRACE("%p increasing refcount to %u.\n", iface, refcount);
391 if (refcount == 1)
393 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
394 wined3d_mutex_lock();
395 if (buffer->draw_buffer)
396 wined3d_buffer_incref(buffer->draw_buffer);
397 else
398 wined3d_buffer_incref(buffer->wined3d_buffer);
399 wined3d_mutex_unlock();
402 return refcount;
405 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
407 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
408 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
410 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
412 if (!refcount)
414 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
415 IDirect3DDevice9Ex *device = buffer->parent_device;
417 wined3d_mutex_lock();
418 if (draw_buffer)
419 wined3d_buffer_decref(draw_buffer);
420 else
421 wined3d_buffer_decref(buffer->wined3d_buffer);
422 wined3d_mutex_unlock();
424 /* Release the device last, as it may cause the device to be destroyed. */
425 IDirect3DDevice9Ex_Release(device);
428 return refcount;
431 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
433 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
435 TRACE("iface %p, device %p.\n", iface, device);
437 *device = (IDirect3DDevice9 *)buffer->parent_device;
438 IDirect3DDevice9_AddRef(*device);
440 TRACE("Returning device %p.\n", *device);
442 return D3D_OK;
445 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
446 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
448 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
449 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
450 iface, debugstr_guid(guid), data, data_size, flags);
452 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
455 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
456 REFGUID guid, void *data, DWORD *data_size)
458 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
459 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
460 iface, debugstr_guid(guid), data, data_size);
462 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
465 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
467 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
468 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
470 return d3d9_resource_free_private_data(&buffer->resource, guid);
473 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
475 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
476 struct wined3d_resource *resource;
477 DWORD previous;
479 TRACE("iface %p, priority %u.\n", iface, priority);
481 wined3d_mutex_lock();
482 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
483 previous = wined3d_resource_set_priority(resource, priority);
484 wined3d_mutex_unlock();
486 return previous;
489 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
491 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
492 const struct wined3d_resource *resource;
493 DWORD priority;
495 TRACE("iface %p.\n", iface);
497 wined3d_mutex_lock();
498 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
499 priority = wined3d_resource_get_priority(resource);
500 wined3d_mutex_unlock();
502 return priority;
505 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
507 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
509 TRACE("iface %p.\n", iface);
511 wined3d_mutex_lock();
512 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
513 wined3d_mutex_unlock();
516 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
518 TRACE("iface %p.\n", iface);
520 return D3DRTYPE_INDEXBUFFER;
523 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
524 UINT offset, UINT size, void **data, DWORD flags)
526 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
527 struct wined3d_resource *wined3d_resource;
528 struct wined3d_map_desc wined3d_map_desc;
529 struct wined3d_box wined3d_box = {0};
530 HRESULT hr;
532 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
533 iface, offset, size, data, flags);
535 wined3d_box.left = offset;
536 wined3d_box.right = offset + size;
537 wined3d_mutex_lock();
538 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
539 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
540 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
541 wined3d_mutex_unlock();
542 *data = wined3d_map_desc.data;
544 return hr;
547 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
549 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
551 TRACE("iface %p.\n", iface);
553 wined3d_mutex_lock();
554 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
555 wined3d_mutex_unlock();
557 return D3D_OK;
560 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
562 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
563 struct wined3d_resource_desc wined3d_desc;
564 struct wined3d_resource *wined3d_resource;
566 TRACE("iface %p, desc %p.\n", iface, desc);
568 wined3d_mutex_lock();
569 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
570 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
571 wined3d_mutex_unlock();
573 desc->Format = d3dformat_from_wined3dformat(buffer->format);
574 desc->Type = D3DRTYPE_INDEXBUFFER;
575 desc->Usage = buffer->usage;
576 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
577 desc->Size = wined3d_desc.size;
579 return D3D_OK;
582 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
584 /* IUnknown */
585 d3d9_indexbuffer_QueryInterface,
586 d3d9_indexbuffer_AddRef,
587 d3d9_indexbuffer_Release,
588 /* IDirect3DResource9 */
589 d3d9_indexbuffer_GetDevice,
590 d3d9_indexbuffer_SetPrivateData,
591 d3d9_indexbuffer_GetPrivateData,
592 d3d9_indexbuffer_FreePrivateData,
593 d3d9_indexbuffer_SetPriority,
594 d3d9_indexbuffer_GetPriority,
595 d3d9_indexbuffer_PreLoad,
596 d3d9_indexbuffer_GetType,
597 /* IDirect3DIndexBuffer9 */
598 d3d9_indexbuffer_Lock,
599 d3d9_indexbuffer_Unlock,
600 d3d9_indexbuffer_GetDesc,
603 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
605 struct d3d9_indexbuffer *buffer = parent;
607 if (buffer->draw_buffer)
608 wined3d_buffer_decref(buffer->wined3d_buffer);
609 d3d9_resource_cleanup(&buffer->resource);
610 heap_free(buffer);
613 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
615 d3d9_indexbuffer_wined3d_object_destroyed,
618 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
619 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
621 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
622 struct wined3d_buffer_desc desc;
623 HRESULT hr;
625 if (pool == D3DPOOL_SCRATCH)
626 return D3DERR_INVALIDCALL;
628 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
630 WARN("Managed resources are not supported by d3d9ex devices.\n");
631 return D3DERR_INVALIDCALL;
634 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
635 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
636 return D3DERR_INVALIDCALL;
638 desc.byte_width = size;
639 desc.usage = (usage & WINED3DUSAGE_MASK) | WINED3DUSAGE_STATICDECL;
640 desc.bind_flags = 0;
641 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
642 desc.misc_flags = 0;
643 desc.structure_byte_stride = 0;
645 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
647 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
648 parent_ops = &d3d9_indexbuffer_wined3d_parent_ops;
651 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
652 buffer->format = wined3dformat_from_d3dformat(format);
653 buffer->usage = usage;
654 d3d9_resource_init(&buffer->resource);
656 wined3d_mutex_lock();
657 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
658 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
660 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
661 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
662 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
663 &d3d9_indexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
664 wined3d_buffer_decref(buffer->wined3d_buffer);
666 wined3d_mutex_unlock();
667 if (FAILED(hr))
669 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
670 return hr;
673 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
674 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
676 return D3D_OK;
679 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
681 if (!iface)
682 return NULL;
683 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
685 return impl_from_IDirect3DIndexBuffer9(iface);