include: Make sure __int64 is correctly defined on PPC64.
[wine.git] / dlls / d3d9 / buffer.c
blob7af652e7d1a96e42f810ab52771c35f46f01c80d
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 wined3d_mutex_lock();
60 if (buffer->draw_buffer)
61 wined3d_buffer_incref(buffer->draw_buffer);
62 else
63 wined3d_buffer_incref(buffer->wined3d_buffer);
64 wined3d_mutex_unlock();
67 return refcount;
70 static ULONG WINAPI d3d9_vertexbuffer_Release(IDirect3DVertexBuffer9 *iface)
72 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
73 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
75 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
77 if (!refcount)
79 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
80 IDirect3DDevice9Ex *device = buffer->parent_device;
82 wined3d_mutex_lock();
83 if (draw_buffer)
84 wined3d_buffer_decref(draw_buffer);
85 else
86 wined3d_buffer_decref(buffer->wined3d_buffer);
87 wined3d_mutex_unlock();
89 /* Release the device last, as it may cause the device to be destroyed. */
90 IDirect3DDevice9Ex_Release(device);
93 return refcount;
96 static HRESULT WINAPI d3d9_vertexbuffer_GetDevice(IDirect3DVertexBuffer9 *iface, IDirect3DDevice9 **device)
98 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
100 TRACE("iface %p, device %p.\n", iface, device);
102 *device = (IDirect3DDevice9 *)buffer->parent_device;
103 IDirect3DDevice9_AddRef(*device);
105 TRACE("Returning device %p.\n", *device);
107 return D3D_OK;
110 static HRESULT WINAPI d3d9_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer9 *iface,
111 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
113 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
114 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
115 iface, debugstr_guid(guid), data, data_size, flags);
117 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
120 static HRESULT WINAPI d3d9_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer9 *iface,
121 REFGUID guid, void *data, DWORD *data_size)
123 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
124 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
125 iface, debugstr_guid(guid), data, data_size);
127 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
130 static HRESULT WINAPI d3d9_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer9 *iface, REFGUID guid)
132 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
133 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
135 return d3d9_resource_free_private_data(&buffer->resource, guid);
138 static DWORD WINAPI d3d9_vertexbuffer_SetPriority(IDirect3DVertexBuffer9 *iface, DWORD priority)
140 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
141 struct wined3d_resource *resource;
142 DWORD previous;
144 TRACE("iface %p, priority %u.\n", iface, priority);
146 wined3d_mutex_lock();
147 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
148 previous = wined3d_resource_set_priority(resource, priority);
149 wined3d_mutex_unlock();
151 return previous;
154 static DWORD WINAPI d3d9_vertexbuffer_GetPriority(IDirect3DVertexBuffer9 *iface)
156 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
157 const struct wined3d_resource *resource;
158 DWORD priority;
160 TRACE("iface %p.\n", iface);
162 wined3d_mutex_lock();
163 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
164 priority = wined3d_resource_get_priority(resource);
165 wined3d_mutex_unlock();
167 return priority;
170 static void WINAPI d3d9_vertexbuffer_PreLoad(IDirect3DVertexBuffer9 *iface)
172 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
174 TRACE("iface %p.\n", iface);
176 wined3d_mutex_lock();
177 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
178 wined3d_mutex_unlock();
181 static D3DRESOURCETYPE WINAPI d3d9_vertexbuffer_GetType(IDirect3DVertexBuffer9 *iface)
183 TRACE("iface %p.\n", iface);
185 return D3DRTYPE_VERTEXBUFFER;
188 static HRESULT WINAPI d3d9_vertexbuffer_Lock(IDirect3DVertexBuffer9 *iface, UINT offset, UINT size,
189 void **data, DWORD flags)
191 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
192 struct wined3d_resource *wined3d_resource;
193 struct wined3d_map_desc wined3d_map_desc;
194 struct wined3d_box wined3d_box = {0};
195 HRESULT hr;
197 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
198 iface, offset, size, data, flags);
200 wined3d_box.left = offset;
201 wined3d_box.right = offset + size;
202 wined3d_mutex_lock();
203 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
204 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
205 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
206 wined3d_mutex_unlock();
207 *data = wined3d_map_desc.data;
209 return hr;
212 static HRESULT WINAPI d3d9_vertexbuffer_Unlock(IDirect3DVertexBuffer9 *iface)
214 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
216 TRACE("iface %p.\n", iface);
218 wined3d_mutex_lock();
219 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
220 wined3d_mutex_unlock();
222 return D3D_OK;
225 static HRESULT WINAPI d3d9_vertexbuffer_GetDesc(IDirect3DVertexBuffer9 *iface,
226 D3DVERTEXBUFFER_DESC *desc)
228 struct d3d9_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer9(iface);
229 struct wined3d_resource_desc wined3d_desc;
230 struct wined3d_resource *wined3d_resource;
232 TRACE("iface %p, desc %p.\n", iface, desc);
234 wined3d_mutex_lock();
235 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
236 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
237 wined3d_mutex_unlock();
239 desc->Format = D3DFMT_VERTEXDATA;
240 desc->Type = D3DRTYPE_VERTEXBUFFER;
241 desc->Usage = buffer->usage;
242 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
243 desc->Size = wined3d_desc.size;
244 desc->FVF = buffer->fvf;
246 return D3D_OK;
249 static const IDirect3DVertexBuffer9Vtbl d3d9_vertexbuffer_vtbl =
251 /* IUnknown */
252 d3d9_vertexbuffer_QueryInterface,
253 d3d9_vertexbuffer_AddRef,
254 d3d9_vertexbuffer_Release,
255 /* IDirect3DResource9 */
256 d3d9_vertexbuffer_GetDevice,
257 d3d9_vertexbuffer_SetPrivateData,
258 d3d9_vertexbuffer_GetPrivateData,
259 d3d9_vertexbuffer_FreePrivateData,
260 d3d9_vertexbuffer_SetPriority,
261 d3d9_vertexbuffer_GetPriority,
262 d3d9_vertexbuffer_PreLoad,
263 d3d9_vertexbuffer_GetType,
264 /* IDirect3DVertexBuffer9 */
265 d3d9_vertexbuffer_Lock,
266 d3d9_vertexbuffer_Unlock,
267 d3d9_vertexbuffer_GetDesc,
270 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
272 struct d3d9_vertexbuffer *buffer = parent;
274 if (buffer->draw_buffer)
275 wined3d_buffer_decref(buffer->wined3d_buffer);
276 d3d9_resource_cleanup(&buffer->resource);
277 heap_free(buffer);
280 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
282 d3d9_vertexbuffer_wined3d_object_destroyed,
285 HRESULT vertexbuffer_init(struct d3d9_vertexbuffer *buffer, struct d3d9_device *device,
286 UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
288 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
289 struct wined3d_buffer_desc desc;
290 HRESULT hr;
292 if (pool == D3DPOOL_SCRATCH)
294 WARN("Vertex buffer with D3DPOOL_SCRATCH requested.\n");
295 return D3DERR_INVALIDCALL;
298 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
300 WARN("Managed resources are not supported by d3d9ex devices.\n");
301 return D3DERR_INVALIDCALL;
304 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
305 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
306 return D3DERR_INVALIDCALL;
308 buffer->IDirect3DVertexBuffer9_iface.lpVtbl = &d3d9_vertexbuffer_vtbl;
309 buffer->fvf = fvf;
310 buffer->usage = usage;
311 d3d9_resource_init(&buffer->resource);
313 desc.byte_width = size;
314 desc.usage = usage & WINED3DUSAGE_MASK;
315 desc.bind_flags = 0;
316 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
317 /* Buffers are always readable. */
318 if (pool != D3DPOOL_DEFAULT)
319 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
320 desc.misc_flags = 0;
321 desc.structure_byte_stride = 0;
323 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
325 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
326 parent_ops = &d3d9_vertexbuffer_wined3d_parent_ops;
329 wined3d_mutex_lock();
330 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
331 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
333 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
334 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
335 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
336 &d3d9_vertexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
337 wined3d_buffer_decref(buffer->wined3d_buffer);
339 wined3d_mutex_unlock();
340 if (FAILED(hr))
342 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
343 return hr;
346 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
347 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
349 return D3D_OK;
352 struct d3d9_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer9(IDirect3DVertexBuffer9 *iface)
354 if (!iface)
355 return NULL;
356 assert(iface->lpVtbl == &d3d9_vertexbuffer_vtbl);
358 return impl_from_IDirect3DVertexBuffer9(iface);
361 static inline struct d3d9_indexbuffer *impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
363 return CONTAINING_RECORD(iface, struct d3d9_indexbuffer, IDirect3DIndexBuffer9_iface);
366 static HRESULT WINAPI d3d9_indexbuffer_QueryInterface(IDirect3DIndexBuffer9 *iface, REFIID riid, void **out)
368 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
370 if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)
371 || IsEqualGUID(riid, &IID_IDirect3DResource9)
372 || IsEqualGUID(riid, &IID_IUnknown))
374 IDirect3DIndexBuffer9_AddRef(iface);
375 *out = iface;
376 return S_OK;
379 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
381 *out = NULL;
382 return E_NOINTERFACE;
385 static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
387 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
388 ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
390 TRACE("%p increasing refcount to %u.\n", iface, refcount);
392 if (refcount == 1)
394 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
395 wined3d_mutex_lock();
396 if (buffer->draw_buffer)
397 wined3d_buffer_incref(buffer->draw_buffer);
398 else
399 wined3d_buffer_incref(buffer->wined3d_buffer);
400 wined3d_mutex_unlock();
403 return refcount;
406 static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
408 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
409 ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
411 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
413 if (!refcount)
415 struct wined3d_buffer *draw_buffer = buffer->draw_buffer;
416 IDirect3DDevice9Ex *device = buffer->parent_device;
418 wined3d_mutex_lock();
419 if (draw_buffer)
420 wined3d_buffer_decref(draw_buffer);
421 else
422 wined3d_buffer_decref(buffer->wined3d_buffer);
423 wined3d_mutex_unlock();
425 /* Release the device last, as it may cause the device to be destroyed. */
426 IDirect3DDevice9Ex_Release(device);
429 return refcount;
432 static HRESULT WINAPI d3d9_indexbuffer_GetDevice(IDirect3DIndexBuffer9 *iface, IDirect3DDevice9 **device)
434 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
436 TRACE("iface %p, device %p.\n", iface, device);
438 *device = (IDirect3DDevice9 *)buffer->parent_device;
439 IDirect3DDevice9_AddRef(*device);
441 TRACE("Returning device %p.\n", *device);
443 return D3D_OK;
446 static HRESULT WINAPI d3d9_indexbuffer_SetPrivateData(IDirect3DIndexBuffer9 *iface,
447 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
449 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
450 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
451 iface, debugstr_guid(guid), data, data_size, flags);
453 return d3d9_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
456 static HRESULT WINAPI d3d9_indexbuffer_GetPrivateData(IDirect3DIndexBuffer9 *iface,
457 REFGUID guid, void *data, DWORD *data_size)
459 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
460 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
461 iface, debugstr_guid(guid), data, data_size);
463 return d3d9_resource_get_private_data(&buffer->resource, guid, data, data_size);
466 static HRESULT WINAPI d3d9_indexbuffer_FreePrivateData(IDirect3DIndexBuffer9 *iface, REFGUID guid)
468 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
469 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
471 return d3d9_resource_free_private_data(&buffer->resource, guid);
474 static DWORD WINAPI d3d9_indexbuffer_SetPriority(IDirect3DIndexBuffer9 *iface, DWORD priority)
476 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
477 struct wined3d_resource *resource;
478 DWORD previous;
480 TRACE("iface %p, priority %u.\n", iface, priority);
482 wined3d_mutex_lock();
483 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
484 previous = wined3d_resource_set_priority(resource, priority);
485 wined3d_mutex_unlock();
487 return previous;
490 static DWORD WINAPI d3d9_indexbuffer_GetPriority(IDirect3DIndexBuffer9 *iface)
492 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
493 const struct wined3d_resource *resource;
494 DWORD priority;
496 TRACE("iface %p.\n", iface);
498 wined3d_mutex_lock();
499 resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
500 priority = wined3d_resource_get_priority(resource);
501 wined3d_mutex_unlock();
503 return priority;
506 static void WINAPI d3d9_indexbuffer_PreLoad(IDirect3DIndexBuffer9 *iface)
508 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
510 TRACE("iface %p.\n", iface);
512 wined3d_mutex_lock();
513 wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
514 wined3d_mutex_unlock();
517 static D3DRESOURCETYPE WINAPI d3d9_indexbuffer_GetType(IDirect3DIndexBuffer9 *iface)
519 TRACE("iface %p.\n", iface);
521 return D3DRTYPE_INDEXBUFFER;
524 static HRESULT WINAPI d3d9_indexbuffer_Lock(IDirect3DIndexBuffer9 *iface,
525 UINT offset, UINT size, void **data, DWORD flags)
527 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
528 struct wined3d_resource *wined3d_resource;
529 struct wined3d_map_desc wined3d_map_desc;
530 struct wined3d_box wined3d_box = {0};
531 HRESULT hr;
533 TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
534 iface, offset, size, data, flags);
536 wined3d_box.left = offset;
537 wined3d_box.right = offset + size;
538 wined3d_mutex_lock();
539 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
540 hr = wined3d_resource_map(wined3d_resource, 0, &wined3d_map_desc, &wined3d_box,
541 wined3dmapflags_from_d3dmapflags(flags, buffer->usage));
542 wined3d_mutex_unlock();
543 *data = wined3d_map_desc.data;
545 return hr;
548 static HRESULT WINAPI d3d9_indexbuffer_Unlock(IDirect3DIndexBuffer9 *iface)
550 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
552 TRACE("iface %p.\n", iface);
554 wined3d_mutex_lock();
555 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
556 wined3d_mutex_unlock();
558 return D3D_OK;
561 static HRESULT WINAPI d3d9_indexbuffer_GetDesc(IDirect3DIndexBuffer9 *iface, D3DINDEXBUFFER_DESC *desc)
563 struct d3d9_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer9(iface);
564 struct wined3d_resource_desc wined3d_desc;
565 struct wined3d_resource *wined3d_resource;
567 TRACE("iface %p, desc %p.\n", iface, desc);
569 wined3d_mutex_lock();
570 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
571 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
572 wined3d_mutex_unlock();
574 desc->Format = d3dformat_from_wined3dformat(buffer->format);
575 desc->Type = D3DRTYPE_INDEXBUFFER;
576 desc->Usage = buffer->usage;
577 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
578 desc->Size = wined3d_desc.size;
580 return D3D_OK;
583 static const IDirect3DIndexBuffer9Vtbl d3d9_indexbuffer_vtbl =
585 /* IUnknown */
586 d3d9_indexbuffer_QueryInterface,
587 d3d9_indexbuffer_AddRef,
588 d3d9_indexbuffer_Release,
589 /* IDirect3DResource9 */
590 d3d9_indexbuffer_GetDevice,
591 d3d9_indexbuffer_SetPrivateData,
592 d3d9_indexbuffer_GetPrivateData,
593 d3d9_indexbuffer_FreePrivateData,
594 d3d9_indexbuffer_SetPriority,
595 d3d9_indexbuffer_GetPriority,
596 d3d9_indexbuffer_PreLoad,
597 d3d9_indexbuffer_GetType,
598 /* IDirect3DIndexBuffer9 */
599 d3d9_indexbuffer_Lock,
600 d3d9_indexbuffer_Unlock,
601 d3d9_indexbuffer_GetDesc,
604 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
606 struct d3d9_indexbuffer *buffer = parent;
608 if (buffer->draw_buffer)
609 wined3d_buffer_decref(buffer->wined3d_buffer);
610 d3d9_resource_cleanup(&buffer->resource);
611 heap_free(buffer);
614 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
616 d3d9_indexbuffer_wined3d_object_destroyed,
619 HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
620 UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
622 const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
623 struct wined3d_buffer_desc desc;
624 HRESULT hr;
626 if (pool == D3DPOOL_SCRATCH)
627 return D3DERR_INVALIDCALL;
629 if (pool == D3DPOOL_MANAGED && device->d3d_parent->extended)
631 WARN("Managed resources are not supported by d3d9ex devices.\n");
632 return D3DERR_INVALIDCALL;
635 /* In d3d9, buffers can't be used as rendertarget or depth/stencil buffer. */
636 if (usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
637 return D3DERR_INVALIDCALL;
639 desc.byte_width = size;
640 desc.usage = (usage & WINED3DUSAGE_MASK) | WINED3DUSAGE_STATICDECL;
641 desc.bind_flags = 0;
642 desc.access = wined3daccess_from_d3dpool(pool, usage) | map_access_from_usage(usage);
643 /* Buffers are always readable. */
644 if (pool != D3DPOOL_DEFAULT)
645 desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
646 desc.misc_flags = 0;
647 desc.structure_byte_stride = 0;
649 if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
651 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
652 parent_ops = &d3d9_indexbuffer_wined3d_parent_ops;
655 buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
656 buffer->format = wined3dformat_from_d3dformat(format);
657 buffer->usage = usage;
658 d3d9_resource_init(&buffer->resource);
660 wined3d_mutex_lock();
661 hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
662 if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
664 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
665 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
666 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
667 &d3d9_indexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
668 wined3d_buffer_decref(buffer->wined3d_buffer);
670 wined3d_mutex_unlock();
671 if (FAILED(hr))
673 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
674 return hr;
677 buffer->parent_device = &device->IDirect3DDevice9Ex_iface;
678 IDirect3DDevice9Ex_AddRef(buffer->parent_device);
680 return D3D_OK;
683 struct d3d9_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer9(IDirect3DIndexBuffer9 *iface)
685 if (!iface)
686 return NULL;
687 assert(iface->lpVtbl == &d3d9_indexbuffer_vtbl);
689 return impl_from_IDirect3DIndexBuffer9(iface);