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"
38 #include "wine/exception.h"
44 #include "ddraw_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(d3d7
);
47 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk
);
50 /*****************************************************************************
52 *****************************************************************************/
54 /*****************************************************************************
55 * IDirect3DVertexBuffer7::QueryInterface
57 * The QueryInterface Method for Vertex Buffers
58 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
61 * riid: Queryied Interface id
62 * obj: Address to return the interface pointer
66 * E_NOINTERFACE if the interface wasn't found
68 *****************************************************************************/
70 IDirect3DVertexBufferImpl_QueryInterface(IDirect3DVertexBuffer7
*iface
,
74 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
75 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), obj
);
77 /* By default, set the object pointer to NULL */
80 if ( IsEqualGUID( &IID_IUnknown
, riid
) )
82 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This
,IDirect3DVertexBuffer7
));
84 TRACE(" Creating IUnknown interface at %p.\n", *obj
);
87 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer
, riid
) )
89 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This
,IDirect3DVertexBuffer7
));
90 *obj
= ICOM_INTERFACE(This
, IDirect3DVertexBuffer
);
91 TRACE(" Creating IDirect3DVertexBuffer interface %p\n", *obj
);
94 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7
, riid
) )
96 IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This
,IDirect3DVertexBuffer7
));
97 *obj
= ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
);
98 TRACE(" Creating IDirect3DVertexBuffer7 interface %p\n", *obj
);
101 FIXME("(%p): interface for IID %s NOT found!\n", This
, debugstr_guid(riid
));
102 return E_NOINTERFACE
;
105 static HRESULT WINAPI
106 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface(IDirect3DVertexBuffer
*iface
,
110 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
111 TRACE_(ddraw_thunk
)("(%p)->(%s,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This
, debugstr_guid(riid
), obj
);
113 return IDirect3DVertexBuffer7_QueryInterface(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
),
118 /*****************************************************************************
119 * IDirect3DVertexBuffer7::AddRef
121 * AddRef for Vertex Buffers
126 *****************************************************************************/
128 IDirect3DVertexBufferImpl_AddRef(IDirect3DVertexBuffer7
*iface
)
130 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
131 ULONG ref
= InterlockedIncrement(&This
->ref
);
133 TRACE("(%p/%p)->() incrementing from %u.\n", This
, iface
, ref
- 1);
139 Thunk_IDirect3DVertexBufferImpl_1_AddRef(IDirect3DVertexBuffer
*iface
)
141 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
142 TRACE_(ddraw_thunk
)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This
);
144 return IDirect3DVertexBuffer7_AddRef(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
));
148 /*****************************************************************************
149 * IDirect3DVertexBuffer7::Release
151 * Release for Vertex Buffers
156 *****************************************************************************/
158 IDirect3DVertexBufferImpl_Release(IDirect3DVertexBuffer7
*iface
)
160 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
161 ULONG ref
= InterlockedDecrement(&This
->ref
);
163 TRACE("(%p)->() decrementing from %u.\n", This
, ref
+ 1);
167 IWineD3DVertexBuffer
*curVB
= NULL
;
170 /* D3D7 Vertex buffers don't stay bound in the device, they are passed as a parameter
171 * to drawPrimitiveVB. DrawPrimitiveVB sets them as the stream source in wined3d,
172 * and they should get unset there before they are destroyed
174 IWineD3DDevice_GetStreamSource(This
->ddraw
->wineD3DDevice
,
175 0 /* Stream number */,
179 if(curVB
== This
->wineD3DVertexBuffer
)
181 IWineD3DDevice_SetStreamSource(This
->ddraw
->wineD3DDevice
,
182 0 /* Steam number */,
183 NULL
/* stream data */,
189 IWineD3DVertexBuffer_Release(curVB
); /* For the GetStreamSource */
193 IWineD3DVertexBuffer_Release(This
->wineD3DVertexBuffer
);
194 HeapFree(GetProcessHeap(), 0, This
);
201 Thunk_IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer
*iface
)
203 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
204 TRACE_(ddraw_thunk
)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This
);
206 return IDirect3DVertexBuffer7_Release(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
));
209 /*****************************************************************************
210 * IDirect3DVertexBuffer Methods
211 *****************************************************************************/
213 /*****************************************************************************
214 * IDirect3DVertexBuffer7::Lock
216 * Locks the vertex buffer and returns a pointer to the vertex data
217 * Locking vertex buffers is similar to locking surfaces, because Windows
218 * uses surfaces to store vertex data internally (According to the DX sdk)
221 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
222 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
223 * Data: Returns a pointer to the vertex data
224 * Size: Returns the size of the buffer if not NULL
228 * DDERR_INVALIDPARAMS if Data is NULL
229 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
231 *****************************************************************************/
232 static HRESULT WINAPI
233 IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7
*iface
,
238 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
239 WINED3DVERTEXBUFFER_DESC Desc
;
241 TRACE("(%p)->(%08x,%p,%p)\n", This
, Flags
, Data
, Size
);
245 /* Get the size, for returning it, and for locking */
246 hr
= IWineD3DVertexBuffer_GetDesc(This
->wineD3DVertexBuffer
,
250 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This
, hr
);
256 return IWineD3DVertexBuffer_Lock(This
->wineD3DVertexBuffer
,
257 0 /* OffsetToLock */,
258 0 /* SizeToLock, 0 == Full lock */,
263 static HRESULT WINAPI
264 Thunk_IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer
*iface
,
269 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
270 TRACE_(ddraw_thunk
)("(%p)->(%08x,%p,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This
, Flags
, Data
, Size
);
272 return IDirect3DVertexBuffer7_Lock(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
),
278 /*****************************************************************************
279 * IDirect3DVertexBuffer7::Unlock
281 * Unlocks a vertex Buffer
286 *****************************************************************************/
287 static HRESULT WINAPI
288 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7
*iface
)
290 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
291 TRACE("(%p)->()\n", This
);
293 /* This is easy :) */
294 return IWineD3DVertexBuffer_Unlock(This
->wineD3DVertexBuffer
);
297 static HRESULT WINAPI
298 Thunk_IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer
*iface
)
300 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
301 TRACE_(ddraw_thunk
)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This
);
303 return IDirect3DVertexBuffer7_Unlock(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
));
307 /*****************************************************************************
308 * IDirect3DVertexBuffer7::ProcessVertices
310 * Processes untransformed Vertices into a transformed or optimized vertex
311 * buffer. It can also perform other operations, such as lighting or clipping
314 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
315 * DestIndex: Index in the destination buffer(This), where the vertices are
317 * Count: Number of Vertices in the Source buffer to process
318 * SrcBuffer: Source vertex buffer
319 * SrcIndex: Index of the first vertex in the src buffer to process
320 * D3DDevice: Device to use for transformation
321 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
326 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
328 *****************************************************************************/
329 static HRESULT WINAPI
330 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7
*iface
,
334 IDirect3DVertexBuffer7
*SrcBuffer
,
336 IDirect3DDevice7
*D3DDevice
,
339 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
340 IDirect3DVertexBufferImpl
*Src
= ICOM_OBJECT(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, SrcBuffer
);
341 IDirect3DDeviceImpl
*D3D
= ICOM_OBJECT(IDirect3DDeviceImpl
, IDirect3DDevice7
, D3DDevice
);
342 BOOL oldClip
, doClip
;
345 TRACE("(%p)->(%08x,%d,%d,%p,%d,%p,%08x)\n", This
, VertexOp
, DestIndex
, Count
, Src
, SrcIndex
, D3D
, Flags
);
347 /* Vertex operations:
348 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
349 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
350 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
351 * D3DVOP_LIGHT: Lights the vertices
352 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
354 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
355 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
357 if( !(VertexOp
& D3DVOP_TRANSFORM
) ) return DDERR_INVALIDPARAMS
;
359 /* WineD3D doesn't know d3d7 vertex operation, it uses
360 * render states instead. Set the render states according to
363 doClip
= VertexOp
& D3DVOP_CLIP
? TRUE
: FALSE
;
364 IWineD3DDevice_GetRenderState(D3D
->wineD3DDevice
,
367 if(doClip
!= oldClip
)
369 IWineD3DDevice_SetRenderState(D3D
->wineD3DDevice
,
375 hr
= IWineD3DDevice_ProcessVertices(D3D
->wineD3DDevice
,
379 This
->wineD3DVertexBuffer
,
380 Src
->wineD3DVertexBuffer
,
383 /* Restore the states if needed */
384 if(doClip
!= oldClip
)
385 IWineD3DDevice_SetRenderState(D3D
->wineD3DDevice
,
391 static HRESULT WINAPI
392 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer
*iface
,
396 IDirect3DVertexBuffer
*SrcBuffer
,
398 IDirect3DDevice3
*D3DDevice
,
401 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
402 IDirect3DVertexBufferImpl
*Src
= ICOM_OBJECT(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, SrcBuffer
);
403 IDirect3DDeviceImpl
*D3D
= ICOM_OBJECT(IDirect3DDeviceImpl
, IDirect3DDevice3
, D3DDevice
);
405 TRACE_(ddraw_thunk
)("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This
, VertexOp
, DestIndex
, Count
, Src
, SrcIndex
, D3D
, Flags
);
407 return IDirect3DVertexBuffer7_ProcessVertices(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
),
411 ICOM_INTERFACE(Src
, IDirect3DVertexBuffer7
),
413 ICOM_INTERFACE(D3D
, IDirect3DDevice7
),
417 /*****************************************************************************
418 * IDirect3DVertexBuffer7::GetVertexBufferDesc
420 * Returns the description of a vertex buffer
423 * Desc: Address to write the description to
426 * DDERR_INVALIDPARAMS if Desc is NULL
429 *****************************************************************************/
430 static HRESULT WINAPI
431 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7
*iface
,
432 D3DVERTEXBUFFERDESC
*Desc
)
434 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
435 WINED3DVERTEXBUFFER_DESC WDesc
;
438 TRACE("(%p)->(%p)\n", This
, Desc
);
440 if(!Desc
) return DDERR_INVALIDPARAMS
;
442 hr
= IWineD3DVertexBuffer_GetDesc(This
->wineD3DVertexBuffer
,
446 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This
, hr
);
450 /* Clear the return value of garbage */
452 memset(Desc
, 0, size
);
454 /* Now fill the Desc structure */
456 Desc
->dwCaps
= This
->Caps
;
457 Desc
->dwFVF
= WDesc
.FVF
;
458 Desc
->dwNumVertices
= WDesc
.Size
/ get_flexible_vertex_size(WDesc
.FVF
);
463 static HRESULT WINAPI
464 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer
*iface
,
465 D3DVERTEXBUFFERDESC
*Desc
)
467 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
468 TRACE_(ddraw_thunk
)("(%p)->(%p) thunking to IDirect3DVertexBuffer7 interface.\n", This
, Desc
);
470 return IDirect3DVertexBuffer7_GetVertexBufferDesc(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
),
475 /*****************************************************************************
476 * IDirect3DVertexBuffer7::Optimize
478 * Converts an unoptimized vertex buffer into an optimized buffer
481 * D3DDevice: Device for which this buffer is optimized
482 * Flags: Not used, should be set to 0
485 * D3D_OK, because it's a stub
487 *****************************************************************************/
488 static HRESULT WINAPI
489 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7
*iface
,
490 IDirect3DDevice7
*D3DDevice
,
493 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
494 IDirect3DDeviceImpl
*D3D
= ICOM_OBJECT(IDirect3DDeviceImpl
, IDirect3DDevice7
, D3DDevice
);
495 FIXME("(%p)->(%p,%08x): stub!\n", This
, D3D
, Flags
);
497 /* We could forward this call to WineD3D and take advantage
498 * of it once we use OpenGL vertex buffers
500 This
->Caps
|= D3DVBCAPS_OPTIMIZED
;
505 static HRESULT WINAPI
506 Thunk_IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer
*iface
,
507 IDirect3DDevice3
*D3DDevice
,
510 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer
, iface
);
511 IDirect3DDeviceImpl
*D3D
= ICOM_OBJECT(IDirect3DDeviceImpl
, IDirect3DDevice3
, D3DDevice
);
512 TRACE_(ddraw_thunk
)("(%p)->(%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This
, D3D
, Flags
);
514 return IDirect3DVertexBuffer7_Optimize(ICOM_INTERFACE(This
, IDirect3DVertexBuffer7
),
515 ICOM_INTERFACE(D3D
, IDirect3DDevice7
),
519 /*****************************************************************************
520 * IDirect3DVertexBuffer7::ProcessVerticesStrided
522 * This method processes untransformed strided vertices into a processed
523 * or optimized vertex buffer.
525 * For more details on the parameters, see
526 * IDirect3DVertexBuffer7::ProcessVertices
529 * VertexOp: Operations to perform
530 * DestIndex: Destination index to write the vertices to
531 * Count: Number of input vertices
532 * StrideData: Array containing the input vertices
533 * VertexTypeDesc: Vertex Description or source index?????????
534 * D3DDevice: IDirect3DDevice7 to use for processing
535 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
538 * D3D_OK on success, or DDERR_*
540 *****************************************************************************/
541 static HRESULT WINAPI
542 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7
*iface
,
546 D3DDRAWPRIMITIVESTRIDEDDATA
*StrideData
,
547 DWORD VertexTypeDesc
,
548 IDirect3DDevice7
*D3DDevice
,
551 ICOM_THIS_FROM(IDirect3DVertexBufferImpl
, IDirect3DVertexBuffer7
, iface
);
552 IDirect3DDeviceImpl
*D3D
= ICOM_OBJECT(IDirect3DDeviceImpl
, IDirect3DDevice7
, D3DDevice
);
553 FIXME("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x): stub!\n", This
, VertexOp
, DestIndex
, Count
, StrideData
, VertexTypeDesc
, D3D
, Flags
);
557 /*****************************************************************************
559 *****************************************************************************/
561 const IDirect3DVertexBuffer7Vtbl IDirect3DVertexBuffer7_Vtbl
=
563 /*** IUnknown Methods ***/
564 IDirect3DVertexBufferImpl_QueryInterface
,
565 IDirect3DVertexBufferImpl_AddRef
,
566 IDirect3DVertexBufferImpl_Release
,
567 /*** IDirect3DVertexBuffer Methods ***/
568 IDirect3DVertexBufferImpl_Lock
,
569 IDirect3DVertexBufferImpl_Unlock
,
570 IDirect3DVertexBufferImpl_ProcessVertices
,
571 IDirect3DVertexBufferImpl_GetVertexBufferDesc
,
572 IDirect3DVertexBufferImpl_Optimize
,
573 /*** IDirect3DVertexBuffer7 Methods ***/
574 IDirect3DVertexBufferImpl_ProcessVerticesStrided
577 const IDirect3DVertexBufferVtbl IDirect3DVertexBuffer1_Vtbl
=
579 /*** IUnknown Methods ***/
580 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface
,
581 Thunk_IDirect3DVertexBufferImpl_1_AddRef
,
582 Thunk_IDirect3DVertexBufferImpl_1_Release
,
583 /*** IDirect3DVertexBuffer Methods ***/
584 Thunk_IDirect3DVertexBufferImpl_1_Lock
,
585 Thunk_IDirect3DVertexBufferImpl_1_Unlock
,
586 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices
,
587 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc
,
588 Thunk_IDirect3DVertexBufferImpl_1_Optimize