1 /* Direct3D Vertex Buffer 2 * Copyright (c) 2002 Lionel ULMER 3 * Copyright (c) 2006 Stefan DÖSINGER 4 * 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. 9 * 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. 14 * 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 18 */ 19 20 #include "config.h" 21 #include "wine/port.h" 22 23 #include "ddraw_private.h" 24 25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw); 26 27 static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface) 28 { 29 return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer7_iface); 30 } 31 32 /***************************************************************************** 33 * IUnknown Methods 34 *****************************************************************************/ 35 36 static HRESULT WINAPI d3d_vertex_buffer7_QueryInterface(IDirect3DVertexBuffer7 *iface, REFIID riid, void **obj) 37 { 38 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 39 40 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj); 41 42 *obj = NULL; 43 44 if (IsEqualGUID(&IID_IUnknown, riid)) 45 { 46 IDirect3DVertexBuffer7_AddRef(iface); 47 *obj = iface; 48 return S_OK; 49 } 50 if (IsEqualGUID(&IID_IDirect3DVertexBuffer7, riid) && buffer->version == 7) 51 { 52 IDirect3DVertexBuffer7_AddRef(iface); 53 *obj = iface; 54 return S_OK; 55 } 56 if (IsEqualGUID(&IID_IDirect3DVertexBuffer, riid) && buffer->version == 3) 57 { 58 IDirect3DVertexBuffer7_AddRef(iface); 59 *obj = iface; 60 return S_OK; 61 } 62 63 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid)); 64 return E_NOINTERFACE; 65 } 66 67 static ULONG WINAPI d3d_vertex_buffer7_AddRef(IDirect3DVertexBuffer7 *iface) 68 { 69 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 70 ULONG ref = InterlockedIncrement(&buffer->ref); 71 72 TRACE("%p increasing refcount to %u.\n", buffer, ref); 73 74 return ref; 75 } 76 77 static ULONG WINAPI d3d_vertex_buffer7_Release(IDirect3DVertexBuffer7 *iface) 78 { 79 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 80 ULONG ref = InterlockedDecrement(&buffer->ref); 81 82 TRACE("%p decreasing refcount to %u.\n", buffer, ref); 83 84 if (!ref) 85 { 86 struct wined3d_buffer *vb = NULL; 87 UINT offset, stride; 88 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); 97 98 wined3d_vertex_declaration_decref(buffer->wined3d_declaration); 99 wined3d_buffer_decref(buffer->wined3d_buffer); 100 wined3d_mutex_unlock(); 101 102 if (buffer->version == 7) 103 IDirectDraw7_Release(&buffer->ddraw->IDirectDraw7_iface); 104 105 heap_free(buffer); 106 } 107 108 return ref; 109 } 110 111 /***************************************************************************** 112 * IDirect3DVertexBuffer Methods 113 *****************************************************************************/ 114 115 static HRESULT d3d_vertex_buffer_create_wined3d_buffer(struct d3d_vertex_buffer *buffer, BOOL dynamic, 116 struct wined3d_buffer **wined3d_buffer) 117 { 118 struct wined3d_buffer_desc desc; 119 120 desc.byte_width = buffer->size; 121 desc.usage = WINED3DUSAGE_STATICDECL; 122 if (buffer->Caps & D3DVBCAPS_WRITEONLY) 123 desc.usage |= WINED3DUSAGE_WRITEONLY; 124 if (dynamic) 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; 129 else 130 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W; 131 desc.misc_flags = 0; 132 desc.structure_byte_stride = 0; 133 134 return wined3d_buffer_create(buffer->ddraw->wined3d_device, &desc, 135 NULL, buffer, &ddraw_null_wined3d_parent_ops, wined3d_buffer); 136 } 137 138 /***************************************************************************** 139 * IDirect3DVertexBuffer7::Lock 140 * 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) 144 * 145 * Params: 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 150 * 151 * Returns: 152 * D3D_OK on success 153 * DDERR_INVALIDPARAMS if Data is NULL 154 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D) 155 * 156 *****************************************************************************/ 157 static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface, 158 DWORD flags, void **data, DWORD *data_size) 159 { 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; 164 HRESULT hr; 165 166 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size); 167 168 if (buffer->version != 7) 169 flags &= ~(DDLOCK_NOOVERWRITE | DDLOCK_DISCARDCONTENTS); 170 171 if (!(flags & DDLOCK_WAIT)) 172 flags |= DDLOCK_DONOTWAIT; 173 if (flags & DDLOCK_DISCARDCONTENTS) 174 { 175 if (!buffer->dynamic) 176 { 177 struct wined3d_buffer *new_buffer; 178 wined3d_mutex_lock(); 179 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer); 180 if (SUCCEEDED(hr)) 181 { 182 buffer->dynamic = TRUE; 183 wined3d_buffer_decref(buffer->wined3d_buffer); 184 buffer->wined3d_buffer = new_buffer; 185 } 186 else 187 { 188 WARN("Failed to create a dynamic buffer\n"); 189 } 190 wined3d_mutex_unlock(); 191 } 192 } 193 194 wined3d_mutex_lock(); 195 if (data_size) 196 { 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; 201 } 202 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; 206 207 wined3d_mutex_unlock(); 208 209 return hr; 210 } 211 212 /***************************************************************************** 213 * IDirect3DVertexBuffer7::Unlock 214 * 215 * Unlocks a vertex Buffer 216 * 217 * Returns: 218 * D3D_OK on success 219 * 220 *****************************************************************************/ 221 static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface) 222 { 223 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 224 225 TRACE("iface %p.\n", iface); 226 227 wined3d_mutex_lock(); 228 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0); 229 wined3d_mutex_unlock(); 230 231 return D3D_OK; 232 } 233 234 /***************************************************************************** 235 * IDirect3DVertexBuffer7::ProcessVertices 236 * 237 * Processes untransformed Vertices into a transformed or optimized vertex 238 * buffer. It can also perform other operations, such as lighting or clipping 239 * 240 * Params 241 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM 242 * DestIndex: Index in the destination buffer(This), where the vertices are 243 * placed 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 249 * unchanged vertices 250 * 251 * Returns: 252 * D3D_OK on success 253 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed 254 * 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) 259 { 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; 266 HRESULT hr; 267 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); 270 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 277 * 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. 280 */ 281 if (!(vertex_op & D3DVOP_TRANSFORM)) 282 return DDERR_INVALIDPARAMS; 283 284 wined3d_mutex_lock(); 285 286 /* WineD3D doesn't know d3d7 vertex operation, it uses 287 * render states instead. Set the render states according to 288 * the vertex ops 289 */ 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); 294 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); 300 301 /* Restore the states if needed */ 302 if (doClip != oldClip) 303 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip); 304 305 wined3d_mutex_unlock(); 306 307 return hr; 308 } 309 310 /***************************************************************************** 311 * IDirect3DVertexBuffer7::GetVertexBufferDesc 312 * 313 * Returns the description of a vertex buffer 314 * 315 * Params: 316 * Desc: Address to write the description to 317 * 318 * Returns 319 * DDERR_INVALIDPARAMS if Desc is NULL 320 * D3D_OK on success 321 * 322 *****************************************************************************/ 323 static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc) 324 { 325 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 326 struct wined3d_resource_desc wined3d_desc; 327 struct wined3d_resource *wined3d_resource; 328 329 TRACE("iface %p, desc %p.\n", iface, desc); 330 331 if (!desc) return DDERR_INVALIDPARAMS; 332 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(); 337 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); 342 343 return D3D_OK; 344 } 345 346 /***************************************************************************** 347 * IDirect3DVertexBuffer7::Optimize 348 * 349 * Converts an unoptimized vertex buffer into an optimized buffer 350 * 351 * Params: 352 * D3DDevice: Device for which this buffer is optimized 353 * Flags: Not used, should be set to 0 354 * 355 * Returns 356 * D3D_OK, because it's a stub 357 * 358 *****************************************************************************/ 359 static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface, 360 IDirect3DDevice7 *device, DWORD flags) 361 { 362 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface); 363 static BOOL hide = FALSE; 364 365 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags); 366 367 if (!hide) 368 { 369 FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags); 370 hide = TRUE; 371 } 372 373 /* We could forward this call to WineD3D and take advantage 374 * of it once we use OpenGL vertex buffers 375 */ 376 wined3d_mutex_lock(); 377 buffer->Caps |= D3DVBCAPS_OPTIMIZED; 378 wined3d_mutex_unlock(); 379 380 return DD_OK; 381 } 382 383 /***************************************************************************** 384 * IDirect3DVertexBuffer7::ProcessVerticesStrided 385 * 386 * This method processes untransformed strided vertices into a processed 387 * or optimized vertex buffer. 388 * 389 * For more details on the parameters, see 390 * IDirect3DVertexBuffer7::ProcessVertices 391 * 392 * Params: 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 400 * 401 * Returns 402 * D3D_OK on success, or DDERR_* 403 * 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) 408 { 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); 411 412 return DD_OK; 413 } 414 415 /***************************************************************************** 416 * The VTables 417 *****************************************************************************/ 418 419 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl = 420 { 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, 430 }; 431 432 HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf, 433 struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc) 434 { 435 struct d3d_vertex_buffer *buffer; 436 HRESULT hr = D3D_OK; 437 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); 443 444 if (!(buffer = heap_alloc_zero(sizeof(*buffer)))) 445 return DDERR_OUTOFMEMORY; 446 447 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl; 448 buffer->ref = 1; 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; 456 457 wined3d_mutex_lock(); 458 459 if (FAILED(hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wined3d_buffer))) 460 { 461 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr); 462 if (hr == WINED3DERR_INVALIDCALL) 463 hr = DDERR_INVALIDPARAMS; 464 goto end; 465 } 466 467 if (!(buffer->wined3d_declaration = ddraw_find_decl(ddraw, desc->dwFVF))) 468 { 469 ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF); 470 wined3d_buffer_decref(buffer->wined3d_buffer); 471 hr = DDERR_INVALIDPARAMS; 472 goto end; 473 } 474 wined3d_vertex_declaration_incref(buffer->wined3d_declaration); 475 476 end: 477 wined3d_mutex_unlock(); 478 if (hr == D3D_OK) 479 *vertex_buf = buffer; 480 else 481 heap_free(buffer); 482 483 return hr; 484 } 485 486 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface) 487 { 488 if (!iface) 489 return NULL; 490 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl); 491 492 return impl_from_IDirect3DVertexBuffer7(iface); 493 } 494