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