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