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