1 /* Direct3D Vertex Buffer
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/port.h"
23 #include "ddraw_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
27 static inline struct d3d_vertex_buffer
*impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7
*iface
)
29 return CONTAINING_RECORD(iface
, struct d3d_vertex_buffer
, IDirect3DVertexBuffer7_iface
);
32 /*****************************************************************************
34 *****************************************************************************/
36 static HRESULT WINAPI
d3d_vertex_buffer7_QueryInterface(IDirect3DVertexBuffer7
*iface
, REFIID riid
, void **obj
)
38 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
40 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), obj
);
44 if (IsEqualGUID(&IID_IUnknown
, riid
))
46 IDirect3DVertexBuffer7_AddRef(iface
);
50 if (IsEqualGUID(&IID_IDirect3DVertexBuffer7
, riid
) && buffer
->version
== 7)
52 IDirect3DVertexBuffer7_AddRef(iface
);
56 if (IsEqualGUID(&IID_IDirect3DVertexBuffer
, riid
) && buffer
->version
== 3)
58 IDirect3DVertexBuffer7_AddRef(iface
);
63 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
67 static ULONG WINAPI
d3d_vertex_buffer7_AddRef(IDirect3DVertexBuffer7
*iface
)
69 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
70 ULONG ref
= InterlockedIncrement(&buffer
->ref
);
72 TRACE("%p increasing refcount to %u.\n", buffer
, ref
);
77 static ULONG WINAPI
d3d_vertex_buffer7_Release(IDirect3DVertexBuffer7
*iface
)
79 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
80 ULONG ref
= InterlockedDecrement(&buffer
->ref
);
82 TRACE("%p decreasing refcount to %u.\n", buffer
, ref
);
86 struct wined3d_buffer
*vb
= NULL
;
89 /* D3D7 vertex buffers don't stay bound in the device, they are passed
90 * as a parameter to DrawPrimitiveVB. DrawPrimitiveVB sets them as the
91 * stream source in wined3d and they should get unset there before
92 * they are destroyed. */
94 wined3d_device_get_stream_source(buffer
->ddraw
->wined3d_device
, 0, &vb
, &offset
, &stride
);
95 if (vb
== buffer
->wined3d_buffer
)
96 wined3d_device_set_stream_source(buffer
->ddraw
->wined3d_device
, 0, NULL
, 0, 0);
98 wined3d_vertex_declaration_decref(buffer
->wined3d_declaration
);
99 wined3d_buffer_decref(buffer
->wined3d_buffer
);
100 wined3d_mutex_unlock();
102 if (buffer
->version
== 7)
103 IDirectDraw7_Release(&buffer
->ddraw
->IDirectDraw7_iface
);
111 /*****************************************************************************
112 * IDirect3DVertexBuffer Methods
113 *****************************************************************************/
115 static HRESULT
d3d_vertex_buffer_create_wined3d_buffer(struct d3d_vertex_buffer
*buffer
, BOOL dynamic
,
116 struct wined3d_buffer
**wined3d_buffer
)
118 struct wined3d_buffer_desc desc
;
120 desc
.byte_width
= buffer
->size
;
121 desc
.usage
= WINED3DUSAGE_STATICDECL
;
122 if (buffer
->Caps
& D3DVBCAPS_WRITEONLY
)
123 desc
.usage
|= WINED3DUSAGE_WRITEONLY
;
125 desc
.usage
|= WINED3DUSAGE_DYNAMIC
;
126 desc
.bind_flags
= WINED3D_BIND_VERTEX_BUFFER
;
127 if (buffer
->Caps
& D3DVBCAPS_SYSTEMMEMORY
)
128 desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
| WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
130 desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
132 desc
.structure_byte_stride
= 0;
134 return wined3d_buffer_create(buffer
->ddraw
->wined3d_device
, &desc
,
135 NULL
, buffer
, &ddraw_null_wined3d_parent_ops
, wined3d_buffer
);
138 /*****************************************************************************
139 * IDirect3DVertexBuffer7::Lock
141 * Locks the vertex buffer and returns a pointer to the vertex data
142 * Locking vertex buffers is similar to locking surfaces, because Windows
143 * uses surfaces to store vertex data internally (According to the DX sdk)
146 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
147 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
148 * Data: Returns a pointer to the vertex data
149 * Size: Returns the size of the buffer if not NULL
153 * DDERR_INVALIDPARAMS if Data is NULL
154 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
156 *****************************************************************************/
157 static HRESULT WINAPI
d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7
*iface
,
158 DWORD flags
, void **data
, DWORD
*data_size
)
160 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
161 struct wined3d_resource_desc wined3d_desc
;
162 struct wined3d_resource
*wined3d_resource
;
163 struct wined3d_map_desc wined3d_map_desc
;
166 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface
, flags
, data
, data_size
);
168 if (buffer
->version
!= 7)
169 flags
&= ~(DDLOCK_NOOVERWRITE
| DDLOCK_DISCARDCONTENTS
);
171 if (!(flags
& DDLOCK_WAIT
))
172 flags
|= DDLOCK_DONOTWAIT
;
173 if (flags
& DDLOCK_DISCARDCONTENTS
)
175 if (!buffer
->dynamic
)
177 struct wined3d_buffer
*new_buffer
;
178 wined3d_mutex_lock();
179 hr
= d3d_vertex_buffer_create_wined3d_buffer(buffer
, TRUE
, &new_buffer
);
182 buffer
->dynamic
= TRUE
;
183 wined3d_buffer_decref(buffer
->wined3d_buffer
);
184 buffer
->wined3d_buffer
= new_buffer
;
188 WARN("Failed to create a dynamic buffer\n");
190 wined3d_mutex_unlock();
194 wined3d_mutex_lock();
197 /* Get the size, for returning it, and for locking */
198 wined3d_resource
= wined3d_buffer_get_resource(buffer
->wined3d_buffer
);
199 wined3d_resource_get_desc(wined3d_resource
, &wined3d_desc
);
200 *data_size
= wined3d_desc
.size
;
203 hr
= wined3d_resource_map(wined3d_buffer_get_resource(buffer
->wined3d_buffer
),
204 0, &wined3d_map_desc
, NULL
, wined3dmapflags_from_ddrawmapflags(flags
));
205 *data
= wined3d_map_desc
.data
;
207 wined3d_mutex_unlock();
212 /*****************************************************************************
213 * IDirect3DVertexBuffer7::Unlock
215 * Unlocks a vertex Buffer
220 *****************************************************************************/
221 static HRESULT WINAPI
d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7
*iface
)
223 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
225 TRACE("iface %p.\n", iface
);
227 wined3d_mutex_lock();
228 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer
->wined3d_buffer
), 0);
229 wined3d_mutex_unlock();
234 /*****************************************************************************
235 * IDirect3DVertexBuffer7::ProcessVertices
237 * Processes untransformed Vertices into a transformed or optimized vertex
238 * buffer. It can also perform other operations, such as lighting or clipping
241 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
242 * DestIndex: Index in the destination buffer(This), where the vertices are
244 * Count: Number of Vertices in the Source buffer to process
245 * SrcBuffer: Source vertex buffer
246 * SrcIndex: Index of the first vertex in the src buffer to process
247 * D3DDevice: Device to use for transformation
248 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
253 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
255 *****************************************************************************/
256 static HRESULT WINAPI
d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7
*iface
,
257 DWORD vertex_op
, DWORD dst_idx
, DWORD count
, IDirect3DVertexBuffer7
*src_buffer
,
258 DWORD src_idx
, IDirect3DDevice7
*device
, DWORD flags
)
260 struct d3d_vertex_buffer
*dst_buffer_impl
= impl_from_IDirect3DVertexBuffer7(iface
);
261 struct d3d_vertex_buffer
*src_buffer_impl
= unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer
);
262 struct d3d_device
*device_impl
= dst_buffer_impl
->version
== 7
263 ? unsafe_impl_from_IDirect3DDevice7(device
)
264 : unsafe_impl_from_IDirect3DDevice3((IDirect3DDevice3
*)device
);
265 BOOL oldClip
, doClip
;
268 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
269 iface
, vertex_op
, dst_idx
, count
, src_buffer
, src_idx
, device
, flags
);
271 /* Vertex operations:
272 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
273 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
274 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
275 * D3DVOP_LIGHT: Lights the vertices
276 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
278 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
279 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
281 if (!(vertex_op
& D3DVOP_TRANSFORM
))
282 return DDERR_INVALIDPARAMS
;
284 wined3d_mutex_lock();
286 /* WineD3D doesn't know d3d7 vertex operation, it uses
287 * render states instead. Set the render states according to
290 doClip
= !!(vertex_op
& D3DVOP_CLIP
);
291 oldClip
= wined3d_device_get_render_state(device_impl
->wined3d_device
, WINED3D_RS_CLIPPING
);
292 if (doClip
!= oldClip
)
293 wined3d_device_set_render_state(device_impl
->wined3d_device
, WINED3D_RS_CLIPPING
, doClip
);
295 wined3d_device_set_stream_source(device_impl
->wined3d_device
,
296 0, src_buffer_impl
->wined3d_buffer
, 0, get_flexible_vertex_size(src_buffer_impl
->fvf
));
297 wined3d_device_set_vertex_declaration(device_impl
->wined3d_device
, src_buffer_impl
->wined3d_declaration
);
298 hr
= wined3d_device_process_vertices(device_impl
->wined3d_device
, src_idx
, dst_idx
,
299 count
, dst_buffer_impl
->wined3d_buffer
, NULL
, flags
, dst_buffer_impl
->fvf
);
301 /* Restore the states if needed */
302 if (doClip
!= oldClip
)
303 wined3d_device_set_render_state(device_impl
->wined3d_device
, WINED3D_RS_CLIPPING
, oldClip
);
305 wined3d_mutex_unlock();
310 /*****************************************************************************
311 * IDirect3DVertexBuffer7::GetVertexBufferDesc
313 * Returns the description of a vertex buffer
316 * Desc: Address to write the description to
319 * DDERR_INVALIDPARAMS if Desc is NULL
322 *****************************************************************************/
323 static HRESULT WINAPI
d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7
*iface
, D3DVERTEXBUFFERDESC
*desc
)
325 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
326 struct wined3d_resource_desc wined3d_desc
;
327 struct wined3d_resource
*wined3d_resource
;
329 TRACE("iface %p, desc %p.\n", iface
, desc
);
331 if (!desc
) return DDERR_INVALIDPARAMS
;
333 wined3d_mutex_lock();
334 wined3d_resource
= wined3d_buffer_get_resource(buffer
->wined3d_buffer
);
335 wined3d_resource_get_desc(wined3d_resource
, &wined3d_desc
);
336 wined3d_mutex_unlock();
338 /* Now fill the desc structure */
339 desc
->dwCaps
= buffer
->Caps
;
340 desc
->dwFVF
= buffer
->fvf
;
341 desc
->dwNumVertices
= wined3d_desc
.size
/ get_flexible_vertex_size(buffer
->fvf
);
346 /*****************************************************************************
347 * IDirect3DVertexBuffer7::Optimize
349 * Converts an unoptimized vertex buffer into an optimized buffer
352 * D3DDevice: Device for which this buffer is optimized
353 * Flags: Not used, should be set to 0
356 * D3D_OK, because it's a stub
358 *****************************************************************************/
359 static HRESULT WINAPI
d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7
*iface
,
360 IDirect3DDevice7
*device
, DWORD flags
)
362 struct d3d_vertex_buffer
*buffer
= impl_from_IDirect3DVertexBuffer7(iface
);
363 static BOOL hide
= FALSE
;
365 TRACE("iface %p, device %p, flags %#x.\n", iface
, device
, flags
);
369 FIXME("iface %p, device %p, flags %#x stub!\n", iface
, device
, flags
);
373 /* We could forward this call to WineD3D and take advantage
374 * of it once we use OpenGL vertex buffers
376 wined3d_mutex_lock();
377 buffer
->Caps
|= D3DVBCAPS_OPTIMIZED
;
378 wined3d_mutex_unlock();
383 /*****************************************************************************
384 * IDirect3DVertexBuffer7::ProcessVerticesStrided
386 * This method processes untransformed strided vertices into a processed
387 * or optimized vertex buffer.
389 * For more details on the parameters, see
390 * IDirect3DVertexBuffer7::ProcessVertices
393 * VertexOp: Operations to perform
394 * DestIndex: Destination index to write the vertices to
395 * Count: Number of input vertices
396 * StrideData: Array containing the input vertices
397 * VertexTypeDesc: Vertex Description or source index?????????
398 * D3DDevice: IDirect3DDevice7 to use for processing
399 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
402 * D3D_OK on success, or DDERR_*
404 *****************************************************************************/
405 static HRESULT WINAPI
d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7
*iface
,
406 DWORD vertex_op
, DWORD dst_idx
, DWORD count
, D3DDRAWPRIMITIVESTRIDEDDATA
*data
,
407 DWORD fvf
, IDirect3DDevice7
*device
, DWORD flags
)
409 FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
410 iface
, vertex_op
, dst_idx
, count
, data
, fvf
, device
, flags
);
415 /*****************************************************************************
417 *****************************************************************************/
419 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl
=
421 d3d_vertex_buffer7_QueryInterface
,
422 d3d_vertex_buffer7_AddRef
,
423 d3d_vertex_buffer7_Release
,
424 d3d_vertex_buffer7_Lock
,
425 d3d_vertex_buffer7_Unlock
,
426 d3d_vertex_buffer7_ProcessVertices
,
427 d3d_vertex_buffer7_GetVertexBufferDesc
,
428 d3d_vertex_buffer7_Optimize
,
429 d3d_vertex_buffer7_ProcessVerticesStrided
,
432 HRESULT
d3d_vertex_buffer_create(struct d3d_vertex_buffer
**vertex_buf
,
433 struct ddraw
*ddraw
, D3DVERTEXBUFFERDESC
*desc
)
435 struct d3d_vertex_buffer
*buffer
;
438 TRACE("Vertex buffer description:\n");
439 TRACE(" dwSize %u\n", desc
->dwSize
);
440 TRACE(" dwCaps %#x\n", desc
->dwCaps
);
441 TRACE(" FVF %#x\n", desc
->dwFVF
);
442 TRACE(" dwNumVertices %u\n", desc
->dwNumVertices
);
444 if (!(buffer
= heap_alloc_zero(sizeof(*buffer
))))
445 return DDERR_OUTOFMEMORY
;
447 buffer
->IDirect3DVertexBuffer7_iface
.lpVtbl
= &d3d_vertex_buffer7_vtbl
;
449 buffer
->version
= ddraw
->d3dversion
;
450 if (buffer
->version
== 7)
451 IDirectDraw7_AddRef(&ddraw
->IDirectDraw7_iface
);
452 buffer
->ddraw
= ddraw
;
453 buffer
->Caps
= desc
->dwCaps
;
454 buffer
->fvf
= desc
->dwFVF
;
455 buffer
->size
= get_flexible_vertex_size(desc
->dwFVF
) * desc
->dwNumVertices
;
457 wined3d_mutex_lock();
459 if (FAILED(hr
= d3d_vertex_buffer_create_wined3d_buffer(buffer
, FALSE
, &buffer
->wined3d_buffer
)))
461 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr
);
462 if (hr
== WINED3DERR_INVALIDCALL
)
463 hr
= DDERR_INVALIDPARAMS
;
467 if (!(buffer
->wined3d_declaration
= ddraw_find_decl(ddraw
, desc
->dwFVF
)))
469 ERR("Failed to find vertex declaration for fvf %#x.\n", desc
->dwFVF
);
470 wined3d_buffer_decref(buffer
->wined3d_buffer
);
471 hr
= DDERR_INVALIDPARAMS
;
474 wined3d_vertex_declaration_incref(buffer
->wined3d_declaration
);
477 wined3d_mutex_unlock();
479 *vertex_buf
= buffer
;
486 struct d3d_vertex_buffer
*unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7
*iface
)
490 assert(iface
->lpVtbl
== &d3d_vertex_buffer7_vtbl
);
492 return impl_from_IDirect3DVertexBuffer7(iface
);