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
23 #include "wine/port.h"
24 #include "wine/debug.h"
37 #include "wine/exception.h"
42 #include "ddraw_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
45 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
48 /*****************************************************************************
50 *****************************************************************************/
52 /*****************************************************************************
53 * IDirect3DVertexBuffer7::QueryInterface
55 * The QueryInterface Method for Vertex Buffers
56 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
59 * riid: Queryied Interface id
60 * obj: Address to return the interface pointer
64 * E_NOINTERFACE if the interface wasn't found
66 *****************************************************************************/
68 IDirect3DVertexBufferImpl_QueryInterface(IDirect3DVertexBuffer7 *iface,
72 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
73 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
75 /* By default, set the object pointer to NULL */
78 if ( IsEqualGUID( &IID_IUnknown, riid ) )
80 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This,IDirect3DVertexBuffer7));
82 TRACE(" Creating IUnknown interface at %p.\n", *obj);
85 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
87 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This,IDirect3DVertexBuffer7));
88 *obj = ICOM_INTERFACE(This, IDirect3DVertexBuffer);
89 TRACE(" Creating IDirect3DVertexBuffer interface %p\n", *obj);
92 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
94 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This,IDirect3DVertexBuffer7));
95 *obj = ICOM_INTERFACE(This, IDirect3DVertexBuffer7);
96 TRACE(" Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
99 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
100 return E_NOINTERFACE;
103 static HRESULT WINAPI
104 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface(IDirect3DVertexBuffer *iface,
108 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
109 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, debugstr_guid(riid), obj);
111 return IDirect3DVertexBuffer7_QueryInterface(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
116 /*****************************************************************************
117 * IDirect3DVertexBuffer7::AddRef
119 * AddRef for Vertex Buffers
124 *****************************************************************************/
126 IDirect3DVertexBufferImpl_AddRef(IDirect3DVertexBuffer7 *iface)
128 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
129 ULONG ref = InterlockedIncrement(&This->ref);
131 TRACE("(%p/%p)->() incrementing from %u.\n", This, iface, ref - 1);
137 Thunk_IDirect3DVertexBufferImpl_1_AddRef(IDirect3DVertexBuffer *iface)
139 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
140 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
142 return IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This, IDirect3DVertexBuffer7));
146 /*****************************************************************************
147 * IDirect3DVertexBuffer7::Release
149 * Release for Vertex Buffers
154 *****************************************************************************/
156 IDirect3DVertexBufferImpl_Release(IDirect3DVertexBuffer7 *iface)
158 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
159 ULONG ref = InterlockedDecrement(&This->ref);
161 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
165 IWineD3DVertexBuffer *curVB = NULL;
168 EnterCriticalSection(&ddraw_cs);
169 /* D3D7 Vertex buffers don't stay bound in the device, they are passed as a parameter
170 * to drawPrimitiveVB. DrawPrimitiveVB sets them as the stream source in wined3d,
171 * and they should get unset there before they are destroyed
173 IWineD3DDevice_GetStreamSource(This->ddraw->wineD3DDevice,
174 0 /* Stream number */,
178 if(curVB == This->wineD3DVertexBuffer)
180 IWineD3DDevice_SetStreamSource(This->ddraw->wineD3DDevice,
181 0 /* Steam number */,
182 NULL /* stream data */,
188 IWineD3DVertexBuffer_Release(curVB); /* For the GetStreamSource */
191 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
192 IWineD3DVertexBuffer_Release(This->wineD3DVertexBuffer);
193 LeaveCriticalSection(&ddraw_cs);
194 HeapFree(GetProcessHeap(), 0, This);
202 Thunk_IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer *iface)
204 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
205 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
207 return IDirect3DVertexBuffer7_Release(ICOM_INTERFACE(This, IDirect3DVertexBuffer7));
210 /*****************************************************************************
211 * IDirect3DVertexBuffer Methods
212 *****************************************************************************/
214 /*****************************************************************************
215 * IDirect3DVertexBuffer7::Lock
217 * Locks the vertex buffer and returns a pointer to the vertex data
218 * Locking vertex buffers is similar to locking surfaces, because Windows
219 * uses surfaces to store vertex data internally (According to the DX sdk)
222 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
223 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
224 * Data: Returns a pointer to the vertex data
225 * Size: Returns the size of the buffer if not NULL
229 * DDERR_INVALIDPARAMS if Data is NULL
230 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
232 *****************************************************************************/
233 static HRESULT WINAPI
234 IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7 *iface,
239 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
240 WINED3DVERTEXBUFFER_DESC Desc;
242 TRACE("(%p)->(%08x,%p,%p)\n", This, Flags, Data, Size);
244 EnterCriticalSection(&ddraw_cs);
247 /* Get the size, for returning it, and for locking */
248 hr = IWineD3DVertexBuffer_GetDesc(This->wineD3DVertexBuffer,
252 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This, hr);
258 hr = IWineD3DVertexBuffer_Lock(This->wineD3DVertexBuffer,
259 0 /* OffsetToLock */,
260 0 /* SizeToLock, 0 == Full lock */,
263 LeaveCriticalSection(&ddraw_cs);
267 static HRESULT WINAPI
268 Thunk_IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface,
273 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
274 TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Flags, Data, Size);
276 return IDirect3DVertexBuffer7_Lock(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
282 /*****************************************************************************
283 * IDirect3DVertexBuffer7::Unlock
285 * Unlocks a vertex Buffer
290 *****************************************************************************/
291 static HRESULT WINAPI
292 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
294 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
296 TRACE("(%p)->()\n", This);
298 EnterCriticalSection(&ddraw_cs);
299 hr = IWineD3DVertexBuffer_Unlock(This->wineD3DVertexBuffer);
300 LeaveCriticalSection(&ddraw_cs);
305 static HRESULT WINAPI
306 Thunk_IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
308 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
309 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
311 return IDirect3DVertexBuffer7_Unlock(ICOM_INTERFACE(This, IDirect3DVertexBuffer7));
315 /*****************************************************************************
316 * IDirect3DVertexBuffer7::ProcessVertices
318 * Processes untransformed Vertices into a transformed or optimized vertex
319 * buffer. It can also perform other operations, such as lighting or clipping
322 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
323 * DestIndex: Index in the destination buffer(This), where the vertices are
325 * Count: Number of Vertices in the Source buffer to process
326 * SrcBuffer: Source vertex buffer
327 * SrcIndex: Index of the first vertex in the src buffer to process
328 * D3DDevice: Device to use for transformation
329 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
334 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
336 *****************************************************************************/
337 static HRESULT WINAPI
338 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
342 IDirect3DVertexBuffer7 *SrcBuffer,
344 IDirect3DDevice7 *D3DDevice,
347 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
348 IDirect3DVertexBufferImpl *Src = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, SrcBuffer);
349 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
350 BOOL oldClip, doClip;
352 WINED3DVERTEXBUFFER_DESC Desc;
354 TRACE("(%p)->(%08x,%d,%d,%p,%d,%p,%08x)\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
356 /* Vertex operations:
357 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
358 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
359 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
360 * D3DVOP_LIGHT: Lights the vertices
361 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
363 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
364 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
366 if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
368 EnterCriticalSection(&ddraw_cs);
369 /* WineD3D doesn't know d3d7 vertex operation, it uses
370 * render states instead. Set the render states according to
373 doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
374 IWineD3DDevice_GetRenderState(D3D->wineD3DDevice,
377 if(doClip != oldClip)
379 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
384 IWineD3DVertexBuffer_GetDesc(Src->wineD3DVertexBuffer,
386 IWineD3DDevice_SetStreamSource(D3D->wineD3DDevice,
388 Src->wineD3DVertexBuffer,
390 get_flexible_vertex_size(Desc.FVF));
391 IWineD3DDevice_SetVertexDeclaration(D3D->wineD3DDevice,
392 Src->wineD3DVertexDeclaration);
393 hr = IWineD3DDevice_ProcessVertices(D3D->wineD3DDevice,
397 This->wineD3DVertexBuffer,
398 NULL /* Output vdecl */,
401 /* Restore the states if needed */
402 if(doClip != oldClip)
403 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
406 LeaveCriticalSection(&ddraw_cs);
410 static HRESULT WINAPI
411 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
415 IDirect3DVertexBuffer *SrcBuffer,
417 IDirect3DDevice3 *D3DDevice,
420 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
421 IDirect3DVertexBufferImpl *Src = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, SrcBuffer);
422 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, D3DDevice);
424 TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
426 return IDirect3DVertexBuffer7_ProcessVertices(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
430 ICOM_INTERFACE(Src, IDirect3DVertexBuffer7),
432 ICOM_INTERFACE(D3D, IDirect3DDevice7),
436 /*****************************************************************************
437 * IDirect3DVertexBuffer7::GetVertexBufferDesc
439 * Returns the description of a vertex buffer
442 * Desc: Address to write the description to
445 * DDERR_INVALIDPARAMS if Desc is NULL
448 *****************************************************************************/
449 static HRESULT WINAPI
450 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
451 D3DVERTEXBUFFERDESC *Desc)
453 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
454 WINED3DVERTEXBUFFER_DESC WDesc;
457 TRACE("(%p)->(%p)\n", This, Desc);
459 if(!Desc) return DDERR_INVALIDPARAMS;
461 EnterCriticalSection(&ddraw_cs);
462 hr = IWineD3DVertexBuffer_GetDesc(This->wineD3DVertexBuffer,
466 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This, hr);
467 LeaveCriticalSection(&ddraw_cs);
471 /* Clear the return value of garbage */
473 memset(Desc, 0, size);
475 /* Now fill the Desc structure */
477 Desc->dwCaps = This->Caps;
478 Desc->dwFVF = WDesc.FVF;
479 Desc->dwNumVertices = WDesc.Size / get_flexible_vertex_size(WDesc.FVF);
480 LeaveCriticalSection(&ddraw_cs);
485 static HRESULT WINAPI
486 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
487 D3DVERTEXBUFFERDESC *Desc)
489 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
490 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Desc);
492 return IDirect3DVertexBuffer7_GetVertexBufferDesc(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
497 /*****************************************************************************
498 * IDirect3DVertexBuffer7::Optimize
500 * Converts an unoptimized vertex buffer into an optimized buffer
503 * D3DDevice: Device for which this buffer is optimized
504 * Flags: Not used, should be set to 0
507 * D3D_OK, because it's a stub
509 *****************************************************************************/
510 static HRESULT WINAPI
511 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
512 IDirect3DDevice7 *D3DDevice,
515 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
516 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
517 FIXME("(%p)->(%p,%08x): stub!\n", This, D3D, Flags);
519 /* We could forward this call to WineD3D and take advantage
520 * of it once we use OpenGL vertex buffers
522 EnterCriticalSection(&ddraw_cs);
523 This->Caps |= D3DVBCAPS_OPTIMIZED;
524 LeaveCriticalSection(&ddraw_cs);
529 static HRESULT WINAPI
530 Thunk_IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
531 IDirect3DDevice3 *D3DDevice,
534 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
535 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, D3DDevice);
536 TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, D3D, Flags);
538 return IDirect3DVertexBuffer7_Optimize(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
539 ICOM_INTERFACE(D3D, IDirect3DDevice7),
543 /*****************************************************************************
544 * IDirect3DVertexBuffer7::ProcessVerticesStrided
546 * This method processes untransformed strided vertices into a processed
547 * or optimized vertex buffer.
549 * For more details on the parameters, see
550 * IDirect3DVertexBuffer7::ProcessVertices
553 * VertexOp: Operations to perform
554 * DestIndex: Destination index to write the vertices to
555 * Count: Number of input vertices
556 * StrideData: Array containing the input vertices
557 * VertexTypeDesc: Vertex Description or source index?????????
558 * D3DDevice: IDirect3DDevice7 to use for processing
559 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
562 * D3D_OK on success, or DDERR_*
564 *****************************************************************************/
565 static HRESULT WINAPI
566 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
570 D3DDRAWPRIMITIVESTRIDEDDATA *StrideData,
571 DWORD VertexTypeDesc,
572 IDirect3DDevice7 *D3DDevice,
575 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
576 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
577 FIXME("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x): stub!\n", This, VertexOp, DestIndex, Count, StrideData, VertexTypeDesc, D3D, Flags);
581 /*****************************************************************************
583 *****************************************************************************/
585 const IDirect3DVertexBuffer7Vtbl IDirect3DVertexBuffer7_Vtbl =
587 /*** IUnknown Methods ***/
588 IDirect3DVertexBufferImpl_QueryInterface,
589 IDirect3DVertexBufferImpl_AddRef,
590 IDirect3DVertexBufferImpl_Release,
591 /*** IDirect3DVertexBuffer Methods ***/
592 IDirect3DVertexBufferImpl_Lock,
593 IDirect3DVertexBufferImpl_Unlock,
594 IDirect3DVertexBufferImpl_ProcessVertices,
595 IDirect3DVertexBufferImpl_GetVertexBufferDesc,
596 IDirect3DVertexBufferImpl_Optimize,
597 /*** IDirect3DVertexBuffer7 Methods ***/
598 IDirect3DVertexBufferImpl_ProcessVerticesStrided
601 const IDirect3DVertexBufferVtbl IDirect3DVertexBuffer1_Vtbl =
603 /*** IUnknown Methods ***/
604 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface,
605 Thunk_IDirect3DVertexBufferImpl_1_AddRef,
606 Thunk_IDirect3DVertexBufferImpl_1_Release,
607 /*** IDirect3DVertexBuffer Methods ***/
608 Thunk_IDirect3DVertexBufferImpl_1_Lock,
609 Thunk_IDirect3DVertexBufferImpl_1_Unlock,
610 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices,
611 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc,
612 Thunk_IDirect3DVertexBufferImpl_1_Optimize