ddraw: Use unsafe_impl_from_IDirect3DDevice7 for application provided interfaces.
[wine/multimedia.git] / dlls / ddraw / vertexbuffer.c
blob8c4d20159144de9922fc9740d87fadae7843762d
1 /* Direct3D Vertex Buffer
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DVertexBuffer COM object
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29 static inline IDirect3DVertexBufferImpl *impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
31 return CONTAINING_RECORD(iface, IDirect3DVertexBufferImpl, IDirect3DVertexBuffer_iface);
34 static inline IDirect3DVertexBufferImpl *impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
36 return CONTAINING_RECORD(iface, IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7_iface);
39 /*****************************************************************************
40 * IUnknown Methods
41 *****************************************************************************/
43 /*****************************************************************************
44 * IDirect3DVertexBuffer7::QueryInterface
46 * The QueryInterface Method for Vertex Buffers
47 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
49 * Params
50 * riid: Queried Interface id
51 * obj: Address to return the interface pointer
53 * Returns:
54 * S_OK on success
55 * E_NOINTERFACE if the interface wasn't found
57 *****************************************************************************/
58 static HRESULT WINAPI IDirect3DVertexBufferImpl_QueryInterface(IDirect3DVertexBuffer7 *iface,
59 REFIID riid, void **obj)
61 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
63 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
65 /* By default, set the object pointer to NULL */
66 *obj = NULL;
68 if ( IsEqualGUID( &IID_IUnknown, riid ) )
70 IUnknown_AddRef(iface);
71 *obj = iface;
72 TRACE(" Creating IUnknown interface at %p.\n", *obj);
73 return S_OK;
75 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
77 IUnknown_AddRef(iface);
78 *obj = &This->IDirect3DVertexBuffer_iface;
79 TRACE(" Creating IDirect3DVertexBuffer interface %p\n", *obj);
80 return S_OK;
82 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
84 IUnknown_AddRef(iface);
85 *obj = iface;
86 TRACE(" Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
87 return S_OK;
89 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
90 return E_NOINTERFACE;
93 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_QueryInterface(IDirect3DVertexBuffer *iface,
94 REFIID riid, void **obj)
96 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
98 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
100 return IDirect3DVertexBuffer7_QueryInterface(&This->IDirect3DVertexBuffer7_iface, riid, obj);
103 /*****************************************************************************
104 * IDirect3DVertexBuffer7::AddRef
106 * AddRef for Vertex Buffers
108 * Returns:
109 * The new refcount
111 *****************************************************************************/
112 static ULONG WINAPI IDirect3DVertexBufferImpl_AddRef(IDirect3DVertexBuffer7 *iface)
114 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
115 ULONG ref = InterlockedIncrement(&This->ref);
117 TRACE("%p increasing refcount to %u.\n", This, ref);
119 return ref;
122 static ULONG WINAPI IDirect3DVertexBufferImpl_1_AddRef(IDirect3DVertexBuffer *iface)
124 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
126 TRACE("iface %p.\n", iface);
128 return IDirect3DVertexBuffer7_AddRef(&This->IDirect3DVertexBuffer7_iface);
132 /*****************************************************************************
133 * IDirect3DVertexBuffer7::Release
135 * Release for Vertex Buffers
137 * Returns:
138 * The new refcount
140 *****************************************************************************/
141 static ULONG WINAPI IDirect3DVertexBufferImpl_Release(IDirect3DVertexBuffer7 *iface)
143 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
144 ULONG ref = InterlockedDecrement(&This->ref);
146 TRACE("%p decreasing refcount to %u.\n", This, ref);
148 if (ref == 0)
150 struct wined3d_buffer *curVB = NULL;
151 UINT offset, stride;
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 */
165 wined3d_vertex_declaration_decref(This->wineD3DVertexDeclaration);
166 wined3d_buffer_decref(This->wineD3DVertexBuffer);
167 LeaveCriticalSection(&ddraw_cs);
168 HeapFree(GetProcessHeap(), 0, This);
170 return 0;
172 return ref;
175 static ULONG WINAPI IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer *iface)
177 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
179 TRACE("iface %p.\n", iface);
181 return IDirect3DVertexBuffer7_Release(&This->IDirect3DVertexBuffer7_iface);
184 /*****************************************************************************
185 * IDirect3DVertexBuffer Methods
186 *****************************************************************************/
188 /*****************************************************************************
189 * IDirect3DVertexBuffer7::Lock
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)
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
201 * Returns:
202 * D3D_OK on success
203 * DDERR_INVALIDPARAMS if Data is NULL
204 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
206 *****************************************************************************/
207 static HRESULT WINAPI IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7 *iface, DWORD Flags,
208 void **Data, DWORD *Size)
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;
216 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
218 /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
219 * nosyslock: Not applicable
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;
226 EnterCriticalSection(&ddraw_cs);
227 if(Size)
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;
235 hr = wined3d_buffer_map(This->wineD3DVertexBuffer, 0, 0, (BYTE **)Data, wined3d_flags);
236 LeaveCriticalSection(&ddraw_cs);
237 return hr;
240 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface, DWORD Flags,
241 void **Data, DWORD *Size)
243 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
245 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, Flags, Data, Size);
247 return IDirect3DVertexBuffer7_Lock(&This->IDirect3DVertexBuffer7_iface, Flags, Data, Size);
250 /*****************************************************************************
251 * IDirect3DVertexBuffer7::Unlock
253 * Unlocks a vertex Buffer
255 * Returns:
256 * D3D_OK on success
258 *****************************************************************************/
259 static HRESULT WINAPI IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
261 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
263 TRACE("iface %p.\n", iface);
265 EnterCriticalSection(&ddraw_cs);
266 wined3d_buffer_unmap(This->wineD3DVertexBuffer);
267 LeaveCriticalSection(&ddraw_cs);
269 return D3D_OK;
272 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
274 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
276 TRACE("iface %p.\n", iface);
278 return IDirect3DVertexBuffer7_Unlock(&This->IDirect3DVertexBuffer7_iface);
282 /*****************************************************************************
283 * IDirect3DVertexBuffer7::ProcessVertices
285 * Processes untransformed Vertices into a transformed or optimized vertex
286 * buffer. It can also perform other operations, such as lighting or clipping
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
299 * Returns:
300 * D3D_OK on success
301 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
303 *****************************************************************************/
304 static HRESULT WINAPI IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
305 DWORD VertexOp, DWORD DestIndex, DWORD Count, IDirect3DVertexBuffer7 *SrcBuffer,
306 DWORD SrcIndex, IDirect3DDevice7 *device, DWORD Flags)
308 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
309 IDirect3DVertexBufferImpl *Src = unsafe_impl_from_IDirect3DVertexBuffer7(SrcBuffer);
310 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice7(device);
311 BOOL oldClip, doClip;
312 HRESULT hr;
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, device, Flags);
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
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.
327 if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
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
334 doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
335 wined3d_device_get_render_state(device_impl->wined3d_device, WINED3DRS_CLIPPING, (DWORD *)&oldClip);
336 if (doClip != oldClip)
337 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3DRS_CLIPPING, doClip);
339 wined3d_device_set_stream_source(device_impl->wined3d_device,
340 0, Src->wineD3DVertexBuffer, 0, get_flexible_vertex_size(Src->fvf));
341 wined3d_device_set_vertex_declaration(device_impl->wined3d_device, Src->wineD3DVertexDeclaration);
342 hr = wined3d_device_process_vertices(device_impl->wined3d_device, SrcIndex, DestIndex,
343 Count, This->wineD3DVertexBuffer, NULL, Flags, This->fvf);
345 /* Restore the states if needed */
346 if (doClip != oldClip)
347 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3DRS_CLIPPING, oldClip);
348 LeaveCriticalSection(&ddraw_cs);
349 return hr;
352 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
353 DWORD VertexOp, DWORD DestIndex, DWORD Count, IDirect3DVertexBuffer *SrcBuffer,
354 DWORD SrcIndex, IDirect3DDevice3 *device, DWORD Flags)
356 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
357 IDirect3DVertexBufferImpl *Src = unsafe_impl_from_IDirect3DVertexBuffer(SrcBuffer);
358 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
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, device, Flags);
363 return IDirect3DVertexBuffer7_ProcessVertices(&This->IDirect3DVertexBuffer7_iface, VertexOp,
364 DestIndex, Count, &Src->IDirect3DVertexBuffer7_iface, SrcIndex, (IDirect3DDevice7 *)device_impl,
365 Flags);
368 /*****************************************************************************
369 * IDirect3DVertexBuffer7::GetVertexBufferDesc
371 * Returns the description of a vertex buffer
373 * Params:
374 * Desc: Address to write the description to
376 * Returns
377 * DDERR_INVALIDPARAMS if Desc is NULL
378 * D3D_OK on success
380 *****************************************************************************/
381 static HRESULT WINAPI IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
382 D3DVERTEXBUFFERDESC *Desc)
384 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
385 struct wined3d_resource_desc wined3d_desc;
386 struct wined3d_resource *wined3d_resource;
388 TRACE("iface %p, desc %p.\n", iface, Desc);
390 if(!Desc) return DDERR_INVALIDPARAMS;
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);
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);
402 return D3D_OK;
405 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
406 D3DVERTEXBUFFERDESC *Desc)
408 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
410 TRACE("iface %p, desc %p.\n", iface, Desc);
412 return IDirect3DVertexBuffer7_GetVertexBufferDesc(&This->IDirect3DVertexBuffer7_iface, Desc);
416 /*****************************************************************************
417 * IDirect3DVertexBuffer7::Optimize
419 * Converts an unoptimized vertex buffer into an optimized buffer
421 * Params:
422 * D3DDevice: Device for which this buffer is optimized
423 * Flags: Not used, should be set to 0
425 * Returns
426 * D3D_OK, because it's a stub
428 *****************************************************************************/
429 static HRESULT WINAPI IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
430 IDirect3DDevice7 *D3DDevice, DWORD Flags)
432 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer7(iface);
433 static BOOL hide = FALSE;
435 TRACE("iface %p, device %p, flags %#x.\n", iface, D3DDevice, Flags);
437 if (!hide)
439 FIXME("iface %p, device %p, flags %#x stub!\n", iface, D3DDevice, Flags);
440 hide = TRUE;
443 /* We could forward this call to WineD3D and take advantage
444 * of it once we use OpenGL vertex buffers
446 EnterCriticalSection(&ddraw_cs);
447 This->Caps |= D3DVBCAPS_OPTIMIZED;
448 LeaveCriticalSection(&ddraw_cs);
450 return DD_OK;
453 static HRESULT WINAPI IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
454 IDirect3DDevice3 *device, DWORD Flags)
456 IDirect3DVertexBufferImpl *This = impl_from_IDirect3DVertexBuffer(iface);
457 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
459 TRACE("iface %p, device %p, flags %#x.\n", iface, device, Flags);
461 return IDirect3DVertexBuffer7_Optimize(&This->IDirect3DVertexBuffer7_iface,
462 (IDirect3DDevice7 *)device_impl, Flags);
465 /*****************************************************************************
466 * IDirect3DVertexBuffer7::ProcessVerticesStrided
468 * This method processes untransformed strided vertices into a processed
469 * or optimized vertex buffer.
471 * For more details on the parameters, see
472 * IDirect3DVertexBuffer7::ProcessVertices
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
483 * Returns
484 * D3D_OK on success, or DDERR_*
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)
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);
500 return DD_OK;
503 /*****************************************************************************
504 * The VTables
505 *****************************************************************************/
507 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
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
523 static const struct IDirect3DVertexBufferVtbl d3d_vertex_buffer1_vtbl =
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
537 HRESULT d3d_vertex_buffer_create(IDirect3DVertexBufferImpl **vertex_buf, IDirectDrawImpl *ddraw,
538 D3DVERTEXBUFFERDESC *desc)
540 IDirect3DVertexBufferImpl *buffer;
541 DWORD usage;
542 HRESULT hr = D3D_OK;
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);
550 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buffer));
551 if (!buffer)
552 return DDERR_OUTOFMEMORY;
554 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
555 buffer->IDirect3DVertexBuffer_iface.lpVtbl = &d3d_vertex_buffer1_vtbl;
556 buffer->ref = 1;
558 buffer->ddraw = ddraw;
559 buffer->Caps = desc->dwCaps;
560 buffer->fvf = desc->dwFVF;
562 usage = desc->dwCaps & D3DVBCAPS_WRITEONLY ? WINED3DUSAGE_WRITEONLY : 0;
563 usage |= WINED3DUSAGE_STATICDECL;
565 EnterCriticalSection(&ddraw_cs);
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))
573 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
574 if (hr == WINED3DERR_INVALIDCALL)
575 hr = DDERR_INVALIDPARAMS;
576 goto end;
579 buffer->wineD3DVertexDeclaration = ddraw_find_decl(ddraw, desc->dwFVF);
580 if (!buffer->wineD3DVertexDeclaration)
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;
587 wined3d_vertex_declaration_incref(buffer->wineD3DVertexDeclaration);
589 end:
590 LeaveCriticalSection(&ddraw_cs);
591 if (hr == D3D_OK)
592 *vertex_buf = buffer;
593 else
594 HeapFree(GetProcessHeap(), 0, buffer);
596 return hr;
599 IDirect3DVertexBufferImpl *unsafe_impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
601 if (!iface)
602 return NULL;
603 assert(iface->lpVtbl == &d3d_vertex_buffer1_vtbl);
605 return impl_from_IDirect3DVertexBuffer(iface);
608 IDirect3DVertexBufferImpl *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
610 if (!iface)
611 return NULL;
612 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);
614 return impl_from_IDirect3DVertexBuffer7(iface);