wined3d: Use GL_STATIC_DRAW_ARB for static buffers.
[wine/multimedia.git] / dlls / wined3d / buffer.c
blobdc5b4d27b6c5d44c7e71260bdf4e453d4687f43d
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-2010 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 VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
33 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
34 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
35 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
37 static inline BOOL buffer_add_dirty_area(struct wined3d_buffer *This, UINT offset, UINT size)
39 if (!This->buffer_object) return TRUE;
41 if (This->maps_size <= This->modified_areas)
43 void *new = HeapReAlloc(GetProcessHeap(), 0, This->maps,
44 This->maps_size * 2 * sizeof(*This->maps));
45 if (!new)
47 ERR("Out of memory\n");
48 return FALSE;
50 else
52 This->maps = new;
53 This->maps_size *= 2;
57 if(offset > This->resource.size || offset + size > This->resource.size)
59 WARN("Invalid range dirtified, marking entire buffer dirty\n");
60 offset = 0;
61 size = This->resource.size;
63 else if(!offset && !size)
65 size = This->resource.size;
68 This->maps[This->modified_areas].offset = offset;
69 This->maps[This->modified_areas].size = size;
70 This->modified_areas++;
71 return TRUE;
74 static inline void buffer_clear_dirty_areas(struct wined3d_buffer *This)
76 This->modified_areas = 0;
79 static BOOL buffer_is_dirty(const struct wined3d_buffer *buffer)
81 return !!buffer->modified_areas;
84 static BOOL buffer_is_fully_dirty(const struct wined3d_buffer *buffer)
86 unsigned int i;
88 for (i = 0; i < buffer->modified_areas; ++i)
90 if (!buffer->maps[i].offset && buffer->maps[i].size == buffer->resource.size)
91 return TRUE;
93 return FALSE;
96 /* Context activation is done by the caller */
97 static void delete_gl_buffer(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
99 if(!This->buffer_object) return;
101 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
102 checkGLcall("glDeleteBuffersARB");
103 This->buffer_object = 0;
105 if(This->query)
107 wined3d_event_query_destroy(This->query);
108 This->query = NULL;
110 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
113 /* Context activation is done by the caller. */
114 static void buffer_create_buffer_object(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
116 GLenum gl_usage = GL_STATIC_DRAW_ARB;
117 GLenum error;
119 TRACE("Creating an OpenGL vertex buffer object for wined3d_buffer %p with usage %s.\n",
120 This, debug_d3dusage(This->resource.usage));
122 /* Make sure that the gl error is cleared. Do not use checkGLcall
123 * here because checkGLcall just prints a fixme and continues. However,
124 * if an error during VBO creation occurs we can fall back to non-vbo operation
125 * with full functionality(but performance loss)
127 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
129 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
130 * The vertex declaration from the device determines how the data in the
131 * buffer is interpreted. This means that on each draw call the buffer has
132 * to be verified to check if the rhw and color values are in the correct
133 * format. */
135 GL_EXTCALL(glGenBuffersARB(1, &This->buffer_object));
136 error = gl_info->gl_ops.gl.p_glGetError();
137 if (!This->buffer_object || error != GL_NO_ERROR)
139 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
140 goto fail;
143 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
144 device_invalidate_state(This->resource.device, STATE_INDEXBUFFER);
145 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
146 error = gl_info->gl_ops.gl.p_glGetError();
147 if (error != GL_NO_ERROR)
149 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
150 goto fail;
153 if (This->resource.usage & WINED3DUSAGE_DYNAMIC)
155 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
156 gl_usage = GL_STREAM_DRAW_ARB;
158 if(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
160 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
161 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
162 This->flags |= WINED3D_BUFFER_FLUSH;
164 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
165 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
166 This->flags |= WINED3D_BUFFER_APPLESYNC;
168 /* No setup is needed here for GL_ARB_map_buffer_range */
171 /* Reserve memory for the buffer. The amount of data won't change
172 * so we are safe with calling glBufferData once and
173 * calling glBufferSubData on updates. Upload the actual data in case
174 * we're not double buffering, so we can release the heap mem afterwards
176 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, This->resource.allocatedMemory, gl_usage));
177 error = gl_info->gl_ops.gl.p_glGetError();
178 if (error != GL_NO_ERROR)
180 ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
181 goto fail;
184 This->buffer_object_size = This->resource.size;
185 This->buffer_object_usage = gl_usage;
187 if(This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
189 if(!buffer_add_dirty_area(This, 0, 0))
191 ERR("buffer_add_dirty_area failed, this is not expected\n");
192 goto fail;
195 else
197 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
198 This->resource.allocatedMemory = NULL;
199 This->resource.heapMemory = NULL;
202 return;
204 fail:
205 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
206 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
207 delete_gl_buffer(This, gl_info);
208 buffer_clear_dirty_areas(This);
211 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
212 const enum wined3d_buffer_conversion_type conversion_type,
213 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
215 DWORD attrib_size;
216 BOOL ret = FALSE;
217 unsigned int i;
218 DWORD_PTR data;
220 /* Check for some valid situations which cause us pain. One is if the buffer is used for
221 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
222 * with different strides. In the 2nd case we might have to drop conversion entirely,
223 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
225 if (!attrib->stride)
227 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
228 debug_d3dformat(attrib->format->id));
230 else if(attrib->stride != *stride_this_run && *stride_this_run)
232 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
234 else
236 *stride_this_run = attrib->stride;
237 if (This->stride != *stride_this_run)
239 /* We rely that this happens only on the first converted attribute that is found,
240 * if at all. See above check
242 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
243 This->stride = *stride_this_run;
244 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
245 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
246 sizeof(*This->conversion_map) * This->stride);
247 ret = TRUE;
251 data = ((DWORD_PTR)attrib->data.addr) % This->stride;
252 attrib_size = attrib->format->component_count * attrib->format->component_size;
253 for (i = 0; i < attrib_size; ++i)
255 DWORD_PTR idx = (data + i) % This->stride;
256 if (This->conversion_map[idx] != conversion_type)
258 TRACE("Byte %ld in vertex changed\n", idx);
259 TRACE("It was type %d, is %d now\n", This->conversion_map[idx], conversion_type);
260 ret = TRUE;
261 This->conversion_map[idx] = conversion_type;
265 return ret;
268 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
269 UINT attrib_idx, const BOOL check_d3dcolor, const BOOL is_ffp_position, const BOOL is_ffp_color,
270 DWORD *stride_this_run)
272 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
273 enum wined3d_format_id format;
274 BOOL ret = FALSE;
276 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
277 * there, on nonexistent attribs the vbo is 0.
279 if (!(si->use_map & (1 << attrib_idx))
280 || attrib->data.buffer_object != This->buffer_object)
281 return FALSE;
283 format = attrib->format->id;
284 /* Look for newly appeared conversion */
285 if (check_d3dcolor && format == WINED3DFMT_B8G8R8A8_UNORM)
287 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
289 if (!is_ffp_color) FIXME("Test for non-color fixed function WINED3DFMT_B8G8R8A8_UNORM format\n");
291 else if (is_ffp_position && si->position_transformed)
293 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
295 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
296 return FALSE;
299 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
301 else if (This->conversion_map)
303 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
306 return ret;
309 static BOOL buffer_find_decl(struct wined3d_buffer *This)
311 struct wined3d_device *device = This->resource.device;
312 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
313 const struct wined3d_stream_info *si = &device->strided_streams;
314 const struct wined3d_state *state = &device->stateBlock->state;
315 UINT stride_this_run = 0;
316 BOOL ret = FALSE;
317 BOOL support_d3dcolor = gl_info->supported[ARB_VERTEX_ARRAY_BGRA];
319 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
320 * Once we have our declaration there is no need to look it up again. Index buffers also never need
321 * conversion, so once the (empty) conversion structure is created don't bother checking again
323 if (This->flags & WINED3D_BUFFER_HASDESC)
325 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
328 if (use_vs(state))
330 TRACE("Vertex shaders used, no VBO conversion is needed\n");
331 if(This->conversion_map)
333 HeapFree(GetProcessHeap(), 0, This->conversion_map);
334 This->conversion_map = NULL;
335 This->stride = 0;
336 return TRUE;
339 return FALSE;
342 TRACE("Finding vertex buffer conversion information\n");
343 /* Certain declaration types need some fixups before we can pass them to
344 * opengl. This means D3DCOLOR attributes with fixed function vertex
345 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
346 * GL_ARB_half_float_vertex is not supported.
348 * Note for d3d8 and d3d9:
349 * The vertex buffer FVF doesn't help with finding them, we have to use
350 * the decoded vertex declaration and pick the things that concern the
351 * current buffer. A problem with this is that this can change between
352 * draws, so we have to validate the information and reprocess the buffer
353 * if it changes, and avoid false positives for performance reasons.
354 * WineD3D doesn't even know the vertex buffer any more, it is managed
355 * by the client libraries and passed to SetStreamSource and ProcessVertices
356 * as needed.
358 * We have to distinguish between vertex shaders and fixed function to
359 * pick the way we access the strided vertex information.
361 * This code sets up a per-byte array with the size of the detected
362 * stride of the arrays in the buffer. For each byte we have a field
363 * that marks the conversion needed on this byte. For example, the
364 * following declaration with fixed function vertex processing:
366 * POSITIONT, FLOAT4
367 * NORMAL, FLOAT3
368 * DIFFUSE, FLOAT16_4
369 * SPECULAR, D3DCOLOR
371 * Will result in
372 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
373 * [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]
375 * Where in this example map P means 4 component position conversion, 0
376 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
377 * conversion (red / blue swizzle).
379 * If we're doing conversion and the stride changes we have to reconvert
380 * the whole buffer. Note that we do not mind if the semantic changes,
381 * we only care for the conversion type. So if the NORMAL is replaced
382 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
383 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
384 * conversion types depend on the semantic as well, for example a FLOAT4
385 * texcoord needs no conversion while a FLOAT4 positiont needs one
388 ret = buffer_check_attribute(This, si, WINED3D_FFP_POSITION,
389 TRUE, TRUE, FALSE, &stride_this_run) || ret;
390 ret = buffer_check_attribute(This, si, WINED3D_FFP_NORMAL,
391 TRUE, FALSE, FALSE, &stride_this_run) || ret;
392 ret = buffer_check_attribute(This, si, WINED3D_FFP_DIFFUSE,
393 !support_d3dcolor, FALSE, TRUE, &stride_this_run) || ret;
394 ret = buffer_check_attribute(This, si, WINED3D_FFP_SPECULAR,
395 !support_d3dcolor, FALSE, TRUE, &stride_this_run) || ret;
396 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD0,
397 TRUE, FALSE, FALSE, &stride_this_run) || ret;
398 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD1,
399 TRUE, FALSE, FALSE, &stride_this_run) || ret;
400 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD2,
401 TRUE, FALSE, FALSE, &stride_this_run) || ret;
402 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD3,
403 TRUE, FALSE, FALSE, &stride_this_run) || ret;
404 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD4,
405 TRUE, FALSE, FALSE, &stride_this_run) || ret;
406 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD5,
407 TRUE, FALSE, FALSE, &stride_this_run) || ret;
408 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD6,
409 TRUE, FALSE, FALSE, &stride_this_run) || ret;
410 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD7,
411 TRUE, FALSE, FALSE, &stride_this_run) || ret;
413 if (!stride_this_run && This->conversion_map)
415 /* Sanity test */
416 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
417 HeapFree(GetProcessHeap(), 0, This->conversion_map);
418 This->conversion_map = NULL;
419 This->stride = 0;
422 if (ret) TRACE("Conversion information changed\n");
424 return ret;
427 static inline void fixup_d3dcolor(DWORD *dst_color)
429 DWORD src_color = *dst_color;
431 /* Color conversion like in drawStridedSlow. watch out for little endianity
432 * If we want that stuff to work on big endian machines too we have to consider more things
434 * 0xff000000: Alpha mask
435 * 0x00ff0000: Blue mask
436 * 0x0000ff00: Green mask
437 * 0x000000ff: Red mask
439 *dst_color = 0;
440 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
441 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
442 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
445 static inline void fixup_transformed_pos(float *p)
447 /* rhw conversion like in position_float4(). */
448 if (p[3] != 1.0f && p[3] != 0.0f)
450 float w = 1.0f / p[3];
451 p[0] *= w;
452 p[1] *= w;
453 p[2] *= w;
454 p[3] = w;
458 /* Context activation is done by the caller. */
459 void buffer_get_memory(struct wined3d_buffer *buffer, const struct wined3d_gl_info *gl_info,
460 struct wined3d_bo_address *data)
462 data->buffer_object = buffer->buffer_object;
463 if (!buffer->buffer_object)
465 if ((buffer->flags & WINED3D_BUFFER_CREATEBO) && !buffer->resource.map_count)
467 buffer_create_buffer_object(buffer, gl_info);
468 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
469 if (buffer->buffer_object)
471 data->buffer_object = buffer->buffer_object;
472 data->addr = NULL;
473 return;
476 data->addr = buffer->resource.allocatedMemory;
478 else
480 data->addr = NULL;
484 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
486 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
488 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
490 return refcount;
493 /* Context activation is done by the caller. */
494 BYTE *buffer_get_sysmem(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
496 /* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
497 if(This->resource.allocatedMemory) return This->resource.allocatedMemory;
499 This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT);
500 This->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
502 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
503 device_invalidate_state(This->resource.device, STATE_INDEXBUFFER);
505 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
506 GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.allocatedMemory));
507 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
509 return This->resource.allocatedMemory;
512 /* Do not call while under the GL lock. */
513 static void buffer_unload(struct wined3d_resource *resource)
515 struct wined3d_buffer *buffer = buffer_from_resource(resource);
517 TRACE("buffer %p.\n", buffer);
519 if (buffer->buffer_object)
521 struct wined3d_device *device = resource->device;
522 struct wined3d_context *context;
524 context = context_acquire(device, NULL);
526 /* Download the buffer, but don't permanently enable double buffering */
527 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
529 buffer_get_sysmem(buffer, context->gl_info);
530 buffer->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
533 delete_gl_buffer(buffer, context->gl_info);
534 buffer->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
535 buffer_clear_dirty_areas(buffer);
537 context_release(context);
539 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
540 buffer->conversion_map = NULL;
541 buffer->stride = 0;
542 buffer->conversion_stride = 0;
543 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
546 resource_unload(resource);
549 /* Do not call while under the GL lock. */
550 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
552 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
554 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
556 if (!refcount)
558 buffer_unload(&buffer->resource);
559 resource_cleanup(&buffer->resource);
560 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
561 HeapFree(GetProcessHeap(), 0, buffer->maps);
562 HeapFree(GetProcessHeap(), 0, buffer);
565 return refcount;
568 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
570 TRACE("buffer %p.\n", buffer);
572 return buffer->resource.parent;
575 DWORD CDECL wined3d_buffer_set_priority(struct wined3d_buffer *buffer, DWORD priority)
577 return resource_set_priority(&buffer->resource, priority);
580 DWORD CDECL wined3d_buffer_get_priority(const struct wined3d_buffer *buffer)
582 return resource_get_priority(&buffer->resource);
585 /* The caller provides a context and binds the buffer */
586 static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
588 enum wined3d_event_query_result ret;
590 /* No fencing needs to be done if the app promises not to overwrite
591 * existing data. */
592 if (flags & WINED3D_MAP_NOOVERWRITE)
593 return;
595 if (flags & WINED3D_MAP_DISCARD)
597 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
598 checkGLcall("glBufferDataARB\n");
599 return;
602 if(!This->query)
604 TRACE("Creating event query for buffer %p\n", This);
606 if (!wined3d_event_query_supported(gl_info))
608 FIXME("Event queries not supported, dropping async buffer locks.\n");
609 goto drop_query;
612 This->query = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->query));
613 if (!This->query)
615 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
616 goto drop_query;
619 /* Since we don't know about old draws a glFinish is needed once */
620 gl_info->gl_ops.gl.p_glFinish();
621 return;
623 TRACE("Synchronizing buffer %p\n", This);
624 ret = wined3d_event_query_finish(This->query, This->resource.device);
625 switch(ret)
627 case WINED3D_EVENT_QUERY_NOT_STARTED:
628 case WINED3D_EVENT_QUERY_OK:
629 /* All done */
630 return;
632 case WINED3D_EVENT_QUERY_WRONG_THREAD:
633 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
634 goto drop_query;
636 default:
637 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret);
638 goto drop_query;
641 drop_query:
642 if(This->query)
644 wined3d_event_query_destroy(This->query);
645 This->query = NULL;
648 gl_info->gl_ops.gl.p_glFinish();
649 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
650 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
651 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
654 /* The caller provides a GL context */
655 static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
657 BYTE *map;
658 UINT start = 0, len = 0;
660 /* This potentially invalidates the element array buffer binding, but the
661 * caller always takes care of this. */
662 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
663 checkGLcall("glBindBufferARB");
664 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
666 GLbitfield mapflags;
667 mapflags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
668 if (flags & WINED3D_BUFFER_DISCARD)
669 mapflags |= GL_MAP_INVALIDATE_BUFFER_BIT;
670 if (flags & WINED3D_BUFFER_NOSYNC)
671 mapflags |= GL_MAP_UNSYNCHRONIZED_BIT;
672 map = GL_EXTCALL(glMapBufferRange(This->buffer_type_hint, 0,
673 This->resource.size, mapflags));
674 checkGLcall("glMapBufferRange");
676 else
678 if (This->flags & WINED3D_BUFFER_APPLESYNC)
680 DWORD syncflags = 0;
681 if (flags & WINED3D_BUFFER_DISCARD)
682 syncflags |= WINED3D_MAP_DISCARD;
683 if (flags & WINED3D_BUFFER_NOSYNC)
684 syncflags |= WINED3D_MAP_NOOVERWRITE;
685 buffer_sync_apple(This, syncflags, gl_info);
687 map = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_WRITE_ONLY_ARB));
688 checkGLcall("glMapBufferARB");
690 if (!map)
692 ERR("Failed to map opengl buffer\n");
693 return;
696 while (This->modified_areas)
698 This->modified_areas--;
699 start = This->maps[This->modified_areas].offset;
700 len = This->maps[This->modified_areas].size;
702 memcpy(map + start, This->resource.allocatedMemory + start, len);
704 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
706 GL_EXTCALL(glFlushMappedBufferRange(This->buffer_type_hint, start, len));
707 checkGLcall("glFlushMappedBufferRange");
709 else if (This->flags & WINED3D_BUFFER_FLUSH)
711 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This->buffer_type_hint, start, len));
712 checkGLcall("glFlushMappedBufferRangeAPPLE");
715 GL_EXTCALL(glUnmapBufferARB(This->buffer_type_hint));
716 checkGLcall("glUnmapBufferARB");
719 /* Do not call while under the GL lock. */
720 void CDECL wined3d_buffer_preload(struct wined3d_buffer *buffer)
722 DWORD flags = buffer->flags & (WINED3D_BUFFER_NOSYNC | WINED3D_BUFFER_DISCARD);
723 struct wined3d_device *device = buffer->resource.device;
724 UINT start = 0, end = 0, len = 0, vertices;
725 const struct wined3d_gl_info *gl_info;
726 struct wined3d_context *context;
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->flags &= ~(WINED3D_BUFFER_NOSYNC | WINED3D_BUFFER_DISCARD);
741 if (!buffer->buffer_object)
743 /* TODO: Make converting independent from VBOs */
744 if (buffer->flags & WINED3D_BUFFER_CREATEBO)
746 context = context_acquire(device, NULL);
747 buffer_create_buffer_object(buffer, context->gl_info);
748 context_release(context);
749 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
751 else
753 /* Not doing any conversion */
754 return;
758 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
759 if (device->isInDraw && buffer->resource.bind_count > 0)
761 decl_changed = buffer_find_decl(buffer);
762 buffer->flags |= WINED3D_BUFFER_HASDESC;
765 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
767 ++buffer->draw_count;
768 if (buffer->draw_count > VB_RESETDECLCHANGE)
769 buffer->decl_change_count = 0;
770 if (buffer->draw_count > VB_RESETFULLCONVS)
771 buffer->full_conversion_count = 0;
772 return;
775 /* If applications change the declaration over and over, reconverting all the time is a huge
776 * performance hit. So count the declaration changes and release the VBO if there are too many
777 * of them (and thus stop converting)
779 if (decl_changed)
781 ++buffer->decl_change_count;
782 buffer->draw_count = 0;
784 if (buffer->decl_change_count > VB_MAXDECLCHANGES
785 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
787 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
789 buffer_unload(&buffer->resource);
790 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
792 /* The stream source state handler might have read the memory of
793 * the vertex buffer already and got the memory in the vbo which
794 * is not valid any longer. Dirtify the stream source to force a
795 * reload. This happens only once per changed vertexbuffer and
796 * should occur rather rarely. */
797 device_invalidate_state(device, STATE_STREAMSRC);
798 return;
801 /* The declaration changed, reload the whole buffer */
802 WARN("Reloading buffer because of decl change\n");
803 buffer_clear_dirty_areas(buffer);
804 if (!buffer_add_dirty_area(buffer, 0, 0))
806 ERR("buffer_add_dirty_area failed, this is not expected\n");
807 return;
809 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
810 * cleared for unsynchronized updates
812 flags = 0;
814 else
816 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
817 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
818 * decl changes and reset the decl change count after a specific number of them
820 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
822 ++buffer->full_conversion_count;
823 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
825 FIXME("Too many full buffer conversions, stopping converting.\n");
826 buffer_unload(&buffer->resource);
827 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
828 if (buffer->resource.bind_count)
829 device_invalidate_state(device, STATE_STREAMSRC);
830 return;
833 else
835 ++buffer->draw_count;
836 if (buffer->draw_count > VB_RESETDECLCHANGE)
837 buffer->decl_change_count = 0;
838 if (buffer->draw_count > VB_RESETFULLCONVS)
839 buffer->full_conversion_count = 0;
843 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
844 device_invalidate_state(device, STATE_INDEXBUFFER);
846 if (!buffer->conversion_map)
848 /* That means that there is nothing to fixup. Just upload from
849 * buffer->resource.allocatedMemory directly into the vbo. Do not
850 * free the system memory copy because drawPrimitive may need it if
851 * the stride is 0, for instancing emulation, vertex blending
852 * emulation or shader emulation. */
853 TRACE("No conversion needed.\n");
855 /* Nothing to do because we locked directly into the vbo */
856 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
858 return;
861 context = context_acquire(device, NULL);
862 buffer_direct_upload(buffer, context->gl_info, flags);
864 context_release(context);
865 return;
868 context = context_acquire(device, NULL);
869 gl_info = context->gl_info;
871 if(!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
873 buffer_get_sysmem(buffer, gl_info);
876 /* Now for each vertex in the buffer that needs conversion */
877 vertices = buffer->resource.size / buffer->stride;
879 data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size);
881 while(buffer->modified_areas)
883 buffer->modified_areas--;
884 start = buffer->maps[buffer->modified_areas].offset;
885 len = buffer->maps[buffer->modified_areas].size;
886 end = start + len;
888 memcpy(data + start, buffer->resource.allocatedMemory + start, end - start);
889 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertices); ++i)
891 for (j = 0; j < buffer->stride; ++j)
893 switch (buffer->conversion_map[j])
895 case CONV_NONE:
896 /* Done already */
897 j += 3;
898 break;
899 case CONV_D3DCOLOR:
900 fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
901 j += 3;
902 break;
904 case CONV_POSITIONT:
905 fixup_transformed_pos((float *) (data + i * buffer->stride + j));
906 j += 15;
907 break;
908 default:
909 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer->conversion_map[j]);
914 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
915 checkGLcall("glBindBufferARB");
916 GL_EXTCALL(glBufferSubDataARB(buffer->buffer_type_hint, start, len, data + start));
917 checkGLcall("glBufferSubDataARB");
920 HeapFree(GetProcessHeap(), 0, data);
921 context_release(context);
924 static DWORD buffer_sanitize_flags(const struct wined3d_buffer *buffer, DWORD flags)
926 /* Not all flags make sense together, but Windows never returns an error.
927 * Catch the cases that could cause issues. */
928 if (flags & WINED3D_MAP_READONLY)
930 if (flags & WINED3D_MAP_DISCARD)
932 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
933 return 0;
935 if (flags & WINED3D_MAP_NOOVERWRITE)
937 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
938 return 0;
941 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
942 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
944 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
945 return 0;
947 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
948 && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
950 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
951 return 0;
954 return flags;
957 static GLbitfield buffer_gl_map_flags(DWORD d3d_flags)
959 GLbitfield ret = 0;
961 if (!(d3d_flags & WINED3D_MAP_READONLY))
962 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
963 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
964 ret |= GL_MAP_READ_BIT;
966 if (d3d_flags & WINED3D_MAP_DISCARD)
967 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
968 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
969 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
971 return ret;
974 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
976 TRACE("buffer %p.\n", buffer);
978 return &buffer->resource;
981 HRESULT CDECL wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
983 BOOL dirty = buffer_is_dirty(buffer);
984 LONG count;
986 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x\n", buffer, offset, size, data, flags);
988 flags = buffer_sanitize_flags(buffer, flags);
989 if (!(flags & WINED3D_MAP_READONLY))
991 if (flags & WINED3D_MAP_DISCARD)
993 /* DISCARD invalidates the entire buffer, regardless of the
994 * specified offset and size. Some applications also depend on the
995 * entire buffer being uploaded in that case. Two such
996 * applications are Port Royale and Darkstar One. */
997 if (!buffer_add_dirty_area(buffer, 0, 0))
998 return E_OUTOFMEMORY;
1000 else
1002 if (!buffer_add_dirty_area(buffer, offset, size))
1003 return E_OUTOFMEMORY;
1007 count = ++buffer->resource.map_count;
1009 if (buffer->buffer_object)
1011 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
1013 if (count == 1)
1015 struct wined3d_device *device = buffer->resource.device;
1016 struct wined3d_context *context;
1017 const struct wined3d_gl_info *gl_info;
1019 context = context_acquire(device, NULL);
1020 gl_info = context->gl_info;
1022 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1023 context_invalidate_state(context, STATE_INDEXBUFFER);
1024 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
1026 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1028 GLbitfield mapflags = buffer_gl_map_flags(flags);
1029 buffer->resource.allocatedMemory = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
1030 0, buffer->resource.size, mapflags));
1031 checkGLcall("glMapBufferRange");
1033 else
1035 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1036 buffer_sync_apple(buffer, flags, gl_info);
1037 buffer->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(buffer->buffer_type_hint,
1038 GL_READ_WRITE_ARB));
1039 checkGLcall("glMapBufferARB");
1042 if (((DWORD_PTR)buffer->resource.allocatedMemory) & (RESOURCE_ALIGNMENT - 1))
1044 WARN("Pointer %p is not %u byte aligned.\n", buffer->resource.allocatedMemory, RESOURCE_ALIGNMENT);
1046 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1047 checkGLcall("glUnmapBufferARB");
1048 buffer->resource.allocatedMemory = NULL;
1050 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1052 /* The extra copy is more expensive than not using VBOs at
1053 * all on the Nvidia Linux driver, which is the only driver
1054 * that returns unaligned pointers
1056 TRACE("Dynamic buffer, dropping VBO\n");
1057 buffer_unload(&buffer->resource);
1058 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
1059 if (buffer->resource.bind_count)
1060 device_invalidate_state(device, STATE_STREAMSRC);
1062 else
1064 TRACE("Falling back to doublebuffered operation\n");
1065 buffer_get_sysmem(buffer, gl_info);
1067 TRACE("New pointer is %p.\n", buffer->resource.allocatedMemory);
1069 context_release(context);
1072 else
1074 if (dirty)
1076 if (buffer->flags & WINED3D_BUFFER_NOSYNC && !(flags & WINED3D_MAP_NOOVERWRITE))
1078 buffer->flags &= ~WINED3D_BUFFER_NOSYNC;
1081 else if(flags & WINED3D_MAP_NOOVERWRITE)
1083 buffer->flags |= WINED3D_BUFFER_NOSYNC;
1086 if (flags & WINED3D_MAP_DISCARD)
1088 buffer->flags |= WINED3D_BUFFER_DISCARD;
1093 *data = buffer->resource.allocatedMemory + offset;
1095 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, buffer->resource.allocatedMemory, offset);
1096 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1098 return WINED3D_OK;
1101 void CDECL wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1103 ULONG i;
1105 TRACE("buffer %p.\n", buffer);
1107 /* In the case that the number of Unmap calls > the
1108 * number of Map calls, d3d returns always D3D_OK.
1109 * This is also needed to prevent Map from returning garbage on
1110 * the next call (this will happen if the lock_count is < 0). */
1111 if (!buffer->resource.map_count)
1113 WARN("Unmap called without a previous map call.\n");
1114 return;
1117 if (--buffer->resource.map_count)
1119 /* Delay loading the buffer until everything is unlocked */
1120 TRACE("Ignoring unmap.\n");
1121 return;
1124 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER) && buffer->buffer_object)
1126 struct wined3d_device *device = buffer->resource.device;
1127 const struct wined3d_gl_info *gl_info;
1128 struct wined3d_context *context;
1130 context = context_acquire(device, NULL);
1131 gl_info = context->gl_info;
1133 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1134 context_invalidate_state(context, STATE_INDEXBUFFER);
1135 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
1137 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1139 for (i = 0; i < buffer->modified_areas; ++i)
1141 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1142 buffer->maps[i].offset, buffer->maps[i].size));
1143 checkGLcall("glFlushMappedBufferRange");
1146 else if (buffer->flags & WINED3D_BUFFER_FLUSH)
1148 for (i = 0; i < buffer->modified_areas; ++i)
1150 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1151 buffer->maps[i].offset, buffer->maps[i].size));
1152 checkGLcall("glFlushMappedBufferRangeAPPLE");
1156 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1157 if (wined3d_settings.strict_draw_ordering)
1158 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1159 context_release(context);
1161 buffer->resource.allocatedMemory = NULL;
1162 buffer_clear_dirty_areas(buffer);
1164 else if (buffer->flags & WINED3D_BUFFER_HASDESC)
1166 wined3d_buffer_preload(buffer);
1170 static const struct wined3d_resource_ops buffer_resource_ops =
1172 buffer_unload,
1175 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1176 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, GLenum bind_hint,
1177 const char *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1179 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1180 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1181 HRESULT hr;
1182 BOOL dynamic_buffer_ok;
1184 if (!size)
1186 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1187 return WINED3DERR_INVALIDCALL;
1190 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1191 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size,
1192 parent, parent_ops, &buffer_resource_ops);
1193 if (FAILED(hr))
1195 WARN("Failed to initialize resource, hr %#x\n", hr);
1196 return hr;
1198 buffer->buffer_type_hint = bind_hint;
1200 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1201 debug_d3dformat(buffer->resource.format->id), buffer->resource.allocatedMemory, buffer);
1203 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING)
1205 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1206 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1207 * Still use a VBO to support OpenGL 3 core contexts. */
1208 TRACE("Using doublebuffer mode because of software vertex processing\n");
1209 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
1212 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1214 /* Observations show that drawStridedSlow is faster on dynamic VBs than converting +
1215 * drawStridedFast (half-life 2 and others).
1217 * Basically converting the vertices in the buffer is quite expensive, and observations
1218 * show that drawStridedSlow is faster than converting + uploading + drawStridedFast.
1219 * Therefore do not create a VBO for WINED3DUSAGE_DYNAMIC buffers.
1221 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1223 TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
1225 else if(buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1227 TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
1229 else if(!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1231 TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
1233 else
1235 buffer->flags |= WINED3D_BUFFER_CREATEBO;
1238 if (data)
1240 BYTE *ptr;
1242 hr = wined3d_buffer_map(buffer, 0, size, &ptr, 0);
1243 if (FAILED(hr))
1245 ERR("Failed to map buffer, hr %#x\n", hr);
1246 buffer_unload(&buffer->resource);
1247 resource_cleanup(&buffer->resource);
1248 return hr;
1251 memcpy(ptr, data, size);
1253 wined3d_buffer_unmap(buffer);
1256 buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps));
1257 if (!buffer->maps)
1259 ERR("Out of memory\n");
1260 buffer_unload(&buffer->resource);
1261 resource_cleanup(&buffer->resource);
1262 return E_OUTOFMEMORY;
1264 buffer->maps_size = 1;
1266 return WINED3D_OK;
1269 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, struct wined3d_buffer_desc *desc, const void *data,
1270 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1272 struct wined3d_buffer *object;
1273 HRESULT hr;
1275 TRACE("device %p, desc %p, data %p, parent %p, buffer %p\n", device, desc, data, parent, buffer);
1277 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1278 if (!object)
1279 return E_OUTOFMEMORY;
1281 FIXME("Ignoring access flags (pool)\n");
1283 hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1284 WINED3D_POOL_MANAGED, GL_ARRAY_BUFFER_ARB, data, 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;
1291 object->desc = *desc;
1293 TRACE("Created buffer %p.\n", object);
1295 *buffer = object;
1297 return WINED3D_OK;
1300 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1301 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1303 struct wined3d_buffer *object;
1304 HRESULT hr;
1306 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1307 device, size, usage, pool, parent, parent_ops, buffer);
1309 if (pool == WINED3D_POOL_SCRATCH)
1311 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1312 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1313 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1314 *buffer = NULL;
1315 return WINED3DERR_INVALIDCALL;
1318 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1319 if (!object)
1321 *buffer = NULL;
1322 return WINED3DERR_OUTOFVIDEOMEMORY;
1325 hr = buffer_init(object, device, size, usage, WINED3DFMT_VERTEXDATA,
1326 pool, GL_ARRAY_BUFFER_ARB, NULL, parent, parent_ops);
1327 if (FAILED(hr))
1329 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1330 HeapFree(GetProcessHeap(), 0, object);
1331 return hr;
1334 TRACE("Created buffer %p.\n", object);
1335 *buffer = object;
1337 return WINED3D_OK;
1340 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1341 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1343 struct wined3d_buffer *object;
1344 HRESULT hr;
1346 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1347 device, size, usage, pool, parent, parent_ops, buffer);
1349 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1350 if (!object)
1352 *buffer = NULL;
1353 return WINED3DERR_OUTOFVIDEOMEMORY;
1356 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1357 WINED3DFMT_UNKNOWN, pool, GL_ELEMENT_ARRAY_BUFFER_ARB, NULL,
1358 parent, parent_ops);
1359 if (FAILED(hr))
1361 WARN("Failed to initialize buffer, hr %#x\n", hr);
1362 HeapFree(GetProcessHeap(), 0, object);
1363 return hr;
1366 TRACE("Created buffer %p.\n", object);
1367 *buffer = object;
1369 return WINED3D_OK;