ole32: Cleanup return value handling in CoWaitForMultipleHandles.
[wine] / dlls / ddraw / vertexbuffer.c
1 /* Direct3D Vertex Buffer
2  * Copyright (c) 2002 Lionel ULMER
3  * Copyright (c) 2006 Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DVertexBuffer COM object
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include "ddraw_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28
29 /*****************************************************************************
30  * IUnknown Methods
31  *****************************************************************************/
32
33 /*****************************************************************************
34  * IDirect3DVertexBuffer7::QueryInterface
35  *
36  * The QueryInterface Method for Vertex Buffers
37  * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
38  *
39  * Params
40  *  riid: Queryied Interface id
41  *  obj: Address to return the interface pointer
42  *
43  * Returns:
44  *  S_OK on success
45  *  E_NOINTERFACE if the interface wasn't found
46  *
47  *****************************************************************************/
48 static HRESULT WINAPI
49 IDirect3DVertexBufferImpl_QueryInterface(IDirect3DVertexBuffer7 *iface,
50                                          REFIID riid,
51                                          void  **obj)
52 {
53     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
54
55     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
56
57     /* By default, set the object pointer to NULL */
58     *obj = NULL;
59
60     if ( IsEqualGUID( &IID_IUnknown,  riid ) )
61     {
62         IUnknown_AddRef(iface);
63         *obj = iface;
64         TRACE("  Creating IUnknown interface at %p.\n", *obj);
65         return S_OK;
66     }
67     if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
68     {
69         IUnknown_AddRef(iface);
70         *obj = &This->IDirect3DVertexBuffer_vtbl;
71         TRACE("  Creating IDirect3DVertexBuffer interface %p\n", *obj);
72         return S_OK;
73     }
74     if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
75     {
76         IUnknown_AddRef(iface);
77         *obj = iface;
78         TRACE("  Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
79         return S_OK;
80     }
81     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
82     return E_NOINTERFACE;
83 }
84
85 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_QueryInterface(IDirect3DVertexBuffer *iface,
86         REFIID riid, void **obj)
87 {
88     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
89
90     return IDirect3DVertexBuffer7_QueryInterface((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), riid, obj);
91 }
92
93 /*****************************************************************************
94  * IDirect3DVertexBuffer7::AddRef
95  *
96  * AddRef for Vertex Buffers
97  *
98  * Returns:
99  *  The new refcount
100  *
101  *****************************************************************************/
102 static ULONG WINAPI
103 IDirect3DVertexBufferImpl_AddRef(IDirect3DVertexBuffer7 *iface)
104 {
105     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
106     ULONG ref = InterlockedIncrement(&This->ref);
107
108     TRACE("%p increasing refcount to %u.\n", This, ref);
109
110     return ref;
111 }
112
113 static ULONG WINAPI IDirect3DVertexBufferImpl_1_AddRef(IDirect3DVertexBuffer *iface)
114 {
115     TRACE("iface %p.\n", iface);
116
117     return IDirect3DVertexBuffer7_AddRef((IDirect3DVertexBuffer7 *)vb_from_vb1(iface));
118 }
119
120
121 /*****************************************************************************
122  * IDirect3DVertexBuffer7::Release
123  *
124  * Release for Vertex Buffers
125  *
126  * Returns:
127  *  The new refcount
128  *
129  *****************************************************************************/
130 static ULONG WINAPI
131 IDirect3DVertexBufferImpl_Release(IDirect3DVertexBuffer7 *iface)
132 {
133     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
134     ULONG ref = InterlockedDecrement(&This->ref);
135
136     TRACE("%p decreasing refcount to %u.\n", This, ref);
137
138     if (ref == 0)
139     {
140         IWineD3DBuffer *curVB = NULL;
141         UINT offset, stride;
142
143         EnterCriticalSection(&ddraw_cs);
144         /* D3D7 Vertex buffers don't stay bound in the device, they are passed as a parameter
145          * to drawPrimitiveVB. DrawPrimitiveVB sets them as the stream source in wined3d,
146          * and they should get unset there before they are destroyed
147          */
148         IWineD3DDevice_GetStreamSource(This->ddraw->wineD3DDevice,
149                                        0 /* Stream number */,
150                                        &curVB,
151                                        &offset,
152                                        &stride);
153         if(curVB == This->wineD3DVertexBuffer)
154         {
155             IWineD3DDevice_SetStreamSource(This->ddraw->wineD3DDevice,
156                                         0 /* Steam number */,
157                                         NULL /* stream data */,
158                                         0 /* Offset */,
159                                         0 /* stride */);
160         }
161         if(curVB)
162         {
163             IWineD3DBuffer_Release(curVB); /* For the GetStreamSource */
164         }
165
166         IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
167         IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
168         LeaveCriticalSection(&ddraw_cs);
169         HeapFree(GetProcessHeap(), 0, This);
170
171         return 0;
172     }
173     return ref;
174 }
175
176 static ULONG WINAPI IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer *iface)
177 {
178     TRACE("iface %p.\n", iface);
179
180     return IDirect3DVertexBuffer7_Release((IDirect3DVertexBuffer7 *)vb_from_vb1(iface));
181 }
182
183 /*****************************************************************************
184  * IDirect3DVertexBuffer Methods
185  *****************************************************************************/
186
187 /*****************************************************************************
188  * IDirect3DVertexBuffer7::Lock
189  *
190  * Locks the vertex buffer and returns a pointer to the vertex data
191  * Locking vertex buffers is similar to locking surfaces, because Windows
192  * uses surfaces to store vertex data internally (According to the DX sdk)
193  *
194  * Params:
195  *  Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
196  *         DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
197  *  Data:  Returns a pointer to the vertex data
198  *  Size:  Returns the size of the buffer if not NULL
199  *
200  * Returns:
201  *  D3D_OK on success
202  *  DDERR_INVALIDPARAMS if Data is NULL
203  *  D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
204  *
205  *****************************************************************************/
206 static HRESULT WINAPI
207 IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7 *iface,
208                                DWORD Flags,
209                                void **Data,
210                                DWORD *Size)
211 {
212     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
213     WINED3DBUFFER_DESC Desc;
214     HRESULT hr;
215     DWORD wined3d_flags = 0;
216
217     TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
218
219     /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
220      * nosyslock: Not applicable
221      */
222     if(!(Flags & DDLOCK_WAIT))          wined3d_flags |= WINED3DLOCK_DONOTWAIT;
223     if(Flags & DDLOCK_READONLY)         wined3d_flags |= WINED3DLOCK_READONLY;
224     if(Flags & DDLOCK_NOOVERWRITE)      wined3d_flags |= WINED3DLOCK_NOOVERWRITE;
225     if(Flags & DDLOCK_DISCARDCONTENTS)  wined3d_flags |= WINED3DLOCK_DISCARD;
226
227     EnterCriticalSection(&ddraw_cs);
228     if(Size)
229     {
230         /* Get the size, for returning it, and for locking */
231         IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &Desc);
232         *Size = Desc.Size;
233     }
234
235     hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, 0 /* OffsetToLock */,
236             0 /* SizeToLock, 0 == Full lock */, (BYTE **)Data, wined3d_flags);
237     LeaveCriticalSection(&ddraw_cs);
238     return hr;
239 }
240
241 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface, DWORD Flags,
242         void **Data, DWORD *Size)
243 {
244     TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
245
246     return IDirect3DVertexBuffer7_Lock((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), Flags, Data, Size);
247 }
248
249 /*****************************************************************************
250  * IDirect3DVertexBuffer7::Unlock
251  *
252  * Unlocks a vertex Buffer
253  *
254  * Returns:
255  *  D3D_OK on success
256  *
257  *****************************************************************************/
258 static HRESULT WINAPI
259 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
260 {
261     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
262
263     TRACE("iface %p.\n", iface);
264
265     EnterCriticalSection(&ddraw_cs);
266     IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
267     LeaveCriticalSection(&ddraw_cs);
268
269     return D3D_OK;
270 }
271
272 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
273 {
274     TRACE("iface %p.\n", iface);
275
276     return IDirect3DVertexBuffer7_Unlock((IDirect3DVertexBuffer7 *)vb_from_vb1(iface));
277 }
278
279
280 /*****************************************************************************
281  * IDirect3DVertexBuffer7::ProcessVertices
282  *
283  * Processes untransformed Vertices into a transformed or optimized vertex
284  * buffer. It can also perform other operations, such as lighting or clipping
285  *
286  * Params
287  *  VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
288  *  DestIndex: Index in the destination buffer(This), where the vertices are
289  *             placed
290  *  Count: Number of Vertices in the Source buffer to process
291  *  SrcBuffer: Source vertex buffer
292  *  SrcIndex: Index of the first vertex in the src buffer to process
293  *  D3DDevice: Device to use for transformation
294  *  Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
295  *         unchaned vertices
296  *
297  * Returns:
298  *  D3D_OK on success
299  *  DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
300  *
301  *****************************************************************************/
302 static HRESULT WINAPI
303 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
304                                           DWORD VertexOp,
305                                           DWORD DestIndex,
306                                           DWORD Count,
307                                           IDirect3DVertexBuffer7 *SrcBuffer,
308                                           DWORD SrcIndex,
309                                           IDirect3DDevice7 *D3DDevice,
310                                           DWORD Flags)
311 {
312     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
313     IDirect3DVertexBufferImpl *Src = (IDirect3DVertexBufferImpl *)SrcBuffer;
314     IDirect3DDeviceImpl *D3D = (IDirect3DDeviceImpl *)D3DDevice;
315     BOOL oldClip, doClip;
316     HRESULT hr;
317
318     TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
319             iface, VertexOp, DestIndex, Count, SrcBuffer, SrcIndex, D3DDevice, Flags);
320
321     /* Vertex operations:
322      * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
323      * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
324      * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
325      * D3DVOP_LIGHT: Lights the vertices
326      * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
327      *
328      * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
329      * are not implemented. Clipping is disabled ATM, because of unsure conditions.
330      */
331     if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
332
333     EnterCriticalSection(&ddraw_cs);
334     /* WineD3D doesn't know d3d7 vertex operation, it uses
335      * render states instead. Set the render states according to
336      * the vertex ops
337      */
338     doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
339     IWineD3DDevice_GetRenderState(D3D->wineD3DDevice,
340                                   WINED3DRS_CLIPPING,
341                                   (DWORD *) &oldClip);
342     if(doClip != oldClip)
343     {
344         IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
345                                       WINED3DRS_CLIPPING,
346                                       doClip);
347     }
348
349     IWineD3DDevice_SetStreamSource(D3D->wineD3DDevice,
350                                    0, /* Stream No */
351                                    Src->wineD3DVertexBuffer,
352                                    0, /* Offset */
353                                    get_flexible_vertex_size(Src->fvf));
354     IWineD3DDevice_SetVertexDeclaration(D3D->wineD3DDevice,
355                                         Src->wineD3DVertexDeclaration);
356     hr = IWineD3DDevice_ProcessVertices(D3D->wineD3DDevice,
357                                         SrcIndex,
358                                         DestIndex,
359                                         Count,
360                                         This->wineD3DVertexBuffer,
361                                         NULL /* Output vdecl */,
362                                         Flags,
363                                         This->fvf);
364
365     /* Restore the states if needed */
366     if(doClip != oldClip)
367         IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
368                                       WINED3DRS_CLIPPING,
369                                       oldClip);
370     LeaveCriticalSection(&ddraw_cs);
371     return hr;
372 }
373
374 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
375         DWORD VertexOp, DWORD DestIndex, DWORD Count, IDirect3DVertexBuffer *SrcBuffer,
376         DWORD SrcIndex, IDirect3DDevice3 *D3DDevice, DWORD Flags)
377 {
378     IDirect3DVertexBufferImpl *Src = SrcBuffer ? vb_from_vb1(SrcBuffer) : NULL;
379     IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
380
381     TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
382             iface, VertexOp, DestIndex, Count, SrcBuffer, SrcIndex, D3DDevice, Flags);
383
384     return IDirect3DVertexBuffer7_ProcessVertices((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), VertexOp,
385             DestIndex, Count, (IDirect3DVertexBuffer7 *)Src, SrcIndex, (IDirect3DDevice7 *)D3D, Flags);
386 }
387
388 /*****************************************************************************
389  * IDirect3DVertexBuffer7::GetVertexBufferDesc
390  *
391  * Returns the description of a vertex buffer
392  *
393  * Params:
394  *  Desc: Address to write the description to
395  *
396  * Returns
397  *  DDERR_INVALIDPARAMS if Desc is NULL
398  *  D3D_OK on success
399  *
400  *****************************************************************************/
401 static HRESULT WINAPI
402 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
403                                               D3DVERTEXBUFFERDESC *Desc)
404 {
405     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
406     WINED3DBUFFER_DESC WDesc;
407
408     TRACE("iface %p, desc %p.\n", iface, Desc);
409
410     if(!Desc) return DDERR_INVALIDPARAMS;
411
412     EnterCriticalSection(&ddraw_cs);
413     IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &WDesc);
414     LeaveCriticalSection(&ddraw_cs);
415
416     /* Now fill the Desc structure */
417     Desc->dwCaps = This->Caps;
418     Desc->dwFVF = This->fvf;
419     Desc->dwNumVertices = WDesc.Size / get_flexible_vertex_size(This->fvf);
420
421     return D3D_OK;
422 }
423
424 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
425         D3DVERTEXBUFFERDESC *Desc)
426 {
427     TRACE("iface %p, desc %p.\n", iface, Desc);
428
429     return IDirect3DVertexBuffer7_GetVertexBufferDesc((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), Desc);
430 }
431
432
433 /*****************************************************************************
434  * IDirect3DVertexBuffer7::Optimize
435  *
436  * Converts an unoptimized vertex buffer into an optimized buffer
437  *
438  * Params:
439  *  D3DDevice: Device for which this buffer is optimized
440  *  Flags: Not used, should be set to 0
441  *
442  * Returns
443  *  D3D_OK, because it's a stub
444  *
445  *****************************************************************************/
446 static HRESULT WINAPI
447 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
448                                    IDirect3DDevice7 *D3DDevice,
449                                    DWORD Flags)
450 {
451     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
452     static BOOL hide = FALSE;
453
454     TRACE("iface %p, device %p, flags %#x.\n", iface, D3DDevice, Flags);
455
456     if (!hide)
457     {
458         FIXME("iface %p, device %p, flags %#x stub!\n", iface, D3DDevice, Flags);
459         hide = TRUE;
460     }
461
462     /* We could forward this call to WineD3D and take advantage
463      * of it once we use OpenGL vertex buffers
464      */
465     EnterCriticalSection(&ddraw_cs);
466     This->Caps |= D3DVBCAPS_OPTIMIZED;
467     LeaveCriticalSection(&ddraw_cs);
468
469     return DD_OK;
470 }
471
472 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
473         IDirect3DDevice3 *D3DDevice, DWORD Flags)
474 {
475     IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
476
477     TRACE("iface %p, device %p, flags %#x.\n", iface, D3DDevice, Flags);
478
479     return IDirect3DVertexBuffer7_Optimize((IDirect3DVertexBuffer7 *)vb_from_vb1(iface),
480             (IDirect3DDevice7 *)D3D, Flags);
481 }
482
483 /*****************************************************************************
484  * IDirect3DVertexBuffer7::ProcessVerticesStrided
485  *
486  * This method processes untransformed strided vertices into a processed
487  * or optimized vertex buffer.
488  *
489  * For more details on the parameters, see
490  * IDirect3DVertexBuffer7::ProcessVertices
491  *
492  * Params:
493  *  VertexOp: Operations to perform
494  *  DestIndex: Destination index to write the vertices to
495  *  Count: Number of input vertices
496  *  StrideData: Array containing the input vertices
497  *  VertexTypeDesc: Vertex Description or source index?????????
498  *  D3DDevice: IDirect3DDevice7 to use for processing
499  *  Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
500  *
501  * Returns
502  *  D3D_OK on success, or DDERR_*
503  *
504  *****************************************************************************/
505 static HRESULT WINAPI
506 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
507                                                  DWORD VertexOp,
508                                                  DWORD DestIndex,
509                                                  DWORD Count,
510                                                  D3DDRAWPRIMITIVESTRIDEDDATA *StrideData,
511                                                  DWORD VertexTypeDesc,
512                                                  IDirect3DDevice7 *D3DDevice,
513                                                  DWORD Flags)
514 {
515     FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, vertex_type %#x, device %p, flags %#x stub!\n",
516             iface, VertexOp, DestIndex, Count, StrideData, VertexTypeDesc, D3DDevice, Flags);
517
518     return DD_OK;
519 }
520
521 /*****************************************************************************
522  * The VTables
523  *****************************************************************************/
524
525 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
526 {
527     /*** IUnknown Methods ***/
528     IDirect3DVertexBufferImpl_QueryInterface,
529     IDirect3DVertexBufferImpl_AddRef,
530     IDirect3DVertexBufferImpl_Release,
531     /*** IDirect3DVertexBuffer Methods ***/
532     IDirect3DVertexBufferImpl_Lock,
533     IDirect3DVertexBufferImpl_Unlock,
534     IDirect3DVertexBufferImpl_ProcessVertices,
535     IDirect3DVertexBufferImpl_GetVertexBufferDesc,
536     IDirect3DVertexBufferImpl_Optimize,
537     /*** IDirect3DVertexBuffer7 Methods ***/
538     IDirect3DVertexBufferImpl_ProcessVerticesStrided
539 };
540
541 static const struct IDirect3DVertexBufferVtbl d3d_vertex_buffer1_vtbl =
542 {
543     /*** IUnknown Methods ***/
544     IDirect3DVertexBufferImpl_1_QueryInterface,
545     IDirect3DVertexBufferImpl_1_AddRef,
546     IDirect3DVertexBufferImpl_1_Release,
547     /*** IDirect3DVertexBuffer Methods ***/
548     IDirect3DVertexBufferImpl_1_Lock,
549     IDirect3DVertexBufferImpl_1_Unlock,
550     IDirect3DVertexBufferImpl_1_ProcessVertices,
551     IDirect3DVertexBufferImpl_1_GetVertexBufferDesc,
552     IDirect3DVertexBufferImpl_1_Optimize
553 };
554
555 HRESULT d3d_vertex_buffer_init(IDirect3DVertexBufferImpl *buffer,
556         IDirectDrawImpl *ddraw, D3DVERTEXBUFFERDESC *desc)
557 {
558     DWORD usage;
559     HRESULT hr;
560
561     buffer->lpVtbl = &d3d_vertex_buffer7_vtbl;
562     buffer->IDirect3DVertexBuffer_vtbl = &d3d_vertex_buffer1_vtbl;
563     buffer->ref = 1;
564
565     buffer->ddraw = ddraw;
566     buffer->Caps = desc->dwCaps;
567     buffer->fvf = desc->dwFVF;
568
569     usage = desc->dwCaps & D3DVBCAPS_WRITEONLY ? WINED3DUSAGE_WRITEONLY : 0;
570     usage |= WINED3DUSAGE_STATICDECL;
571
572     EnterCriticalSection(&ddraw_cs);
573
574     hr = IWineD3DDevice_CreateVertexBuffer(ddraw->wineD3DDevice,
575             get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices,
576             usage, desc->dwCaps & D3DVBCAPS_SYSTEMMEMORY ? WINED3DPOOL_SYSTEMMEM : WINED3DPOOL_DEFAULT,
577             buffer, &ddraw_null_wined3d_parent_ops, &buffer->wineD3DVertexBuffer);
578     if (FAILED(hr))
579     {
580         WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
581         LeaveCriticalSection(&ddraw_cs);
582
583         if (hr == WINED3DERR_INVALIDCALL)
584             return DDERR_INVALIDPARAMS;
585         else
586             return hr;
587     }
588
589     buffer->wineD3DVertexDeclaration = ddraw_find_decl(ddraw, desc->dwFVF);
590     if (!buffer->wineD3DVertexDeclaration)
591     {
592         ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
593         IWineD3DBuffer_Release(buffer->wineD3DVertexBuffer);
594         LeaveCriticalSection(&ddraw_cs);
595
596         return DDERR_INVALIDPARAMS;
597     }
598     IWineD3DVertexDeclaration_AddRef(buffer->wineD3DVertexDeclaration);
599
600     LeaveCriticalSection(&ddraw_cs);
601
602     return D3D_OK;
603 }