Sync to trunk head(r38096)
[reactos.git] / reactos / dll / directx / wine / wined3d / vertexbuffer.c
1 /*
2 * IWineD3DVertexBuffer Implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
31 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
32
33 /* *******************************************
34 IWineD3DVertexBuffer IUnknown parts follow
35 ******************************************* */
36 static HRESULT WINAPI IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer *iface, REFIID riid, LPVOID *ppobj)
37 {
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);
45 *ppobj = This;
46 return S_OK;
47 }
48 *ppobj = NULL;
49 return E_NOINTERFACE;
50 }
51
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);
56 return ref;
57 }
58
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);
63 if (ref == 0) {
64
65 if(This->vbo) {
66 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
67
68 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
69 ENTER_GL();
70 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
71 checkGLcall("glDeleteBuffersARB");
72 LEAVE_GL();
73 }
74
75 resource_cleanup((IWineD3DResource *)iface);
76 HeapFree(GetProcessHeap(), 0, This);
77 }
78 return ref;
79 }
80
81 /* ****************************************************
82 IWineD3DVertexBuffer IWineD3DResource parts follow
83 **************************************************** */
84 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer *iface, IWineD3DDevice** ppDevice) {
85 return resource_get_device((IWineD3DResource *)iface, ppDevice);
86 }
87
88 static HRESULT WINAPI IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
89 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
90 }
91
92 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
93 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
94 }
95
96 static HRESULT WINAPI IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid) {
97 return resource_free_private_data((IWineD3DResource *)iface, refguid);
98 }
99
100 static DWORD WINAPI IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer *iface, DWORD PriorityNew) {
101 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
102 }
103
104 static DWORD WINAPI IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer *iface) {
105 return resource_get_priority((IWineD3DResource *)iface);
106 }
107
108 static inline void fixup_d3dcolor(DWORD *pos) {
109 DWORD srcColor = *pos;
110
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
113 *
114 * 0xff000000: Alpha mask
115 * 0x00ff0000: Blue mask
116 * 0x0000ff00: Green mask
117 * 0x000000ff: Red mask
118 */
119 *pos = 0;
120 *pos |= (srcColor & 0xff00ff00) ; /* Alpha Green */
121 *pos |= (srcColor & 0x00ff0000) >> 16; /* Red */
122 *pos |= (srcColor & 0x000000ff) << 16; /* Blue */
123 }
124
125 static inline void fixup_transformed_pos(float *p) {
126 float x, y, z, w;
127
128 /* rhw conversion like in drawStridedSlow */
129 if(p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps))) {
130 x = p[0];
131 y = p[1];
132 z = p[2];
133 w = 1.0;
134 } else {
135 w = 1.0 / p[3];
136 x = p[0] * w;
137 y = p[1] * w;
138 z = p[2] * w;
139 }
140 p[0] = x;
141 p[1] = y;
142 p[2] = z;
143 p[3] = w;
144 }
145
146 static DWORD *find_conversion_shift(IWineD3DVertexBufferImpl *This, const WineDirect3DVertexStridedData *strided, DWORD stride)
147 {
148 DWORD *ret, i, shift, j, type;
149 DWORD orig_type_size;
150
151 if(!stride) {
152 TRACE("No shift\n");
153 return NULL;
154 }
155
156 This->conv_stride = stride;
157 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
158 for(i = 0; i < MAX_ATTRIBS; i++) {
159 if(strided->u.input[i].VBO != This->vbo) continue;
160
161 type = strided->u.input[i].dwType;
162 if(type == WINED3DDECLTYPE_FLOAT16_2) {
163 shift = 4;
164 } else if(type == WINED3DDECLTYPE_FLOAT16_4) {
165 shift = 8;
166 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
167 * compatible
168 */
169 for(j = 4; j < 8; j++) {
170 ret[(DWORD_PTR) strided->u.input[i].lpData + j ] += 4;
171 }
172 } else {
173 shift = 0;
174 }
175 This->conv_stride += shift;
176
177 if(shift) {
178 orig_type_size = WINED3D_ATR_TYPESIZE(type) * WINED3D_ATR_SIZE(type);
179 for(j = (DWORD_PTR) strided->u.input[i].lpData + orig_type_size; j < stride; j++) {
180 ret[j] += shift;
181 }
182 }
183 }
184
185 if(TRACE_ON(d3d)) {
186 TRACE("Dumping conversion shift:\n");
187 for(i = 0; i < stride; i++) {
188 TRACE("[%d]", ret[i]);
189 }
190 TRACE("\n");
191 }
192 return ret;
193 }
194
195 static inline BOOL process_converted_attribute(IWineD3DVertexBufferImpl *This,
196 const enum vbo_conversion_type conv_type,
197 const WineDirect3DStridedData *attrib,
198 DWORD *stride_this_run, const DWORD type) {
199 DWORD attrib_size;
200 BOOL ret = FALSE;
201 int i;
202 DWORD offset = This->resource.wineD3DDevice->stateBlock->streamOffset[attrib->streamNo];
203 DWORD_PTR data;
204
205 /* Check for some valid situations which cause us pain. One is if the buffer is used for
206 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
207 * with different strides. In the 2nd case we might have to drop conversion entirely,
208 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
209 */
210 if(attrib->dwStride == 0) {
211 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
212 debug_d3ddecltype(type));
213 } else if(attrib->dwStride != *stride_this_run &&
214 *stride_this_run) {
215 FIXME("Got two concurrent strides, %d and %d\n", attrib->dwStride, *stride_this_run);
216 } else {
217 *stride_this_run = attrib->dwStride;
218 if(This->stride != *stride_this_run) {
219 /* We rely that this happens only on the first converted attribute that is found,
220 * if at all. See above check
221 */
222 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
223 This->stride = *stride_this_run;
224 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conv_map);
225 This->conv_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->conv_map) * This->stride);
226 ret = TRUE;
227 }
228 }
229
230 data = (((DWORD_PTR) attrib->lpData) + offset) % This->stride;
231 attrib_size = WINED3D_ATR_SIZE(type) * WINED3D_ATR_TYPESIZE(type);
232 for(i = 0; i < attrib_size; i++) {
233 if(This->conv_map[data + i] != conv_type) {
234 TRACE("Byte %ld in vertex changed\n", i + data);
235 TRACE("It was type %d, is %d now\n", This->conv_map[data + i], conv_type);
236 ret = TRUE;
237 This->conv_map[data + i] = conv_type;
238 }
239 }
240 return ret;
241 }
242
243 static inline BOOL check_attribute(IWineD3DVertexBufferImpl *This, const WineDirect3DStridedData *attrib,
244 const BOOL check_d3dcolor, const BOOL is_ffp_position, const BOOL is_ffp_color,
245 DWORD *stride_this_run, BOOL *float16_used) {
246 BOOL ret = FALSE;
247 DWORD type;
248
249 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
250 * there, on nonexistent attribs the vbo is 0.
251 */
252 if(attrib->VBO != This->vbo) return FALSE;
253
254 type = attrib->dwType;
255 /* Look for newly appeared conversion */
256 if(!GL_SUPPORT(NV_HALF_FLOAT) && (
257 type == WINED3DDECLTYPE_FLOAT16_2 ||
258 type == WINED3DDECLTYPE_FLOAT16_4)) {
259
260 ret = process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run, type);
261
262 if(is_ffp_position) {
263 FIXME("Test FLOAT16 fixed function processing positions\n");
264 } else if(is_ffp_color) {
265 FIXME("test FLOAT16 fixed function processing colors\n");
266 }
267 *float16_used = TRUE;
268 } else if(check_d3dcolor && type == WINED3DDECLTYPE_D3DCOLOR) {
269
270 ret = process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run, WINED3DDECLTYPE_D3DCOLOR);
271
272 if(!is_ffp_color) {
273 FIXME("Test for non-color fixed function D3DCOLOR type\n");
274 }
275 } else if(is_ffp_position && type == WINED3DDECLTYPE_FLOAT4) {
276 ret = process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run, WINED3DDECLTYPE_FLOAT4);
277 } else if(This->conv_map) {
278 ret = process_converted_attribute(This, CONV_NONE, attrib, stride_this_run, type);
279 }
280 return ret;
281 }
282
283 static inline BOOL IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl *This)
284 {
285 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
286 BOOL ret = FALSE;
287 int i;
288 DWORD stride_this_run = 0;
289 BOOL float16_used = FALSE;
290
291 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
292 * Once we have our declaration there is no need to look it up again.
293 */
294 if(((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->Flags & VBFLAG_HASDESC) {
295 return FALSE;
296 }
297
298 TRACE("Finding vertex buffer conversion information\n");
299 /* Certain declaration types need some fixups before we can pass them to opengl. This means D3DCOLOR attributes with fixed
300 * function vertex processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if GL_NV_half_float is not supported.
301 *
302 * The vertex buffer FVF doesn't help with finding them, we have to use the decoded vertex declaration and pick the things
303 * that concern the current buffer. A problem with this is that this can change between draws, so we have to validate
304 * the information and reprocess the buffer if it changes, and avoid false positives for performance reasons.
305 *
306 * We have to distinguish between vertex shaders and fixed function to pick the way we access the
307 * strided vertex information.
308 *
309 * This code sets up a per-byte array with the size of the detected stride of the arrays in the
310 * buffer. For each byte we have a field that marks the conversion needed on this byte. For example,
311 * the following declaration with fixed function vertex processing:
312 *
313 * POSITIONT, FLOAT4
314 * NORMAL, FLOAT3
315 * DIFFUSE, FLOAT16_4
316 * SPECULAR, D3DCOLOR
317 *
318 * Will result in
319 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
320 * [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 *
322 * Where in this example map P means 4 component position conversion, 0 means no conversion, F means FLOAT16_2 conversion
323 * and C means D3DCOLOR conversion(red / blue swizzle).
324 *
325 * If we're doing conversion and the stride changes we have to reconvert the whole buffer. Note that we do not mind if the
326 * semantic changes, we only care for the conversion type. So if the NORMAL is replaced with a TEXCOORD, nothing has to be
327 * done, or if the DIFFUSE is replaced with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some conversion types
328 * depend on the semantic as well, for example a FLOAT4 texcoord needs no conversion while a FLOAT4 positiont needs one
329 */
330 if(use_vs(device)) {
331 TRACE("vhsader\n");
332 /* If the current vertex declaration is marked for no half float conversion don't bother to
333 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
334 * if we used conversion before
335 */
336 if(!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed) {
337 if(This->conv_map) {
338 TRACE("Now using shaders without conversion, but conversion used before\n");
339 HeapFree(GetProcessHeap(), 0, This->conv_map);
340 HeapFree(GetProcessHeap(), 0, This->conv_shift);
341 This->conv_map = NULL;
342 This->stride = 0;
343 This->conv_shift = NULL;
344 This->conv_stride = 0;
345 return TRUE;
346 } else {
347 return FALSE;
348 }
349 }
350 for(i = 0; i < MAX_ATTRIBS; i++) {
351 ret = check_attribute(This, &device->strided_streams.u.input[i], FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
352 }
353
354 /* Recalculate the conversion shift map if the declaration has changed,
355 * and we're using float16 conversion or used it on the last run
356 */
357 if(ret && (float16_used || This->conv_map)) {
358 HeapFree(GetProcessHeap(), 0, This->conv_shift);
359 This->conv_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
360 }
361 } else {
362 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
363 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
364 * the attributes that our current fixed function pipeline implementation cares for.
365 */
366 ret = check_attribute(This, &device->strided_streams.u.s.position, TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
367 ret = check_attribute(This, &device->strided_streams.u.s.normal, TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
368 ret = check_attribute(This, &device->strided_streams.u.s.diffuse, TRUE, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
369 ret = check_attribute(This, &device->strided_streams.u.s.specular, TRUE, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
370 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[0], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
371 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[1], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
372 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[2], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
373 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[3], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
374 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[4], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
375 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[5], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
376 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[6], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
377 ret = check_attribute(This, &device->strided_streams.u.s.texCoords[7], TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
378
379 if(float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
380 }
381
382 if(stride_this_run == 0 && This->conv_map) {
383 /* Sanity test */
384 if(ret == FALSE) {
385 ERR("no converted attributes found, old conversion map exists, and no declaration change???\n");
386 }
387 HeapFree(GetProcessHeap(), 0, This->conv_map);
388 This->conv_map = NULL;
389 This->stride = 0;
390 }
391 This->Flags |= VBFLAG_HASDESC;
392
393 if(ret) TRACE("Conversion information changed\n");
394 return ret;
395 }
396
397 static void check_vbo_size(IWineD3DVertexBufferImpl *This) {
398 DWORD size = This->conv_stride ? This->conv_stride * (This->resource.size / This->stride) : This->resource.size;
399 if(This->vbo_size != size) {
400 TRACE("Old size %d, creating new size %d\n", This->vbo_size, size);
401 ENTER_GL();
402 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
403 checkGLcall("glBindBufferARB");
404 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, NULL, This->vbo_usage));
405 This->vbo_size = size;
406 checkGLcall("glBufferDataARB");
407 LEAVE_GL();
408 }
409 }
410
411 static void CreateVBO(IWineD3DVertexBufferImpl *This) {
412 GLenum error, glUsage;
413 DWORD vboUsage = This->resource.usage;
414 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
415
416 TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n", This, debug_d3dusage(vboUsage));
417
418 /* Make sure that a context is there. Needed in a multithreaded environment. Otherwise this call is a nop */
419 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
420 ENTER_GL();
421
422 /* Make sure that the gl error is cleared. Do not use checkGLcall
423 * here because checkGLcall just prints a fixme and continues. However,
424 * if an error during VBO creation occurs we can fall back to non-vbo operation
425 * with full functionality(but performance loss)
426 */
427 while(glGetError() != GL_NO_ERROR);
428
429 /* Basically the FVF parameter passed to CreateVertexBuffer is no good
430 * It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
431 * IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
432 * look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
433 * to check if the rhw and color values are in the correct format.
434 */
435
436 GL_EXTCALL(glGenBuffersARB(1, &This->vbo));
437 error = glGetError();
438 if(This->vbo == 0 || error != GL_NO_ERROR) {
439 WARN("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
440 goto error;
441 }
442
443 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
444 error = glGetError();
445 if(error != GL_NO_ERROR) {
446 WARN("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
447 goto error;
448 }
449
450 /* Don't use static, because dx apps tend to update the buffer
451 * quite often even if they specify 0 usage. Because we always keep the local copy
452 * we never read from the vbo and can create a write only opengl buffer.
453 */
454 switch(vboUsage & (WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC) ) {
455 case WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC:
456 case WINED3DUSAGE_DYNAMIC:
457 TRACE("Gl usage = GL_STREAM_DRAW\n");
458 glUsage = GL_STREAM_DRAW_ARB;
459 break;
460 case WINED3DUSAGE_WRITEONLY:
461 default:
462 TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
463 glUsage = GL_DYNAMIC_DRAW_ARB;
464 break;
465 }
466
467 /* Reserve memory for the buffer. The amount of data won't change
468 * so we are safe with calling glBufferData once with a NULL ptr and
469 * calling glBufferSubData on updates
470 */
471 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, This->resource.size, NULL, glUsage));
472 error = glGetError();
473 if(error != GL_NO_ERROR) {
474 WARN("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
475 goto error;
476 }
477 This->vbo_size = This->resource.size;
478 This->vbo_usage = glUsage;
479 This->dirtystart = 0;
480 This->dirtyend = This->resource.size;
481 This->Flags |= VBFLAG_DIRTY;
482
483 LEAVE_GL();
484
485 return;
486 error:
487 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
488 FIXME("Failed to create a vertex buffer object. Continuing, but performance issues can occur\n");
489 if(This->vbo) GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
490 This->vbo = 0;
491 LEAVE_GL();
492 return;
493 }
494
495 static void WINAPI IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer *iface) {
496 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
497 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
498 BYTE *data;
499 UINT start = 0, end = 0, vertices;
500 BOOL declChanged = FALSE;
501 int i, j;
502 TRACE("(%p)->()\n", This);
503
504 if(!This->vbo) {
505 /* TODO: Make converting independent from VBOs */
506 if(This->Flags & VBFLAG_CREATEVBO) {
507 CreateVBO(This);
508 This->Flags &= ~VBFLAG_CREATEVBO;
509 } else {
510 return; /* Not doing any conversion */
511 }
512 }
513
514 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
515 if(device->isInDraw && This->bindCount > 0) {
516 declChanged = IWineD3DVertexBufferImpl_FindDecl(This);
517 } else if(This->Flags & VBFLAG_HASDESC) {
518 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
519 * the stream source state handler will call PreLoad again and the change will be caught
520 */
521 } else {
522 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
523 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
524 * declaration for the buffer can be found
525 */
526 return;
527 }
528
529 /* If applications change the declaration over and over, reconverting all the time is a huge
530 * performance hit. So count the declaration changes and release the VBO if there are too many
531 * of them (and thus stop converting)
532 */
533 if(declChanged) {
534 This->declChanges++;
535 This->draws = 0;
536
537 if(This->declChanges > VB_MAXDECLCHANGES) {
538 FIXME("Too many declaration changes, stopping converting\n");
539 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
540 ENTER_GL();
541 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
542 checkGLcall("glDeleteBuffersARB");
543 LEAVE_GL();
544 This->vbo = 0;
545 HeapFree(GetProcessHeap(), 0, This->conv_shift);
546
547 /* The stream source state handler might have read the memory of the vertex buffer already
548 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
549 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
550 * rarely
551 */
552 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
553
554 return;
555 }
556 check_vbo_size(This);
557 } else {
558 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
559 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
560 * decl changes and reset the decl change count after a specific number of them
561 */
562 This->draws++;
563 if(This->draws > VB_RESETDECLCHANGE) This->declChanges = 0;
564 }
565
566 if(declChanged) {
567 /* The declaration changed, reload the whole buffer */
568 WARN("Reloading buffer because of decl change\n");
569 start = 0;
570 end = This->resource.size;
571 } else if(This->Flags & VBFLAG_DIRTY) {
572 /* No decl change, but dirty data, reload the changed stuff */
573 if(This->conv_shift) {
574 if(This->dirtystart != 0 || This->dirtyend != 0) {
575 FIXME("Implement partial buffer loading with shifted conversion\n");
576 }
577 }
578 start = This->dirtystart;
579 end = This->dirtyend;
580 } else {
581 /* Desc not changed, buffer not dirty, nothing to do :-) */
582 return;
583 }
584
585 /* Mark the buffer clean */
586 This->Flags &= ~VBFLAG_DIRTY;
587 This->dirtystart = 0;
588 This->dirtyend = 0;
589
590 if(!This->conv_map) {
591 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
592 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
593 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
594 */
595 TRACE("No conversion needed\n");
596
597 if(!device->isInDraw) {
598 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
599 }
600 ENTER_GL();
601 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
602 checkGLcall("glBindBufferARB");
603 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end-start, This->resource.allocatedMemory + start));
604 checkGLcall("glBufferSubDataARB");
605 LEAVE_GL();
606 return;
607 }
608
609 /* Now for each vertex in the buffer that needs conversion */
610 vertices = This->resource.size / This->stride;
611
612 if(This->conv_shift) {
613 TRACE("Shifted conversion\n");
614 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conv_stride);
615
616 for(i = start / This->stride; i < min((end / This->stride) + 1, vertices); i++) {
617 for(j = 0; j < This->stride; j++) {
618 switch(This->conv_map[j]) {
619 case CONV_NONE:
620 data[This->conv_stride * i + j + This->conv_shift[j]] = This->resource.allocatedMemory[This->stride * i + j];
621 break;
622
623 case CONV_FLOAT16_2:
624 {
625 float *out = (float *) (&data[This->conv_stride * i + j + This->conv_shift[j]]);
626 const WORD *in = (WORD *) (&This->resource.allocatedMemory[i * This->stride + j]);
627
628 out[1] = float_16_to_32(in + 1);
629 out[0] = float_16_to_32(in + 0);
630 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
631 break;
632 }
633
634 default:
635 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
636 }
637 }
638 }
639
640 ENTER_GL();
641 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
642 checkGLcall("glBindBufferARB");
643 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, vertices * This->conv_stride, data));
644 checkGLcall("glBufferSubDataARB");
645 LEAVE_GL();
646
647 } else {
648 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
649 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
650 for(i = start / This->stride; i < min((end / This->stride) + 1, vertices); i++) {
651 for(j = 0; j < This->stride; j++) {
652 switch(This->conv_map[j]) {
653 case CONV_NONE:
654 /* Done already */
655 j += 3;
656 break;
657 case CONV_D3DCOLOR:
658 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
659 j += 3;
660 break;
661
662 case CONV_POSITIONT:
663 fixup_transformed_pos((float *) (data + i * This->stride + j));
664 j += 15;
665 break;
666
667 case CONV_FLOAT16_2:
668 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
669 default:
670 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conv_map[j]);
671 }
672 }
673 }
674
675 ENTER_GL();
676 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
677 checkGLcall("glBindBufferARB");
678 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data + start));
679 checkGLcall("glBufferSubDataARB");
680 LEAVE_GL();
681 }
682
683 HeapFree(GetProcessHeap(), 0, data);
684 }
685
686 static void WINAPI IWineD3DVertexBufferImpl_UnLoad(IWineD3DVertexBuffer *iface) {
687 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
688 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
689 TRACE("(%p)\n", This);
690
691 /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
692 * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
693 * that vbos are supported
694 */
695 if(This->vbo) {
696 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
697 ENTER_GL();
698 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
699 checkGLcall("glDeleteBuffersARB");
700 LEAVE_GL();
701 This->vbo = 0;
702 This->Flags |= VBFLAG_CREATEVBO; /* Recreate the VBO next load */
703 }
704 }
705
706 static WINED3DRESOURCETYPE WINAPI IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer *iface) {
707 return resource_get_type((IWineD3DResource *)iface);
708 }
709
710 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer *iface, IUnknown **pParent) {
711 return resource_get_parent((IWineD3DResource *)iface, pParent);
712 }
713
714 /* ******************************************************
715 IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
716 ****************************************************** */
717 static HRESULT WINAPI IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
718 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
719 BYTE *data;
720 TRACE("(%p)->%d, %d, %p, %08x\n", This, OffsetToLock, SizeToLock, ppbData, Flags);
721
722 InterlockedIncrement(&This->lockcount);
723
724 if(This->Flags & VBFLAG_DIRTY) {
725 if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
726 if(SizeToLock) {
727 if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
728 } else {
729 This->dirtyend = This->resource.size;
730 }
731 } else {
732 This->dirtystart = OffsetToLock;
733 if(SizeToLock)
734 This->dirtyend = OffsetToLock + SizeToLock;
735 else
736 This->dirtyend = This->resource.size;
737 }
738
739 data = This->resource.allocatedMemory;
740 This->Flags |= VBFLAG_DIRTY;
741 *ppbData = data + OffsetToLock;
742
743 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This, data + OffsetToLock, data, OffsetToLock);
744 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
745 return WINED3D_OK;
746 }
747 static HRESULT WINAPI IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer *iface) {
748 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
749 LONG lockcount;
750 TRACE("(%p)\n", This);
751
752 lockcount = InterlockedDecrement(&This->lockcount);
753 if(lockcount > 0) {
754 /* Delay loading the buffer until everything is unlocked */
755 TRACE("Ignoring the unlock\n");
756 return WINED3D_OK;
757 }
758
759 if(This->Flags & VBFLAG_HASDESC) {
760 IWineD3DVertexBufferImpl_PreLoad(iface);
761 }
762 return WINED3D_OK;
763 }
764 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer *iface, WINED3DVERTEXBUFFER_DESC *pDesc) {
765 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
766
767 TRACE("(%p)\n", This);
768 pDesc->Format = This->resource.format;
769 pDesc->Type = This->resource.resourceType;
770 pDesc->Usage = This->resource.usage;
771 pDesc->Pool = This->resource.pool;
772 pDesc->Size = This->resource.size;
773 pDesc->FVF = This->fvf;
774 return WINED3D_OK;
775 }
776
777 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl =
778 {
779 /* IUnknown */
780 IWineD3DVertexBufferImpl_QueryInterface,
781 IWineD3DVertexBufferImpl_AddRef,
782 IWineD3DVertexBufferImpl_Release,
783 /* IWineD3DResource */
784 IWineD3DVertexBufferImpl_GetParent,
785 IWineD3DVertexBufferImpl_GetDevice,
786 IWineD3DVertexBufferImpl_SetPrivateData,
787 IWineD3DVertexBufferImpl_GetPrivateData,
788 IWineD3DVertexBufferImpl_FreePrivateData,
789 IWineD3DVertexBufferImpl_SetPriority,
790 IWineD3DVertexBufferImpl_GetPriority,
791 IWineD3DVertexBufferImpl_PreLoad,
792 IWineD3DVertexBufferImpl_UnLoad,
793 IWineD3DVertexBufferImpl_GetType,
794 /* IWineD3DVertexBuffer */
795 IWineD3DVertexBufferImpl_Lock,
796 IWineD3DVertexBufferImpl_Unlock,
797 IWineD3DVertexBufferImpl_GetDesc
798 };
799
800 const BYTE* IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo)
801 {
802 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
803
804 *vbo = This->vbo;
805 if(This->vbo == 0) {
806 if(This->Flags & VBFLAG_CREATEVBO) {
807 CreateVBO(This);
808 This->Flags &= ~VBFLAG_CREATEVBO;
809 if(This->vbo) {
810 *vbo = This->vbo;
811 return (const BYTE *)iOffset;
812 }
813 }
814 return This->resource.allocatedMemory + iOffset;
815 } else {
816 return (const BYTE *)iOffset;
817 }
818 }