d3d: Unify WINED3DVERTEXBUFFER_DESC and WINED3DINDEXBUFFER_DESC.
[wine/multimedia.git] / dlls / wined3d / buffer.c
blob6a66c734ada9219da9d9f0f52623065d06e87a6c
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2007 Stefan Dösinger for CodeWeavers
7 * Copyright 2009 Henri Verbeet 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 "config.h"
26 #include "wine/port.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
34 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
35 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
37 static void buffer_create_buffer_object(struct wined3d_buffer *This)
39 GLenum error, gl_usage;
40 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
42 TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n",
43 This, debug_d3dusage(This->resource.usage));
45 /* Make sure that a context is there. Needed in a multithreaded environment. Otherwise this call is a nop */
46 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
47 ENTER_GL();
49 /* Make sure that the gl error is cleared. Do not use checkGLcall
50 * here because checkGLcall just prints a fixme and continues. However,
51 * if an error during VBO creation occurs we can fall back to non-vbo operation
52 * with full functionality(but performance loss)
54 while (glGetError() != GL_NO_ERROR);
56 /* Basically the FVF parameter passed to CreateVertexBuffer is no good
57 * It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
58 * IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
59 * look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
60 * to check if the rhw and color values are in the correct format.
63 GL_EXTCALL(glGenBuffersARB(1, &This->buffer_object));
64 error = glGetError();
65 if (!This->buffer_object || error != GL_NO_ERROR)
67 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
68 goto fail;
71 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
72 error = glGetError();
73 if (error != GL_NO_ERROR)
75 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
76 goto fail;
79 /* Don't use static, because dx apps tend to update the buffer
80 * quite often even if they specify 0 usage. Because we always keep the local copy
81 * we never read from the vbo and can create a write only opengl buffer.
83 switch(This->resource.usage & (WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC))
85 case WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC:
86 case WINED3DUSAGE_DYNAMIC:
87 TRACE("Gl usage = GL_STREAM_DRAW\n");
88 gl_usage = GL_STREAM_DRAW_ARB;
89 break;
91 case WINED3DUSAGE_WRITEONLY:
92 default:
93 TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
94 gl_usage = GL_DYNAMIC_DRAW_ARB;
95 break;
98 /* Reserve memory for the buffer. The amount of data won't change
99 * so we are safe with calling glBufferData once with a NULL ptr and
100 * calling glBufferSubData on updates
102 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, This->resource.size, NULL, gl_usage));
103 error = glGetError();
104 if (error != GL_NO_ERROR)
106 ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
107 goto fail;
110 LEAVE_GL();
112 This->buffer_object_size = This->resource.size;
113 This->buffer_object_usage = gl_usage;
114 This->dirty_start = 0;
115 This->dirty_end = This->resource.size;
116 This->flags |= WINED3D_BUFFER_DIRTY;
118 return;
120 fail:
121 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
122 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
123 if (This->buffer_object) GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
124 This->buffer_object = 0;
125 LEAVE_GL();
127 return;
130 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
131 const enum wined3d_buffer_conversion_type conversion_type,
132 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
134 DWORD attrib_size;
135 BOOL ret = FALSE;
136 unsigned int i;
137 DWORD offset = This->resource.wineD3DDevice->stateBlock->streamOffset[attrib->stream_idx];
138 DWORD_PTR data;
140 /* Check for some valid situations which cause us pain. One is if the buffer is used for
141 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
142 * with different strides. In the 2nd case we might have to drop conversion entirely,
143 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
145 if (!attrib->stride)
147 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
148 debug_d3dformat(attrib->format_desc->format));
150 else if(attrib->stride != *stride_this_run && *stride_this_run)
152 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
154 else
156 *stride_this_run = attrib->stride;
157 if (This->stride != *stride_this_run)
159 /* We rely that this happens only on the first converted attribute that is found,
160 * if at all. See above check
162 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
163 This->stride = *stride_this_run;
164 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
165 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
166 sizeof(*This->conversion_map) * This->stride);
167 ret = TRUE;
171 data = (((DWORD_PTR)attrib->data) + offset) % This->stride;
172 attrib_size = attrib->format_desc->component_count * attrib->format_desc->component_size;
173 for (i = 0; i < attrib_size; ++i)
175 if (This->conversion_map[data + i] != conversion_type)
177 TRACE("Byte %ld in vertex changed\n", i + data);
178 TRACE("It was type %d, is %d now\n", This->conversion_map[data + i], conversion_type);
179 ret = TRUE;
180 This->conversion_map[data + i] = conversion_type;
184 return ret;
187 static BOOL buffer_check_attribute(struct wined3d_buffer *This,
188 const struct wined3d_stream_info_element *attrib, const BOOL check_d3dcolor, const BOOL is_ffp_position,
189 const BOOL is_ffp_color, DWORD *stride_this_run, BOOL *float16_used)
191 BOOL ret = FALSE;
192 WINED3DFORMAT format;
194 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
195 * there, on nonexistent attribs the vbo is 0.
197 if (attrib->buffer_object != This->buffer_object) return FALSE;
199 format = attrib->format_desc->format;
200 /* Look for newly appeared conversion */
201 if (!GL_SUPPORT(NV_HALF_FLOAT) && (format == WINED3DFMT_R16G16_FLOAT || format == WINED3DFMT_R16G16B16A16_FLOAT))
203 ret = buffer_process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run);
205 if (is_ffp_position) FIXME("Test FLOAT16 fixed function processing positions\n");
206 else if (is_ffp_color) FIXME("test FLOAT16 fixed function processing colors\n");
207 *float16_used = TRUE;
209 else if (check_d3dcolor && format == WINED3DFMT_A8R8G8B8)
211 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
213 if (!is_ffp_color) FIXME("Test for non-color fixed function WINED3DFMT_A8R8G8B8 format\n");
215 else if (is_ffp_position && format == WINED3DFMT_R32G32B32A32_FLOAT)
217 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
219 else if (This->conversion_map)
221 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
224 return ret;
227 static UINT *find_conversion_shift(struct wined3d_buffer *This,
228 const struct wined3d_stream_info *strided, UINT stride)
230 UINT *ret, i, j, shift, orig_type_size;
232 if (!stride)
234 TRACE("No shift\n");
235 return NULL;
238 This->conversion_stride = stride;
239 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
240 for (i = 0; i < MAX_ATTRIBS; ++i)
242 WINED3DFORMAT format;
244 if (strided->elements[i].buffer_object != This->buffer_object) continue;
246 format = strided->elements[i].format_desc->format;
247 if (format == WINED3DFMT_R16G16_FLOAT)
249 shift = 4;
251 else if (format == WINED3DFMT_R16G16B16A16_FLOAT)
253 shift = 8;
254 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
255 * compatible
257 for (j = 4; j < 8; ++j)
259 ret[(DWORD_PTR)strided->elements[i].data + j] += 4;
262 else
264 shift = 0;
266 This->conversion_stride += shift;
268 if (shift)
270 orig_type_size = strided->elements[i].format_desc->component_count
271 * strided->elements[i].format_desc->component_size;
272 for (j = (DWORD_PTR)strided->elements[i].data + orig_type_size; j < stride; ++j)
274 ret[j] += shift;
279 if (TRACE_ON(d3d))
281 TRACE("Dumping conversion shift:\n");
282 for (i = 0; i < stride; ++i)
284 TRACE("[%d]", ret[i]);
286 TRACE("\n");
289 return ret;
292 static BOOL buffer_find_decl(struct wined3d_buffer *This)
294 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
295 UINT stride_this_run = 0;
296 BOOL float16_used = FALSE;
297 BOOL ret = FALSE;
298 unsigned int i;
300 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
301 * Once we have our declaration there is no need to look it up again.
303 if (((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->flags & WINED3D_BUFFER_HASDESC) return FALSE;
305 TRACE("Finding vertex buffer conversion information\n");
306 /* Certain declaration types need some fixups before we can pass them to
307 * opengl. This means D3DCOLOR attributes with fixed function vertex
308 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
309 * GL_NV_half_float is not supported.
311 * The vertex buffer FVF doesn't help with finding them, we have to use
312 * the decoded vertex declaration and pick the things that concern the
313 * current buffer. A problem with this is that this can change between
314 * draws, so we have to validate the information and reprocess the buffer
315 * if it changes, and avoid false positives for performance reasons.
317 * We have to distinguish between vertex shaders and fixed function to
318 * pick the way we access the strided vertex information.
320 * This code sets up a per-byte array with the size of the detected
321 * stride of the arrays in the buffer. For each byte we have a field
322 * that marks the conversion needed on this byte. For example, the
323 * following declaration with fixed function vertex processing:
325 * POSITIONT, FLOAT4
326 * NORMAL, FLOAT3
327 * DIFFUSE, FLOAT16_4
328 * SPECULAR, D3DCOLOR
330 * Will result in
331 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
332 * [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]
334 * Where in this example map P means 4 component position conversion, 0
335 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
336 * conversion (red / blue swizzle).
338 * If we're doing conversion and the stride changes we have to reconvert
339 * the whole buffer. Note that we do not mind if the semantic changes,
340 * we only care for the conversion type. So if the NORMAL is replaced
341 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
342 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
343 * conversion types depend on the semantic as well, for example a FLOAT4
344 * texcoord needs no conversion while a FLOAT4 positiont needs one
346 if (use_vs(device->stateBlock))
348 TRACE("vshader\n");
349 /* If the current vertex declaration is marked for no half float conversion don't bother to
350 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
351 * if we used conversion before
353 if (!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed)
355 if (This->conversion_map)
357 TRACE("Now using shaders without conversion, but conversion used before\n");
358 HeapFree(GetProcessHeap(), 0, This->conversion_map);
359 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
360 This->conversion_map = NULL;
361 This->stride = 0;
362 This->conversion_shift = NULL;
363 This->conversion_stride = 0;
364 return TRUE;
366 else
368 return FALSE;
371 for (i = 0; i < MAX_ATTRIBS; ++i)
373 ret = buffer_check_attribute(This, &device->strided_streams.elements[i],
374 FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
377 /* Recalculate the conversion shift map if the declaration has changed,
378 * and we're using float16 conversion or used it on the last run
380 if (ret && (float16_used || This->conversion_map))
382 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
383 This->conversion_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
386 else
388 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
389 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
390 * the attributes that our current fixed function pipeline implementation cares for.
392 BOOL support_d3dcolor = GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA);
393 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_POSITION],
394 TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
395 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_NORMAL],
396 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
397 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_DIFFUSE],
398 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
399 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_SPECULAR],
400 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
401 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD0],
402 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
403 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD1],
404 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
405 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD2],
406 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
407 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD3],
408 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
409 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD4],
410 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
411 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD5],
412 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
413 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD6],
414 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
415 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD7],
416 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
418 if (float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
421 if (stride_this_run == 0 && This->conversion_map)
423 /* Sanity test */
424 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
425 HeapFree(GetProcessHeap(), 0, This->conversion_map);
426 This->conversion_map = NULL;
427 This->stride = 0;
430 if (ret) TRACE("Conversion information changed\n");
432 return ret;
435 static void buffer_check_buffer_object_size(struct wined3d_buffer *This)
437 UINT size = This->conversion_stride ?
438 This->conversion_stride * (This->resource.size / This->stride) : This->resource.size;
439 if (This->buffer_object_size != size)
441 TRACE("Old size %u, creating new size %u\n", This->buffer_object_size, size);
442 ENTER_GL();
443 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
444 checkGLcall("glBindBufferARB");
445 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, NULL, This->buffer_object_usage));
446 This->buffer_object_size = size;
447 checkGLcall("glBufferDataARB");
448 LEAVE_GL();
452 static inline void fixup_d3dcolor(DWORD *dst_color)
454 DWORD src_color = *dst_color;
456 /* Color conversion like in drawStridedSlow. watch out for little endianity
457 * If we want that stuff to work on big endian machines too we have to consider more things
459 * 0xff000000: Alpha mask
460 * 0x00ff0000: Blue mask
461 * 0x0000ff00: Green mask
462 * 0x000000ff: Red mask
464 *dst_color = 0;
465 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
466 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
467 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
470 static inline void fixup_transformed_pos(float *p)
472 float x, y, z, w;
474 /* rhw conversion like in drawStridedSlow */
475 if (p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps)))
477 x = p[0];
478 y = p[1];
479 z = p[2];
480 w = 1.0;
482 else
484 w = 1.0 / p[3];
485 x = p[0] * w;
486 y = p[1] * w;
487 z = p[2] * w;
489 p[0] = x;
490 p[1] = y;
491 p[2] = z;
492 p[3] = w;
495 const BYTE *buffer_get_memory(IWineD3DBuffer *iface, UINT offset, GLuint *buffer_object)
497 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
499 *buffer_object = This->buffer_object;
500 if (!This->buffer_object)
502 if (This->flags & WINED3D_BUFFER_CREATEBO)
504 buffer_create_buffer_object(This);
505 This->flags &= ~WINED3D_BUFFER_CREATEBO;
506 if (This->buffer_object)
508 *buffer_object = This->buffer_object;
509 return (const BYTE *)offset;
512 return This->resource.allocatedMemory + offset;
514 else
516 return (const BYTE *)offset;
520 /* IUnknown methods */
522 static HRESULT STDMETHODCALLTYPE buffer_QueryInterface(IWineD3DBuffer *iface,
523 REFIID riid, void **object)
525 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
527 if (IsEqualGUID(riid, &IID_IWineD3DBuffer)
528 || IsEqualGUID(riid, &IID_IWineD3DResource)
529 || IsEqualGUID(riid, &IID_IWineD3DBase)
530 || IsEqualGUID(riid, &IID_IUnknown))
532 IUnknown_AddRef(iface);
533 *object = iface;
534 return S_OK;
537 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
539 *object = NULL;
541 return E_NOINTERFACE;
544 static ULONG STDMETHODCALLTYPE buffer_AddRef(IWineD3DBuffer *iface)
546 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
547 ULONG refcount = InterlockedIncrement(&This->resource.ref);
549 TRACE("%p increasing refcount to %u\n", This, refcount);
551 return refcount;
554 static void STDMETHODCALLTYPE buffer_UnLoad(IWineD3DBuffer *iface)
556 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
558 TRACE("iface %p\n", iface);
560 /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
561 * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
562 * that vbos are supported
564 if (This->buffer_object)
566 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
568 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
569 ENTER_GL();
570 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
571 checkGLcall("glDeleteBuffersARB");
572 LEAVE_GL();
573 This->buffer_object = 0;
574 This->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
578 static ULONG STDMETHODCALLTYPE buffer_Release(IWineD3DBuffer *iface)
580 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
581 ULONG refcount = InterlockedDecrement(&This->resource.ref);
583 TRACE("%p decreasing refcount to %u\n", This, refcount);
585 if (!refcount)
587 buffer_UnLoad(iface);
588 resource_cleanup((IWineD3DResource *)iface);
589 HeapFree(GetProcessHeap(), 0, This);
592 return refcount;
595 /* IWineD3DBase methods */
597 static HRESULT STDMETHODCALLTYPE buffer_GetParent(IWineD3DBuffer *iface, IUnknown **parent)
599 return resource_get_parent((IWineD3DResource *)iface, parent);
602 /* IWineD3DResource methods */
604 static HRESULT STDMETHODCALLTYPE buffer_GetDevice(IWineD3DBuffer *iface, IWineD3DDevice **device)
606 return resource_get_device((IWineD3DResource *)iface, device);
609 static HRESULT STDMETHODCALLTYPE buffer_SetPrivateData(IWineD3DBuffer *iface,
610 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
612 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
615 static HRESULT STDMETHODCALLTYPE buffer_GetPrivateData(IWineD3DBuffer *iface,
616 REFGUID guid, void *data, DWORD *data_size)
618 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
621 static HRESULT STDMETHODCALLTYPE buffer_FreePrivateData(IWineD3DBuffer *iface, REFGUID guid)
623 return resource_free_private_data((IWineD3DResource *)iface, guid);
626 static DWORD STDMETHODCALLTYPE buffer_SetPriority(IWineD3DBuffer *iface, DWORD priority)
628 return resource_set_priority((IWineD3DResource *)iface, priority);
631 static DWORD STDMETHODCALLTYPE buffer_GetPriority(IWineD3DBuffer *iface)
633 return resource_get_priority((IWineD3DResource *)iface);
636 static void STDMETHODCALLTYPE buffer_PreLoad(IWineD3DBuffer *iface)
638 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
639 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
640 UINT start = 0, end = 0, vertices;
641 BOOL decl_changed = FALSE;
642 unsigned int i, j;
643 BYTE *data;
645 TRACE("iface %p\n", iface);
647 if (!This->buffer_object)
649 /* TODO: Make converting independent from VBOs */
650 if (This->flags & WINED3D_BUFFER_CREATEBO)
652 buffer_create_buffer_object(This);
653 This->flags &= ~WINED3D_BUFFER_CREATEBO;
655 else
657 return; /* Not doing any conversion */
661 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
662 if (device->isInDraw && This->bind_count > 0)
664 decl_changed = buffer_find_decl(This);
665 This->flags |= WINED3D_BUFFER_HASDESC;
668 if (!decl_changed && !(This->flags & WINED3D_BUFFER_HASDESC && This->flags & WINED3D_BUFFER_DIRTY)) return;
670 /* If applications change the declaration over and over, reconverting all the time is a huge
671 * performance hit. So count the declaration changes and release the VBO if there are too many
672 * of them (and thus stop converting)
674 if (decl_changed)
676 ++This->conversion_count;
677 This->draw_count = 0;
679 if (This->conversion_count > VB_MAXDECLCHANGES)
681 FIXME("Too many declaration changes, stopping converting\n");
682 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
683 ENTER_GL();
684 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
685 checkGLcall("glDeleteBuffersARB");
686 LEAVE_GL();
687 This->buffer_object = 0;
688 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
690 /* The stream source state handler might have read the memory of the vertex buffer already
691 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
692 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
693 * rarely
695 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
697 return;
699 buffer_check_buffer_object_size(This);
701 else
703 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
704 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
705 * decl changes and reset the decl change count after a specific number of them
707 ++This->draw_count;
708 if (This->draw_count > VB_RESETDECLCHANGE) This->conversion_count = 0;
711 if (decl_changed)
713 /* The declaration changed, reload the whole buffer */
714 WARN("Reloading buffer because of decl change\n");
715 start = 0;
716 end = This->resource.size;
718 else
720 /* No decl change, but dirty data, reload the changed stuff */
721 if (This->conversion_shift)
723 if (This->dirty_start != 0 || This->dirty_end != 0)
725 FIXME("Implement partial buffer loading with shifted conversion\n");
728 start = This->dirty_start;
729 end = This->dirty_end;
732 /* Mark the buffer clean */
733 This->flags &= ~WINED3D_BUFFER_DIRTY;
734 This->dirty_start = 0;
735 This->dirty_end = 0;
737 if (!This->conversion_map)
739 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
740 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
741 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
743 TRACE("No conversion needed\n");
745 if (!device->isInDraw)
747 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
749 ENTER_GL();
750 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
751 checkGLcall("glBindBufferARB");
752 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end-start, This->resource.allocatedMemory + start));
753 checkGLcall("glBufferSubDataARB");
754 LEAVE_GL();
755 return;
758 /* Now for each vertex in the buffer that needs conversion */
759 vertices = This->resource.size / This->stride;
761 if (This->conversion_shift)
763 TRACE("Shifted conversion\n");
764 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conversion_stride);
766 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
768 for (j = 0; j < This->stride; ++j)
770 switch(This->conversion_map[j])
772 case CONV_NONE:
773 data[This->conversion_stride * i + j + This->conversion_shift[j]]
774 = This->resource.allocatedMemory[This->stride * i + j];
775 break;
777 case CONV_FLOAT16_2:
779 float *out = (float *)(&data[This->conversion_stride * i + j + This->conversion_shift[j]]);
780 const WORD *in = (WORD *)(&This->resource.allocatedMemory[i * This->stride + j]);
782 out[1] = float_16_to_32(in + 1);
783 out[0] = float_16_to_32(in + 0);
784 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
785 break;
788 default:
789 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
790 break;
795 ENTER_GL();
796 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
797 checkGLcall("glBindBufferARB");
798 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, vertices * This->conversion_stride, data));
799 checkGLcall("glBufferSubDataARB");
800 LEAVE_GL();
802 else
804 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
805 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
806 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
808 for (j = 0; j < This->stride; ++j)
810 switch(This->conversion_map[j])
812 case CONV_NONE:
813 /* Done already */
814 j += 3;
815 break;
816 case CONV_D3DCOLOR:
817 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
818 j += 3;
819 break;
821 case CONV_POSITIONT:
822 fixup_transformed_pos((float *) (data + i * This->stride + j));
823 j += 15;
824 break;
826 case CONV_FLOAT16_2:
827 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
828 default:
829 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
834 ENTER_GL();
835 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
836 checkGLcall("glBindBufferARB");
837 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data + start));
838 checkGLcall("glBufferSubDataARB");
839 LEAVE_GL();
842 HeapFree(GetProcessHeap(), 0, data);
845 static WINED3DRESOURCETYPE STDMETHODCALLTYPE buffer_GetType(IWineD3DBuffer *iface)
847 return resource_get_type((IWineD3DResource *)iface);
850 /* IWineD3DBuffer methods */
852 static HRESULT STDMETHODCALLTYPE buffer_Map(IWineD3DBuffer *iface, UINT offset, UINT size, BYTE **data, DWORD flags)
854 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
856 TRACE("iface %p, offset %u, size %u, data %p, flags %#x\n", iface, offset, size, data, flags);
858 InterlockedIncrement(&This->lock_count);
860 if (This->flags & WINED3D_BUFFER_DIRTY)
862 if (This->dirty_start > offset) This->dirty_start = offset;
864 if (size)
866 if (This->dirty_end < offset + size) This->dirty_end = offset + size;
868 else
870 This->dirty_end = This->resource.size;
873 else
875 This->dirty_start = offset;
876 if (size) This->dirty_end = offset + size;
877 else This->dirty_end = This->resource.size;
880 This->flags |= WINED3D_BUFFER_DIRTY;
882 *data = This->resource.allocatedMemory + offset;
884 TRACE("Returning memory at %p (base %p, offset %u)\n", *data, This->resource.allocatedMemory, offset);
885 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
887 return WINED3D_OK;
890 static HRESULT STDMETHODCALLTYPE buffer_Unmap(IWineD3DBuffer *iface)
892 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
894 TRACE("(%p)\n", This);
896 if (InterlockedDecrement(&This->lock_count))
898 /* Delay loading the buffer until everything is unlocked */
899 TRACE("Ignoring unlock\n");
900 return WINED3D_OK;
903 if (This->flags & WINED3D_BUFFER_HASDESC)
905 buffer_PreLoad(iface);
908 return WINED3D_OK;
911 static HRESULT STDMETHODCALLTYPE buffer_GetDesc(IWineD3DBuffer *iface, WINED3DBUFFER_DESC *desc)
913 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
915 TRACE("(%p)\n", This);
917 desc->Format = This->resource.format_desc->format;
918 desc->Type = This->resource.resourceType;
919 desc->Usage = This->resource.usage;
920 desc->Pool = This->resource.pool;
921 desc->Size = This->resource.size;
922 desc->FVF = This->fvf;
924 return WINED3D_OK;
927 const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
929 /* IUnknown methods */
930 buffer_QueryInterface,
931 buffer_AddRef,
932 buffer_Release,
933 /* IWineD3DBase methods */
934 buffer_GetParent,
935 /* IWineD3DResource methods */
936 buffer_GetDevice,
937 buffer_SetPrivateData,
938 buffer_GetPrivateData,
939 buffer_FreePrivateData,
940 buffer_SetPriority,
941 buffer_GetPriority,
942 buffer_PreLoad,
943 buffer_UnLoad,
944 buffer_GetType,
945 /* IWineD3DBuffer methods */
946 buffer_Map,
947 buffer_Unmap,
948 buffer_GetDesc,
951 /* IUnknown methods */
953 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_QueryInterface(IWineD3DIndexBuffer *iface,
954 REFIID riid, void **object)
956 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
958 if (IsEqualGUID(riid, &IID_IWineD3DIndexBuffer)
959 || IsEqualGUID(riid, &IID_IWineD3DResource)
960 || IsEqualGUID(riid, &IID_IWineD3DBase)
961 || IsEqualGUID(riid, &IID_IUnknown))
963 IUnknown_AddRef(iface);
964 *object = iface;
965 return S_OK;
968 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
970 *object = NULL;
972 return E_NOINTERFACE;
975 static ULONG STDMETHODCALLTYPE IWineD3DIndexBufferImpl_AddRef(IWineD3DIndexBuffer *iface)
977 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
978 ULONG refcount = InterlockedIncrement(&This->resource.ref);
980 TRACE("%p increasing refcount to %u\n", This, refcount);
982 return refcount;
985 static ULONG STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Release(IWineD3DIndexBuffer *iface)
987 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
988 ULONG refcount = InterlockedDecrement(&This->resource.ref);
990 TRACE("%p decreasing refcount to %u\n", This, refcount);
992 if (!refcount)
994 if (This->vbo)
996 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
998 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
999 ENTER_GL();
1000 /* No need to manually unset the buffer. glDeleteBuffers unsets it for the current context,
1001 * but not for other contexts. However, because the d3d buffer is destroyed the app has to
1002 * unset it before doing the next draw, thus dirtifying the index buffer state and forcing
1003 * binding a new buffer
1005 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
1006 checkGLcall("glDeleteBuffersARB");
1007 LEAVE_GL();
1010 resource_cleanup((IWineD3DResource *)iface);
1011 HeapFree(GetProcessHeap(), 0, This);
1014 return refcount;
1017 /* IWineD3DBase methods */
1019 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetParent(IWineD3DIndexBuffer *iface, IUnknown **parent)
1021 return resource_get_parent((IWineD3DResource *)iface, parent);
1024 /* IWineD3DResource methods */
1026 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetDevice(IWineD3DIndexBuffer *iface, IWineD3DDevice **device)
1028 return resource_get_device((IWineD3DResource *)iface, device);
1031 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_SetPrivateData(IWineD3DIndexBuffer *iface,
1032 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
1034 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
1037 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetPrivateData(IWineD3DIndexBuffer *iface,
1038 REFGUID guid, void *data, DWORD *data_size)
1040 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
1043 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_FreePrivateData(IWineD3DIndexBuffer *iface, REFGUID guid)
1045 return resource_free_private_data((IWineD3DResource *)iface, guid);
1048 static DWORD STDMETHODCALLTYPE IWineD3DIndexBufferImpl_SetPriority(IWineD3DIndexBuffer *iface, DWORD priority)
1050 return resource_set_priority((IWineD3DResource *)iface, priority);
1053 static DWORD STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetPriority(IWineD3DIndexBuffer *iface)
1055 return resource_get_priority((IWineD3DResource *)iface);
1058 static void STDMETHODCALLTYPE IWineD3DIndexBufferImpl_PreLoad(IWineD3DIndexBuffer *iface)
1060 TRACE("iface %p\n", iface);
1063 static void STDMETHODCALLTYPE IWineD3DIndexBufferImpl_UnLoad(IWineD3DIndexBuffer *iface)
1065 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *) iface;
1066 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1068 TRACE("(%p)\n", This);
1070 /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
1071 * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
1072 * that vbos are supported.
1073 * (TODO: Make a IWineD3DBuffer base class for index and vertex buffers and share
1074 * this code. Also needed for D3D10)
1076 if (This->vbo)
1078 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1079 ENTER_GL();
1080 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
1081 checkGLcall("glDeleteBuffersARB");
1082 LEAVE_GL();
1083 This->vbo = 0;
1087 static WINED3DRESOURCETYPE STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetType(IWineD3DIndexBuffer *iface)
1089 return resource_get_type((IWineD3DResource *)iface);
1092 /* IWineD3DIndexBuffer methods */
1094 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Lock(IWineD3DIndexBuffer *iface,
1095 UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags)
1097 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1099 TRACE("(%p) : offset %d, size %d, Flags=%x\n", This, OffsetToLock, SizeToLock, Flags);
1101 InterlockedIncrement(&This->lockcount);
1102 *ppbData = This->resource.allocatedMemory + OffsetToLock;
1104 if (Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) || !This->vbo)
1106 return WINED3D_OK;
1109 if (This->dirtystart != This->dirtyend)
1111 if (This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
1112 if (SizeToLock)
1114 if (This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
1116 else
1118 This->dirtyend = This->resource.size;
1121 else
1123 This->dirtystart = OffsetToLock;
1124 if (SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
1125 else This->dirtyend = This->resource.size;
1128 return WINED3D_OK;
1131 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Unlock(IWineD3DIndexBuffer *iface)
1133 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1134 ULONG locks = InterlockedDecrement(&This->lockcount);
1136 TRACE("(%p)\n", This);
1138 /* For now load in unlock */
1139 if (!locks && This->vbo && (This->dirtyend - This->dirtystart) > 0)
1141 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1143 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1145 ENTER_GL();
1146 GL_EXTCALL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, This->vbo));
1147 checkGLcall("glBindBufferARB");
1148 GL_EXTCALL(glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, This->dirtystart,
1149 This->dirtyend - This->dirtystart, This->resource.allocatedMemory + This->dirtystart));
1150 checkGLcall("glBufferSubDataARB");
1151 LEAVE_GL();
1152 This->dirtystart = 0;
1153 This->dirtyend = 0;
1154 /* TODO: Move loading into preload when the buffer is used, that avoids dirtifying the state */
1155 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_INDEXBUFFER);
1158 return WINED3D_OK;
1161 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetDesc(IWineD3DIndexBuffer *iface,
1162 WINED3DBUFFER_DESC *pDesc)
1164 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1166 TRACE("(%p)\n", This);
1168 pDesc->Format = This->resource.format_desc->format;
1169 pDesc->Type = This->resource.resourceType;
1170 pDesc->Usage = This->resource.usage;
1171 pDesc->Pool = This->resource.pool;
1172 pDesc->Size = This->resource.size;
1174 return WINED3D_OK;
1177 const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl =
1179 /* IUnknown methods */
1180 IWineD3DIndexBufferImpl_QueryInterface,
1181 IWineD3DIndexBufferImpl_AddRef,
1182 IWineD3DIndexBufferImpl_Release,
1183 /* IWineD3DBase methods */
1184 IWineD3DIndexBufferImpl_GetParent,
1185 /* IWineD3DResource methods */
1186 IWineD3DIndexBufferImpl_GetDevice,
1187 IWineD3DIndexBufferImpl_SetPrivateData,
1188 IWineD3DIndexBufferImpl_GetPrivateData,
1189 IWineD3DIndexBufferImpl_FreePrivateData,
1190 IWineD3DIndexBufferImpl_SetPriority,
1191 IWineD3DIndexBufferImpl_GetPriority,
1192 IWineD3DIndexBufferImpl_PreLoad,
1193 IWineD3DIndexBufferImpl_UnLoad,
1194 IWineD3DIndexBufferImpl_GetType,
1195 /* IWineD3DIndexBuffer methods */
1196 IWineD3DIndexBufferImpl_Lock,
1197 IWineD3DIndexBufferImpl_Unlock,
1198 IWineD3DIndexBufferImpl_GetDesc,