wined3d: Change the vertex buffer declaration's data format.
[wine/multimedia.git] / dlls / wined3d / vertexbuffer.c
blob638266224a13e81476853aab0f78743a7fc84757
1 /*
2 * IWineD3DVertexBuffer Implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
6 * Copyright 2004 Christian Costa
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
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
30 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
32 /* *******************************************
33 IWineD3DVertexBuffer IUnknown parts follow
34 ******************************************* */
35 static HRESULT WINAPI IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer *iface, REFIID riid, LPVOID *ppobj)
37 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
38 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
39 if (IsEqualGUID(riid, &IID_IUnknown)
40 || IsEqualGUID(riid, &IID_IWineD3DBase)
41 || IsEqualGUID(riid, &IID_IWineD3DResource)
42 || IsEqualGUID(riid, &IID_IWineD3DVertexBuffer)){
43 IUnknown_AddRef(iface);
44 *ppobj = This;
45 return S_OK;
47 *ppobj = NULL;
48 return E_NOINTERFACE;
51 static ULONG WINAPI IWineD3DVertexBufferImpl_AddRef(IWineD3DVertexBuffer *iface) {
52 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
53 ULONG ref = InterlockedIncrement(&This->resource.ref);
54 TRACE("(%p) : AddRef increasing from %d\n", This, ref - 1);
55 return ref;
58 static ULONG WINAPI IWineD3DVertexBufferImpl_Release(IWineD3DVertexBuffer *iface) {
59 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
60 ULONG ref = InterlockedDecrement(&This->resource.ref);
61 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
62 if (ref == 0) {
64 if(This->vbo) {
65 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
67 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
68 ENTER_GL();
69 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
70 checkGLcall("glDeleteBuffersARB");
71 LEAVE_GL();
74 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
75 HeapFree(GetProcessHeap(), 0, This);
77 return ref;
80 /* ****************************************************
81 IWineD3DVertexBuffer IWineD3DResource parts follow
82 **************************************************** */
83 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer *iface, IWineD3DDevice** ppDevice) {
84 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
87 static HRESULT WINAPI IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
88 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
91 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
92 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
95 static HRESULT WINAPI IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid) {
96 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
99 static DWORD WINAPI IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer *iface, DWORD PriorityNew) {
100 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
103 static DWORD WINAPI IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer *iface) {
104 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
107 static inline void fixup_d3dcolor(DWORD *pos) {
108 DWORD srcColor = *pos;
110 /* Color conversion like in drawStridedSlow. watch out for little endianity
111 * If we want that stuff to work on big endian machines too we have to consider more things
113 * 0xff000000: Alpha mask
114 * 0x00ff0000: Blue mask
115 * 0x0000ff00: Green mask
116 * 0x000000ff: Red mask
118 *pos = 0;
119 *pos |= (srcColor & 0xff00ff00) ; /* Alpha Green */
120 *pos |= (srcColor & 0x00ff0000) >> 16; /* Red */
121 *pos |= (srcColor & 0x000000ff) << 16; /* Blue */
124 static inline void fixup_transformed_pos(float *p) {
125 float x, y, z, w;
127 /* rhw conversion like in drawStridedSlow */
128 if(p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps))) {
129 x = p[0];
130 y = p[1];
131 z = p[2];
132 w = 1.0;
133 } else {
134 w = 1.0 / p[3];
135 x = p[0] * w;
136 y = p[1] * w;
137 z = p[2] * w;
139 p[0] = x;
140 p[1] = y;
141 p[2] = z;
142 p[3] = w;
145 DWORD *find_conversion_shift(IWineD3DVertexBufferImpl *This, WineDirect3DVertexStridedData *strided, DWORD stride) {
146 DWORD *ret, i, shift, j, type;
147 DWORD orig_type_size;
149 if(!stride) {
150 TRACE("No shift\n");
151 return NULL;
154 This->conv_stride = stride;
155 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
156 for(i = 0; i < MAX_ATTRIBS; i++) {
157 if(strided->u.input[i].VBO != This->vbo) continue;
159 type = strided->u.input[i].dwType;
160 if(type == WINED3DDECLTYPE_FLOAT16_2) {
161 shift = 4;
162 } else if(type == WINED3DDECLTYPE_FLOAT16_4) {
163 shift = 8;
164 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
165 * compatible
167 for(j = 4; j < 8; j++) {
168 ret[(DWORD_PTR) strided->u.input[i].lpData + j ] += 4;
170 } else {
171 shift = 0;
173 This->conv_stride += shift;
175 if(shift) {
176 orig_type_size = WINED3D_ATR_TYPESIZE(type) * WINED3D_ATR_SIZE(type);
177 for(j = (DWORD_PTR) strided->u.input[i].lpData + orig_type_size; j < stride; j++) {
178 ret[j] += shift;
183 if(TRACE_ON(d3d)) {
184 TRACE("Dumping conversion shift:\n");
185 for(i = 0; i < stride; i++) {
186 TRACE("[%d]", ret[i]);
188 TRACE("\n");
190 return ret;
193 static inline BOOL process_converted_attribute(IWineD3DVertexBufferImpl *This,
194 const enum vbo_conversion_type conv_type,
195 const WineDirect3DStridedData *attrib,
196 DWORD *stride_this_run, const DWORD type) {
197 DWORD attrib_size;
198 BOOL ret = FALSE;
199 int i;
201 /* Check for some valid situations which cause us pain. One is if the buffer is used for
202 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
203 * with different strides. In the 2nd case we might have to drop conversion entirely,
204 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
206 if(attrib->dwStride == 0) {
207 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
208 debug_d3ddecltype(type));
209 } else if(attrib->dwStride != *stride_this_run &&
210 *stride_this_run) {
211 FIXME("Got two concurrent strides, %d and %d\n", attrib->dwStride, *stride_this_run);
212 } else {
213 *stride_this_run = attrib->dwStride;
214 if(This->stride != *stride_this_run) {
215 /* We rely that this happens only on the first converted attribute that is found,
216 * if at all. See above check
218 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
219 This->stride = *stride_this_run;
220 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conv_map);
221 This->conv_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->conv_map) * This->stride);
222 ret = TRUE;
226 attrib_size = WINED3D_ATR_SIZE(type) * WINED3D_ATR_TYPESIZE(type);
227 for(i = 0; i < attrib_size; i++) {
228 if(This->conv_map[(DWORD_PTR) attrib->lpData + i] != conv_type) {
229 TRACE("Byte %ld in vertex changed\n", i + (DWORD_PTR) attrib->lpData);
230 TRACE("It was type %d, is %d now\n", This->conv_map[(DWORD_PTR) attrib->lpData + i], conv_type);
231 ret = TRUE;
232 This->conv_map[(DWORD_PTR) attrib->lpData + i] = conv_type;
235 return ret;
238 static inline BOOL check_attribute(IWineD3DVertexBufferImpl *This, const WineDirect3DStridedData *attrib,
239 const BOOL check_d3dcolor, const BOOL is_ffp_position, const BOOL is_ffp_color,
240 DWORD *stride_this_run, BOOL *float16_used) {
241 BOOL ret = FALSE;
242 DWORD type, attrib_size;
243 int i;
245 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
246 * there, on nonexistant attribs the vbo is 0.
248 if(attrib->VBO != This->vbo) return FALSE;
250 type = attrib->dwType;
251 /* Look for newly appeared conversion */
252 if(!GL_SUPPORT(NV_HALF_FLOAT) && (
253 type == WINED3DDECLTYPE_FLOAT16_2 ||
254 type == WINED3DDECLTYPE_FLOAT16_4)) {
256 ret = process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run, type);
258 if(is_ffp_position) {
259 FIXME("Test FLOAT16 fixed function processing positions\n");
260 } else if(is_ffp_color) {
261 FIXME("test FLOAT16 fixed function processing colors\n");
263 *float16_used = TRUE;
264 } else if(check_d3dcolor && type == WINED3DDECLTYPE_D3DCOLOR) {
266 ret = process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run, WINED3DDECLTYPE_D3DCOLOR);
268 if(!is_ffp_color) {
269 FIXME("Test for non-color fixed function D3DCOLOR type\n");
271 } else if(is_ffp_position && type == WINED3DDECLTYPE_FLOAT4) {
272 ret = process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run, WINED3DDECLTYPE_FLOAT4);
273 } else if(This->conv_map) {
274 attrib_size = WINED3D_ATR_SIZE(type) * WINED3D_ATR_TYPESIZE(type);
276 for(i = 0; i < attrib_size; i++) {
277 if(This->conv_map[(DWORD_PTR) attrib->lpData + i] != CONV_NONE) {
278 TRACE("Byte %ld in vertex changed\n", i + (DWORD_PTR) attrib->lpData);
279 TRACE("It was type %d, is CONV_NONE now\n", This->conv_map[(DWORD_PTR) attrib->lpData + i]);
280 ret = TRUE;
281 This->conv_map[(DWORD_PTR) attrib->lpData + i] = CONV_NONE;
285 return ret;
288 inline BOOL WINAPI IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl *This)
290 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
291 BOOL ret = FALSE;
292 int i;
293 DWORD stride_this_run = 0;
294 BOOL float16_used = FALSE;
296 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
297 * Once we have our declaration there is no need to look it up again.
299 if(((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->Flags & VBFLAG_HASDESC) {
300 return FALSE;
303 TRACE("Finding vertex buffer conversion information\n");
304 /* Certain declaration types need some fixups before we can pass them to opengl. This means D3DCOLOR attributes with fixed
305 * function vertex processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if GL_NV_half_float is not supported.
307 * The vertex buffer FVF doesn't help with finding them, we have to use the decoded vertex declaration and pick the things
308 * that concern the current buffer. A problem with this is that this can change between draws, so we have to validate
309 * the information and reprocess the buffer if it changes, and avoid false positives for performance reasons.
311 * We have do destinguish between vertex shaders and fixed function to pick the way we access the
312 * strided vertex information.
314 * This code sets up a per-byte array with the size of the detected stride of the arrays in the
315 * buffer. For each byte we have a field that marks the conversion needed on this byte. For example,
316 * the following declaration with fixed function vertex processing:
318 * POSITIONT, FLOAT4
319 * NORMAL, FLOAT3
320 * DIFFUSE, FLOAT16_4
321 * SPECULAR, D3DCOLOR
323 * Will result in
324 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
325 * [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]
327 * Where in this example map P means 4 component position conversion, 0 means no conversion, F means FLOAT16_2 conversion
328 * and C means D3DCOLOR conversion(red / blue swizzle).
330 * If we're doing conversion and the stride changes we have to reconvert the whole buffer. Note that we do not mind if the
331 * semantic changes, we only care for the conversion type. So if the NORMAL is replaced with a TEXCOORD, nothing has to be
332 * done, or if the DIFFUSE is replaced with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some conversion types
333 * depend on the semantic as well, for example a FLOAT4 texcoord needs no conversion while a FLOAT4 positiont needs one
335 if(use_vs(device)) {
336 TRACE("vhsader\n");
337 /* If the current vertex declaration is marked for no half float conversion don't bother to
338 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
339 * if we used conversion before
341 if(!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed) {
342 if(This->conv_map) {
343 TRACE("Now using shaders without conversion, but conversion used before\n");
344 HeapFree(GetProcessHeap(), 0, This->conv_map);
345 HeapFree(GetProcessHeap(), 0, This->conv_shift);
346 This->conv_map = NULL;
347 This->stride = 0;
348 This->conv_shift = NULL;
349 This->conv_stride = 0;
350 return TRUE;
351 } else {
352 return FALSE;
355 for(i = 0; i < MAX_ATTRIBS; i++) {
356 ret = check_attribute(This, &device->strided_streams.u.input[i], FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
359 /* Recalculate the conversion shift map if the declaration has changed,
360 * and we're using float16 conversion or used it on the last run
362 if(ret && (float16_used || This->conv_map)) {
363 HeapFree(GetProcessHeap(), 0, This->conv_shift);
364 This->conv_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
366 } else {
367 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
368 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
369 * the attributes that our current fixed function pipeline implementation cares for.
371 ret = check_attribute(This, &device->strided_streams.u.s.position, TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
372 ret = check_attribute(This, &device->strided_streams.u.s.normal, TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
373 ret = check_attribute(This, &device->strided_streams.u.s.diffuse, TRUE, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
374 ret = check_attribute(This, &device->strided_streams.u.s.specular, TRUE, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
375 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[0], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
376 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[1], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
377 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[2], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
378 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[3], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
379 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[4], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
380 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[5], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
381 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[6], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
382 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[7], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
384 if(float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
387 if(stride_this_run == 0 && This->conv_map) {
388 /* Sanity test */
389 if(ret == FALSE) {
390 ERR("no converted attributes found, old conversion map exists, and no declaration change???\n");
392 HeapFree(GetProcessHeap(), 0, This->conv_map);
393 This->conv_map = NULL;
394 This->stride = 0;
396 This->Flags |= VBFLAG_HASDESC;
398 if(ret) TRACE("Conversion information changed\n");
399 return ret;
402 static void check_vbo_size(IWineD3DVertexBufferImpl *This) {
403 DWORD size = This->conv_stride ? This->conv_stride * (This->resource.size / This->stride) : This->resource.size;
404 if(This->vbo_size != size) {
405 TRACE("Old size %d, creating new size %d\n", This->vbo_size, size);
406 ENTER_GL();
407 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
408 checkGLcall("glBindBufferARB");
409 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, NULL, This->vbo_usage));
410 This->vbo_size = size;
411 checkGLcall("glBufferDataARB");
412 LEAVE_GL();
416 static void WINAPI IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer *iface) {
417 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
418 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
419 BYTE *data;
420 UINT start = 0, end = 0, vertices;
421 BOOL declChanged = FALSE;
422 int i, j;
423 TRACE("(%p)->()\n", This);
425 if(This->Flags & VBFLAG_LOAD) {
426 return; /* Already doing that stuff */
429 if(!This->vbo) {
430 /* TODO: Make converting independent from VBOs */
431 return; /* Not doing any conversion */
434 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
435 if(device->isInDraw && This->bindCount > 0) {
436 declChanged = IWineD3DVertexBufferImpl_FindDecl(This);
437 } else if(This->Flags & VBFLAG_HASDESC) {
438 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
439 * the stream source state handler will call PreLoad again and the change will be cought
441 } else {
442 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
443 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
444 * declaration for the buffer can be found
446 return;
449 /* If applications change the declaration over and over, reconverting all the time is a huge
450 * performance hit. So count the declaration changes and release the VBO if there are too much
451 * of them(and thus stop converting)
453 if(declChanged) {
454 This->declChanges++;
455 This->draws = 0;
457 if(This->declChanges > VB_MAXDECLCHANGES) {
458 FIXME("Too many declaration changes, stopping converting\n");
459 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
460 ENTER_GL();
461 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
462 checkGLcall("glDeleteBuffersARB");
463 LEAVE_GL();
464 This->vbo = 0;
465 HeapFree(GetProcessHeap(), 0, This->conv_shift);
467 /* The stream source state handler might have read the memory of the vertex buffer already
468 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
469 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
470 * rarely
472 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
474 return;
476 check_vbo_size(This);
477 } else {
478 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
479 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
480 * decl changes and reset the decl change count after a specific number of them
482 This->draws++;
483 if(This->draws > VB_RESETDECLCHANGE) This->declChanges = 0;
486 if(declChanged) {
487 /* The declaration changed, reload the whole buffer */
488 WARN("Reloading buffer because of decl change\n");
489 start = 0;
490 end = This->resource.size;
491 } else if(This->Flags & VBFLAG_DIRTY) {
492 /* No decl change, but dirty data, reload the changed stuff */
493 if(This->conv_shift) {
494 if(This->dirtystart != 0 || This->dirtyend != 0) {
495 FIXME("Implement partial buffer loading with shifted conversion\n");
498 start = This->dirtystart;
499 end = This->dirtyend;
500 } else {
501 /* Desc not changed, buffer not dirty, nothing to do :-) */
502 return;
505 /* Mark the buffer clean */
506 This->Flags &= ~VBFLAG_DIRTY;
507 This->dirtystart = 0;
508 This->dirtyend = 0;
510 if(!This->conv_map) {
511 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
512 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
513 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
515 TRACE("No conversion needed\n");
517 if(!device->isInDraw) {
518 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
520 ENTER_GL();
521 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
522 checkGLcall("glBindBufferARB");
523 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end-start, This->resource.allocatedMemory + start));
524 checkGLcall("glBufferSubDataARB");
525 LEAVE_GL();
526 return;
529 /* Now for each vertex in the buffer that needs conversion */
530 vertices = This->resource.size / This->stride;
532 if(This->conv_shift) {
533 TRACE("Shifted conversion\n");
534 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conv_stride);
536 for(i = start / This->stride; i < min((end / This->stride) + 1, vertices); i++) {
537 for(j = 0; j < This->stride; j++) {
538 switch(This->conv_map[j]) {
539 case CONV_NONE:
540 data[This->conv_stride * i + j + This->conv_shift[j]] = This->resource.allocatedMemory[This->stride * i + j];
541 break;
543 case CONV_FLOAT16_2:
545 float *out = (float *) (&data[This->conv_stride * i + j + This->conv_shift[j]]);
546 WORD *in = (WORD *) (&This->resource.allocatedMemory[i * This->stride + j]);
548 out[1] = float_16_to_32(in + 1);
549 out[0] = float_16_to_32(in + 0);
550 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
551 break;
554 default:
555 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
560 ENTER_GL();
561 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
562 checkGLcall("glBindBufferARB");
563 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, vertices * This->conv_stride, data));
564 checkGLcall("glBufferSubDataARB");
565 LEAVE_GL();
567 } else {
568 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
569 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
570 for(i = start / This->stride; i < min((end / This->stride) + 1, vertices); i++) {
571 for(j = 0; j < This->stride; j++) {
572 switch(This->conv_map[j]) {
573 case CONV_NONE:
574 /* Done already */
575 /* Vertex attribute sizes are always multiples of 4, but we can't skip 3 additional bytes
576 * because the attributes don't have to start at aligned offsets
578 break;
579 case CONV_D3DCOLOR:
580 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
581 j += 3;
582 break;
584 case CONV_POSITIONT:
585 fixup_transformed_pos((float *) (data + i * This->stride + j));
586 j += 15;
587 break;
589 case CONV_FLOAT16_2:
590 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
591 default:
592 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
597 ENTER_GL();
598 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
599 checkGLcall("glBindBufferARB");
600 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data + start));
601 checkGLcall("glBufferSubDataARB");
602 LEAVE_GL();
605 HeapFree(GetProcessHeap(), 0, data);
608 static WINED3DRESOURCETYPE WINAPI IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer *iface) {
609 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
612 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer *iface, IUnknown **pParent) {
613 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
616 /* ******************************************************
617 IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
618 ****************************************************** */
619 static HRESULT WINAPI IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
620 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
621 BYTE *data;
622 TRACE("(%p)->%d, %d, %p, %08x\n", This, OffsetToLock, SizeToLock, ppbData, Flags);
624 InterlockedIncrement(&This->lockcount);
626 if(This->Flags & VBFLAG_DIRTY) {
627 if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
628 if(SizeToLock) {
629 if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
630 } else {
631 This->dirtyend = This->resource.size;
633 } else {
634 This->dirtystart = OffsetToLock;
635 if(SizeToLock)
636 This->dirtyend = OffsetToLock + SizeToLock;
637 else
638 This->dirtyend = This->resource.size;
641 data = This->resource.allocatedMemory;
642 This->Flags |= VBFLAG_DIRTY;
643 *ppbData = data + OffsetToLock;
645 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This, data + OffsetToLock, data, OffsetToLock);
646 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
647 return WINED3D_OK;
649 HRESULT WINAPI IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer *iface) {
650 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
651 LONG lockcount;
652 TRACE("(%p)\n", This);
654 lockcount = InterlockedDecrement(&This->lockcount);
655 if(lockcount > 0) {
656 /* Delay loading the buffer until everything is unlocked */
657 TRACE("Ignoring the unlock\n");
658 return WINED3D_OK;
661 if(This->Flags & VBFLAG_HASDESC) {
662 IWineD3DVertexBufferImpl_PreLoad(iface);
664 return WINED3D_OK;
666 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer *iface, WINED3DVERTEXBUFFER_DESC *pDesc) {
667 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
669 TRACE("(%p)\n", This);
670 pDesc->Format = This->resource.format;
671 pDesc->Type = This->resource.resourceType;
672 pDesc->Usage = This->resource.usage;
673 pDesc->Pool = This->resource.pool;
674 pDesc->Size = This->resource.size;
675 pDesc->FVF = This->fvf;
676 return WINED3D_OK;
679 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl =
681 /* IUnknown */
682 IWineD3DVertexBufferImpl_QueryInterface,
683 IWineD3DVertexBufferImpl_AddRef,
684 IWineD3DVertexBufferImpl_Release,
685 /* IWineD3DResource */
686 IWineD3DVertexBufferImpl_GetParent,
687 IWineD3DVertexBufferImpl_GetDevice,
688 IWineD3DVertexBufferImpl_SetPrivateData,
689 IWineD3DVertexBufferImpl_GetPrivateData,
690 IWineD3DVertexBufferImpl_FreePrivateData,
691 IWineD3DVertexBufferImpl_SetPriority,
692 IWineD3DVertexBufferImpl_GetPriority,
693 IWineD3DVertexBufferImpl_PreLoad,
694 IWineD3DVertexBufferImpl_GetType,
695 /* IWineD3DVertexBuffer */
696 IWineD3DVertexBufferImpl_Lock,
697 IWineD3DVertexBufferImpl_Unlock,
698 IWineD3DVertexBufferImpl_GetDesc
701 BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo) {
702 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
704 *vbo = This->vbo;
705 if(This->vbo == 0) {
706 return This->resource.allocatedMemory + iOffset;
707 } else {
708 return (BYTE *) iOffset;
712 HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface) {
713 return WINED3D_OK;