2 * IWineD3DVertexBuffer Implementation
4 * Copyright 2002-2005 Jason Edmeades
6 * Copyright 2004 Christian Costa
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
30 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
31 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
33 /* *******************************************
34 IWineD3DVertexBuffer IUnknown parts follow
35 ******************************************* */
36 static HRESULT WINAPI
IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer
*iface
, REFIID riid
, LPVOID
*ppobj
)
38 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
39 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
40 if (IsEqualGUID(riid
, &IID_IUnknown
)
41 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
42 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
43 || IsEqualGUID(riid
, &IID_IWineD3DVertexBuffer
)){
44 IUnknown_AddRef(iface
);
52 static ULONG WINAPI
IWineD3DVertexBufferImpl_AddRef(IWineD3DVertexBuffer
*iface
) {
53 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
54 ULONG ref
= InterlockedIncrement(&This
->resource
.ref
);
55 TRACE("(%p) : AddRef increasing from %d\n", This
, ref
- 1);
59 static ULONG WINAPI
IWineD3DVertexBufferImpl_Release(IWineD3DVertexBuffer
*iface
) {
60 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
61 ULONG ref
= InterlockedDecrement(&This
->resource
.ref
);
62 TRACE("(%p) : Releasing from %d\n", This
, ref
+ 1);
66 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
68 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
70 GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
71 checkGLcall("glDeleteBuffersARB");
75 IWineD3DResourceImpl_CleanUp((IWineD3DResource
*)iface
);
76 HeapFree(GetProcessHeap(), 0, This
);
81 /* ****************************************************
82 IWineD3DVertexBuffer IWineD3DResource parts follow
83 **************************************************** */
84 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer
*iface
, IWineD3DDevice
** ppDevice
) {
85 return IWineD3DResourceImpl_GetDevice((IWineD3DResource
*)iface
, ppDevice
);
88 static HRESULT WINAPI
IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
, CONST
void* pData
, DWORD SizeOfData
, DWORD Flags
) {
89 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, SizeOfData
, Flags
);
92 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
, void* pData
, DWORD
* pSizeOfData
) {
93 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, pSizeOfData
);
96 static HRESULT WINAPI
IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
) {
97 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource
*)iface
, refguid
);
100 static DWORD WINAPI
IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer
*iface
, DWORD PriorityNew
) {
101 return IWineD3DResourceImpl_SetPriority((IWineD3DResource
*)iface
, PriorityNew
);
104 static DWORD WINAPI
IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer
*iface
) {
105 return IWineD3DResourceImpl_GetPriority((IWineD3DResource
*)iface
);
108 static inline void fixup_d3dcolor(DWORD
*pos
) {
109 DWORD srcColor
= *pos
;
111 /* Color conversion like in drawStridedSlow. watch out for little endianity
112 * If we want that stuff to work on big endian machines too we have to consider more things
114 * 0xff000000: Alpha mask
115 * 0x00ff0000: Blue mask
116 * 0x0000ff00: Green mask
117 * 0x000000ff: Red mask
120 *pos
|= (srcColor
& 0xff00ff00) ; /* Alpha Green */
121 *pos
|= (srcColor
& 0x00ff0000) >> 16; /* Red */
122 *pos
|= (srcColor
& 0x000000ff) << 16; /* Blue */
125 static inline void fixup_transformed_pos(float *p
) {
128 /* rhw conversion like in drawStridedSlow */
129 if(p
[3] == 1.0 || ((p
[3] < eps
) && (p
[3] > -eps
))) {
146 DWORD
*find_conversion_shift(IWineD3DVertexBufferImpl
*This
, WineDirect3DVertexStridedData
*strided
, DWORD stride
) {
147 DWORD
*ret
, i
, shift
, j
, type
;
148 DWORD orig_type_size
;
155 This
->conv_stride
= stride
;
156 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DWORD
) * stride
);
157 for(i
= 0; i
< MAX_ATTRIBS
; i
++) {
158 if(strided
->u
.input
[i
].VBO
!= This
->vbo
) continue;
160 type
= strided
->u
.input
[i
].dwType
;
161 if(type
== WINED3DDECLTYPE_FLOAT16_2
) {
163 } else if(type
== WINED3DDECLTYPE_FLOAT16_4
) {
165 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
168 for(j
= 4; j
< 8; j
++) {
169 ret
[(DWORD_PTR
) strided
->u
.input
[i
].lpData
+ j
] += 4;
174 This
->conv_stride
+= shift
;
177 orig_type_size
= WINED3D_ATR_TYPESIZE(type
) * WINED3D_ATR_SIZE(type
);
178 for(j
= (DWORD_PTR
) strided
->u
.input
[i
].lpData
+ orig_type_size
; j
< stride
; j
++) {
185 TRACE("Dumping conversion shift:\n");
186 for(i
= 0; i
< stride
; i
++) {
187 TRACE("[%d]", ret
[i
]);
194 static inline BOOL
process_converted_attribute(IWineD3DVertexBufferImpl
*This
,
195 const enum vbo_conversion_type conv_type
,
196 const WineDirect3DStridedData
*attrib
,
197 DWORD
*stride_this_run
, const DWORD type
) {
201 DWORD offset
= This
->resource
.wineD3DDevice
->stateBlock
->streamOffset
[attrib
->streamNo
];
204 /* Check for some valid situations which cause us pain. One is if the buffer is used for
205 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
206 * with different strides. In the 2nd case we might have to drop conversion entirely,
207 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
209 if(attrib
->dwStride
== 0) {
210 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
211 debug_d3ddecltype(type
));
212 } else if(attrib
->dwStride
!= *stride_this_run
&&
214 FIXME("Got two concurrent strides, %d and %d\n", attrib
->dwStride
, *stride_this_run
);
216 *stride_this_run
= attrib
->dwStride
;
217 if(This
->stride
!= *stride_this_run
) {
218 /* We rely that this happens only on the first converted attribute that is found,
219 * if at all. See above check
221 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
222 This
->stride
= *stride_this_run
;
223 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->conv_map
);
224 This
->conv_map
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->conv_map
) * This
->stride
);
229 data
= (((DWORD_PTR
) attrib
->lpData
) + offset
) % This
->stride
;
230 attrib_size
= WINED3D_ATR_SIZE(type
) * WINED3D_ATR_TYPESIZE(type
);
231 for(i
= 0; i
< attrib_size
; i
++) {
232 if(This
->conv_map
[data
+ i
] != conv_type
) {
233 TRACE("Byte %ld in vertex changed\n", i
+ data
);
234 TRACE("It was type %d, is %d now\n", This
->conv_map
[data
+ i
], conv_type
);
236 This
->conv_map
[data
+ i
] = conv_type
;
242 static inline BOOL
check_attribute(IWineD3DVertexBufferImpl
*This
, const WineDirect3DStridedData
*attrib
,
243 const BOOL check_d3dcolor
, const BOOL is_ffp_position
, const BOOL is_ffp_color
,
244 DWORD
*stride_this_run
, BOOL
*float16_used
) {
248 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
249 * there, on nonexistent attribs the vbo is 0.
251 if(attrib
->VBO
!= This
->vbo
) return FALSE
;
253 type
= attrib
->dwType
;
254 /* Look for newly appeared conversion */
255 if(!GL_SUPPORT(NV_HALF_FLOAT
) && (
256 type
== WINED3DDECLTYPE_FLOAT16_2
||
257 type
== WINED3DDECLTYPE_FLOAT16_4
)) {
259 ret
= process_converted_attribute(This
, CONV_FLOAT16_2
, attrib
, stride_this_run
, type
);
261 if(is_ffp_position
) {
262 FIXME("Test FLOAT16 fixed function processing positions\n");
263 } else if(is_ffp_color
) {
264 FIXME("test FLOAT16 fixed function processing colors\n");
266 *float16_used
= TRUE
;
267 } else if(check_d3dcolor
&& type
== WINED3DDECLTYPE_D3DCOLOR
) {
269 ret
= process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
, WINED3DDECLTYPE_D3DCOLOR
);
272 FIXME("Test for non-color fixed function D3DCOLOR type\n");
274 } else if(is_ffp_position
&& type
== WINED3DDECLTYPE_FLOAT4
) {
275 ret
= process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
, WINED3DDECLTYPE_FLOAT4
);
276 } else if(This
->conv_map
) {
277 ret
= process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
, type
);
282 inline BOOL WINAPI
IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl
*This
)
284 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
287 DWORD stride_this_run
= 0;
288 BOOL float16_used
= FALSE
;
290 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
291 * Once we have our declaration there is no need to look it up again.
293 if(((IWineD3DImpl
*)device
->wineD3D
)->dxVersion
== 7 && This
->Flags
& VBFLAG_HASDESC
) {
297 TRACE("Finding vertex buffer conversion information\n");
298 /* Certain declaration types need some fixups before we can pass them to opengl. This means D3DCOLOR attributes with fixed
299 * function vertex processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if GL_NV_half_float is not supported.
301 * The vertex buffer FVF doesn't help with finding them, we have to use the decoded vertex declaration and pick the things
302 * that concern the current buffer. A problem with this is that this can change between draws, so we have to validate
303 * the information and reprocess the buffer if it changes, and avoid false positives for performance reasons.
305 * We have to distinguish between vertex shaders and fixed function to pick the way we access the
306 * strided vertex information.
308 * This code sets up a per-byte array with the size of the detected stride of the arrays in the
309 * buffer. For each byte we have a field that marks the conversion needed on this byte. For example,
310 * the following declaration with fixed function vertex processing:
318 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
319 * [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]
321 * Where in this example map P means 4 component position conversion, 0 means no conversion, F means FLOAT16_2 conversion
322 * and C means D3DCOLOR conversion(red / blue swizzle).
324 * If we're doing conversion and the stride changes we have to reconvert the whole buffer. Note that we do not mind if the
325 * semantic changes, we only care for the conversion type. So if the NORMAL is replaced with a TEXCOORD, nothing has to be
326 * done, or if the DIFFUSE is replaced with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some conversion types
327 * depend on the semantic as well, for example a FLOAT4 texcoord needs no conversion while a FLOAT4 positiont needs one
331 /* If the current vertex declaration is marked for no half float conversion don't bother to
332 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
333 * if we used conversion before
335 if(!((IWineD3DVertexDeclarationImpl
*) device
->stateBlock
->vertexDecl
)->half_float_conv_needed
) {
337 TRACE("Now using shaders without conversion, but conversion used before\n");
338 HeapFree(GetProcessHeap(), 0, This
->conv_map
);
339 HeapFree(GetProcessHeap(), 0, This
->conv_shift
);
340 This
->conv_map
= NULL
;
342 This
->conv_shift
= NULL
;
343 This
->conv_stride
= 0;
349 for(i
= 0; i
< MAX_ATTRIBS
; i
++) {
350 ret
= check_attribute(This
, &device
->strided_streams
.u
.input
[i
], FALSE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
353 /* Recalculate the conversion shift map if the declaration has changed,
354 * and we're using float16 conversion or used it on the last run
356 if(ret
&& (float16_used
|| This
->conv_map
)) {
357 HeapFree(GetProcessHeap(), 0, This
->conv_shift
);
358 This
->conv_shift
= find_conversion_shift(This
, &device
->strided_streams
, This
->stride
);
361 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
362 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
363 * the attributes that our current fixed function pipeline implementation cares for.
365 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.position
, TRUE
, TRUE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
366 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.normal
, TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
367 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.diffuse
, TRUE
, FALSE
, TRUE
, &stride_this_run
, &float16_used
) || ret
;
368 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.specular
, TRUE
, FALSE
, TRUE
, &stride_this_run
, &float16_used
) || ret
;
369 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[0], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
370 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[1], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
371 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[2], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
372 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[3], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
373 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[4], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
374 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[5], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
375 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[6], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
376 ret
= check_attribute(This
, &device
->strided_streams
.u
.s
.texCoords
[7], TRUE
, FALSE
, FALSE
, &stride_this_run
, &float16_used
) || ret
;
378 if(float16_used
) FIXME("Float16 conversion used with fixed function vertex processing\n");
381 if(stride_this_run
== 0 && This
->conv_map
) {
384 ERR("no converted attributes found, old conversion map exists, and no declaration change???\n");
386 HeapFree(GetProcessHeap(), 0, This
->conv_map
);
387 This
->conv_map
= NULL
;
390 This
->Flags
|= VBFLAG_HASDESC
;
392 if(ret
) TRACE("Conversion information changed\n");
396 static void check_vbo_size(IWineD3DVertexBufferImpl
*This
) {
397 DWORD size
= This
->conv_stride
? This
->conv_stride
* (This
->resource
.size
/ This
->stride
) : This
->resource
.size
;
398 if(This
->vbo_size
!= size
) {
399 TRACE("Old size %d, creating new size %d\n", This
->vbo_size
, size
);
401 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
402 checkGLcall("glBindBufferARB");
403 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB
, size
, NULL
, This
->vbo_usage
));
404 This
->vbo_size
= size
;
405 checkGLcall("glBufferDataARB");
410 static void CreateVBO(IWineD3DVertexBufferImpl
*This
) {
411 GLenum error
, glUsage
;
412 DWORD vboUsage
= This
->resource
.usage
;
413 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
415 TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n", This
, debug_d3dusage(vboUsage
));
417 /* Make sure that a context is there. Needed in a multithreaded environment. Otherwise this call is a nop */
418 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
421 /* Make sure that the gl error is cleared. Do not use checkGLcall
422 * here because checkGLcall just prints a fixme and continues. However,
423 * if an error during VBO creation occurs we can fall back to non-vbo operation
424 * with full functionality(but performance loss)
426 while(glGetError() != GL_NO_ERROR
);
428 /* Basically the FVF parameter passed to CreateVertexBuffer is no good
429 * It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
430 * IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
431 * look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
432 * to check if the rhw and color values are in the correct format.
435 GL_EXTCALL(glGenBuffersARB(1, &This
->vbo
));
436 error
= glGetError();
437 if(This
->vbo
== 0 || error
!= GL_NO_ERROR
) {
438 WARN("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error
), error
);
442 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
443 error
= glGetError();
444 if(error
!= GL_NO_ERROR
) {
445 WARN("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error
), error
);
449 /* Don't use static, because dx apps tend to update the buffer
450 * quite often even if they specify 0 usage. Because we always keep the local copy
451 * we never read from the vbo and can create a write only opengl buffer.
453 switch(vboUsage
& (WINED3DUSAGE_WRITEONLY
| WINED3DUSAGE_DYNAMIC
) ) {
454 case WINED3DUSAGE_WRITEONLY
| WINED3DUSAGE_DYNAMIC
:
455 case WINED3DUSAGE_DYNAMIC
:
456 TRACE("Gl usage = GL_STREAM_DRAW\n");
457 glUsage
= GL_STREAM_DRAW_ARB
;
459 case WINED3DUSAGE_WRITEONLY
:
461 TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
462 glUsage
= GL_DYNAMIC_DRAW_ARB
;
466 /* Reserve memory for the buffer. The amount of data won't change
467 * so we are safe with calling glBufferData once with a NULL ptr and
468 * calling glBufferSubData on updates
470 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB
, This
->resource
.size
, NULL
, glUsage
));
471 error
= glGetError();
472 if(error
!= GL_NO_ERROR
) {
473 WARN("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error
), error
);
476 This
->vbo_size
= This
->resource
.size
;
477 This
->vbo_usage
= glUsage
;
478 This
->dirtystart
= 0;
479 This
->dirtyend
= This
->resource
.size
;
480 This
->Flags
|= VBFLAG_DIRTY
;
486 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
487 FIXME("Failed to create a vertex buffer object. Continuing, but performance issues can occur\n");
488 if(This
->vbo
) GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
494 static void WINAPI
IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer
*iface
) {
495 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*) iface
;
496 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
498 UINT start
= 0, end
= 0, vertices
;
499 BOOL declChanged
= FALSE
;
501 TRACE("(%p)->()\n", This
);
504 /* TODO: Make converting independent from VBOs */
505 if(This
->Flags
& VBFLAG_CREATEVBO
) {
507 This
->Flags
&= ~VBFLAG_CREATEVBO
;
509 return; /* Not doing any conversion */
513 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
514 if(device
->isInDraw
&& This
->bindCount
> 0) {
515 declChanged
= IWineD3DVertexBufferImpl_FindDecl(This
);
516 } else if(This
->Flags
& VBFLAG_HASDESC
) {
517 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
518 * the stream source state handler will call PreLoad again and the change will be caught
521 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
522 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
523 * declaration for the buffer can be found
528 /* If applications change the declaration over and over, reconverting all the time is a huge
529 * performance hit. So count the declaration changes and release the VBO if there are too many
530 * of them (and thus stop converting)
536 if(This
->declChanges
> VB_MAXDECLCHANGES
) {
537 FIXME("Too many declaration changes, stopping converting\n");
538 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
540 GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
541 checkGLcall("glDeleteBuffersARB");
544 HeapFree(GetProcessHeap(), 0, This
->conv_shift
);
546 /* The stream source state handler might have read the memory of the vertex buffer already
547 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
548 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
551 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_STREAMSRC
);
555 check_vbo_size(This
);
557 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
558 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
559 * decl changes and reset the decl change count after a specific number of them
562 if(This
->draws
> VB_RESETDECLCHANGE
) This
->declChanges
= 0;
566 /* The declaration changed, reload the whole buffer */
567 WARN("Reloading buffer because of decl change\n");
569 end
= This
->resource
.size
;
570 } else if(This
->Flags
& VBFLAG_DIRTY
) {
571 /* No decl change, but dirty data, reload the changed stuff */
572 if(This
->conv_shift
) {
573 if(This
->dirtystart
!= 0 || This
->dirtyend
!= 0) {
574 FIXME("Implement partial buffer loading with shifted conversion\n");
577 start
= This
->dirtystart
;
578 end
= This
->dirtyend
;
580 /* Desc not changed, buffer not dirty, nothing to do :-) */
584 /* Mark the buffer clean */
585 This
->Flags
&= ~VBFLAG_DIRTY
;
586 This
->dirtystart
= 0;
589 if(!This
->conv_map
) {
590 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
591 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
592 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
594 TRACE("No conversion needed\n");
596 if(!device
->isInDraw
) {
597 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
600 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
601 checkGLcall("glBindBufferARB");
602 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
, start
, end
-start
, This
->resource
.allocatedMemory
+ start
));
603 checkGLcall("glBufferSubDataARB");
608 /* Now for each vertex in the buffer that needs conversion */
609 vertices
= This
->resource
.size
/ This
->stride
;
611 if(This
->conv_shift
) {
612 TRACE("Shifted conversion\n");
613 data
= HeapAlloc(GetProcessHeap(), 0, vertices
* This
->conv_stride
);
615 for(i
= start
/ This
->stride
; i
< min((end
/ This
->stride
) + 1, vertices
); i
++) {
616 for(j
= 0; j
< This
->stride
; j
++) {
617 switch(This
->conv_map
[j
]) {
619 data
[This
->conv_stride
* i
+ j
+ This
->conv_shift
[j
]] = This
->resource
.allocatedMemory
[This
->stride
* i
+ j
];
624 float *out
= (float *) (&data
[This
->conv_stride
* i
+ j
+ This
->conv_shift
[j
]]);
625 WORD
*in
= (WORD
*) (&This
->resource
.allocatedMemory
[i
* This
->stride
+ j
]);
627 out
[1] = float_16_to_32(in
+ 1);
628 out
[0] = float_16_to_32(in
+ 0);
629 j
+= 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
634 FIXME("Unimplemented conversion %d in shifted conversion\n", This
->conv_map
[j
]);
640 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
641 checkGLcall("glBindBufferARB");
642 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, vertices
* This
->conv_stride
, data
));
643 checkGLcall("glBufferSubDataARB");
647 data
= HeapAlloc(GetProcessHeap(), 0, This
->resource
.size
);
648 memcpy(data
+ start
, This
->resource
.allocatedMemory
+ start
, end
- start
);
649 for(i
= start
/ This
->stride
; i
< min((end
/ This
->stride
) + 1, vertices
); i
++) {
650 for(j
= 0; j
< This
->stride
; j
++) {
651 switch(This
->conv_map
[j
]) {
657 fixup_d3dcolor((DWORD
*) (data
+ i
* This
->stride
+ j
));
662 fixup_transformed_pos((float *) (data
+ i
* This
->stride
+ j
));
667 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
669 FIXME("Unimplemented conversion %d in shifted conversion\n", This
->conv_map
[j
]);
675 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
676 checkGLcall("glBindBufferARB");
677 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
, start
, end
- start
, data
+ start
));
678 checkGLcall("glBufferSubDataARB");
682 HeapFree(GetProcessHeap(), 0, data
);
685 static void WINAPI
IWineD3DVertexBufferImpl_UnLoad(IWineD3DVertexBuffer
*iface
) {
686 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*) iface
;
687 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
688 TRACE("(%p)\n", This
);
690 /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
691 * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
692 * that vbos are supported
695 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
697 GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
698 checkGLcall("glDeleteBuffersARB");
701 This
->Flags
|= VBFLAG_CREATEVBO
; /* Recreate the VBO next load */
705 static WINED3DRESOURCETYPE WINAPI
IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer
*iface
) {
706 return IWineD3DResourceImpl_GetType((IWineD3DResource
*)iface
);
709 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer
*iface
, IUnknown
**pParent
) {
710 return IWineD3DResourceImpl_GetParent((IWineD3DResource
*)iface
, pParent
);
713 /* ******************************************************
714 IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
715 ****************************************************** */
716 static HRESULT WINAPI
IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer
*iface
, UINT OffsetToLock
, UINT SizeToLock
, BYTE
** ppbData
, DWORD Flags
) {
717 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
719 TRACE("(%p)->%d, %d, %p, %08x\n", This
, OffsetToLock
, SizeToLock
, ppbData
, Flags
);
721 InterlockedIncrement(&This
->lockcount
);
723 if(This
->Flags
& VBFLAG_DIRTY
) {
724 if(This
->dirtystart
> OffsetToLock
) This
->dirtystart
= OffsetToLock
;
726 if(This
->dirtyend
< OffsetToLock
+ SizeToLock
) This
->dirtyend
= OffsetToLock
+ SizeToLock
;
728 This
->dirtyend
= This
->resource
.size
;
731 This
->dirtystart
= OffsetToLock
;
733 This
->dirtyend
= OffsetToLock
+ SizeToLock
;
735 This
->dirtyend
= This
->resource
.size
;
738 data
= This
->resource
.allocatedMemory
;
739 This
->Flags
|= VBFLAG_DIRTY
;
740 *ppbData
= data
+ OffsetToLock
;
742 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This
, data
+ OffsetToLock
, data
, OffsetToLock
);
743 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
746 HRESULT WINAPI
IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer
*iface
) {
747 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*) iface
;
749 TRACE("(%p)\n", This
);
751 lockcount
= InterlockedDecrement(&This
->lockcount
);
753 /* Delay loading the buffer until everything is unlocked */
754 TRACE("Ignoring the unlock\n");
758 if(This
->Flags
& VBFLAG_HASDESC
) {
759 IWineD3DVertexBufferImpl_PreLoad(iface
);
763 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer
*iface
, WINED3DVERTEXBUFFER_DESC
*pDesc
) {
764 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
766 TRACE("(%p)\n", This
);
767 pDesc
->Format
= This
->resource
.format
;
768 pDesc
->Type
= This
->resource
.resourceType
;
769 pDesc
->Usage
= This
->resource
.usage
;
770 pDesc
->Pool
= This
->resource
.pool
;
771 pDesc
->Size
= This
->resource
.size
;
772 pDesc
->FVF
= This
->fvf
;
776 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl
=
779 IWineD3DVertexBufferImpl_QueryInterface
,
780 IWineD3DVertexBufferImpl_AddRef
,
781 IWineD3DVertexBufferImpl_Release
,
782 /* IWineD3DResource */
783 IWineD3DVertexBufferImpl_GetParent
,
784 IWineD3DVertexBufferImpl_GetDevice
,
785 IWineD3DVertexBufferImpl_SetPrivateData
,
786 IWineD3DVertexBufferImpl_GetPrivateData
,
787 IWineD3DVertexBufferImpl_FreePrivateData
,
788 IWineD3DVertexBufferImpl_SetPriority
,
789 IWineD3DVertexBufferImpl_GetPriority
,
790 IWineD3DVertexBufferImpl_PreLoad
,
791 IWineD3DVertexBufferImpl_UnLoad
,
792 IWineD3DVertexBufferImpl_GetType
,
793 /* IWineD3DVertexBuffer */
794 IWineD3DVertexBufferImpl_Lock
,
795 IWineD3DVertexBufferImpl_Unlock
,
796 IWineD3DVertexBufferImpl_GetDesc
799 BYTE
* WINAPI
IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer
* iface
, DWORD iOffset
, GLint
*vbo
) {
800 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
804 if(This
->Flags
& VBFLAG_CREATEVBO
) {
806 This
->Flags
&= ~VBFLAG_CREATEVBO
;
809 return (BYTE
*) iOffset
;
812 return This
->resource
.allocatedMemory
+ iOffset
;
814 return (BYTE
*) iOffset
;
818 HRESULT WINAPI
IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer
* iface
) {