winmm: Restrict some MCI actions to the creating thread.
[wine.git] / dlls / ddraw / vertexbuffer.c
blob84c0e696c5f93aa3976ef92847b8a8e445218eb7
1 /* Direct3D Vertex Buffer
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "ddraw_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
27 static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
29 return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer_iface);
32 static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
34 return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer7_iface);
37 /*****************************************************************************
38 * IUnknown Methods
39 *****************************************************************************/
41 /*****************************************************************************
42 * IDirect3DVertexBuffer7::QueryInterface
44 * The QueryInterface Method for Vertex Buffers
45 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
47 * Params
48 * riid: Queried Interface id
49 * obj: Address to return the interface pointer
51 * Returns:
52 * S_OK on success
53 * E_NOINTERFACE if the interface wasn't found
55 *****************************************************************************/
56 static HRESULT WINAPI d3d_vertex_buffer7_QueryInterface(IDirect3DVertexBuffer7 *iface, REFIID riid, void **obj)
58 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
60 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
62 /* By default, set the object pointer to NULL */
63 *obj = NULL;
65 if ( IsEqualGUID( &IID_IUnknown, riid ) )
67 IDirect3DVertexBuffer7_AddRef(iface);
68 *obj = iface;
69 TRACE(" Creating IUnknown interface at %p.\n", *obj);
70 return S_OK;
72 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
74 IDirect3DVertexBuffer7_AddRef(iface);
75 *obj = &buffer->IDirect3DVertexBuffer_iface;
76 TRACE(" Creating IDirect3DVertexBuffer interface %p\n", *obj);
77 return S_OK;
79 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
81 IDirect3DVertexBuffer7_AddRef(iface);
82 *obj = iface;
83 TRACE(" Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
84 return S_OK;
87 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
89 return E_NOINTERFACE;
92 static HRESULT WINAPI d3d_vertex_buffer1_QueryInterface(IDirect3DVertexBuffer *iface, REFIID riid, void **obj)
94 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
96 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
98 return d3d_vertex_buffer7_QueryInterface(&buffer->IDirect3DVertexBuffer7_iface, riid, obj);
101 /*****************************************************************************
102 * IDirect3DVertexBuffer7::AddRef
104 * AddRef for Vertex Buffers
106 * Returns:
107 * The new refcount
109 *****************************************************************************/
110 static ULONG WINAPI d3d_vertex_buffer7_AddRef(IDirect3DVertexBuffer7 *iface)
112 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
113 ULONG ref = InterlockedIncrement(&buffer->ref);
115 TRACE("%p increasing refcount to %u.\n", buffer, ref);
117 return ref;
120 static ULONG WINAPI d3d_vertex_buffer1_AddRef(IDirect3DVertexBuffer *iface)
122 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
124 TRACE("iface %p.\n", iface);
126 return d3d_vertex_buffer7_AddRef(&buffer->IDirect3DVertexBuffer7_iface);
130 /*****************************************************************************
131 * IDirect3DVertexBuffer7::Release
133 * Release for Vertex Buffers
135 * Returns:
136 * The new refcount
138 *****************************************************************************/
139 static ULONG WINAPI d3d_vertex_buffer7_Release(IDirect3DVertexBuffer7 *iface)
141 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
142 ULONG ref = InterlockedDecrement(&buffer->ref);
144 TRACE("%p decreasing refcount to %u.\n", buffer, ref);
146 if (ref == 0)
148 struct wined3d_buffer *curVB = NULL;
149 UINT offset, stride;
151 /* D3D7 Vertex buffers don't stay bound in the device, they are passed
152 * as a parameter to drawPrimitiveVB. DrawPrimitiveVB sets them as the
153 * stream source in wined3d, and they should get unset there before
154 * they are destroyed. */
155 wined3d_mutex_lock();
156 wined3d_device_get_stream_source(buffer->ddraw->wined3d_device,
157 0, &curVB, &offset, &stride);
158 if (curVB == buffer->wineD3DVertexBuffer)
159 wined3d_device_set_stream_source(buffer->ddraw->wined3d_device, 0, NULL, 0, 0);
161 wined3d_vertex_declaration_decref(buffer->wineD3DVertexDeclaration);
162 wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
163 wined3d_mutex_unlock();
165 HeapFree(GetProcessHeap(), 0, buffer);
168 return ref;
171 static ULONG WINAPI d3d_vertex_buffer1_Release(IDirect3DVertexBuffer *iface)
173 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
175 TRACE("iface %p.\n", iface);
177 return d3d_vertex_buffer7_Release(&buffer->IDirect3DVertexBuffer7_iface);
180 /*****************************************************************************
181 * IDirect3DVertexBuffer Methods
182 *****************************************************************************/
184 static HRESULT d3d_vertex_buffer_create_wined3d_buffer(struct d3d_vertex_buffer *buffer, BOOL dynamic,
185 struct wined3d_buffer **wined3d_buffer)
187 DWORD usage = WINED3DUSAGE_STATICDECL;
188 enum wined3d_pool pool;
190 if (buffer->Caps & D3DVBCAPS_SYSTEMMEMORY)
191 pool = WINED3D_POOL_SYSTEM_MEM;
192 else
193 pool = WINED3D_POOL_DEFAULT;
195 if (buffer->Caps & D3DVBCAPS_WRITEONLY)
196 usage |= WINED3DUSAGE_WRITEONLY;
197 if (dynamic)
198 usage |= WINED3DUSAGE_DYNAMIC;
200 return wined3d_buffer_create_vb(buffer->ddraw->wined3d_device,
201 buffer->size, usage, pool, buffer, &ddraw_null_wined3d_parent_ops,
202 wined3d_buffer);
205 /*****************************************************************************
206 * IDirect3DVertexBuffer7::Lock
208 * Locks the vertex buffer and returns a pointer to the vertex data
209 * Locking vertex buffers is similar to locking surfaces, because Windows
210 * uses surfaces to store vertex data internally (According to the DX sdk)
212 * Params:
213 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
214 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
215 * Data: Returns a pointer to the vertex data
216 * Size: Returns the size of the buffer if not NULL
218 * Returns:
219 * D3D_OK on success
220 * DDERR_INVALIDPARAMS if Data is NULL
221 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
223 *****************************************************************************/
224 static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface,
225 DWORD flags, void **data, DWORD *data_size)
227 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
228 struct wined3d_resource_desc wined3d_desc;
229 struct wined3d_resource *wined3d_resource;
230 HRESULT hr;
231 DWORD wined3d_flags = 0;
233 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
235 /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
236 * nosyslock: Not applicable
238 if (!(flags & DDLOCK_WAIT))
239 wined3d_flags |= WINED3D_MAP_DONOTWAIT;
240 if (flags & DDLOCK_READONLY)
241 wined3d_flags |= WINED3D_MAP_READONLY;
242 if (flags & DDLOCK_NOOVERWRITE)
243 wined3d_flags |= WINED3D_MAP_NOOVERWRITE;
244 if (flags & DDLOCK_DISCARDCONTENTS)
246 wined3d_flags |= WINED3D_MAP_DISCARD;
248 if (!buffer->dynamic)
250 struct wined3d_buffer *new_buffer;
251 wined3d_mutex_lock();
252 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer);
253 if (SUCCEEDED(hr))
255 buffer->dynamic = TRUE;
256 wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
257 buffer->wineD3DVertexBuffer = new_buffer;
259 else
261 WARN("Failed to create a dynamic buffer\n");
263 wined3d_mutex_unlock();
267 wined3d_mutex_lock();
268 if (data_size)
270 /* Get the size, for returning it, and for locking */
271 wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
272 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
273 *data_size = wined3d_desc.size;
276 hr = wined3d_buffer_map(buffer->wineD3DVertexBuffer, 0, 0, (BYTE **)data, wined3d_flags);
278 wined3d_mutex_unlock();
280 return hr;
283 static HRESULT WINAPI d3d_vertex_buffer1_Lock(IDirect3DVertexBuffer *iface,
284 DWORD flags, void **data, DWORD *data_size)
286 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
288 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
290 return d3d_vertex_buffer7_Lock(&buffer->IDirect3DVertexBuffer7_iface, flags, data, data_size);
293 /*****************************************************************************
294 * IDirect3DVertexBuffer7::Unlock
296 * Unlocks a vertex Buffer
298 * Returns:
299 * D3D_OK on success
301 *****************************************************************************/
302 static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface)
304 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
306 TRACE("iface %p.\n", iface);
308 wined3d_mutex_lock();
309 wined3d_buffer_unmap(buffer->wineD3DVertexBuffer);
310 wined3d_mutex_unlock();
312 return D3D_OK;
315 static HRESULT WINAPI d3d_vertex_buffer1_Unlock(IDirect3DVertexBuffer *iface)
317 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
319 TRACE("iface %p.\n", iface);
321 return d3d_vertex_buffer7_Unlock(&buffer->IDirect3DVertexBuffer7_iface);
325 /*****************************************************************************
326 * IDirect3DVertexBuffer7::ProcessVertices
328 * Processes untransformed Vertices into a transformed or optimized vertex
329 * buffer. It can also perform other operations, such as lighting or clipping
331 * Params
332 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
333 * DestIndex: Index in the destination buffer(This), where the vertices are
334 * placed
335 * Count: Number of Vertices in the Source buffer to process
336 * SrcBuffer: Source vertex buffer
337 * SrcIndex: Index of the first vertex in the src buffer to process
338 * D3DDevice: Device to use for transformation
339 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
340 * unchaned vertices
342 * Returns:
343 * D3D_OK on success
344 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
346 *****************************************************************************/
347 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7 *iface,
348 DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer7 *src_buffer,
349 DWORD src_idx, IDirect3DDevice7 *device, DWORD flags)
351 struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer7(iface);
352 struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer);
353 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice7(device);
354 BOOL oldClip, doClip;
355 HRESULT hr;
357 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
358 iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
360 /* Vertex operations:
361 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
362 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
363 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
364 * D3DVOP_LIGHT: Lights the vertices
365 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
367 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
368 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
370 if (!(vertex_op & D3DVOP_TRANSFORM))
371 return DDERR_INVALIDPARAMS;
373 wined3d_mutex_lock();
375 /* WineD3D doesn't know d3d7 vertex operation, it uses
376 * render states instead. Set the render states according to
377 * the vertex ops
379 doClip = !!(vertex_op & D3DVOP_CLIP);
380 oldClip = wined3d_device_get_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING);
381 if (doClip != oldClip)
382 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, doClip);
384 wined3d_device_set_stream_source(device_impl->wined3d_device,
385 0, src_buffer_impl->wineD3DVertexBuffer, 0, get_flexible_vertex_size(src_buffer_impl->fvf));
386 wined3d_device_set_vertex_declaration(device_impl->wined3d_device, src_buffer_impl->wineD3DVertexDeclaration);
387 hr = wined3d_device_process_vertices(device_impl->wined3d_device, src_idx, dst_idx,
388 count, dst_buffer_impl->wineD3DVertexBuffer, NULL, flags, dst_buffer_impl->fvf);
390 /* Restore the states if needed */
391 if (doClip != oldClip)
392 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip);
394 wined3d_mutex_unlock();
396 return hr;
399 static HRESULT WINAPI d3d_vertex_buffer1_ProcessVertices(IDirect3DVertexBuffer *iface,
400 DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer *src_buffer,
401 DWORD src_idx, IDirect3DDevice3 *device, DWORD flags)
403 struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer(iface);
404 struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer(src_buffer);
405 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
407 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
408 iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
410 return d3d_vertex_buffer7_ProcessVertices(&dst_buffer_impl->IDirect3DVertexBuffer7_iface, vertex_op,
411 dst_idx, count, &src_buffer_impl->IDirect3DVertexBuffer7_iface, src_idx,
412 device_impl ? &device_impl->IDirect3DDevice7_iface : NULL, flags);
415 /*****************************************************************************
416 * IDirect3DVertexBuffer7::GetVertexBufferDesc
418 * Returns the description of a vertex buffer
420 * Params:
421 * Desc: Address to write the description to
423 * Returns
424 * DDERR_INVALIDPARAMS if Desc is NULL
425 * D3D_OK on success
427 *****************************************************************************/
428 static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc)
430 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
431 struct wined3d_resource_desc wined3d_desc;
432 struct wined3d_resource *wined3d_resource;
434 TRACE("iface %p, desc %p.\n", iface, desc);
436 if (!desc) return DDERR_INVALIDPARAMS;
438 wined3d_mutex_lock();
439 wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
440 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
441 wined3d_mutex_unlock();
443 /* Now fill the desc structure */
444 desc->dwCaps = buffer->Caps;
445 desc->dwFVF = buffer->fvf;
446 desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(buffer->fvf);
448 return D3D_OK;
451 static HRESULT WINAPI d3d_vertex_buffer1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface, D3DVERTEXBUFFERDESC *desc)
453 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
455 TRACE("iface %p, desc %p.\n", iface, desc);
457 return d3d_vertex_buffer7_GetVertexBufferDesc(&buffer->IDirect3DVertexBuffer7_iface, desc);
461 /*****************************************************************************
462 * IDirect3DVertexBuffer7::Optimize
464 * Converts an unoptimized vertex buffer into an optimized buffer
466 * Params:
467 * D3DDevice: Device for which this buffer is optimized
468 * Flags: Not used, should be set to 0
470 * Returns
471 * D3D_OK, because it's a stub
473 *****************************************************************************/
474 static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface,
475 IDirect3DDevice7 *device, DWORD flags)
477 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
478 static BOOL hide = FALSE;
480 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
482 if (!hide)
484 FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags);
485 hide = TRUE;
488 /* We could forward this call to WineD3D and take advantage
489 * of it once we use OpenGL vertex buffers
491 wined3d_mutex_lock();
492 buffer->Caps |= D3DVBCAPS_OPTIMIZED;
493 wined3d_mutex_unlock();
495 return DD_OK;
498 static HRESULT WINAPI d3d_vertex_buffer1_Optimize(IDirect3DVertexBuffer *iface,
499 IDirect3DDevice3 *device, DWORD flags)
501 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
502 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
504 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
506 return d3d_vertex_buffer7_Optimize(&buffer->IDirect3DVertexBuffer7_iface,
507 device_impl ? &device_impl->IDirect3DDevice7_iface : NULL, flags);
510 /*****************************************************************************
511 * IDirect3DVertexBuffer7::ProcessVerticesStrided
513 * This method processes untransformed strided vertices into a processed
514 * or optimized vertex buffer.
516 * For more details on the parameters, see
517 * IDirect3DVertexBuffer7::ProcessVertices
519 * Params:
520 * VertexOp: Operations to perform
521 * DestIndex: Destination index to write the vertices to
522 * Count: Number of input vertices
523 * StrideData: Array containing the input vertices
524 * VertexTypeDesc: Vertex Description or source index?????????
525 * D3DDevice: IDirect3DDevice7 to use for processing
526 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
528 * Returns
529 * D3D_OK on success, or DDERR_*
531 *****************************************************************************/
532 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
533 DWORD vertex_op, DWORD dst_idx, DWORD count, D3DDRAWPRIMITIVESTRIDEDDATA *data,
534 DWORD fvf, IDirect3DDevice7 *device, DWORD flags)
536 FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
537 iface, vertex_op, dst_idx, count, data, fvf, device, flags);
539 return DD_OK;
542 /*****************************************************************************
543 * The VTables
544 *****************************************************************************/
546 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
548 d3d_vertex_buffer7_QueryInterface,
549 d3d_vertex_buffer7_AddRef,
550 d3d_vertex_buffer7_Release,
551 d3d_vertex_buffer7_Lock,
552 d3d_vertex_buffer7_Unlock,
553 d3d_vertex_buffer7_ProcessVertices,
554 d3d_vertex_buffer7_GetVertexBufferDesc,
555 d3d_vertex_buffer7_Optimize,
556 d3d_vertex_buffer7_ProcessVerticesStrided,
559 static const struct IDirect3DVertexBufferVtbl d3d_vertex_buffer1_vtbl =
561 d3d_vertex_buffer1_QueryInterface,
562 d3d_vertex_buffer1_AddRef,
563 d3d_vertex_buffer1_Release,
564 d3d_vertex_buffer1_Lock,
565 d3d_vertex_buffer1_Unlock,
566 d3d_vertex_buffer1_ProcessVertices,
567 d3d_vertex_buffer1_GetVertexBufferDesc,
568 d3d_vertex_buffer1_Optimize,
571 HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf,
572 struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc)
574 struct d3d_vertex_buffer *buffer;
575 HRESULT hr = D3D_OK;
577 TRACE("Vertex buffer description:\n");
578 TRACE(" dwSize %u\n", desc->dwSize);
579 TRACE(" dwCaps %#x\n", desc->dwCaps);
580 TRACE(" FVF %#x\n", desc->dwFVF);
581 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
583 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buffer));
584 if (!buffer)
585 return DDERR_OUTOFMEMORY;
587 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
588 buffer->IDirect3DVertexBuffer_iface.lpVtbl = &d3d_vertex_buffer1_vtbl;
589 buffer->ref = 1;
591 buffer->ddraw = ddraw;
592 buffer->Caps = desc->dwCaps;
593 buffer->fvf = desc->dwFVF;
594 buffer->size = get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices;
596 wined3d_mutex_lock();
598 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wineD3DVertexBuffer);
599 if (FAILED(hr))
601 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
602 if (hr == WINED3DERR_INVALIDCALL)
603 hr = DDERR_INVALIDPARAMS;
604 goto end;
607 buffer->wineD3DVertexDeclaration = ddraw_find_decl(ddraw, desc->dwFVF);
608 if (!buffer->wineD3DVertexDeclaration)
610 ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
611 wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
612 hr = DDERR_INVALIDPARAMS;
613 goto end;
615 wined3d_vertex_declaration_incref(buffer->wineD3DVertexDeclaration);
617 end:
618 wined3d_mutex_unlock();
619 if (hr == D3D_OK)
620 *vertex_buf = buffer;
621 else
622 HeapFree(GetProcessHeap(), 0, buffer);
624 return hr;
627 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
629 if (!iface)
630 return NULL;
631 assert(iface->lpVtbl == &d3d_vertex_buffer1_vtbl);
633 return impl_from_IDirect3DVertexBuffer(iface);
636 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
638 if (!iface)
639 return NULL;
640 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);
642 return impl_from_IDirect3DVertexBuffer7(iface);