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