comctl32: Fix a typo in comment.
[wine.git] / dlls / ddraw / vertexbuffer.c
blobcc676f398590dd78be0959f932efb8e1b62fe045
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
20 #include "config.h"
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 /*****************************************************************************
33 * IUnknown Methods
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);
42 *obj = NULL;
44 if (IsEqualGUID(&IID_IUnknown, riid))
46 IDirect3DVertexBuffer7_AddRef(iface);
47 *obj = iface;
48 return S_OK;
50 if (IsEqualGUID(&IID_IDirect3DVertexBuffer7, riid) && buffer->version == 7)
52 IDirect3DVertexBuffer7_AddRef(iface);
53 *obj = iface;
54 return S_OK;
56 if (IsEqualGUID(&IID_IDirect3DVertexBuffer, riid) && buffer->version == 3)
58 IDirect3DVertexBuffer7_AddRef(iface);
59 *obj = iface;
60 return S_OK;
63 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
64 return E_NOINTERFACE;
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);
74 return 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);
84 if (!ref)
86 struct wined3d_buffer *vb = NULL;
87 UINT offset, stride;
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. */
93 wined3d_mutex_lock();
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);
105 HeapFree(GetProcessHeap(), 0, buffer);
108 return ref;
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 DWORD usage = WINED3DUSAGE_STATICDECL;
119 enum wined3d_pool pool;
121 if (buffer->Caps & D3DVBCAPS_SYSTEMMEMORY)
122 pool = WINED3D_POOL_SYSTEM_MEM;
123 else
124 pool = WINED3D_POOL_DEFAULT;
126 if (buffer->Caps & D3DVBCAPS_WRITEONLY)
127 usage |= WINED3DUSAGE_WRITEONLY;
128 if (dynamic)
129 usage |= WINED3DUSAGE_DYNAMIC;
131 return wined3d_buffer_create_vb(buffer->ddraw->wined3d_device,
132 buffer->size, usage, pool, buffer, &ddraw_null_wined3d_parent_ops,
133 wined3d_buffer);
136 /*****************************************************************************
137 * IDirect3DVertexBuffer7::Lock
139 * Locks the vertex buffer and returns a pointer to the vertex data
140 * Locking vertex buffers is similar to locking surfaces, because Windows
141 * uses surfaces to store vertex data internally (According to the DX sdk)
143 * Params:
144 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
145 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
146 * Data: Returns a pointer to the vertex data
147 * Size: Returns the size of the buffer if not NULL
149 * Returns:
150 * D3D_OK on success
151 * DDERR_INVALIDPARAMS if Data is NULL
152 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
154 *****************************************************************************/
155 static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface,
156 DWORD flags, void **data, DWORD *data_size)
158 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
159 struct wined3d_resource_desc wined3d_desc;
160 struct wined3d_resource *wined3d_resource;
161 struct wined3d_map_desc wined3d_map_desc;
162 HRESULT hr;
163 DWORD wined3d_flags = 0;
165 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
167 if (buffer->version != 7)
168 flags &= ~(DDLOCK_NOOVERWRITE | DDLOCK_DISCARDCONTENTS);
170 /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
171 * nosyslock: Not applicable
173 if (!(flags & DDLOCK_WAIT))
174 wined3d_flags |= WINED3D_MAP_DONOTWAIT;
175 if (flags & DDLOCK_READONLY)
176 wined3d_flags |= WINED3D_MAP_READONLY;
177 if (flags & DDLOCK_NOOVERWRITE)
178 wined3d_flags |= WINED3D_MAP_NOOVERWRITE;
179 if (flags & DDLOCK_DISCARDCONTENTS)
181 wined3d_flags |= WINED3D_MAP_DISCARD;
183 if (!buffer->dynamic)
185 struct wined3d_buffer *new_buffer;
186 wined3d_mutex_lock();
187 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer);
188 if (SUCCEEDED(hr))
190 buffer->dynamic = TRUE;
191 wined3d_buffer_decref(buffer->wined3d_buffer);
192 buffer->wined3d_buffer = new_buffer;
194 else
196 WARN("Failed to create a dynamic buffer\n");
198 wined3d_mutex_unlock();
202 wined3d_mutex_lock();
203 if (data_size)
205 /* Get the size, for returning it, and for locking */
206 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
207 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
208 *data_size = wined3d_desc.size;
211 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
212 0, &wined3d_map_desc, NULL, wined3d_flags);
213 *data = wined3d_map_desc.data;
215 wined3d_mutex_unlock();
217 return hr;
220 /*****************************************************************************
221 * IDirect3DVertexBuffer7::Unlock
223 * Unlocks a vertex Buffer
225 * Returns:
226 * D3D_OK on success
228 *****************************************************************************/
229 static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface)
231 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
233 TRACE("iface %p.\n", iface);
235 wined3d_mutex_lock();
236 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
237 wined3d_mutex_unlock();
239 return D3D_OK;
242 /*****************************************************************************
243 * IDirect3DVertexBuffer7::ProcessVertices
245 * Processes untransformed Vertices into a transformed or optimized vertex
246 * buffer. It can also perform other operations, such as lighting or clipping
248 * Params
249 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
250 * DestIndex: Index in the destination buffer(This), where the vertices are
251 * placed
252 * Count: Number of Vertices in the Source buffer to process
253 * SrcBuffer: Source vertex buffer
254 * SrcIndex: Index of the first vertex in the src buffer to process
255 * D3DDevice: Device to use for transformation
256 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
257 * unchaned vertices
259 * Returns:
260 * D3D_OK on success
261 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
263 *****************************************************************************/
264 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7 *iface,
265 DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer7 *src_buffer,
266 DWORD src_idx, IDirect3DDevice7 *device, DWORD flags)
268 struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer7(iface);
269 struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer);
270 struct d3d_device *device_impl = dst_buffer_impl->version == 7
271 ? unsafe_impl_from_IDirect3DDevice7(device)
272 : unsafe_impl_from_IDirect3DDevice3((IDirect3DDevice3 *)device);
273 BOOL oldClip, doClip;
274 HRESULT hr;
276 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
277 iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
279 /* Vertex operations:
280 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
281 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
282 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
283 * D3DVOP_LIGHT: Lights the vertices
284 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
286 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
287 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
289 if (!(vertex_op & D3DVOP_TRANSFORM))
290 return DDERR_INVALIDPARAMS;
292 wined3d_mutex_lock();
294 /* WineD3D doesn't know d3d7 vertex operation, it uses
295 * render states instead. Set the render states according to
296 * the vertex ops
298 doClip = !!(vertex_op & D3DVOP_CLIP);
299 oldClip = wined3d_device_get_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING);
300 if (doClip != oldClip)
301 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, doClip);
303 wined3d_device_set_stream_source(device_impl->wined3d_device,
304 0, src_buffer_impl->wined3d_buffer, 0, get_flexible_vertex_size(src_buffer_impl->fvf));
305 wined3d_device_set_vertex_declaration(device_impl->wined3d_device, src_buffer_impl->wined3d_declaration);
306 hr = wined3d_device_process_vertices(device_impl->wined3d_device, src_idx, dst_idx,
307 count, dst_buffer_impl->wined3d_buffer, NULL, flags, dst_buffer_impl->fvf);
309 /* Restore the states if needed */
310 if (doClip != oldClip)
311 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip);
313 wined3d_mutex_unlock();
315 return hr;
318 /*****************************************************************************
319 * IDirect3DVertexBuffer7::GetVertexBufferDesc
321 * Returns the description of a vertex buffer
323 * Params:
324 * Desc: Address to write the description to
326 * Returns
327 * DDERR_INVALIDPARAMS if Desc is NULL
328 * D3D_OK on success
330 *****************************************************************************/
331 static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc)
333 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
334 struct wined3d_resource_desc wined3d_desc;
335 struct wined3d_resource *wined3d_resource;
337 TRACE("iface %p, desc %p.\n", iface, desc);
339 if (!desc) return DDERR_INVALIDPARAMS;
341 wined3d_mutex_lock();
342 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
343 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
344 wined3d_mutex_unlock();
346 /* Now fill the desc structure */
347 desc->dwCaps = buffer->Caps;
348 desc->dwFVF = buffer->fvf;
349 desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(buffer->fvf);
351 return D3D_OK;
354 /*****************************************************************************
355 * IDirect3DVertexBuffer7::Optimize
357 * Converts an unoptimized vertex buffer into an optimized buffer
359 * Params:
360 * D3DDevice: Device for which this buffer is optimized
361 * Flags: Not used, should be set to 0
363 * Returns
364 * D3D_OK, because it's a stub
366 *****************************************************************************/
367 static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface,
368 IDirect3DDevice7 *device, DWORD flags)
370 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
371 static BOOL hide = FALSE;
373 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
375 if (!hide)
377 FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags);
378 hide = TRUE;
381 /* We could forward this call to WineD3D and take advantage
382 * of it once we use OpenGL vertex buffers
384 wined3d_mutex_lock();
385 buffer->Caps |= D3DVBCAPS_OPTIMIZED;
386 wined3d_mutex_unlock();
388 return DD_OK;
391 /*****************************************************************************
392 * IDirect3DVertexBuffer7::ProcessVerticesStrided
394 * This method processes untransformed strided vertices into a processed
395 * or optimized vertex buffer.
397 * For more details on the parameters, see
398 * IDirect3DVertexBuffer7::ProcessVertices
400 * Params:
401 * VertexOp: Operations to perform
402 * DestIndex: Destination index to write the vertices to
403 * Count: Number of input vertices
404 * StrideData: Array containing the input vertices
405 * VertexTypeDesc: Vertex Description or source index?????????
406 * D3DDevice: IDirect3DDevice7 to use for processing
407 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
409 * Returns
410 * D3D_OK on success, or DDERR_*
412 *****************************************************************************/
413 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
414 DWORD vertex_op, DWORD dst_idx, DWORD count, D3DDRAWPRIMITIVESTRIDEDDATA *data,
415 DWORD fvf, IDirect3DDevice7 *device, DWORD flags)
417 FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
418 iface, vertex_op, dst_idx, count, data, fvf, device, flags);
420 return DD_OK;
423 /*****************************************************************************
424 * The VTables
425 *****************************************************************************/
427 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
429 d3d_vertex_buffer7_QueryInterface,
430 d3d_vertex_buffer7_AddRef,
431 d3d_vertex_buffer7_Release,
432 d3d_vertex_buffer7_Lock,
433 d3d_vertex_buffer7_Unlock,
434 d3d_vertex_buffer7_ProcessVertices,
435 d3d_vertex_buffer7_GetVertexBufferDesc,
436 d3d_vertex_buffer7_Optimize,
437 d3d_vertex_buffer7_ProcessVerticesStrided,
440 HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf,
441 struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc)
443 struct d3d_vertex_buffer *buffer;
444 HRESULT hr = D3D_OK;
446 TRACE("Vertex buffer description:\n");
447 TRACE(" dwSize %u\n", desc->dwSize);
448 TRACE(" dwCaps %#x\n", desc->dwCaps);
449 TRACE(" FVF %#x\n", desc->dwFVF);
450 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
452 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buffer));
453 if (!buffer)
454 return DDERR_OUTOFMEMORY;
456 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
457 buffer->ref = 1;
458 buffer->version = ddraw->d3dversion;
459 if (buffer->version == 7)
460 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
461 buffer->ddraw = ddraw;
462 buffer->Caps = desc->dwCaps;
463 buffer->fvf = desc->dwFVF;
464 buffer->size = get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices;
466 wined3d_mutex_lock();
468 if (FAILED(hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wined3d_buffer)))
470 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
471 if (hr == WINED3DERR_INVALIDCALL)
472 hr = DDERR_INVALIDPARAMS;
473 goto end;
476 if (!(buffer->wined3d_declaration = ddraw_find_decl(ddraw, desc->dwFVF)))
478 ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
479 wined3d_buffer_decref(buffer->wined3d_buffer);
480 hr = DDERR_INVALIDPARAMS;
481 goto end;
483 wined3d_vertex_declaration_incref(buffer->wined3d_declaration);
485 end:
486 wined3d_mutex_unlock();
487 if (hr == D3D_OK)
488 *vertex_buf = buffer;
489 else
490 HeapFree(GetProcessHeap(), 0, buffer);
492 return hr;
495 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
497 if (!iface)
498 return NULL;
499 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);
501 return impl_from_IDirect3DVertexBuffer7(iface);