wined3d: Use a separate STATE_TRANSFORM(WINED3D_TS_VIEW) state handler in the GLSL...
[wine/multimedia.git] / dlls / wined3d / buffer.c
blobf2a22b0048f428be8780a533ff482d8008746f34
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-2011, 2013-2014 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2010 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 WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
33 #define WINED3D_BUFFER_CREATEBO 0x02 /* Create a buffer object for this buffer. */
34 #define WINED3D_BUFFER_DOUBLEBUFFER 0x04 /* Keep both a buffer object and a system memory copy for this buffer. */
35 #define WINED3D_BUFFER_FLUSH 0x08 /* Manual unmap flushing. */
36 #define WINED3D_BUFFER_DISCARD 0x10 /* A DISCARD lock has occurred since the last preload. */
37 #define WINED3D_BUFFER_SYNC 0x20 /* There has been at least one synchronized map since the last preload. */
38 #define WINED3D_BUFFER_APPLESYNC 0x40 /* Using sync as in GL_APPLE_flush_buffer_range. */
40 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
41 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
42 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
43 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
45 static void buffer_invalidate_bo_range(struct wined3d_buffer *buffer, UINT offset, UINT size)
47 if (!offset && !size)
48 goto invalidate_all;
50 if (offset > buffer->resource.size || offset + size > buffer->resource.size)
52 WARN("Invalid range specified, invalidating entire buffer.\n");
53 goto invalidate_all;
56 if (buffer->modified_areas >= buffer->maps_size)
58 struct wined3d_map_range *new;
60 if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer->maps, 2 * buffer->maps_size * sizeof(*buffer->maps))))
62 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
63 goto invalidate_all;
66 buffer->maps = new;
67 buffer->maps_size *= 2;
70 buffer->maps[buffer->modified_areas].offset = offset;
71 buffer->maps[buffer->modified_areas].size = size;
72 ++buffer->modified_areas;
73 return;
75 invalidate_all:
76 buffer->modified_areas = 1;
77 buffer->maps[0].offset = 0;
78 buffer->maps[0].size = buffer->resource.size;
81 static inline void buffer_clear_dirty_areas(struct wined3d_buffer *This)
83 This->modified_areas = 0;
86 static BOOL buffer_is_dirty(const struct wined3d_buffer *buffer)
88 return !!buffer->modified_areas;
91 static BOOL buffer_is_fully_dirty(const struct wined3d_buffer *buffer)
93 unsigned int i;
95 for (i = 0; i < buffer->modified_areas; ++i)
97 if (!buffer->maps[i].offset && buffer->maps[i].size == buffer->resource.size)
98 return TRUE;
100 return FALSE;
103 /* Context activation is done by the caller */
104 static void delete_gl_buffer(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
106 if(!This->buffer_object) return;
108 GL_EXTCALL(glDeleteBuffers(1, &This->buffer_object));
109 checkGLcall("glDeleteBuffers");
110 This->buffer_object = 0;
112 if(This->query)
114 wined3d_event_query_destroy(This->query);
115 This->query = NULL;
117 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
120 /* Context activation is done by the caller. */
121 static void buffer_create_buffer_object(struct wined3d_buffer *This, struct wined3d_context *context)
123 GLenum gl_usage = GL_STATIC_DRAW_ARB;
124 GLenum error;
125 const struct wined3d_gl_info *gl_info = context->gl_info;
127 TRACE("Creating an OpenGL vertex buffer object for wined3d_buffer %p with usage %s.\n",
128 This, debug_d3dusage(This->resource.usage));
130 /* Make sure that the gl error is cleared. Do not use checkGLcall
131 * here because checkGLcall just prints a fixme and continues. However,
132 * if an error during VBO creation occurs we can fall back to non-vbo operation
133 * with full functionality(but performance loss)
135 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
137 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
138 * The vertex declaration from the device determines how the data in the
139 * buffer is interpreted. This means that on each draw call the buffer has
140 * to be verified to check if the rhw and color values are in the correct
141 * format. */
143 GL_EXTCALL(glGenBuffers(1, &This->buffer_object));
144 error = gl_info->gl_ops.gl.p_glGetError();
145 if (!This->buffer_object || error != GL_NO_ERROR)
147 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
148 goto fail;
151 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
152 context_invalidate_state(context, STATE_INDEXBUFFER);
153 GL_EXTCALL(glBindBuffer(This->buffer_type_hint, This->buffer_object));
154 error = gl_info->gl_ops.gl.p_glGetError();
155 if (error != GL_NO_ERROR)
157 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
158 goto fail;
161 if (This->resource.usage & WINED3DUSAGE_DYNAMIC)
163 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
164 gl_usage = GL_STREAM_DRAW_ARB;
166 if(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
168 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
169 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
170 This->flags |= WINED3D_BUFFER_FLUSH;
172 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
173 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
174 This->flags |= WINED3D_BUFFER_APPLESYNC;
176 /* No setup is needed here for GL_ARB_map_buffer_range */
179 /* Reserve memory for the buffer. The amount of data won't change
180 * so we are safe with calling glBufferData once and
181 * calling glBufferSubData on updates. Upload the actual data in case
182 * we're not double buffering, so we can release the heap mem afterwards
184 GL_EXTCALL(glBufferData(This->buffer_type_hint, This->resource.size, This->resource.heap_memory, gl_usage));
185 error = gl_info->gl_ops.gl.p_glGetError();
186 if (error != GL_NO_ERROR)
188 ERR("glBufferData failed with error %s (%#x)\n", debug_glerror(error), error);
189 goto fail;
192 This->buffer_object_usage = gl_usage;
194 if (This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
195 buffer_invalidate_bo_range(This, 0, 0);
196 else
197 wined3d_resource_free_sysmem(&This->resource);
199 return;
201 fail:
202 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
203 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
204 delete_gl_buffer(This, gl_info);
205 buffer_clear_dirty_areas(This);
208 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
209 const enum wined3d_buffer_conversion_type conversion_type,
210 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
212 DWORD attrib_size;
213 BOOL ret = FALSE;
214 unsigned int i;
215 DWORD_PTR data;
217 /* Check for some valid situations which cause us pain. One is if the buffer is used for
218 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
219 * with different strides. In the 2nd case we might have to drop conversion entirely,
220 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
222 if (!attrib->stride)
224 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
225 debug_d3dformat(attrib->format->id));
227 else if(attrib->stride != *stride_this_run && *stride_this_run)
229 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
231 else
233 *stride_this_run = attrib->stride;
234 if (This->stride != *stride_this_run)
236 /* We rely that this happens only on the first converted attribute that is found,
237 * if at all. See above check
239 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
240 This->stride = *stride_this_run;
241 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
242 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
243 sizeof(*This->conversion_map) * This->stride);
244 ret = TRUE;
248 data = ((DWORD_PTR)attrib->data.addr) % This->stride;
249 attrib_size = attrib->format->component_count * attrib->format->component_size;
250 for (i = 0; i < attrib_size; ++i)
252 DWORD_PTR idx = (data + i) % This->stride;
253 if (This->conversion_map[idx] != conversion_type)
255 TRACE("Byte %ld in vertex changed\n", idx);
256 TRACE("It was type %d, is %d now\n", This->conversion_map[idx], conversion_type);
257 ret = TRUE;
258 This->conversion_map[idx] = conversion_type;
262 return ret;
265 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
266 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
268 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
269 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
271 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
272 enum wined3d_format_id format;
273 BOOL ret = FALSE;
275 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
276 * there, on nonexistent attribs the vbo is 0.
278 if (!(si->use_map & (1 << attrib_idx))
279 || state->streams[attrib->stream_idx].buffer != This)
280 return FALSE;
282 format = attrib->format->id;
283 /* Look for newly appeared conversion */
284 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
286 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
288 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
290 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
292 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
293 return FALSE;
296 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
298 else if (This->conversion_map)
300 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
303 return ret;
306 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
307 const struct wined3d_state *state, DWORD fixup_flags)
309 UINT stride_this_run = 0;
310 BOOL ret = FALSE;
312 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
313 * Once we have our declaration there is no need to look it up again. Index buffers also never need
314 * conversion, so once the (empty) conversion structure is created don't bother checking again
316 if (This->flags & WINED3D_BUFFER_HASDESC)
318 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
321 if (!fixup_flags)
323 TRACE("No fixup required.\n");
324 if(This->conversion_map)
326 HeapFree(GetProcessHeap(), 0, This->conversion_map);
327 This->conversion_map = NULL;
328 This->stride = 0;
329 return TRUE;
332 return FALSE;
335 TRACE("Finding vertex buffer conversion information\n");
336 /* Certain declaration types need some fixups before we can pass them to
337 * opengl. This means D3DCOLOR attributes with fixed function vertex
338 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
339 * GL_ARB_half_float_vertex is not supported.
341 * Note for d3d8 and d3d9:
342 * The vertex buffer FVF doesn't help with finding them, we have to use
343 * the decoded vertex declaration and pick the things that concern the
344 * current buffer. A problem with this is that this can change between
345 * draws, so we have to validate the information and reprocess the buffer
346 * if it changes, and avoid false positives for performance reasons.
347 * WineD3D doesn't even know the vertex buffer any more, it is managed
348 * by the client libraries and passed to SetStreamSource and ProcessVertices
349 * as needed.
351 * We have to distinguish between vertex shaders and fixed function to
352 * pick the way we access the strided vertex information.
354 * This code sets up a per-byte array with the size of the detected
355 * stride of the arrays in the buffer. For each byte we have a field
356 * that marks the conversion needed on this byte. For example, the
357 * following declaration with fixed function vertex processing:
359 * POSITIONT, FLOAT4
360 * NORMAL, FLOAT3
361 * DIFFUSE, FLOAT16_4
362 * SPECULAR, D3DCOLOR
364 * Will result in
365 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
366 * [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]
368 * Where in this example map P means 4 component position conversion, 0
369 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
370 * conversion (red / blue swizzle).
372 * If we're doing conversion and the stride changes we have to reconvert
373 * the whole buffer. Note that we do not mind if the semantic changes,
374 * we only care for the conversion type. So if the NORMAL is replaced
375 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
376 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
377 * conversion types depend on the semantic as well, for example a FLOAT4
378 * texcoord needs no conversion while a FLOAT4 positiont needs one
381 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
382 fixup_flags, &stride_this_run) || ret;
383 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
385 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
386 fixup_flags, &stride_this_run) || ret;
387 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
388 fixup_flags, &stride_this_run) || ret;
389 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
390 fixup_flags, &stride_this_run) || ret;
391 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
392 fixup_flags, &stride_this_run) || ret;
393 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
394 fixup_flags, &stride_this_run) || ret;
395 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
396 fixup_flags, &stride_this_run) || ret;
397 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
398 fixup_flags, &stride_this_run) || ret;
399 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
400 fixup_flags, &stride_this_run) || ret;
401 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
402 fixup_flags, &stride_this_run) || ret;
403 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
404 fixup_flags, &stride_this_run) || ret;
405 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
406 fixup_flags, &stride_this_run) || ret;
408 if (!stride_this_run && This->conversion_map)
410 /* Sanity test */
411 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
412 HeapFree(GetProcessHeap(), 0, This->conversion_map);
413 This->conversion_map = NULL;
414 This->stride = 0;
417 if (ret) TRACE("Conversion information changed\n");
419 return ret;
422 static inline void fixup_d3dcolor(DWORD *dst_color)
424 DWORD src_color = *dst_color;
426 /* Color conversion like in drawStridedSlow. watch out for little endianity
427 * If we want that stuff to work on big endian machines too we have to consider more things
429 * 0xff000000: Alpha mask
430 * 0x00ff0000: Blue mask
431 * 0x0000ff00: Green mask
432 * 0x000000ff: Red mask
434 *dst_color = 0;
435 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
436 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
437 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
440 static inline void fixup_transformed_pos(float *p)
442 /* rhw conversion like in position_float4(). */
443 if (p[3] != 1.0f && p[3] != 0.0f)
445 float w = 1.0f / p[3];
446 p[0] *= w;
447 p[1] *= w;
448 p[2] *= w;
449 p[3] = w;
453 /* Context activation is done by the caller. */
454 void buffer_get_memory(struct wined3d_buffer *buffer, struct wined3d_context *context,
455 struct wined3d_bo_address *data)
457 data->buffer_object = buffer->buffer_object;
458 if (!buffer->buffer_object)
460 if ((buffer->flags & WINED3D_BUFFER_CREATEBO) && !buffer->resource.map_count)
462 buffer_create_buffer_object(buffer, context);
463 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
464 if (buffer->buffer_object)
466 data->buffer_object = buffer->buffer_object;
467 data->addr = NULL;
468 return;
471 data->addr = buffer->resource.heap_memory;
473 else
475 data->addr = NULL;
479 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
481 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
483 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
485 return refcount;
488 /* Context activation is done by the caller. */
489 BYTE *buffer_get_sysmem(struct wined3d_buffer *This, struct wined3d_context *context)
491 const struct wined3d_gl_info *gl_info = context->gl_info;
493 /* Heap_memory exists if the buffer is double buffered or has no buffer object at all. */
494 if (This->resource.heap_memory)
495 return This->resource.heap_memory;
497 if (!wined3d_resource_allocate_sysmem(&This->resource))
498 ERR("Failed to allocate system memory.\n");
500 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
501 context_invalidate_state(context, STATE_INDEXBUFFER);
503 GL_EXTCALL(glBindBuffer(This->buffer_type_hint, This->buffer_object));
504 GL_EXTCALL(glGetBufferSubData(This->buffer_type_hint, 0, This->resource.size, This->resource.heap_memory));
505 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
507 return This->resource.heap_memory;
510 static void buffer_unload(struct wined3d_resource *resource)
512 struct wined3d_buffer *buffer = buffer_from_resource(resource);
514 TRACE("buffer %p.\n", buffer);
516 if (buffer->buffer_object)
518 struct wined3d_device *device = resource->device;
519 struct wined3d_context *context;
521 context = context_acquire(device, NULL);
523 /* Download the buffer, but don't permanently enable double buffering */
524 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
526 buffer_get_sysmem(buffer, context);
527 buffer->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
530 delete_gl_buffer(buffer, context->gl_info);
531 buffer->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
532 buffer_clear_dirty_areas(buffer);
534 context_release(context);
536 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
537 buffer->conversion_map = NULL;
538 buffer->stride = 0;
539 buffer->conversion_stride = 0;
540 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
543 resource_unload(resource);
546 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
548 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
549 struct wined3d_context *context;
551 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
553 if (!refcount)
555 if (buffer->buffer_object)
557 context = context_acquire(buffer->resource.device, NULL);
558 delete_gl_buffer(buffer, context->gl_info);
559 context_release(context);
561 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
564 resource_cleanup(&buffer->resource);
565 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
566 HeapFree(GetProcessHeap(), 0, buffer->maps);
567 HeapFree(GetProcessHeap(), 0, buffer);
570 return refcount;
573 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
575 TRACE("buffer %p.\n", buffer);
577 return buffer->resource.parent;
580 /* The caller provides a context and binds the buffer */
581 static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
583 enum wined3d_event_query_result ret;
585 /* No fencing needs to be done if the app promises not to overwrite
586 * existing data. */
587 if (flags & WINED3D_MAP_NOOVERWRITE)
588 return;
590 if (flags & WINED3D_MAP_DISCARD)
592 GL_EXTCALL(glBufferData(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
593 checkGLcall("glBufferData");
594 return;
597 if(!This->query)
599 TRACE("Creating event query for buffer %p\n", This);
601 if (!wined3d_event_query_supported(gl_info))
603 FIXME("Event queries not supported, dropping async buffer locks.\n");
604 goto drop_query;
607 This->query = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->query));
608 if (!This->query)
610 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
611 goto drop_query;
614 /* Since we don't know about old draws a glFinish is needed once */
615 gl_info->gl_ops.gl.p_glFinish();
616 return;
618 TRACE("Synchronizing buffer %p\n", This);
619 ret = wined3d_event_query_finish(This->query, This->resource.device);
620 switch(ret)
622 case WINED3D_EVENT_QUERY_NOT_STARTED:
623 case WINED3D_EVENT_QUERY_OK:
624 /* All done */
625 return;
627 case WINED3D_EVENT_QUERY_WRONG_THREAD:
628 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
629 goto drop_query;
631 default:
632 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret);
633 goto drop_query;
636 drop_query:
637 if(This->query)
639 wined3d_event_query_destroy(This->query);
640 This->query = NULL;
643 gl_info->gl_ops.gl.p_glFinish();
644 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
645 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
646 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
649 /* The caller provides a GL context */
650 static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
652 BYTE *map;
653 UINT start = 0, len = 0;
655 /* This potentially invalidates the element array buffer binding, but the
656 * caller always takes care of this. */
657 GL_EXTCALL(glBindBuffer(This->buffer_type_hint, This->buffer_object));
658 checkGLcall("glBindBuffer");
659 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
661 GLbitfield mapflags;
662 mapflags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
663 if (flags & WINED3D_BUFFER_DISCARD)
664 mapflags |= GL_MAP_INVALIDATE_BUFFER_BIT;
665 else if (!(flags & WINED3D_BUFFER_SYNC))
666 mapflags |= GL_MAP_UNSYNCHRONIZED_BIT;
667 map = GL_EXTCALL(glMapBufferRange(This->buffer_type_hint, 0,
668 This->resource.size, mapflags));
669 checkGLcall("glMapBufferRange");
671 else
673 if (This->flags & WINED3D_BUFFER_APPLESYNC)
675 DWORD syncflags = 0;
676 if (flags & WINED3D_BUFFER_DISCARD)
677 syncflags |= WINED3D_MAP_DISCARD;
678 else if (!(flags & WINED3D_BUFFER_SYNC))
679 syncflags |= WINED3D_MAP_NOOVERWRITE;
680 buffer_sync_apple(This, syncflags, gl_info);
682 map = GL_EXTCALL(glMapBuffer(This->buffer_type_hint, GL_WRITE_ONLY));
683 checkGLcall("glMapBuffer");
685 if (!map)
687 ERR("Failed to map opengl buffer\n");
688 return;
691 while (This->modified_areas)
693 This->modified_areas--;
694 start = This->maps[This->modified_areas].offset;
695 len = This->maps[This->modified_areas].size;
697 memcpy(map + start, (BYTE *)This->resource.heap_memory + start, len);
699 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
701 GL_EXTCALL(glFlushMappedBufferRange(This->buffer_type_hint, start, len));
702 checkGLcall("glFlushMappedBufferRange");
704 else if (This->flags & WINED3D_BUFFER_FLUSH)
706 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This->buffer_type_hint, start, len));
707 checkGLcall("glFlushMappedBufferRangeAPPLE");
710 GL_EXTCALL(glUnmapBuffer(This->buffer_type_hint));
711 checkGLcall("glUnmapBuffer");
714 void buffer_mark_used(struct wined3d_buffer *buffer)
716 buffer->flags &= ~(WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
719 /* Context activation is done by the caller. */
720 void buffer_internal_preload(struct wined3d_buffer *buffer, struct wined3d_context *context,
721 const struct wined3d_state *state)
723 DWORD flags = buffer->flags & (WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
724 struct wined3d_device *device = buffer->resource.device;
725 UINT start = 0, end = 0, len = 0, vertices;
726 const struct wined3d_gl_info *gl_info;
727 BOOL decl_changed = FALSE;
728 unsigned int i, j;
729 BYTE *data;
731 TRACE("buffer %p.\n", buffer);
733 if (buffer->resource.map_count)
735 WARN("Buffer is mapped, skipping preload.\n");
736 return;
739 buffer_mark_used(buffer);
741 if (!buffer->buffer_object)
743 /* TODO: Make converting independent from VBOs */
744 if (buffer->flags & WINED3D_BUFFER_CREATEBO)
746 buffer_create_buffer_object(buffer, context);
747 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
749 else
751 /* Not doing any conversion */
752 return;
756 /* Reading the declaration makes only sense if we have valid state information
757 * (i.e., if this function is called during draws). */
758 if (state)
760 DWORD fixup_flags = 0;
762 if (!use_vs(state))
764 if (!context->gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
765 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
766 if (!context->d3d_info->xyzrhw)
767 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
770 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
771 buffer->flags |= WINED3D_BUFFER_HASDESC;
774 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
776 ++buffer->draw_count;
777 if (buffer->draw_count > VB_RESETDECLCHANGE)
778 buffer->decl_change_count = 0;
779 if (buffer->draw_count > VB_RESETFULLCONVS)
780 buffer->full_conversion_count = 0;
781 return;
784 /* If applications change the declaration over and over, reconverting all the time is a huge
785 * performance hit. So count the declaration changes and release the VBO if there are too many
786 * of them (and thus stop converting)
788 if (decl_changed)
790 ++buffer->decl_change_count;
791 buffer->draw_count = 0;
793 if (buffer->decl_change_count > VB_MAXDECLCHANGES
794 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
796 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
798 buffer_unload(&buffer->resource);
799 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
801 /* The stream source state handler might have read the memory of
802 * the vertex buffer already and got the memory in the vbo which
803 * is not valid any longer. Dirtify the stream source to force a
804 * reload. This happens only once per changed vertexbuffer and
805 * should occur rather rarely. */
806 device_invalidate_state(device, STATE_STREAMSRC);
807 return;
810 /* The declaration changed, reload the whole buffer. */
811 WARN("Reloading buffer because of a vertex declaration change.\n");
812 buffer_invalidate_bo_range(buffer, 0, 0);
814 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
815 * cleared for unsynchronized updates
817 flags = 0;
819 else
821 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
822 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
823 * decl changes and reset the decl change count after a specific number of them
825 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
827 ++buffer->full_conversion_count;
828 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
830 FIXME("Too many full buffer conversions, stopping converting.\n");
831 buffer_unload(&buffer->resource);
832 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
833 if (buffer->resource.bind_count)
834 device_invalidate_state(device, STATE_STREAMSRC);
835 return;
838 else
840 ++buffer->draw_count;
841 if (buffer->draw_count > VB_RESETDECLCHANGE)
842 buffer->decl_change_count = 0;
843 if (buffer->draw_count > VB_RESETFULLCONVS)
844 buffer->full_conversion_count = 0;
848 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
849 device_invalidate_state(device, STATE_INDEXBUFFER);
851 if (!buffer->conversion_map)
853 /* That means that there is nothing to fixup. Just upload from
854 * buffer->resource.heap_memory directly into the vbo. Do not
855 * free the system memory copy because drawPrimitive may need it if
856 * the stride is 0, for instancing emulation, vertex blending
857 * emulation or shader emulation. */
858 TRACE("No conversion needed.\n");
860 /* Nothing to do because we locked directly into the vbo */
861 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
863 return;
866 buffer_direct_upload(buffer, context->gl_info, flags);
868 return;
871 gl_info = context->gl_info;
873 if(!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
875 buffer_get_sysmem(buffer, context);
878 /* Now for each vertex in the buffer that needs conversion */
879 vertices = buffer->resource.size / buffer->stride;
881 data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size);
883 while(buffer->modified_areas)
885 buffer->modified_areas--;
886 start = buffer->maps[buffer->modified_areas].offset;
887 len = buffer->maps[buffer->modified_areas].size;
888 end = start + len;
890 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
891 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertices); ++i)
893 for (j = 0; j < buffer->stride; ++j)
895 switch (buffer->conversion_map[j])
897 case CONV_NONE:
898 /* Done already */
899 j += 3;
900 break;
901 case CONV_D3DCOLOR:
902 fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
903 j += 3;
904 break;
906 case CONV_POSITIONT:
907 fixup_transformed_pos((float *) (data + i * buffer->stride + j));
908 j += 15;
909 break;
910 default:
911 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer->conversion_map[j]);
916 GL_EXTCALL(glBindBuffer(buffer->buffer_type_hint, buffer->buffer_object));
917 checkGLcall("glBindBuffer");
918 GL_EXTCALL(glBufferSubData(buffer->buffer_type_hint, start, len, data + start));
919 checkGLcall("glBufferSubData");
922 HeapFree(GetProcessHeap(), 0, data);
925 void CDECL wined3d_buffer_preload(struct wined3d_buffer *buffer)
927 struct wined3d_context *context;
928 context = context_acquire(buffer->resource.device, NULL);
929 buffer_internal_preload(buffer, context, NULL);
930 context_release(context);
933 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
935 TRACE("buffer %p.\n", buffer);
937 return &buffer->resource;
940 HRESULT CDECL wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
942 LONG count;
943 BYTE *base;
945 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x\n", buffer, offset, size, data, flags);
947 flags = wined3d_resource_sanitize_map_flags(&buffer->resource, flags);
948 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
949 * fill rate test seems to depend on this. When we map a buffer with
950 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
951 * previous contents of the buffer. The r600g driver only does this when
952 * the buffer is currently in use, while the proprietary NVIDIA driver
953 * appears to do this unconditionally. */
954 if (buffer->flags & WINED3D_BUFFER_DISCARD)
955 flags &= ~WINED3D_MAP_DISCARD;
956 count = ++buffer->resource.map_count;
958 if (buffer->buffer_object)
960 /* DISCARD invalidates the entire buffer, regardless of the specified
961 * offset and size. Some applications also depend on the entire buffer
962 * being uploaded in that case. Two such applications are Port Royale
963 * and Darkstar One. */
964 if (flags & WINED3D_MAP_DISCARD)
965 buffer_invalidate_bo_range(buffer, 0, 0);
966 else if (!(flags & WINED3D_MAP_READONLY))
967 buffer_invalidate_bo_range(buffer, offset, size);
969 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
971 if (count == 1)
973 struct wined3d_device *device = buffer->resource.device;
974 struct wined3d_context *context;
975 const struct wined3d_gl_info *gl_info;
977 context = context_acquire(device, NULL);
978 gl_info = context->gl_info;
980 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
981 context_invalidate_state(context, STATE_INDEXBUFFER);
982 GL_EXTCALL(glBindBuffer(buffer->buffer_type_hint, buffer->buffer_object));
984 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
986 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
987 buffer->map_ptr = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
988 0, buffer->resource.size, mapflags));
989 checkGLcall("glMapBufferRange");
991 else
993 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
994 buffer_sync_apple(buffer, flags, gl_info);
995 buffer->map_ptr = GL_EXTCALL(glMapBuffer(buffer->buffer_type_hint,
996 GL_READ_WRITE));
997 checkGLcall("glMapBuffer");
1000 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
1002 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
1004 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1005 checkGLcall("glUnmapBuffer");
1006 buffer->map_ptr = NULL;
1008 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1010 /* The extra copy is more expensive than not using VBOs at
1011 * all on the Nvidia Linux driver, which is the only driver
1012 * that returns unaligned pointers
1014 TRACE("Dynamic buffer, dropping VBO\n");
1015 buffer_unload(&buffer->resource);
1016 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
1017 if (buffer->resource.bind_count)
1018 device_invalidate_state(device, STATE_STREAMSRC);
1020 else
1022 TRACE("Falling back to doublebuffered operation\n");
1023 buffer_get_sysmem(buffer, context);
1025 TRACE("New pointer is %p.\n", buffer->resource.heap_memory);
1026 buffer->map_ptr = NULL;
1028 context_release(context);
1032 if (flags & WINED3D_MAP_DISCARD)
1033 buffer->flags |= WINED3D_BUFFER_DISCARD;
1034 else if (!(flags & WINED3D_MAP_NOOVERWRITE))
1035 buffer->flags |= WINED3D_BUFFER_SYNC;
1038 base = buffer->map_ptr ? buffer->map_ptr : buffer->resource.heap_memory;
1039 *data = base + offset;
1041 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, base, offset);
1042 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1044 return WINED3D_OK;
1047 void CDECL wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1049 ULONG i;
1051 TRACE("buffer %p.\n", buffer);
1053 /* In the case that the number of Unmap calls > the
1054 * number of Map calls, d3d returns always D3D_OK.
1055 * This is also needed to prevent Map from returning garbage on
1056 * the next call (this will happen if the lock_count is < 0). */
1057 if (!buffer->resource.map_count)
1059 WARN("Unmap called without a previous map call.\n");
1060 return;
1063 if (--buffer->resource.map_count)
1065 /* Delay loading the buffer until everything is unlocked */
1066 TRACE("Ignoring unmap.\n");
1067 return;
1070 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER) && buffer->buffer_object)
1072 struct wined3d_device *device = buffer->resource.device;
1073 const struct wined3d_gl_info *gl_info;
1074 struct wined3d_context *context;
1076 context = context_acquire(device, NULL);
1077 gl_info = context->gl_info;
1079 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1080 context_invalidate_state(context, STATE_INDEXBUFFER);
1081 GL_EXTCALL(glBindBuffer(buffer->buffer_type_hint, buffer->buffer_object));
1083 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1085 for (i = 0; i < buffer->modified_areas; ++i)
1087 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1088 buffer->maps[i].offset, buffer->maps[i].size));
1089 checkGLcall("glFlushMappedBufferRange");
1092 else if (buffer->flags & WINED3D_BUFFER_FLUSH)
1094 for (i = 0; i < buffer->modified_areas; ++i)
1096 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1097 buffer->maps[i].offset, buffer->maps[i].size));
1098 checkGLcall("glFlushMappedBufferRangeAPPLE");
1102 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1103 if (wined3d_settings.strict_draw_ordering)
1104 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1105 context_release(context);
1107 buffer_clear_dirty_areas(buffer);
1108 buffer->map_ptr = NULL;
1110 else if (buffer->flags & WINED3D_BUFFER_HASDESC)
1112 wined3d_buffer_preload(buffer);
1116 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1118 return wined3d_buffer_incref(buffer_from_resource(resource));
1121 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1123 return wined3d_buffer_decref(buffer_from_resource(resource));
1126 static const struct wined3d_resource_ops buffer_resource_ops =
1128 buffer_resource_incref,
1129 buffer_resource_decref,
1130 buffer_unload,
1133 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1134 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, GLenum bind_hint,
1135 const char *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1137 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1138 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1139 HRESULT hr;
1140 BOOL dynamic_buffer_ok;
1142 if (!size)
1144 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1145 return WINED3DERR_INVALIDCALL;
1148 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1149 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size,
1150 parent, parent_ops, &buffer_resource_ops);
1151 if (FAILED(hr))
1153 WARN("Failed to initialize resource, hr %#x\n", hr);
1154 return hr;
1156 buffer->buffer_type_hint = bind_hint;
1158 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1159 debug_d3dformat(buffer->resource.format->id), buffer->resource.heap_memory, buffer);
1161 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING)
1163 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1164 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1165 * Still use a VBO to support OpenGL 3 core contexts. */
1166 TRACE("Using doublebuffer mode because of software vertex processing\n");
1167 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
1170 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1172 /* Observations show that drawStridedSlow is faster on dynamic VBs than converting +
1173 * drawStridedFast (half-life 2 and others).
1175 * Basically converting the vertices in the buffer is quite expensive, and observations
1176 * show that drawStridedSlow is faster than converting + uploading + drawStridedFast.
1177 * Therefore do not create a VBO for WINED3DUSAGE_DYNAMIC buffers.
1179 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1181 TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
1183 else if(buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1185 TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
1187 else if(!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1189 TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
1191 else
1193 buffer->flags |= WINED3D_BUFFER_CREATEBO;
1196 if (data)
1198 BYTE *ptr;
1200 hr = wined3d_buffer_map(buffer, 0, size, &ptr, 0);
1201 if (FAILED(hr))
1203 ERR("Failed to map buffer, hr %#x\n", hr);
1204 buffer_unload(&buffer->resource);
1205 resource_cleanup(&buffer->resource);
1206 return hr;
1209 memcpy(ptr, data, size);
1211 wined3d_buffer_unmap(buffer);
1214 buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps));
1215 if (!buffer->maps)
1217 ERR("Out of memory\n");
1218 buffer_unload(&buffer->resource);
1219 resource_cleanup(&buffer->resource);
1220 return E_OUTOFMEMORY;
1222 buffer->maps_size = 1;
1224 return WINED3D_OK;
1227 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1228 const void *data, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1230 struct wined3d_buffer *object;
1231 HRESULT hr;
1233 TRACE("device %p, desc %p, data %p, parent %p, buffer %p\n", device, desc, data, parent, buffer);
1235 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1236 if (!object)
1237 return E_OUTOFMEMORY;
1239 FIXME("Ignoring access flags (pool)\n");
1241 hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1242 WINED3D_POOL_MANAGED, GL_ARRAY_BUFFER_ARB, data, parent, parent_ops);
1243 if (FAILED(hr))
1245 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1246 HeapFree(GetProcessHeap(), 0, object);
1247 return hr;
1249 object->desc = *desc;
1251 TRACE("Created buffer %p.\n", object);
1253 *buffer = object;
1255 return WINED3D_OK;
1258 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1259 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1261 struct wined3d_buffer *object;
1262 HRESULT hr;
1264 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1265 device, size, usage, pool, parent, parent_ops, buffer);
1267 if (pool == WINED3D_POOL_SCRATCH)
1269 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1270 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1271 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1272 *buffer = NULL;
1273 return WINED3DERR_INVALIDCALL;
1276 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1277 if (!object)
1279 *buffer = NULL;
1280 return WINED3DERR_OUTOFVIDEOMEMORY;
1283 hr = buffer_init(object, device, size, usage, WINED3DFMT_VERTEXDATA,
1284 pool, GL_ARRAY_BUFFER_ARB, NULL, parent, parent_ops);
1285 if (FAILED(hr))
1287 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1288 HeapFree(GetProcessHeap(), 0, object);
1289 return hr;
1292 TRACE("Created buffer %p.\n", object);
1293 *buffer = object;
1295 return WINED3D_OK;
1298 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1299 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1301 struct wined3d_buffer *object;
1302 HRESULT hr;
1304 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1305 device, size, usage, pool, parent, parent_ops, buffer);
1307 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1308 if (!object)
1310 *buffer = NULL;
1311 return WINED3DERR_OUTOFVIDEOMEMORY;
1314 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1315 WINED3DFMT_UNKNOWN, pool, GL_ELEMENT_ARRAY_BUFFER_ARB, NULL,
1316 parent, parent_ops);
1317 if (FAILED(hr))
1319 WARN("Failed to initialize buffer, hr %#x\n", hr);
1320 HeapFree(GetProcessHeap(), 0, object);
1321 return hr;
1324 TRACE("Created buffer %p.\n", object);
1325 *buffer = object;
1327 return WINED3D_OK;