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);
253 LeaveCriticalSection(&ddraw_cs);
259 hr = IWineD3DVertexBuffer_Lock(This->wineD3DVertexBuffer,
260 0 /* OffsetToLock */,
261 0 /* SizeToLock, 0 == Full lock */,
264 LeaveCriticalSection(&ddraw_cs);
268 static HRESULT WINAPI
269 Thunk_IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface,
274 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
275 TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Flags, Data, Size);
277 return IDirect3DVertexBuffer7_Lock(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
283 /*****************************************************************************
284 * IDirect3DVertexBuffer7::Unlock
286 * Unlocks a vertex Buffer
291 *****************************************************************************/
292 static HRESULT WINAPI
293 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
295 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
297 TRACE("(%p)->()\n", This);
299 EnterCriticalSection(&ddraw_cs);
300 hr = IWineD3DVertexBuffer_Unlock(This->wineD3DVertexBuffer);
301 LeaveCriticalSection(&ddraw_cs);
306 static HRESULT WINAPI
307 Thunk_IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
309 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
310 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
312 return IDirect3DVertexBuffer7_Unlock(ICOM_INTERFACE(This, IDirect3DVertexBuffer7));
316 /*****************************************************************************
317 * IDirect3DVertexBuffer7::ProcessVertices
319 * Processes untransformed Vertices into a transformed or optimized vertex
320 * buffer. It can also perform other operations, such as lighting or clipping
323 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
324 * DestIndex: Index in the destination buffer(This), where the vertices are
326 * Count: Number of Vertices in the Source buffer to process
327 * SrcBuffer: Source vertex buffer
328 * SrcIndex: Index of the first vertex in the src buffer to process
329 * D3DDevice: Device to use for transformation
330 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
335 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
337 *****************************************************************************/
338 static HRESULT WINAPI
339 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
343 IDirect3DVertexBuffer7 *SrcBuffer,
345 IDirect3DDevice7 *D3DDevice,
348 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
349 IDirect3DVertexBufferImpl *Src = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, SrcBuffer);
350 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
351 BOOL oldClip, doClip;
353 WINED3DVERTEXBUFFER_DESC Desc;
355 TRACE("(%p)->(%08x,%d,%d,%p,%d,%p,%08x)\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
357 /* Vertex operations:
358 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
359 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
360 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
361 * D3DVOP_LIGHT: Lights the vertices
362 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
364 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
365 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
367 if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
369 EnterCriticalSection(&ddraw_cs);
370 /* WineD3D doesn't know d3d7 vertex operation, it uses
371 * render states instead. Set the render states according to
374 doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
375 IWineD3DDevice_GetRenderState(D3D->wineD3DDevice,
378 if(doClip != oldClip)
380 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
385 IWineD3DVertexBuffer_GetDesc(Src->wineD3DVertexBuffer,
387 IWineD3DDevice_SetStreamSource(D3D->wineD3DDevice,
389 Src->wineD3DVertexBuffer,
391 get_flexible_vertex_size(Desc.FVF));
392 IWineD3DDevice_SetVertexDeclaration(D3D->wineD3DDevice,
393 Src->wineD3DVertexDeclaration);
394 hr = IWineD3DDevice_ProcessVertices(D3D->wineD3DDevice,
398 This->wineD3DVertexBuffer,
399 NULL /* Output vdecl */,
402 /* Restore the states if needed */
403 if(doClip != oldClip)
404 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
407 LeaveCriticalSection(&ddraw_cs);
411 static HRESULT WINAPI
412 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
416 IDirect3DVertexBuffer *SrcBuffer,
418 IDirect3DDevice3 *D3DDevice,
421 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
422 IDirect3DVertexBufferImpl *Src = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, SrcBuffer);
423 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, D3DDevice);
425 TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
427 return IDirect3DVertexBuffer7_ProcessVertices(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
431 ICOM_INTERFACE(Src, IDirect3DVertexBuffer7),
433 ICOM_INTERFACE(D3D, IDirect3DDevice7),
437 /*****************************************************************************
438 * IDirect3DVertexBuffer7::GetVertexBufferDesc
440 * Returns the description of a vertex buffer
443 * Desc: Address to write the description to
446 * DDERR_INVALIDPARAMS if Desc is NULL
449 *****************************************************************************/
450 static HRESULT WINAPI
451 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
452 D3DVERTEXBUFFERDESC *Desc)
454 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
455 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 /* Now fill the Desc structure */
472 Desc->dwCaps = This->Caps;
473 Desc->dwFVF = WDesc.FVF;
474 Desc->dwNumVertices = WDesc.Size / get_flexible_vertex_size(WDesc.FVF);
475 LeaveCriticalSection(&ddraw_cs);
480 static HRESULT WINAPI
481 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
482 D3DVERTEXBUFFERDESC *Desc)
484 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
485 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Desc);
487 return IDirect3DVertexBuffer7_GetVertexBufferDesc(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
492 /*****************************************************************************
493 * IDirect3DVertexBuffer7::Optimize
495 * Converts an unoptimized vertex buffer into an optimized buffer
498 * D3DDevice: Device for which this buffer is optimized
499 * Flags: Not used, should be set to 0
502 * D3D_OK, because it's a stub
504 *****************************************************************************/
505 static HRESULT WINAPI
506 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
507 IDirect3DDevice7 *D3DDevice,
510 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
511 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
512 FIXME("(%p)->(%p,%08x): stub!\n", This, D3D, Flags);
514 /* We could forward this call to WineD3D and take advantage
515 * of it once we use OpenGL vertex buffers
517 EnterCriticalSection(&ddraw_cs);
518 This->Caps |= D3DVBCAPS_OPTIMIZED;
519 LeaveCriticalSection(&ddraw_cs);
524 static HRESULT WINAPI
525 Thunk_IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
526 IDirect3DDevice3 *D3DDevice,
529 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, iface);
530 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, D3DDevice);
531 TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, D3D, Flags);
533 return IDirect3DVertexBuffer7_Optimize(ICOM_INTERFACE(This, IDirect3DVertexBuffer7),
534 ICOM_INTERFACE(D3D, IDirect3DDevice7),
538 /*****************************************************************************
539 * IDirect3DVertexBuffer7::ProcessVerticesStrided
541 * This method processes untransformed strided vertices into a processed
542 * or optimized vertex buffer.
544 * For more details on the parameters, see
545 * IDirect3DVertexBuffer7::ProcessVertices
548 * VertexOp: Operations to perform
549 * DestIndex: Destination index to write the vertices to
550 * Count: Number of input vertices
551 * StrideData: Array containing the input vertices
552 * VertexTypeDesc: Vertex Description or source index?????????
553 * D3DDevice: IDirect3DDevice7 to use for processing
554 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
557 * D3D_OK on success, or DDERR_*
559 *****************************************************************************/
560 static HRESULT WINAPI
561 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
565 D3DDRAWPRIMITIVESTRIDEDDATA *StrideData,
566 DWORD VertexTypeDesc,
567 IDirect3DDevice7 *D3DDevice,
570 ICOM_THIS_FROM(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, iface);
571 IDirect3DDeviceImpl *D3D = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice7, D3DDevice);
572 FIXME("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x): stub!\n", This, VertexOp, DestIndex, Count, StrideData, VertexTypeDesc, D3D, Flags);
576 /*****************************************************************************
578 *****************************************************************************/
580 const IDirect3DVertexBuffer7Vtbl IDirect3DVertexBuffer7_Vtbl =
582 /*** IUnknown Methods ***/
583 IDirect3DVertexBufferImpl_QueryInterface,
584 IDirect3DVertexBufferImpl_AddRef,
585 IDirect3DVertexBufferImpl_Release,
586 /*** IDirect3DVertexBuffer Methods ***/
587 IDirect3DVertexBufferImpl_Lock,
588 IDirect3DVertexBufferImpl_Unlock,
589 IDirect3DVertexBufferImpl_ProcessVertices,
590 IDirect3DVertexBufferImpl_GetVertexBufferDesc,
591 IDirect3DVertexBufferImpl_Optimize,
592 /*** IDirect3DVertexBuffer7 Methods ***/
593 IDirect3DVertexBufferImpl_ProcessVerticesStrided
596 const IDirect3DVertexBufferVtbl IDirect3DVertexBuffer1_Vtbl =
598 /*** IUnknown Methods ***/
599 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface,
600 Thunk_IDirect3DVertexBufferImpl_1_AddRef,
601 Thunk_IDirect3DVertexBufferImpl_1_Release,
602 /*** IDirect3DVertexBuffer Methods ***/
603 Thunk_IDirect3DVertexBufferImpl_1_Lock,
604 Thunk_IDirect3DVertexBufferImpl_1_Unlock,
605 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices,
606 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc,
607 Thunk_IDirect3DVertexBufferImpl_1_Optimize