wined3d: Merge the various resource desc structures.
[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         struct wined3d_buffer *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             wined3d_buffer_decref(curVB); /* For the GetStreamSource */
163
164         wined3d_vertex_declaration_decref(This->wineD3DVertexDeclaration);
165         wined3d_buffer_decref(This->wineD3DVertexBuffer);
166         LeaveCriticalSection(&ddraw_cs);
167         HeapFree(GetProcessHeap(), 0, This);
168
169         return 0;
170     }
171     return ref;
172 }
173
174 static ULONG WINAPI IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer *iface)
175 {
176     TRACE("iface %p.\n", iface);
177
178     return IDirect3DVertexBuffer7_Release((IDirect3DVertexBuffer7 *)vb_from_vb1(iface));
179 }
180
181 /*****************************************************************************
182  * IDirect3DVertexBuffer Methods
183  *****************************************************************************/
184
185 /*****************************************************************************
186  * IDirect3DVertexBuffer7::Lock
187  *
188  * Locks the vertex buffer and returns a pointer to the vertex data
189  * Locking vertex buffers is similar to locking surfaces, because Windows
190  * uses surfaces to store vertex data internally (According to the DX sdk)
191  *
192  * Params:
193  *  Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
194  *         DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
195  *  Data:  Returns a pointer to the vertex data
196  *  Size:  Returns the size of the buffer if not NULL
197  *
198  * Returns:
199  *  D3D_OK on success
200  *  DDERR_INVALIDPARAMS if Data is NULL
201  *  D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
202  *
203  *****************************************************************************/
204 static HRESULT WINAPI
205 IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7 *iface,
206                                DWORD Flags,
207                                void **Data,
208                                DWORD *Size)
209 {
210     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
211     struct wined3d_resource_desc wined3d_desc;
212     HRESULT hr;
213     DWORD wined3d_flags = 0;
214
215     TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
216
217     /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
218      * nosyslock: Not applicable
219      */
220     if(!(Flags & DDLOCK_WAIT))          wined3d_flags |= WINED3DLOCK_DONOTWAIT;
221     if(Flags & DDLOCK_READONLY)         wined3d_flags |= WINED3DLOCK_READONLY;
222     if(Flags & DDLOCK_NOOVERWRITE)      wined3d_flags |= WINED3DLOCK_NOOVERWRITE;
223     if(Flags & DDLOCK_DISCARDCONTENTS)  wined3d_flags |= WINED3DLOCK_DISCARD;
224
225     EnterCriticalSection(&ddraw_cs);
226     if(Size)
227     {
228         /* Get the size, for returning it, and for locking */
229         wined3d_buffer_get_desc(This->wineD3DVertexBuffer, &wined3d_desc);
230         *Size = wined3d_desc.size;
231     }
232
233     hr = wined3d_buffer_map(This->wineD3DVertexBuffer, 0, 0, (BYTE **)Data, wined3d_flags);
234     LeaveCriticalSection(&ddraw_cs);
235     return hr;
236 }
237
238 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface, DWORD Flags,
239         void **Data, DWORD *Size)
240 {
241     TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
242
243     return IDirect3DVertexBuffer7_Lock((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), Flags, Data, Size);
244 }
245
246 /*****************************************************************************
247  * IDirect3DVertexBuffer7::Unlock
248  *
249  * Unlocks a vertex Buffer
250  *
251  * Returns:
252  *  D3D_OK on success
253  *
254  *****************************************************************************/
255 static HRESULT WINAPI
256 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
257 {
258     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
259
260     TRACE("iface %p.\n", iface);
261
262     EnterCriticalSection(&ddraw_cs);
263     wined3d_buffer_unmap(This->wineD3DVertexBuffer);
264     LeaveCriticalSection(&ddraw_cs);
265
266     return D3D_OK;
267 }
268
269 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
270 {
271     TRACE("iface %p.\n", iface);
272
273     return IDirect3DVertexBuffer7_Unlock((IDirect3DVertexBuffer7 *)vb_from_vb1(iface));
274 }
275
276
277 /*****************************************************************************
278  * IDirect3DVertexBuffer7::ProcessVertices
279  *
280  * Processes untransformed Vertices into a transformed or optimized vertex
281  * buffer. It can also perform other operations, such as lighting or clipping
282  *
283  * Params
284  *  VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
285  *  DestIndex: Index in the destination buffer(This), where the vertices are
286  *             placed
287  *  Count: Number of Vertices in the Source buffer to process
288  *  SrcBuffer: Source vertex buffer
289  *  SrcIndex: Index of the first vertex in the src buffer to process
290  *  D3DDevice: Device to use for transformation
291  *  Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
292  *         unchaned vertices
293  *
294  * Returns:
295  *  D3D_OK on success
296  *  DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
297  *
298  *****************************************************************************/
299 static HRESULT WINAPI
300 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
301                                           DWORD VertexOp,
302                                           DWORD DestIndex,
303                                           DWORD Count,
304                                           IDirect3DVertexBuffer7 *SrcBuffer,
305                                           DWORD SrcIndex,
306                                           IDirect3DDevice7 *D3DDevice,
307                                           DWORD Flags)
308 {
309     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
310     IDirect3DVertexBufferImpl *Src = (IDirect3DVertexBufferImpl *)SrcBuffer;
311     IDirect3DDeviceImpl *D3D = (IDirect3DDeviceImpl *)D3DDevice;
312     BOOL oldClip, doClip;
313     HRESULT hr;
314
315     TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
316             iface, VertexOp, DestIndex, Count, SrcBuffer, SrcIndex, D3DDevice, Flags);
317
318     /* Vertex operations:
319      * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
320      * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
321      * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
322      * D3DVOP_LIGHT: Lights the vertices
323      * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
324      *
325      * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
326      * are not implemented. Clipping is disabled ATM, because of unsure conditions.
327      */
328     if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
329
330     EnterCriticalSection(&ddraw_cs);
331     /* WineD3D doesn't know d3d7 vertex operation, it uses
332      * render states instead. Set the render states according to
333      * the vertex ops
334      */
335     doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
336     IWineD3DDevice_GetRenderState(D3D->wineD3DDevice,
337                                   WINED3DRS_CLIPPING,
338                                   (DWORD *) &oldClip);
339     if(doClip != oldClip)
340     {
341         IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
342                                       WINED3DRS_CLIPPING,
343                                       doClip);
344     }
345
346     IWineD3DDevice_SetStreamSource(D3D->wineD3DDevice,
347                                    0, /* Stream No */
348                                    Src->wineD3DVertexBuffer,
349                                    0, /* Offset */
350                                    get_flexible_vertex_size(Src->fvf));
351     IWineD3DDevice_SetVertexDeclaration(D3D->wineD3DDevice,
352                                         Src->wineD3DVertexDeclaration);
353     hr = IWineD3DDevice_ProcessVertices(D3D->wineD3DDevice,
354                                         SrcIndex,
355                                         DestIndex,
356                                         Count,
357                                         This->wineD3DVertexBuffer,
358                                         NULL /* Output vdecl */,
359                                         Flags,
360                                         This->fvf);
361
362     /* Restore the states if needed */
363     if(doClip != oldClip)
364         IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
365                                       WINED3DRS_CLIPPING,
366                                       oldClip);
367     LeaveCriticalSection(&ddraw_cs);
368     return hr;
369 }
370
371 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
372         DWORD VertexOp, DWORD DestIndex, DWORD Count, IDirect3DVertexBuffer *SrcBuffer,
373         DWORD SrcIndex, IDirect3DDevice3 *D3DDevice, DWORD Flags)
374 {
375     IDirect3DVertexBufferImpl *Src = SrcBuffer ? vb_from_vb1(SrcBuffer) : NULL;
376     IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
377
378     TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
379             iface, VertexOp, DestIndex, Count, SrcBuffer, SrcIndex, D3DDevice, Flags);
380
381     return IDirect3DVertexBuffer7_ProcessVertices((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), VertexOp,
382             DestIndex, Count, (IDirect3DVertexBuffer7 *)Src, SrcIndex, (IDirect3DDevice7 *)D3D, Flags);
383 }
384
385 /*****************************************************************************
386  * IDirect3DVertexBuffer7::GetVertexBufferDesc
387  *
388  * Returns the description of a vertex buffer
389  *
390  * Params:
391  *  Desc: Address to write the description to
392  *
393  * Returns
394  *  DDERR_INVALIDPARAMS if Desc is NULL
395  *  D3D_OK on success
396  *
397  *****************************************************************************/
398 static HRESULT WINAPI
399 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
400                                               D3DVERTEXBUFFERDESC *Desc)
401 {
402     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
403     struct wined3d_resource_desc wined3d_desc;
404
405     TRACE("iface %p, desc %p.\n", iface, Desc);
406
407     if(!Desc) return DDERR_INVALIDPARAMS;
408
409     EnterCriticalSection(&ddraw_cs);
410     wined3d_buffer_get_desc(This->wineD3DVertexBuffer, &wined3d_desc);
411     LeaveCriticalSection(&ddraw_cs);
412
413     /* Now fill the Desc structure */
414     Desc->dwCaps = This->Caps;
415     Desc->dwFVF = This->fvf;
416     Desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(This->fvf);
417
418     return D3D_OK;
419 }
420
421 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
422         D3DVERTEXBUFFERDESC *Desc)
423 {
424     TRACE("iface %p, desc %p.\n", iface, Desc);
425
426     return IDirect3DVertexBuffer7_GetVertexBufferDesc((IDirect3DVertexBuffer7 *)vb_from_vb1(iface), Desc);
427 }
428
429
430 /*****************************************************************************
431  * IDirect3DVertexBuffer7::Optimize
432  *
433  * Converts an unoptimized vertex buffer into an optimized buffer
434  *
435  * Params:
436  *  D3DDevice: Device for which this buffer is optimized
437  *  Flags: Not used, should be set to 0
438  *
439  * Returns
440  *  D3D_OK, because it's a stub
441  *
442  *****************************************************************************/
443 static HRESULT WINAPI
444 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
445                                    IDirect3DDevice7 *D3DDevice,
446                                    DWORD Flags)
447 {
448     IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
449     static BOOL hide = FALSE;
450
451     TRACE("iface %p, device %p, flags %#x.\n", iface, D3DDevice, Flags);
452
453     if (!hide)
454     {
455         FIXME("iface %p, device %p, flags %#x stub!\n", iface, D3DDevice, Flags);
456         hide = TRUE;
457     }
458
459     /* We could forward this call to WineD3D and take advantage
460      * of it once we use OpenGL vertex buffers
461      */
462     EnterCriticalSection(&ddraw_cs);
463     This->Caps |= D3DVBCAPS_OPTIMIZED;
464     LeaveCriticalSection(&ddraw_cs);
465
466     return DD_OK;
467 }
468
469 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
470         IDirect3DDevice3 *D3DDevice, DWORD Flags)
471 {
472     IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
473
474     TRACE("iface %p, device %p, flags %#x.\n", iface, D3DDevice, Flags);
475
476     return IDirect3DVertexBuffer7_Optimize((IDirect3DVertexBuffer7 *)vb_from_vb1(iface),
477             (IDirect3DDevice7 *)D3D, Flags);
478 }
479
480 /*****************************************************************************
481  * IDirect3DVertexBuffer7::ProcessVerticesStrided
482  *
483  * This method processes untransformed strided vertices into a processed
484  * or optimized vertex buffer.
485  *
486  * For more details on the parameters, see
487  * IDirect3DVertexBuffer7::ProcessVertices
488  *
489  * Params:
490  *  VertexOp: Operations to perform
491  *  DestIndex: Destination index to write the vertices to
492  *  Count: Number of input vertices
493  *  StrideData: Array containing the input vertices
494  *  VertexTypeDesc: Vertex Description or source index?????????
495  *  D3DDevice: IDirect3DDevice7 to use for processing
496  *  Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
497  *
498  * Returns
499  *  D3D_OK on success, or DDERR_*
500  *
501  *****************************************************************************/
502 static HRESULT WINAPI
503 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
504                                                  DWORD VertexOp,
505                                                  DWORD DestIndex,
506                                                  DWORD Count,
507                                                  D3DDRAWPRIMITIVESTRIDEDDATA *StrideData,
508                                                  DWORD VertexTypeDesc,
509                                                  IDirect3DDevice7 *D3DDevice,
510                                                  DWORD Flags)
511 {
512     FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, vertex_type %#x, device %p, flags %#x stub!\n",
513             iface, VertexOp, DestIndex, Count, StrideData, VertexTypeDesc, D3DDevice, Flags);
514
515     return DD_OK;
516 }
517
518 /*****************************************************************************
519  * The VTables
520  *****************************************************************************/
521
522 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
523 {
524     /*** IUnknown Methods ***/
525     IDirect3DVertexBufferImpl_QueryInterface,
526     IDirect3DVertexBufferImpl_AddRef,
527     IDirect3DVertexBufferImpl_Release,
528     /*** IDirect3DVertexBuffer Methods ***/
529     IDirect3DVertexBufferImpl_Lock,
530     IDirect3DVertexBufferImpl_Unlock,
531     IDirect3DVertexBufferImpl_ProcessVertices,
532     IDirect3DVertexBufferImpl_GetVertexBufferDesc,
533     IDirect3DVertexBufferImpl_Optimize,
534     /*** IDirect3DVertexBuffer7 Methods ***/
535     IDirect3DVertexBufferImpl_ProcessVerticesStrided
536 };
537
538 static const struct IDirect3DVertexBufferVtbl d3d_vertex_buffer1_vtbl =
539 {
540     /*** IUnknown Methods ***/
541     IDirect3DVertexBufferImpl_1_QueryInterface,
542     IDirect3DVertexBufferImpl_1_AddRef,
543     IDirect3DVertexBufferImpl_1_Release,
544     /*** IDirect3DVertexBuffer Methods ***/
545     IDirect3DVertexBufferImpl_1_Lock,
546     IDirect3DVertexBufferImpl_1_Unlock,
547     IDirect3DVertexBufferImpl_1_ProcessVertices,
548     IDirect3DVertexBufferImpl_1_GetVertexBufferDesc,
549     IDirect3DVertexBufferImpl_1_Optimize
550 };
551
552 HRESULT d3d_vertex_buffer_init(IDirect3DVertexBufferImpl *buffer,
553         IDirectDrawImpl *ddraw, D3DVERTEXBUFFERDESC *desc)
554 {
555     DWORD usage;
556     HRESULT hr;
557
558     buffer->lpVtbl = &d3d_vertex_buffer7_vtbl;
559     buffer->IDirect3DVertexBuffer_vtbl = &d3d_vertex_buffer1_vtbl;
560     buffer->ref = 1;
561
562     buffer->ddraw = ddraw;
563     buffer->Caps = desc->dwCaps;
564     buffer->fvf = desc->dwFVF;
565
566     usage = desc->dwCaps & D3DVBCAPS_WRITEONLY ? WINED3DUSAGE_WRITEONLY : 0;
567     usage |= WINED3DUSAGE_STATICDECL;
568
569     EnterCriticalSection(&ddraw_cs);
570
571     hr = IWineD3DDevice_CreateVertexBuffer(ddraw->wineD3DDevice,
572             get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices,
573             usage, desc->dwCaps & D3DVBCAPS_SYSTEMMEMORY ? WINED3DPOOL_SYSTEMMEM : WINED3DPOOL_DEFAULT,
574             buffer, &ddraw_null_wined3d_parent_ops, &buffer->wineD3DVertexBuffer);
575     if (FAILED(hr))
576     {
577         WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
578         LeaveCriticalSection(&ddraw_cs);
579
580         if (hr == WINED3DERR_INVALIDCALL)
581             return DDERR_INVALIDPARAMS;
582         else
583             return hr;
584     }
585
586     buffer->wineD3DVertexDeclaration = ddraw_find_decl(ddraw, desc->dwFVF);
587     if (!buffer->wineD3DVertexDeclaration)
588     {
589         ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
590         wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
591         LeaveCriticalSection(&ddraw_cs);
592
593         return DDERR_INVALIDPARAMS;
594     }
595     wined3d_vertex_declaration_incref(buffer->wineD3DVertexDeclaration);
596
597     LeaveCriticalSection(&ddraw_cs);
598
599     return D3D_OK;
600 }