msdasql: Implement IColumnsRowset GetAvailableColumns.
[wine.git] / dlls / d3d9 / buffer.c
blobe6224f957635e8f5bdc2632d657c73c22b85d2f5
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 = 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 (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 wined3d_buffer_incref(buffer->wined3d_buffer);
390 return refcount;
393 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
395 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
396 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
398 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
400 if (!refcount)
402 IDirect3DDevice9Ex *device = buffer->parent_device;
404 wined3d_buffer_decref(buffer->wined3d_buffer);
406 /* Release the device last, as it may cause the device to be destroyed. */
407 IDirect3DDevice9Ex_Release(device);
410 return refcount;
413 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
415 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
417 TRACE("iface %p, device %p.\n", iface, device);
419 *device = (IDirect3DDevice9 *)buffer->parent_device;
420 IDirect3DDevice9_AddRef(*device);
422 TRACE("Returning device %p.\n", *device);
424 return D3D_OK;
427 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
428 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
430 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
431 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
432 iface, debugstr_guid(guid), data, data_size, flags);
434 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
437 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
438 REFGUID guid, void *data, DWORD *data_size)
440 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
441 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
442 iface, debugstr_guid(guid), data, data_size);
444 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
447 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
449 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
450 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
452 return d3d9_resource_free_private_data(&buffer->resource, guid);
455 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
457 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
458 struct wined3d_resource *resource;
459 DWORD previous;
461 TRACE("iface %p, priority %u.\n", iface, priority);
463 wined3d_mutex_lock();
464 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
465 previous = wined3d_resource_set_priority(resource, priority);
466 wined3d_mutex_unlock();
468 return previous;
471 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
473 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
474 const struct wined3d_resource *resource;
475 DWORD priority;
477 TRACE("iface %p.\n", iface);
479 wined3d_mutex_lock();
480 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
481 priority = wined3d_resource_get_priority(resource);
482 wined3d_mutex_unlock();
484 return priority;
487 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
489 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
491 TRACE("iface %p.\n", iface);
493 wined3d_mutex_lock();
494 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
495 wined3d_mutex_unlock();
498 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
500 TRACE("iface %p.\n", iface);
502 return D3DRTYPE_INDEXBUFFER;
505 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
506 UINT offset, UINT size, void **data, DWORD flags)
508 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
509 struct wined3d_resource *wined3d_resource;
510 struct wined3d_map_desc wined3d_map_desc;
511 struct wined3d_box wined3d_box;
512 HRESULT hr;
514 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
515 iface, offset, size, data, flags);
517 wined3d_box_set(&wined3d_box, offset, 0, offset + size, 1, 0, 1);
519 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
520 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
521 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
522 *data = wined3d_map_desc.data;
524 return hr;
527 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
529 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
531 TRACE("iface %p.\n", iface);
533 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
535 return D3D_OK;
538 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
540 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
541 struct wined3d_resource_desc wined3d_desc;
542 struct wined3d_resource *wined3d_resource;
544 TRACE("iface %p, desc %p.\n", iface, desc);
546 wined3d_mutex_lock();
547 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
548 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
549 wined3d_mutex_unlock();
551 desc->Format = d3dformat_from_wined3dformat(buffer->format);
552 desc->Type = D3DRTYPE_INDEXBUFFER;
553 desc->Usage = buffer->usage;
554 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
555 desc->Size = wined3d_desc.size;
557 return D3D_OK;
560 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
562 /* IUnknown */
563 d3d9_indexbuffer_QueryInterface,
564 d3d9_indexbuffer_AddRef,
565 d3d9_indexbuffer_Release,
566 /* IDirect3DResource9 */
567 d3d9_indexbuffer_GetDevice,
568 d3d9_indexbuffer_SetPrivateData,
569 d3d9_indexbuffer_GetPrivateData,
570 d3d9_indexbuffer_FreePrivateData,
571 d3d9_indexbuffer_SetPriority,
572 d3d9_indexbuffer_GetPriority,
573 d3d9_indexbuffer_PreLoad,
574 d3d9_indexbuffer_GetType,
575 /* IDirect3DIndexBuffer9 */
576 d3d9_indexbuffer_Lock,
577 d3d9_indexbuffer_Unlock,
578 d3d9_indexbuffer_GetDesc,
581 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
583 struct d3d9_indexbuffer *buffer = parent;
585 d3d9_resource_cleanup(&buffer->resource);
586 heap_free(buffer);
589 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
591 d3d9_indexbuffer_wined3d_object_destroyed,
594 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
595 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
597 struct wined3d_buffer_desc desc;
598 HRESULT hr;
600 if (pool == D3DPOOL_SCRATCH)
601 return D3DERR_INVALIDCALL;
603 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
605 WARN("Managed resources are not supported by d3d9ex devices.\n");
606 return D3DERR_INVALIDCALL;
609 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
610 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
611 return D3DERR_INVALIDCALL;
613 desc.byte_width = size;
614 desc.usage = wined3d_usage_from_d3d(pool, usage) | WINED3DUSAGE_STATICDECL;
615 desc.bind_flags = 0;
616 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
617 /* Buffers are always readable. */
618 if (pool != D3DPOOL_DEFAULT)
619 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
620 desc.misc_flags = 0;
621 desc.structure_byte_stride = 0;
623 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
624 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
626 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
627 buffer->format = wined3dformat_from_d3dformat(format);
628 buffer->usage = usage;
629 buffer->sysmem = !(desc.access & WINED3D_RESOURCE_ACCESS_GPU);
630 d3d9_resource_init(&buffer->resource);
632 wined3d_mutex_lock();
633 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
634 &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
635 wined3d_mutex_unlock();
636 if (FAILED(hr))
638 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
639 return hr;
642 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
643 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
645 return D3D_OK;
648 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
650 if (!iface)
651 return NULL;
652 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
654 return impl_from_IDirect3DIndexBuffer9(iface);