wined3d: Store the format_desc itself in struct wined3d_stream_info_element.
[wine] / dlls / wined3d / buffer.c
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
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
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include "wined3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31
32 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
33
34 #define VB_MAXDECLCHANGES     100     /* After that number we stop converting */
35 #define VB_RESETDECLCHANGE    1000    /* Reset the changecount after that number of draws */
36
37 static void buffer_create_buffer_object(struct wined3d_buffer *This)
38 {
39     GLenum error, gl_usage;
40     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
41
42     TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n",
43             This, debug_d3dusage(This->resource.usage));
44
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();
48
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)
53     */
54     while (glGetError() != GL_NO_ERROR);
55
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.
61     */
62
63     GL_EXTCALL(glGenBuffersARB(1, &This->buffer_object));
64     error = glGetError();
65     if (!This->buffer_object || error != GL_NO_ERROR)
66     {
67         ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
68         goto fail;
69     }
70
71     GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->buffer_object));
72     error = glGetError();
73     if (error != GL_NO_ERROR)
74     {
75         ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
76         goto fail;
77     }
78
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.
82     */
83     switch(This->resource.usage & (WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC))
84     {
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;
90
91         case WINED3DUSAGE_WRITEONLY:
92         default:
93             TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
94             gl_usage = GL_DYNAMIC_DRAW_ARB;
95             break;
96     }
97
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
101     */
102     GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, This->resource.size, NULL, gl_usage));
103     error = glGetError();
104     if (error != GL_NO_ERROR)
105     {
106         ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
107         goto fail;
108     }
109
110     LEAVE_GL();
111
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;
117
118     return;
119
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();
126
127     return;
128 }
129
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)
133 {
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;
139
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.
144      */
145     if (!attrib->stride)
146     {
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));
149     }
150     else if(attrib->stride != *stride_this_run && *stride_this_run)
151     {
152         FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
153     }
154     else
155     {
156         *stride_this_run = attrib->stride;
157         if (This->stride != *stride_this_run)
158         {
159             /* We rely that this happens only on the first converted attribute that is found,
160              * if at all. See above check
161              */
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;
168         }
169     }
170
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)
174     {
175         if (This->conversion_map[data + i] != conversion_type)
176         {
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;
181         }
182     }
183
184     return ret;
185 }
186
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)
190 {
191     BOOL ret = FALSE;
192     WINED3DFORMAT format;
193
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.
196      */
197     if (attrib->buffer_object != This->buffer_object) return FALSE;
198
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))
202     {
203         ret = buffer_process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run);
204
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;
208     }
209     else if (check_d3dcolor && format == WINED3DFMT_A8R8G8B8)
210     {
211         ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
212
213         if (!is_ffp_color) FIXME("Test for non-color fixed function WINED3DFMT_A8R8G8B8 format\n");
214     }
215     else if (is_ffp_position && format == WINED3DFMT_R32G32B32A32_FLOAT)
216     {
217         ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
218     }
219     else if (This->conversion_map)
220     {
221         ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
222     }
223
224     return ret;
225 }
226
227 static UINT *find_conversion_shift(struct wined3d_buffer *This,
228         const struct wined3d_stream_info *strided, UINT stride)
229 {
230     UINT *ret, i, j, shift, orig_type_size;
231
232     if (!stride)
233     {
234         TRACE("No shift\n");
235         return NULL;
236     }
237
238     This->conversion_stride = stride;
239     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
240     for (i = 0; i < MAX_ATTRIBS; ++i)
241     {
242         WINED3DFORMAT format;
243
244         if (strided->elements[i].buffer_object != This->buffer_object) continue;
245
246         format = strided->elements[i].format_desc->format;
247         if (format == WINED3DFMT_R16G16_FLOAT)
248         {
249             shift = 4;
250         }
251         else if (format == WINED3DFMT_R16G16B16A16_FLOAT)
252         {
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
256              */
257             for (j = 4; j < 8; ++j)
258             {
259                 ret[(DWORD_PTR)strided->elements[i].data + j] += 4;
260             }
261         }
262         else
263         {
264             shift = 0;
265         }
266         This->conversion_stride += shift;
267
268         if (shift)
269         {
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)
273             {
274                 ret[j] += shift;
275             }
276         }
277     }
278
279     if (TRACE_ON(d3d))
280     {
281         TRACE("Dumping conversion shift:\n");
282         for (i = 0; i < stride; ++i)
283         {
284             TRACE("[%d]", ret[i]);
285         }
286         TRACE("\n");
287     }
288
289     return ret;
290 }
291
292 static BOOL buffer_find_decl(struct wined3d_buffer *This)
293 {
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;
299
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.
302      */
303     if (((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->flags & WINED3D_BUFFER_HASDESC) return FALSE;
304
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.
310      *
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.
316      *
317      * We have to distinguish between vertex shaders and fixed function to
318      * pick the way we access the strided vertex information.
319      *
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:
324      *
325      *      POSITIONT, FLOAT4
326      *      NORMAL, FLOAT3
327      *      DIFFUSE, FLOAT16_4
328      *      SPECULAR, D3DCOLOR
329      *
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]
333      *
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).
337      *
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
345      */
346     if (use_vs(device->stateBlock))
347     {
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
352          */
353         if (!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed)
354         {
355             if (This->conversion_map)
356             {
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;
365             }
366             else
367             {
368                 return FALSE;
369             }
370         }
371         for (i = 0; i < MAX_ATTRIBS; ++i)
372         {
373             ret = buffer_check_attribute(This, &device->strided_streams.elements[i],
374                     FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
375         }
376
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
379          */
380         if (ret && (float16_used || This->conversion_map))
381         {
382             HeapFree(GetProcessHeap(), 0, This->conversion_shift);
383             This->conversion_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
384         }
385     }
386     else
387     {
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.
391          */
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;
417
418         if (float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
419     }
420
421     if (stride_this_run == 0 && This->conversion_map)
422     {
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;
428     }
429
430     if (ret) TRACE("Conversion information changed\n");
431
432     return ret;
433 }
434
435 static void buffer_check_buffer_object_size(struct wined3d_buffer *This)
436 {
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)
440     {
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();
449     }
450 }
451
452 static inline void fixup_d3dcolor(DWORD *dst_color)
453 {
454     DWORD src_color = *dst_color;
455
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
458      *
459      * 0xff000000: Alpha mask
460      * 0x00ff0000: Blue mask
461      * 0x0000ff00: Green mask
462      * 0x000000ff: Red mask
463      */
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 */
468 }
469
470 static inline void fixup_transformed_pos(float *p)
471 {
472     float x, y, z, w;
473
474     /* rhw conversion like in drawStridedSlow */
475     if (p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps)))
476     {
477         x = p[0];
478         y = p[1];
479         z = p[2];
480         w = 1.0;
481     }
482     else
483     {
484         w = 1.0 / p[3];
485         x = p[0] * w;
486         y = p[1] * w;
487         z = p[2] * w;
488     }
489     p[0] = x;
490     p[1] = y;
491     p[2] = z;
492     p[3] = w;
493 }
494
495 const BYTE *buffer_get_memory(IWineD3DBuffer *iface, UINT offset, GLuint *buffer_object)
496 {
497     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
498
499     *buffer_object = This->buffer_object;
500     if (!This->buffer_object)
501     {
502         if (This->flags & WINED3D_BUFFER_CREATEBO)
503         {
504             buffer_create_buffer_object(This);
505             This->flags &= ~WINED3D_BUFFER_CREATEBO;
506             if (This->buffer_object)
507             {
508                 *buffer_object = This->buffer_object;
509                 return (const BYTE *)offset;
510             }
511         }
512         return This->resource.allocatedMemory + offset;
513     }
514     else
515     {
516         return (const BYTE *)offset;
517     }
518 }
519
520 /* IUnknown methods */
521
522 static HRESULT STDMETHODCALLTYPE buffer_QueryInterface(IWineD3DBuffer *iface,
523         REFIID riid, void **object)
524 {
525     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
526
527     if (IsEqualGUID(riid, &IID_IWineD3DBuffer)
528             || IsEqualGUID(riid, &IID_IWineD3DResource)
529             || IsEqualGUID(riid, &IID_IWineD3DBase)
530             || IsEqualGUID(riid, &IID_IUnknown))
531     {
532         IUnknown_AddRef(iface);
533         *object = iface;
534         return S_OK;
535     }
536
537     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
538
539     *object = NULL;
540
541     return E_NOINTERFACE;
542 }
543
544 static ULONG STDMETHODCALLTYPE buffer_AddRef(IWineD3DBuffer *iface)
545 {
546     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
547     ULONG refcount = InterlockedIncrement(&This->resource.ref);
548
549     TRACE("%p increasing refcount to %u\n", This, refcount);
550
551     return refcount;
552 }
553
554 static void STDMETHODCALLTYPE buffer_UnLoad(IWineD3DBuffer *iface)
555 {
556     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
557
558     TRACE("iface %p\n", iface);
559
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
563      */
564     if (This->buffer_object)
565     {
566         IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
567
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 */
575     }
576 }
577
578 static ULONG STDMETHODCALLTYPE buffer_Release(IWineD3DBuffer *iface)
579 {
580     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
581     ULONG refcount = InterlockedDecrement(&This->resource.ref);
582
583     TRACE("%p decreasing refcount to %u\n", This, refcount);
584
585     if (!refcount)
586     {
587         buffer_UnLoad(iface);
588         resource_cleanup((IWineD3DResource *)iface);
589         HeapFree(GetProcessHeap(), 0, This);
590     }
591
592     return refcount;
593 }
594
595 /* IWineD3DBase methods */
596
597 static HRESULT STDMETHODCALLTYPE buffer_GetParent(IWineD3DBuffer *iface, IUnknown **parent)
598 {
599     return resource_get_parent((IWineD3DResource *)iface, parent);
600 }
601
602 /* IWineD3DResource methods */
603
604 static HRESULT STDMETHODCALLTYPE buffer_GetDevice(IWineD3DBuffer *iface, IWineD3DDevice **device)
605 {
606     return resource_get_device((IWineD3DResource *)iface, device);
607 }
608
609 static HRESULT STDMETHODCALLTYPE buffer_SetPrivateData(IWineD3DBuffer *iface,
610         REFGUID guid, const void *data, DWORD data_size, DWORD flags)
611 {
612     return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
613 }
614
615 static HRESULT STDMETHODCALLTYPE buffer_GetPrivateData(IWineD3DBuffer *iface,
616         REFGUID guid, void *data, DWORD *data_size)
617 {
618     return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
619 }
620
621 static HRESULT STDMETHODCALLTYPE buffer_FreePrivateData(IWineD3DBuffer *iface, REFGUID guid)
622 {
623     return resource_free_private_data((IWineD3DResource *)iface, guid);
624 }
625
626 static DWORD STDMETHODCALLTYPE buffer_SetPriority(IWineD3DBuffer *iface, DWORD priority)
627 {
628     return resource_set_priority((IWineD3DResource *)iface, priority);
629 }
630
631 static DWORD STDMETHODCALLTYPE buffer_GetPriority(IWineD3DBuffer *iface)
632 {
633     return resource_get_priority((IWineD3DResource *)iface);
634 }
635
636 static void STDMETHODCALLTYPE buffer_PreLoad(IWineD3DBuffer *iface)
637 {
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;
644
645     TRACE("iface %p\n", iface);
646
647     if (!This->buffer_object)
648     {
649         /* TODO: Make converting independent from VBOs */
650         if (This->flags & WINED3D_BUFFER_CREATEBO)
651         {
652             buffer_create_buffer_object(This);
653             This->flags &= ~WINED3D_BUFFER_CREATEBO;
654         }
655         else
656         {
657             return; /* Not doing any conversion */
658         }
659     }
660
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)
663     {
664         decl_changed = buffer_find_decl(This);
665         This->flags |= WINED3D_BUFFER_HASDESC;
666     }
667
668     if (!decl_changed && !(This->flags & WINED3D_BUFFER_HASDESC && This->flags & WINED3D_BUFFER_DIRTY)) return;
669
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)
673      */
674     if (decl_changed)
675     {
676         ++This->conversion_count;
677         This->draw_count = 0;
678
679         if (This->conversion_count > VB_MAXDECLCHANGES)
680         {
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);
689
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
694              */
695             IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
696
697             return;
698         }
699         buffer_check_buffer_object_size(This);
700     }
701     else
702     {
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
706          */
707         ++This->draw_count;
708         if (This->draw_count > VB_RESETDECLCHANGE) This->conversion_count = 0;
709     }
710
711     if (decl_changed)
712     {
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;
717     }
718     else
719     {
720         /* No decl change, but dirty data, reload the changed stuff */
721         if (This->conversion_shift)
722         {
723             if (This->dirty_start != 0 || This->dirty_end != 0)
724             {
725                 FIXME("Implement partial buffer loading with shifted conversion\n");
726             }
727         }
728         start = This->dirty_start;
729         end = This->dirty_end;
730     }
731
732     /* Mark the buffer clean */
733     This->flags &= ~WINED3D_BUFFER_DIRTY;
734     This->dirty_start = 0;
735     This->dirty_end = 0;
736
737     if (!This->conversion_map)
738     {
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.
742          */
743         TRACE("No conversion needed\n");
744
745         if (!device->isInDraw)
746         {
747             ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
748         }
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;
756     }
757
758     /* Now for each vertex in the buffer that needs conversion */
759     vertices = This->resource.size / This->stride;
760
761     if (This->conversion_shift)
762     {
763         TRACE("Shifted conversion\n");
764         data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conversion_stride);
765
766         for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
767         {
768             for (j = 0; j < This->stride; ++j)
769             {
770                 switch(This->conversion_map[j])
771                 {
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;
776
777                     case CONV_FLOAT16_2:
778                     {
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]);
781
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;
786                     }
787
788                     default:
789                         FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
790                         break;
791                 }
792             }
793         }
794
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();
801     }
802     else
803     {
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)
807         {
808             for (j = 0; j < This->stride; ++j)
809             {
810                 switch(This->conversion_map[j])
811                 {
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;
820
821                     case CONV_POSITIONT:
822                         fixup_transformed_pos((float *) (data + i * This->stride + j));
823                         j += 15;
824                         break;
825
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]);
830                 }
831             }
832         }
833
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();
840     }
841
842     HeapFree(GetProcessHeap(), 0, data);
843 }
844
845 static WINED3DRESOURCETYPE STDMETHODCALLTYPE buffer_GetType(IWineD3DBuffer *iface)
846 {
847     return resource_get_type((IWineD3DResource *)iface);
848 }
849
850 /* IWineD3DBuffer methods */
851
852 static HRESULT STDMETHODCALLTYPE buffer_Map(IWineD3DBuffer *iface, UINT offset, UINT size, BYTE **data, DWORD flags)
853 {
854     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
855
856     TRACE("iface %p, offset %u, size %u, data %p, flags %#x\n", iface, offset, size, data, flags);
857
858     InterlockedIncrement(&This->lock_count);
859
860     if (This->flags & WINED3D_BUFFER_DIRTY)
861     {
862         if (This->dirty_start > offset) This->dirty_start = offset;
863
864         if (size)
865         {
866             if (This->dirty_end < offset + size) This->dirty_end = offset + size;
867         }
868         else
869         {
870             This->dirty_end = This->resource.size;
871         }
872     }
873     else
874     {
875         This->dirty_start = offset;
876         if (size) This->dirty_end = offset + size;
877         else This->dirty_end = This->resource.size;
878     }
879
880     This->flags |= WINED3D_BUFFER_DIRTY;
881
882     *data = This->resource.allocatedMemory + offset;
883
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) */
886
887     return WINED3D_OK;
888 }
889
890 static HRESULT STDMETHODCALLTYPE buffer_Unmap(IWineD3DBuffer *iface)
891 {
892     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
893
894     TRACE("(%p)\n", This);
895
896     if (InterlockedDecrement(&This->lock_count))
897     {
898         /* Delay loading the buffer until everything is unlocked */
899         TRACE("Ignoring unlock\n");
900         return WINED3D_OK;
901     }
902
903     if (This->flags & WINED3D_BUFFER_HASDESC)
904     {
905         buffer_PreLoad(iface);
906     }
907
908     return WINED3D_OK;
909 }
910
911 static HRESULT STDMETHODCALLTYPE buffer_GetDesc(IWineD3DBuffer *iface, WINED3DVERTEXBUFFER_DESC *desc)
912 {
913     struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
914
915     TRACE("(%p)\n", This);
916
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;
923
924     return WINED3D_OK;
925 }
926
927 const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
928 {
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,
949 };
950
951 /* IUnknown methods */
952
953 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_QueryInterface(IWineD3DIndexBuffer *iface,
954         REFIID riid, void **object)
955 {
956     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
957
958     if (IsEqualGUID(riid, &IID_IWineD3DIndexBuffer)
959             || IsEqualGUID(riid, &IID_IWineD3DResource)
960             || IsEqualGUID(riid, &IID_IWineD3DBase)
961             || IsEqualGUID(riid, &IID_IUnknown))
962     {
963         IUnknown_AddRef(iface);
964         *object = iface;
965         return S_OK;
966     }
967
968     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
969
970     *object = NULL;
971
972     return E_NOINTERFACE;
973 }
974
975 static ULONG STDMETHODCALLTYPE IWineD3DIndexBufferImpl_AddRef(IWineD3DIndexBuffer *iface)
976 {
977     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
978     ULONG refcount = InterlockedIncrement(&This->resource.ref);
979
980     TRACE("%p increasing refcount to %u\n", This, refcount);
981
982     return refcount;
983 }
984
985 static ULONG STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Release(IWineD3DIndexBuffer *iface)
986 {
987     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
988     ULONG refcount = InterlockedDecrement(&This->resource.ref);
989
990     TRACE("%p decreasing refcount to %u\n", This, refcount);
991
992     if (!refcount)
993     {
994         if (This->vbo)
995         {
996             IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
997
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
1004              */
1005             GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
1006             checkGLcall("glDeleteBuffersARB");
1007             LEAVE_GL();
1008         }
1009
1010         resource_cleanup((IWineD3DResource *)iface);
1011         HeapFree(GetProcessHeap(), 0, This);
1012     }
1013
1014     return refcount;
1015 }
1016
1017 /* IWineD3DBase methods */
1018
1019 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetParent(IWineD3DIndexBuffer *iface, IUnknown **parent)
1020 {
1021     return resource_get_parent((IWineD3DResource *)iface, parent);
1022 }
1023
1024 /* IWineD3DResource methods */
1025
1026 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetDevice(IWineD3DIndexBuffer *iface, IWineD3DDevice **device)
1027 {
1028     return resource_get_device((IWineD3DResource *)iface, device);
1029 }
1030
1031 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_SetPrivateData(IWineD3DIndexBuffer *iface,
1032         REFGUID guid, const void *data, DWORD data_size, DWORD flags)
1033 {
1034     return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
1035 }
1036
1037 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetPrivateData(IWineD3DIndexBuffer *iface,
1038         REFGUID guid, void *data, DWORD *data_size)
1039 {
1040     return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
1041 }
1042
1043 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_FreePrivateData(IWineD3DIndexBuffer *iface, REFGUID guid)
1044 {
1045     return resource_free_private_data((IWineD3DResource *)iface, guid);
1046 }
1047
1048 static DWORD STDMETHODCALLTYPE IWineD3DIndexBufferImpl_SetPriority(IWineD3DIndexBuffer *iface, DWORD priority)
1049 {
1050     return resource_set_priority((IWineD3DResource *)iface, priority);
1051 }
1052
1053 static DWORD STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetPriority(IWineD3DIndexBuffer *iface)
1054 {
1055     return resource_get_priority((IWineD3DResource *)iface);
1056 }
1057
1058 static void STDMETHODCALLTYPE IWineD3DIndexBufferImpl_PreLoad(IWineD3DIndexBuffer *iface)
1059 {
1060     TRACE("iface %p\n", iface);
1061 }
1062
1063 static void STDMETHODCALLTYPE IWineD3DIndexBufferImpl_UnLoad(IWineD3DIndexBuffer *iface)
1064 {
1065     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *) iface;
1066     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1067
1068     TRACE("(%p)\n", This);
1069
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)
1075      */
1076     if (This->vbo)
1077     {
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;
1084     }
1085 }
1086
1087 static WINED3DRESOURCETYPE STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetType(IWineD3DIndexBuffer *iface)
1088 {
1089     return resource_get_type((IWineD3DResource *)iface);
1090 }
1091
1092 /* IWineD3DIndexBuffer methods */
1093
1094 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Lock(IWineD3DIndexBuffer *iface,
1095         UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags)
1096 {
1097     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1098
1099     TRACE("(%p) : offset %d, size %d, Flags=%x\n", This, OffsetToLock, SizeToLock, Flags);
1100
1101     InterlockedIncrement(&This->lockcount);
1102     *ppbData = This->resource.allocatedMemory + OffsetToLock;
1103
1104     if (Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) || !This->vbo)
1105     {
1106         return WINED3D_OK;
1107     }
1108
1109     if (This->dirtystart != This->dirtyend)
1110     {
1111         if (This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
1112         if (SizeToLock)
1113         {
1114             if (This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
1115         }
1116         else
1117         {
1118             This->dirtyend = This->resource.size;
1119         }
1120     }
1121     else
1122     {
1123         This->dirtystart = OffsetToLock;
1124         if (SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
1125         else This->dirtyend = This->resource.size;
1126     }
1127
1128     return WINED3D_OK;
1129 }
1130
1131 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_Unlock(IWineD3DIndexBuffer *iface)
1132 {
1133     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1134     ULONG locks = InterlockedDecrement(&This->lockcount);
1135
1136     TRACE("(%p)\n", This);
1137
1138     /* For now load in unlock */
1139     if (!locks && This->vbo && (This->dirtyend - This->dirtystart) > 0)
1140     {
1141         IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1142
1143         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1144
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);
1156     }
1157
1158     return WINED3D_OK;
1159 }
1160
1161 static HRESULT STDMETHODCALLTYPE IWineD3DIndexBufferImpl_GetDesc(IWineD3DIndexBuffer *iface,
1162         WINED3DINDEXBUFFER_DESC *pDesc)
1163 {
1164     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
1165
1166     TRACE("(%p)\n", This);
1167
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;
1173
1174     return WINED3D_OK;
1175 }
1176
1177 const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl =
1178 {
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,
1199 };