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