2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2007 Stefan Dösinger for CodeWeavers
6 * Copyright 2009 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
33 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
34 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
36 /* IUnknown methods */
38 static HRESULT STDMETHODCALLTYPE buffer_QueryInterface(IWineD3DBuffer *iface,
39 REFIID riid, void **object)
41 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
43 if (IsEqualGUID(riid, &IID_IWineD3DBuffer)
44 || IsEqualGUID(riid, &IID_IWineD3DResource)
45 || IsEqualGUID(riid, &IID_IWineD3DBase)
46 || IsEqualGUID(riid, &IID_IUnknown))
48 IUnknown_AddRef(iface);
53 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
60 static ULONG STDMETHODCALLTYPE buffer_AddRef(IWineD3DBuffer *iface)
62 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
63 ULONG refcount = InterlockedIncrement(&This->resource.ref);
65 TRACE("%p increasing refcount to %u\n", This, refcount);
70 static ULONG STDMETHODCALLTYPE buffer_Release(IWineD3DBuffer *iface)
72 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
73 ULONG refcount = InterlockedDecrement(&This->resource.ref);
75 TRACE("%p decreasing refcount to %u\n", This, refcount);
79 resource_cleanup((IWineD3DResource *)iface);
80 HeapFree(GetProcessHeap(), 0, This);
86 /* IWineD3DBase methods */
88 static HRESULT STDMETHODCALLTYPE buffer_GetParent(IWineD3DBuffer *iface, IUnknown **parent)
90 return resource_get_parent((IWineD3DResource *)iface, parent);
93 /* IWineD3DResource methods */
95 static HRESULT STDMETHODCALLTYPE buffer_GetDevice(IWineD3DBuffer *iface, IWineD3DDevice **device)
97 return resource_get_device((IWineD3DResource *)iface, device);
100 static HRESULT STDMETHODCALLTYPE buffer_SetPrivateData(IWineD3DBuffer *iface,
101 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
103 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
106 static HRESULT STDMETHODCALLTYPE buffer_GetPrivateData(IWineD3DBuffer *iface,
107 REFGUID guid, void *data, DWORD *data_size)
109 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
112 static HRESULT STDMETHODCALLTYPE buffer_FreePrivateData(IWineD3DBuffer *iface, REFGUID guid)
114 return resource_free_private_data((IWineD3DResource *)iface, guid);
117 static DWORD STDMETHODCALLTYPE buffer_SetPriority(IWineD3DBuffer *iface, DWORD priority)
119 return resource_set_priority((IWineD3DResource *)iface, priority);
122 static DWORD STDMETHODCALLTYPE buffer_GetPriority(IWineD3DBuffer *iface)
124 return resource_get_priority((IWineD3DResource *)iface);
127 static void STDMETHODCALLTYPE buffer_PreLoad(IWineD3DBuffer *iface)
129 TRACE("iface %p\n", iface);
132 static void STDMETHODCALLTYPE buffer_UnLoad(IWineD3DBuffer *iface)
134 TRACE("iface %p\n", iface);
137 static WINED3DRESOURCETYPE STDMETHODCALLTYPE buffer_GetType(IWineD3DBuffer *iface)
139 return resource_get_type((IWineD3DResource *)iface);
142 const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
144 /* IUnknown methods */
145 buffer_QueryInterface,
148 /* IWineD3DBase methods */
150 /* IWineD3DResource methods */
152 buffer_SetPrivateData,
153 buffer_GetPrivateData,
154 buffer_FreePrivateData,
162 /* IUnknown methods */
164 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer *iface,
165 REFIID riid, void **object)
167 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
169 if (IsEqualGUID(riid, &IID_IWineD3DVertexBuffer)
170 || IsEqualGUID(riid, &IID_IWineD3DResource)
171 || IsEqualGUID(riid, &IID_IWineD3DBase)
172 || IsEqualGUID(riid, &IID_IUnknown))
174 IUnknown_AddRef(iface);
179 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
183 return E_NOINTERFACE;
186 static ULONG STDMETHODCALLTYPE IWineD3DVertexBufferImpl_AddRef(IWineD3DVertexBuffer *iface)
188 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
189 ULONG refcount = InterlockedIncrement(&This->resource.ref);
191 TRACE("%p increasing refcount to %u\n", This, refcount);
196 static ULONG STDMETHODCALLTYPE IWineD3DVertexBufferImpl_Release(IWineD3DVertexBuffer *iface)
198 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
199 ULONG refcount = InterlockedDecrement(&This->resource.ref);
201 TRACE("%p decreasing refcount to %u\n", This, refcount);
207 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
209 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
211 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
212 checkGLcall("glDeleteBuffersARB");
216 resource_cleanup((IWineD3DResource *)iface);
217 HeapFree(GetProcessHeap(), 0, This);
223 /* IWineD3DBase methods */
225 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer *iface, IUnknown **parent)
227 return resource_get_parent((IWineD3DResource *)iface, parent);
230 /* IWineD3DResource methods */
232 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer *iface,
233 IWineD3DDevice **device)
235 return resource_get_device((IWineD3DResource *)iface, device);
238 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer *iface,
239 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
241 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
244 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer *iface,
245 REFGUID guid, void *data, DWORD *data_size)
247 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
250 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer *iface, REFGUID guid)
252 return resource_free_private_data((IWineD3DResource *)iface, guid);
255 static DWORD STDMETHODCALLTYPE IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer *iface, DWORD priority)
257 return resource_set_priority((IWineD3DResource *)iface, priority);
260 static DWORD STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer *iface)
262 return resource_get_priority((IWineD3DResource *)iface);
265 static inline void fixup_d3dcolor(DWORD *pos)
267 DWORD srcColor = *pos;
269 /* Color conversion like in drawStridedSlow. watch out for little endianity
270 * If we want that stuff to work on big endian machines too we have to consider more things
272 * 0xff000000: Alpha mask
273 * 0x00ff0000: Blue mask
274 * 0x0000ff00: Green mask
275 * 0x000000ff: Red mask
278 *pos |= (srcColor & 0xff00ff00) ; /* Alpha Green */
279 *pos |= (srcColor & 0x00ff0000) >> 16; /* Red */
280 *pos |= (srcColor & 0x000000ff) << 16; /* Blue */
283 static inline void fixup_transformed_pos(float *p)
287 /* rhw conversion like in drawStridedSlow */
288 if (p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps)))
308 static DWORD *find_conversion_shift(IWineD3DVertexBufferImpl *This,
309 const WineDirect3DVertexStridedData *strided, DWORD stride)
311 DWORD *ret, i, shift, j, type;
312 DWORD orig_type_size;
320 This->conv_stride = stride;
321 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
322 for (i = 0; i < MAX_ATTRIBS; ++i)
324 if (strided->u.input[i].VBO != This->vbo) continue;
326 type = strided->u.input[i].dwType;
327 if (type == WINED3DDECLTYPE_FLOAT16_2)
331 else if (type == WINED3DDECLTYPE_FLOAT16_4)
334 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
337 for (j = 4; j < 8; ++j)
339 ret[(DWORD_PTR)strided->u.input[i].lpData + j] += 4;
346 This->conv_stride += shift;
350 orig_type_size = WINED3D_ATR_TYPESIZE(type) * WINED3D_ATR_SIZE(type);
351 for (j = (DWORD_PTR)strided->u.input[i].lpData + orig_type_size; j < stride; ++j)
360 TRACE("Dumping conversion shift:\n");
361 for (i = 0; i < stride; ++i)
363 TRACE("[%d]", ret[i]);
371 static inline BOOL process_converted_attribute(IWineD3DVertexBufferImpl *This,
372 const enum vbo_conversion_type conv_type, const WineDirect3DStridedData *attrib,
373 DWORD *stride_this_run, const DWORD type)
378 DWORD offset = This->resource.wineD3DDevice->stateBlock->streamOffset[attrib->streamNo];
381 /* Check for some valid situations which cause us pain. One is if the buffer is used for
382 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
383 * with different strides. In the 2nd case we might have to drop conversion entirely,
384 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
386 if (attrib->dwStride == 0)
388 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
389 debug_d3ddecltype(type));
391 else if(attrib->dwStride != *stride_this_run && *stride_this_run)
393 FIXME("Got two concurrent strides, %d and %d\n", attrib->dwStride, *stride_this_run);
397 *stride_this_run = attrib->dwStride;
398 if (This->stride != *stride_this_run)
400 /* We rely that this happens only on the first converted attribute that is found,
401 * if at all. See above check
403 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
404 This->stride = *stride_this_run;
405 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conv_map);
406 This->conv_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->conv_map) * This->stride);
411 data = (((DWORD_PTR) attrib->lpData) + offset) % This->stride;
412 attrib_size = WINED3D_ATR_SIZE(type) * WINED3D_ATR_TYPESIZE(type);
413 for (i = 0; i < attrib_size; ++i)
415 if (This->conv_map[data + i] != conv_type)
417 TRACE("Byte %ld in vertex changed\n", i + data);
418 TRACE("It was type %d, is %d now\n", This->conv_map[data + i], conv_type);
420 This->conv_map[data + i] = conv_type;
427 static inline BOOL check_attribute(IWineD3DVertexBufferImpl *This,
428 const WineDirect3DStridedData *attrib, const BOOL check_d3dcolor, const BOOL is_ffp_position,
429 const BOOL is_ffp_color, DWORD *stride_this_run, BOOL *float16_used)
434 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
435 * there, on nonexistent attribs the vbo is 0.
437 if (attrib->VBO != This->vbo) return FALSE;
439 type = attrib->dwType;
440 /* Look for newly appeared conversion */
441 if (!GL_SUPPORT(NV_HALF_FLOAT) && (type == WINED3DDECLTYPE_FLOAT16_2 || type == WINED3DDECLTYPE_FLOAT16_4))
443 ret = process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run, type);
445 if (is_ffp_position) FIXME("Test FLOAT16 fixed function processing positions\n");
446 else if (is_ffp_color) FIXME("test FLOAT16 fixed function processing colors\n");
447 *float16_used = TRUE;
449 else if (check_d3dcolor && type == WINED3DDECLTYPE_D3DCOLOR)
451 ret = process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run, WINED3DDECLTYPE_D3DCOLOR);
453 if (!is_ffp_color) FIXME("Test for non-color fixed function D3DCOLOR type\n");
455 else if (is_ffp_position && type == WINED3DDECLTYPE_FLOAT4)
457 ret = process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run, WINED3DDECLTYPE_FLOAT4);
459 else if (This->conv_map)
461 ret = process_converted_attribute(This, CONV_NONE, attrib, stride_this_run, type);
467 static inline BOOL IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl *This)
469 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
472 DWORD stride_this_run = 0;
473 BOOL float16_used = FALSE;
475 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
476 * Once we have our declaration there is no need to look it up again.
478 if (((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->Flags & VBFLAG_HASDESC) return FALSE;
480 TRACE("Finding vertex buffer conversion information\n");
481 /* Certain declaration types need some fixups before we can pass them to
482 * opengl. This means D3DCOLOR attributes with fixed function vertex
483 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
484 * GL_NV_half_float is not supported.
486 * The vertex buffer FVF doesn't help with finding them, we have to use
487 * the decoded vertex declaration and pick the things that concern the
488 * current buffer. A problem with this is that this can change between
489 * draws, so we have to validate the information and reprocess the buffer
490 * if it changes, and avoid false positives for performance reasons.
492 * We have to distinguish between vertex shaders and fixed function to
493 * pick the way we access the strided vertex information.
495 * This code sets up a per-byte array with the size of the detected
496 * stride of the arrays in the buffer. For each byte we have a field
497 * that marks the conversion needed on this byte. For example, the
498 * following declaration with fixed function vertex processing:
506 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
507 * [P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][0][0][0][0][0][0][0][0][0][0][0][0][F][F][F][F][F][F][F][F][C][C][C][C]
509 * Where in this example map P means 4 component position conversion, 0
510 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
511 * conversion (red / blue swizzle).
513 * If we're doing conversion and the stride changes we have to reconvert
514 * the whole buffer. Note that we do not mind if the semantic changes,
515 * we only care for the conversion type. So if the NORMAL is replaced
516 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
517 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
518 * conversion types depend on the semantic as well, for example a FLOAT4
519 * texcoord needs no conversion while a FLOAT4 positiont needs one
521 if (use_vs(device->stateBlock))
524 /* If the current vertex declaration is marked for no half float conversion don't bother to
525 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
526 * if we used conversion before
528 if (!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed)
532 TRACE("Now using shaders without conversion, but conversion used before\n");
533 HeapFree(GetProcessHeap(), 0, This->conv_map);
534 HeapFree(GetProcessHeap(), 0, This->conv_shift);
535 This->conv_map = NULL;
537 This->conv_shift = NULL;
538 This->conv_stride = 0;
546 for (i = 0; i < MAX_ATTRIBS; ++i)
548 ret = check_attribute(This, &device->strided_streams.u.input[i],
549 FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
552 /* Recalculate the conversion shift map if the declaration has changed,
553 * and we're using float16 conversion or used it on the last run
555 if (ret && (float16_used || This->conv_map))
557 HeapFree(GetProcessHeap(), 0, This->conv_shift);
558 This->conv_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
563 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
564 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
565 * the attributes that our current fixed function pipeline implementation cares for.
567 BOOL support_d3dcolor = GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA);
568 ret = check_attribute(This, &device->strided_streams.u.s.position,
569 TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
570 ret = check_attribute(This, &device->strided_streams.u.s.normal,
571 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
572 ret = check_attribute(This, &device->strided_streams.u.s.diffuse,
573 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
574 ret = check_attribute(This, &device->strided_streams.u.s.specular,
575 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
576 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[0],
577 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
578 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[1],
579 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
580 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[2],
581 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
582 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[3],
583 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
584 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[4],
585 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
586 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[5],
587 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
588 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[6],
589 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
590 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[7],
591 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
593 if (float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
596 if (stride_this_run == 0 && This->conv_map)
599 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
600 HeapFree(GetProcessHeap(), 0, This->conv_map);
601 This->conv_map = NULL;
604 This->Flags |= VBFLAG_HASDESC;
606 if (ret) TRACE("Conversion information changed\n");
611 static void check_vbo_size(IWineD3DVertexBufferImpl *This)
613 DWORD size = This->conv_stride ? This->conv_stride * (This->resource.size / This->stride) : This->resource.size;
614 if (This->vbo_size != size)
616 TRACE("Old size %d, creating new size %d\n", This->vbo_size, size);
618 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
619 checkGLcall("glBindBufferARB");
620 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, NULL, This->vbo_usage));
621 This->vbo_size = size;
622 checkGLcall("glBufferDataARB");
627 static void CreateVBO(IWineD3DVertexBufferImpl *This)
629 GLenum error, glUsage;
630 DWORD vboUsage = This->resource.usage;
631 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
633 TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n",
634 This, debug_d3dusage(vboUsage));
636 /* Make sure that a context is there. Needed in a multithreaded environment. Otherwise this call is a nop */
637 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
640 /* Make sure that the gl error is cleared. Do not use checkGLcall
641 * here because checkGLcall just prints a fixme and continues. However,
642 * if an error during VBO creation occurs we can fall back to non-vbo operation
643 * with full functionality(but performance loss)
645 while(glGetError() != GL_NO_ERROR);
647 /* Basically the FVF parameter passed to CreateVertexBuffer is no good
648 * It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
649 * IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
650 * look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
651 * to check if the rhw and color values are in the correct format.
654 GL_EXTCALL(glGenBuffersARB(1, &This->vbo));
655 error = glGetError();
656 if (This->vbo == 0 || error != GL_NO_ERROR)
658 WARN("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
662 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
663 error = glGetError();
664 if (error != GL_NO_ERROR)
666 WARN("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
670 /* Don't use static, because dx apps tend to update the buffer
671 * quite often even if they specify 0 usage. Because we always keep the local copy
672 * we never read from the vbo and can create a write only opengl buffer.
674 switch(vboUsage & (WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC))
676 case WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC:
677 case WINED3DUSAGE_DYNAMIC:
678 TRACE("Gl usage = GL_STREAM_DRAW\n");
679 glUsage = GL_STREAM_DRAW_ARB;
682 case WINED3DUSAGE_WRITEONLY:
684 TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
685 glUsage = GL_DYNAMIC_DRAW_ARB;
689 /* Reserve memory for the buffer. The amount of data won't change
690 * so we are safe with calling glBufferData once with a NULL ptr and
691 * calling glBufferSubData on updates
693 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, This->resource.size, NULL, glUsage));
694 error = glGetError();
695 if (error != GL_NO_ERROR)
697 WARN("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
700 This->vbo_size = This->resource.size;
701 This->vbo_usage = glUsage;
702 This->dirtystart = 0;
703 This->dirtyend = This->resource.size;
704 This->Flags |= VBFLAG_DIRTY;
711 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
712 FIXME("Failed to create a vertex buffer object. Continuing, but performance issues can occur\n");
713 if (This->vbo) GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
720 static void STDMETHODCALLTYPE IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer *iface)
722 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
723 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
725 UINT start = 0, end = 0, vertices;
726 BOOL declChanged = FALSE;
728 TRACE("(%p)->()\n", This);
732 /* TODO: Make converting independent from VBOs */
733 if (This->Flags & VBFLAG_CREATEVBO)
736 This->Flags &= ~VBFLAG_CREATEVBO;
740 return; /* Not doing any conversion */
744 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
745 if (device->isInDraw && This->bindCount > 0)
747 declChanged = IWineD3DVertexBufferImpl_FindDecl(This);
749 else if (This->Flags & VBFLAG_HASDESC)
751 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
752 * the stream source state handler will call PreLoad again and the change will be caught
757 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
758 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
759 * declaration for the buffer can be found
764 /* If applications change the declaration over and over, reconverting all the time is a huge
765 * performance hit. So count the declaration changes and release the VBO if there are too many
766 * of them (and thus stop converting)
773 if (This->declChanges > VB_MAXDECLCHANGES)
775 FIXME("Too many declaration changes, stopping converting\n");
776 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
778 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
779 checkGLcall("glDeleteBuffersARB");
782 HeapFree(GetProcessHeap(), 0, This->conv_shift);
784 /* The stream source state handler might have read the memory of the vertex buffer already
785 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
786 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
789 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
793 check_vbo_size(This);
797 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
798 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
799 * decl changes and reset the decl change count after a specific number of them
802 if (This->draws > VB_RESETDECLCHANGE) This->declChanges = 0;
807 /* The declaration changed, reload the whole buffer */
808 WARN("Reloading buffer because of decl change\n");
810 end = This->resource.size;
812 else if(This->Flags & VBFLAG_DIRTY)
814 /* No decl change, but dirty data, reload the changed stuff */
815 if (This->conv_shift)
817 if (This->dirtystart != 0 || This->dirtyend != 0)
819 FIXME("Implement partial buffer loading with shifted conversion\n");
822 start = This->dirtystart;
823 end = This->dirtyend;
827 /* Desc not changed, buffer not dirty, nothing to do :-) */
831 /* Mark the buffer clean */
832 This->Flags &= ~VBFLAG_DIRTY;
833 This->dirtystart = 0;
838 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
839 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
840 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
842 TRACE("No conversion needed\n");
844 if (!device->isInDraw)
846 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
849 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
850 checkGLcall("glBindBufferARB");
851 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end-start, This->resource.allocatedMemory + start));
852 checkGLcall("glBufferSubDataARB");
857 /* Now for each vertex in the buffer that needs conversion */
858 vertices = This->resource.size / This->stride;
860 if (This->conv_shift)
862 TRACE("Shifted conversion\n");
863 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conv_stride);
865 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
867 for (j = 0; j < This->stride; ++j)
869 switch(This->conv_map[j])
872 data[This->conv_stride * i + j + This->conv_shift[j]] = This->resource.allocatedMemory[This->stride * i + j];
877 float *out = (float *)(&data[This->conv_stride * i + j + This->conv_shift[j]]);
878 const WORD *in = (WORD *)(&This->resource.allocatedMemory[i * This->stride + j]);
880 out[1] = float_16_to_32(in + 1);
881 out[0] = float_16_to_32(in + 0);
882 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
887 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
893 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
894 checkGLcall("glBindBufferARB");
895 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, vertices * This->conv_stride, data));
896 checkGLcall("glBufferSubDataARB");
901 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
902 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
903 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
905 for (j = 0; j < This->stride; ++j)
907 switch(This->conv_map[j])
914 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
919 fixup_transformed_pos((float *) (data + i * This->stride + j));
924 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
926 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
932 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
933 checkGLcall("glBindBufferARB");
934 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data + start));
935 checkGLcall("glBufferSubDataARB");
939 HeapFree(GetProcessHeap(), 0, data);
942 static void STDMETHODCALLTYPE IWineD3DVertexBufferImpl_UnLoad(IWineD3DVertexBuffer *iface)
944 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
945 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
947 TRACE("(%p)\n", This);
949 /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
950 * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
951 * that vbos are supported
955 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
957 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
958 checkGLcall("glDeleteBuffersARB");
961 This->Flags |= VBFLAG_CREATEVBO; /* Recreate the VBO next load */
965 static WINED3DRESOURCETYPE STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer *iface)
967 return resource_get_type((IWineD3DResource *)iface);
970 /* IWineD3DVertexBuffer methods */
972 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer *iface,
973 UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags)
975 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
978 TRACE("(%p)->%d, %d, %p, %08x\n", This, OffsetToLock, SizeToLock, ppbData, Flags);
980 InterlockedIncrement(&This->lockcount);
982 if (This->Flags & VBFLAG_DIRTY)
984 if (This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
987 if (This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
991 This->dirtyend = This->resource.size;
996 This->dirtystart = OffsetToLock;
997 if (SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
998 else This->dirtyend = This->resource.size;
1001 data = This->resource.allocatedMemory;
1002 This->Flags |= VBFLAG_DIRTY;
1003 *ppbData = data + OffsetToLock;
1005 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This, data + OffsetToLock, data, OffsetToLock);
1006 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
1011 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer *iface)
1013 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
1016 TRACE("(%p)\n", This);
1018 lockcount = InterlockedDecrement(&This->lockcount);
1021 /* Delay loading the buffer until everything is unlocked */
1022 TRACE("Ignoring the unlock\n");
1026 if (This->Flags & VBFLAG_HASDESC)
1028 IWineD3DVertexBufferImpl_PreLoad(iface);
1034 static HRESULT STDMETHODCALLTYPE IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer *iface,
1035 WINED3DVERTEXBUFFER_DESC *pDesc)
1037 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
1039 TRACE("(%p)\n", This);
1041 pDesc->Format = This->resource.format;
1042 pDesc->Type = This->resource.resourceType;
1043 pDesc->Usage = This->resource.usage;
1044 pDesc->Pool = This->resource.pool;
1045 pDesc->Size = This->resource.size;
1046 pDesc->FVF = This->fvf;
1051 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl =
1053 /* IUnknown methods */
1054 IWineD3DVertexBufferImpl_QueryInterface,
1055 IWineD3DVertexBufferImpl_AddRef,
1056 IWineD3DVertexBufferImpl_Release,
1057 /* IWineD3DBase methods */
1058 IWineD3DVertexBufferImpl_GetParent,
1059 /* IWineD3DResource methods */
1060 IWineD3DVertexBufferImpl_GetDevice,
1061 IWineD3DVertexBufferImpl_SetPrivateData,
1062 IWineD3DVertexBufferImpl_GetPrivateData,
1063 IWineD3DVertexBufferImpl_FreePrivateData,
1064 IWineD3DVertexBufferImpl_SetPriority,
1065 IWineD3DVertexBufferImpl_GetPriority,
1066 IWineD3DVertexBufferImpl_PreLoad,
1067 IWineD3DVertexBufferImpl_UnLoad,
1068 IWineD3DVertexBufferImpl_GetType,
1069 /* IWineD3DVertexBuffer methods */
1070 IWineD3DVertexBufferImpl_Lock,
1071 IWineD3DVertexBufferImpl_Unlock,
1072 IWineD3DVertexBufferImpl_GetDesc,
1075 const BYTE *IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer *iface, DWORD iOffset, GLint *vbo)
1077 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
1082 if (This->Flags & VBFLAG_CREATEVBO)
1085 This->Flags &= ~VBFLAG_CREATEVBO;
1089 return (const BYTE *)iOffset;
1092 return This->resource.allocatedMemory + iOffset;
1096 return (const BYTE *)iOffset;