winex11: Added missing release_win_data() to create_foreign_window().
[wine.git] / dlls / wined3d / buffer.c
blobde06ae8866a8baeee6031c8ba4d2b751f009e833
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 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_NOSYNC 0x20 /* All locks since the last preload had NOOVERWRITE set. */
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(glDeleteBuffersARB(1, &This->buffer_object));
109 checkGLcall("glDeleteBuffersARB");
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(glGenBuffersARB(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(glBindBufferARB(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(glBufferDataARB(This->buffer_type_hint, This->resource.size, This->resource.allocatedMemory, gl_usage));
185 error = gl_info->gl_ops.gl.p_glGetError();
186 if (error != GL_NO_ERROR)
188 ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
189 goto fail;
192 This->buffer_object_size = This->resource.size;
193 This->buffer_object_usage = gl_usage;
195 if (This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
197 buffer_invalidate_bo_range(This, 0, 0);
199 else
201 wined3d_resource_free_sysmem(&This->resource);
202 This->resource.allocatedMemory = NULL;
205 return;
207 fail:
208 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
209 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
210 delete_gl_buffer(This, gl_info);
211 buffer_clear_dirty_areas(This);
214 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
215 const enum wined3d_buffer_conversion_type conversion_type,
216 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
218 DWORD attrib_size;
219 BOOL ret = FALSE;
220 unsigned int i;
221 DWORD_PTR data;
223 /* Check for some valid situations which cause us pain. One is if the buffer is used for
224 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
225 * with different strides. In the 2nd case we might have to drop conversion entirely,
226 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
228 if (!attrib->stride)
230 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
231 debug_d3dformat(attrib->format->id));
233 else if(attrib->stride != *stride_this_run && *stride_this_run)
235 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
237 else
239 *stride_this_run = attrib->stride;
240 if (This->stride != *stride_this_run)
242 /* We rely that this happens only on the first converted attribute that is found,
243 * if at all. See above check
245 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
246 This->stride = *stride_this_run;
247 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
248 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
249 sizeof(*This->conversion_map) * This->stride);
250 ret = TRUE;
254 data = ((DWORD_PTR)attrib->data.addr) % This->stride;
255 attrib_size = attrib->format->component_count * attrib->format->component_size;
256 for (i = 0; i < attrib_size; ++i)
258 DWORD_PTR idx = (data + i) % This->stride;
259 if (This->conversion_map[idx] != conversion_type)
261 TRACE("Byte %ld in vertex changed\n", idx);
262 TRACE("It was type %d, is %d now\n", This->conversion_map[idx], conversion_type);
263 ret = TRUE;
264 This->conversion_map[idx] = conversion_type;
268 return ret;
271 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
272 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
274 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
275 UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
277 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
278 enum wined3d_format_id format;
279 BOOL ret = FALSE;
281 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
282 * there, on nonexistent attribs the vbo is 0.
284 if (!(si->use_map & (1 << attrib_idx))
285 || attrib->data.buffer_object != This->buffer_object)
286 return FALSE;
288 format = attrib->format->id;
289 /* Look for newly appeared conversion */
290 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
292 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
294 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
296 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
298 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
299 return FALSE;
302 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
304 else if (This->conversion_map)
306 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
309 return ret;
312 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
313 DWORD fixup_flags)
315 UINT stride_this_run = 0;
316 BOOL ret = FALSE;
318 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
319 * Once we have our declaration there is no need to look it up again. Index buffers also never need
320 * conversion, so once the (empty) conversion structure is created don't bother checking again
322 if (This->flags & WINED3D_BUFFER_HASDESC)
324 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
327 if (!fixup_flags)
329 TRACE("No fixup required.\n");
330 if(This->conversion_map)
332 HeapFree(GetProcessHeap(), 0, This->conversion_map);
333 This->conversion_map = NULL;
334 This->stride = 0;
335 return TRUE;
338 return FALSE;
341 TRACE("Finding vertex buffer conversion information\n");
342 /* Certain declaration types need some fixups before we can pass them to
343 * opengl. This means D3DCOLOR attributes with fixed function vertex
344 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
345 * GL_ARB_half_float_vertex is not supported.
347 * Note for d3d8 and d3d9:
348 * The vertex buffer FVF doesn't help with finding them, we have to use
349 * the decoded vertex declaration and pick the things that concern the
350 * current buffer. A problem with this is that this can change between
351 * draws, so we have to validate the information and reprocess the buffer
352 * if it changes, and avoid false positives for performance reasons.
353 * WineD3D doesn't even know the vertex buffer any more, it is managed
354 * by the client libraries and passed to SetStreamSource and ProcessVertices
355 * as needed.
357 * We have to distinguish between vertex shaders and fixed function to
358 * pick the way we access the strided vertex information.
360 * This code sets up a per-byte array with the size of the detected
361 * stride of the arrays in the buffer. For each byte we have a field
362 * that marks the conversion needed on this byte. For example, the
363 * following declaration with fixed function vertex processing:
365 * POSITIONT, FLOAT4
366 * NORMAL, FLOAT3
367 * DIFFUSE, FLOAT16_4
368 * SPECULAR, D3DCOLOR
370 * Will result in
371 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
372 * [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]
374 * Where in this example map P means 4 component position conversion, 0
375 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
376 * conversion (red / blue swizzle).
378 * If we're doing conversion and the stride changes we have to reconvert
379 * the whole buffer. Note that we do not mind if the semantic changes,
380 * we only care for the conversion type. So if the NORMAL is replaced
381 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
382 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
383 * conversion types depend on the semantic as well, for example a FLOAT4
384 * texcoord needs no conversion while a FLOAT4 positiont needs one
387 ret = buffer_check_attribute(This, si, WINED3D_FFP_POSITION,
388 fixup_flags, &stride_this_run) || ret;
389 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
391 ret = buffer_check_attribute(This, si, WINED3D_FFP_NORMAL,
392 fixup_flags, &stride_this_run) || ret;
393 ret = buffer_check_attribute(This, si, WINED3D_FFP_DIFFUSE,
394 fixup_flags, &stride_this_run) || ret;
395 ret = buffer_check_attribute(This, si, WINED3D_FFP_SPECULAR,
396 fixup_flags, &stride_this_run) || ret;
397 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD0,
398 fixup_flags, &stride_this_run) || ret;
399 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD1,
400 fixup_flags, &stride_this_run) || ret;
401 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD2,
402 fixup_flags, &stride_this_run) || ret;
403 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD3,
404 fixup_flags, &stride_this_run) || ret;
405 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD4,
406 fixup_flags, &stride_this_run) || ret;
407 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD5,
408 fixup_flags, &stride_this_run) || ret;
409 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD6,
410 fixup_flags, &stride_this_run) || ret;
411 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD7,
412 fixup_flags, &stride_this_run) || ret;
414 if (!stride_this_run && This->conversion_map)
416 /* Sanity test */
417 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
418 HeapFree(GetProcessHeap(), 0, This->conversion_map);
419 This->conversion_map = NULL;
420 This->stride = 0;
423 if (ret) TRACE("Conversion information changed\n");
425 return ret;
428 static inline void fixup_d3dcolor(DWORD *dst_color)
430 DWORD src_color = *dst_color;
432 /* Color conversion like in drawStridedSlow. watch out for little endianity
433 * If we want that stuff to work on big endian machines too we have to consider more things
435 * 0xff000000: Alpha mask
436 * 0x00ff0000: Blue mask
437 * 0x0000ff00: Green mask
438 * 0x000000ff: Red mask
440 *dst_color = 0;
441 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
442 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
443 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
446 static inline void fixup_transformed_pos(float *p)
448 /* rhw conversion like in position_float4(). */
449 if (p[3] != 1.0f && p[3] != 0.0f)
451 float w = 1.0f / p[3];
452 p[0] *= w;
453 p[1] *= w;
454 p[2] *= w;
455 p[3] = w;
459 /* Context activation is done by the caller. */
460 void buffer_get_memory(struct wined3d_buffer *buffer, struct wined3d_context *context,
461 struct wined3d_bo_address *data)
463 data->buffer_object = buffer->buffer_object;
464 if (!buffer->buffer_object)
466 if ((buffer->flags & WINED3D_BUFFER_CREATEBO) && !buffer->resource.map_count)
468 buffer_create_buffer_object(buffer, context);
469 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
470 if (buffer->buffer_object)
472 data->buffer_object = buffer->buffer_object;
473 data->addr = NULL;
474 return;
477 data->addr = buffer->resource.allocatedMemory;
479 else
481 data->addr = NULL;
485 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
487 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
489 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
491 return refcount;
494 /* Context activation is done by the caller. */
495 BYTE *buffer_get_sysmem(struct wined3d_buffer *This, struct wined3d_context *context)
497 const struct wined3d_gl_info *gl_info = context->gl_info;
499 /* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
500 if(This->resource.allocatedMemory) return This->resource.allocatedMemory;
502 if (!wined3d_resource_allocate_sysmem(&This->resource))
503 ERR("Failed to allocate system memory.\n");
504 This->resource.allocatedMemory = This->resource.heap_memory;
506 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
507 context_invalidate_state(context, STATE_INDEXBUFFER);
509 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
510 GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.allocatedMemory));
511 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
513 return This->resource.allocatedMemory;
516 static void buffer_unload(struct wined3d_resource *resource)
518 struct wined3d_buffer *buffer = buffer_from_resource(resource);
520 TRACE("buffer %p.\n", buffer);
522 if (buffer->buffer_object)
524 struct wined3d_device *device = resource->device;
525 struct wined3d_context *context;
527 context = context_acquire(device, NULL);
529 /* Download the buffer, but don't permanently enable double buffering */
530 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
532 buffer_get_sysmem(buffer, context);
533 buffer->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
536 delete_gl_buffer(buffer, context->gl_info);
537 buffer->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
538 buffer_clear_dirty_areas(buffer);
540 context_release(context);
542 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
543 buffer->conversion_map = NULL;
544 buffer->stride = 0;
545 buffer->conversion_stride = 0;
546 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
549 resource_unload(resource);
552 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
554 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
555 struct wined3d_context *context;
557 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
559 if (!refcount)
561 if (buffer->buffer_object)
563 context = context_acquire(buffer->resource.device, NULL);
564 delete_gl_buffer(buffer, context->gl_info);
565 context_release(context);
567 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
570 resource_cleanup(&buffer->resource);
571 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
572 HeapFree(GetProcessHeap(), 0, buffer->maps);
573 HeapFree(GetProcessHeap(), 0, buffer);
576 return refcount;
579 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
581 TRACE("buffer %p.\n", buffer);
583 return buffer->resource.parent;
586 DWORD CDECL wined3d_buffer_set_priority(struct wined3d_buffer *buffer, DWORD priority)
588 return resource_set_priority(&buffer->resource, priority);
591 DWORD CDECL wined3d_buffer_get_priority(const struct wined3d_buffer *buffer)
593 return resource_get_priority(&buffer->resource);
596 /* The caller provides a context and binds the buffer */
597 static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
599 enum wined3d_event_query_result ret;
601 /* No fencing needs to be done if the app promises not to overwrite
602 * existing data. */
603 if (flags & WINED3D_MAP_NOOVERWRITE)
604 return;
606 if (flags & WINED3D_MAP_DISCARD)
608 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
609 checkGLcall("glBufferDataARB\n");
610 return;
613 if(!This->query)
615 TRACE("Creating event query for buffer %p\n", This);
617 if (!wined3d_event_query_supported(gl_info))
619 FIXME("Event queries not supported, dropping async buffer locks.\n");
620 goto drop_query;
623 This->query = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->query));
624 if (!This->query)
626 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
627 goto drop_query;
630 /* Since we don't know about old draws a glFinish is needed once */
631 gl_info->gl_ops.gl.p_glFinish();
632 return;
634 TRACE("Synchronizing buffer %p\n", This);
635 ret = wined3d_event_query_finish(This->query, This->resource.device);
636 switch(ret)
638 case WINED3D_EVENT_QUERY_NOT_STARTED:
639 case WINED3D_EVENT_QUERY_OK:
640 /* All done */
641 return;
643 case WINED3D_EVENT_QUERY_WRONG_THREAD:
644 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
645 goto drop_query;
647 default:
648 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret);
649 goto drop_query;
652 drop_query:
653 if(This->query)
655 wined3d_event_query_destroy(This->query);
656 This->query = NULL;
659 gl_info->gl_ops.gl.p_glFinish();
660 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
661 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
662 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
665 /* The caller provides a GL context */
666 static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
668 BYTE *map;
669 UINT start = 0, len = 0;
671 /* This potentially invalidates the element array buffer binding, but the
672 * caller always takes care of this. */
673 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
674 checkGLcall("glBindBufferARB");
675 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
677 GLbitfield mapflags;
678 mapflags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
679 if (flags & WINED3D_BUFFER_DISCARD)
680 mapflags |= GL_MAP_INVALIDATE_BUFFER_BIT;
681 if (flags & WINED3D_BUFFER_NOSYNC)
682 mapflags |= GL_MAP_UNSYNCHRONIZED_BIT;
683 map = GL_EXTCALL(glMapBufferRange(This->buffer_type_hint, 0,
684 This->resource.size, mapflags));
685 checkGLcall("glMapBufferRange");
687 else
689 if (This->flags & WINED3D_BUFFER_APPLESYNC)
691 DWORD syncflags = 0;
692 if (flags & WINED3D_BUFFER_DISCARD)
693 syncflags |= WINED3D_MAP_DISCARD;
694 if (flags & WINED3D_BUFFER_NOSYNC)
695 syncflags |= WINED3D_MAP_NOOVERWRITE;
696 buffer_sync_apple(This, syncflags, gl_info);
698 map = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_WRITE_ONLY_ARB));
699 checkGLcall("glMapBufferARB");
701 if (!map)
703 ERR("Failed to map opengl buffer\n");
704 return;
707 while (This->modified_areas)
709 This->modified_areas--;
710 start = This->maps[This->modified_areas].offset;
711 len = This->maps[This->modified_areas].size;
713 memcpy(map + start, This->resource.allocatedMemory + start, len);
715 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
717 GL_EXTCALL(glFlushMappedBufferRange(This->buffer_type_hint, start, len));
718 checkGLcall("glFlushMappedBufferRange");
720 else if (This->flags & WINED3D_BUFFER_FLUSH)
722 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This->buffer_type_hint, start, len));
723 checkGLcall("glFlushMappedBufferRangeAPPLE");
726 GL_EXTCALL(glUnmapBufferARB(This->buffer_type_hint));
727 checkGLcall("glUnmapBufferARB");
730 /* Context activation is done by the caller. */
731 void buffer_internal_preload(struct wined3d_buffer *buffer, struct wined3d_context *context,
732 const struct wined3d_state *state)
734 DWORD flags = buffer->flags & (WINED3D_BUFFER_NOSYNC | WINED3D_BUFFER_DISCARD);
735 struct wined3d_device *device = buffer->resource.device;
736 UINT start = 0, end = 0, len = 0, vertices;
737 const struct wined3d_gl_info *gl_info;
738 BOOL decl_changed = FALSE;
739 unsigned int i, j;
740 BYTE *data;
742 TRACE("buffer %p.\n", buffer);
744 if (buffer->resource.map_count)
746 WARN("Buffer is mapped, skipping preload.\n");
747 return;
750 buffer->flags &= ~(WINED3D_BUFFER_NOSYNC | WINED3D_BUFFER_DISCARD);
752 if (!buffer->buffer_object)
754 /* TODO: Make converting independent from VBOs */
755 if (buffer->flags & WINED3D_BUFFER_CREATEBO)
757 buffer_create_buffer_object(buffer, context);
758 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
760 else
762 /* Not doing any conversion */
763 return;
767 /* Reading the declaration makes only sense if we have valid state information
768 * (i.e., if this function is called during draws). */
769 if (state)
771 DWORD fixup_flags = 0;
773 if (!use_vs(state))
775 if (!context->gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
776 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
777 if (!context->d3d_info->xyzrhw)
778 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
781 decl_changed = buffer_find_decl(buffer, &context->stream_info, fixup_flags);
782 buffer->flags |= WINED3D_BUFFER_HASDESC;
785 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
787 ++buffer->draw_count;
788 if (buffer->draw_count > VB_RESETDECLCHANGE)
789 buffer->decl_change_count = 0;
790 if (buffer->draw_count > VB_RESETFULLCONVS)
791 buffer->full_conversion_count = 0;
792 return;
795 /* If applications change the declaration over and over, reconverting all the time is a huge
796 * performance hit. So count the declaration changes and release the VBO if there are too many
797 * of them (and thus stop converting)
799 if (decl_changed)
801 ++buffer->decl_change_count;
802 buffer->draw_count = 0;
804 if (buffer->decl_change_count > VB_MAXDECLCHANGES
805 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
807 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
809 buffer_unload(&buffer->resource);
810 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
812 /* The stream source state handler might have read the memory of
813 * the vertex buffer already and got the memory in the vbo which
814 * is not valid any longer. Dirtify the stream source to force a
815 * reload. This happens only once per changed vertexbuffer and
816 * should occur rather rarely. */
817 device_invalidate_state(device, STATE_STREAMSRC);
818 return;
821 /* The declaration changed, reload the whole buffer. */
822 WARN("Reloading buffer because of a vertex declaration change.\n");
823 buffer_invalidate_bo_range(buffer, 0, 0);
825 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
826 * cleared for unsynchronized updates
828 flags = 0;
830 else
832 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
833 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
834 * decl changes and reset the decl change count after a specific number of them
836 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
838 ++buffer->full_conversion_count;
839 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
841 FIXME("Too many full buffer conversions, stopping converting.\n");
842 buffer_unload(&buffer->resource);
843 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
844 if (buffer->resource.bind_count)
845 device_invalidate_state(device, STATE_STREAMSRC);
846 return;
849 else
851 ++buffer->draw_count;
852 if (buffer->draw_count > VB_RESETDECLCHANGE)
853 buffer->decl_change_count = 0;
854 if (buffer->draw_count > VB_RESETFULLCONVS)
855 buffer->full_conversion_count = 0;
859 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
860 device_invalidate_state(device, STATE_INDEXBUFFER);
862 if (!buffer->conversion_map)
864 /* That means that there is nothing to fixup. Just upload from
865 * buffer->resource.allocatedMemory directly into the vbo. Do not
866 * free the system memory copy because drawPrimitive may need it if
867 * the stride is 0, for instancing emulation, vertex blending
868 * emulation or shader emulation. */
869 TRACE("No conversion needed.\n");
871 /* Nothing to do because we locked directly into the vbo */
872 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
874 return;
877 buffer_direct_upload(buffer, context->gl_info, flags);
879 return;
882 gl_info = context->gl_info;
884 if(!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
886 buffer_get_sysmem(buffer, context);
889 /* Now for each vertex in the buffer that needs conversion */
890 vertices = buffer->resource.size / buffer->stride;
892 data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size);
894 while(buffer->modified_areas)
896 buffer->modified_areas--;
897 start = buffer->maps[buffer->modified_areas].offset;
898 len = buffer->maps[buffer->modified_areas].size;
899 end = start + len;
901 memcpy(data + start, buffer->resource.allocatedMemory + start, end - start);
902 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertices); ++i)
904 for (j = 0; j < buffer->stride; ++j)
906 switch (buffer->conversion_map[j])
908 case CONV_NONE:
909 /* Done already */
910 j += 3;
911 break;
912 case CONV_D3DCOLOR:
913 fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
914 j += 3;
915 break;
917 case CONV_POSITIONT:
918 fixup_transformed_pos((float *) (data + i * buffer->stride + j));
919 j += 15;
920 break;
921 default:
922 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer->conversion_map[j]);
927 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
928 checkGLcall("glBindBufferARB");
929 GL_EXTCALL(glBufferSubDataARB(buffer->buffer_type_hint, start, len, data + start));
930 checkGLcall("glBufferSubDataARB");
933 HeapFree(GetProcessHeap(), 0, data);
936 void CDECL wined3d_buffer_preload(struct wined3d_buffer *buffer)
938 struct wined3d_context *context;
939 context = context_acquire(buffer->resource.device, NULL);
940 buffer_internal_preload(buffer, context, NULL);
941 context_release(context);
944 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
946 TRACE("buffer %p.\n", buffer);
948 return &buffer->resource;
951 HRESULT CDECL wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
953 BOOL dirty = buffer_is_dirty(buffer);
954 LONG count;
956 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x\n", buffer, offset, size, data, flags);
958 flags = wined3d_resource_sanitize_map_flags(&buffer->resource, flags);
959 count = ++buffer->resource.map_count;
961 if (buffer->buffer_object)
963 /* DISCARD invalidates the entire buffer, regardless of the specified
964 * offset and size. Some applications also depend on the entire buffer
965 * being uploaded in that case. Two such applications are Port Royale
966 * and Darkstar One. */
967 if (flags & WINED3D_MAP_DISCARD)
968 buffer_invalidate_bo_range(buffer, 0, 0);
969 else if (!(flags & WINED3D_MAP_READONLY))
970 buffer_invalidate_bo_range(buffer, offset, size);
972 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
974 if (count == 1)
976 struct wined3d_device *device = buffer->resource.device;
977 struct wined3d_context *context;
978 const struct wined3d_gl_info *gl_info;
980 context = context_acquire(device, NULL);
981 gl_info = context->gl_info;
983 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
984 context_invalidate_state(context, STATE_INDEXBUFFER);
985 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
987 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
989 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
990 buffer->resource.allocatedMemory = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
991 0, buffer->resource.size, mapflags));
992 checkGLcall("glMapBufferRange");
994 else
996 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
997 buffer_sync_apple(buffer, flags, gl_info);
998 buffer->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(buffer->buffer_type_hint,
999 GL_READ_WRITE_ARB));
1000 checkGLcall("glMapBufferARB");
1003 if (((DWORD_PTR)buffer->resource.allocatedMemory) & (RESOURCE_ALIGNMENT - 1))
1005 WARN("Pointer %p is not %u byte aligned.\n", buffer->resource.allocatedMemory, RESOURCE_ALIGNMENT);
1007 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1008 checkGLcall("glUnmapBufferARB");
1009 buffer->resource.allocatedMemory = NULL;
1011 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1013 /* The extra copy is more expensive than not using VBOs at
1014 * all on the Nvidia Linux driver, which is the only driver
1015 * that returns unaligned pointers
1017 TRACE("Dynamic buffer, dropping VBO\n");
1018 buffer_unload(&buffer->resource);
1019 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
1020 if (buffer->resource.bind_count)
1021 device_invalidate_state(device, STATE_STREAMSRC);
1023 else
1025 TRACE("Falling back to doublebuffered operation\n");
1026 buffer_get_sysmem(buffer, context);
1028 TRACE("New pointer is %p.\n", buffer->resource.allocatedMemory);
1030 context_release(context);
1033 else
1035 if (dirty)
1037 if (buffer->flags & WINED3D_BUFFER_NOSYNC && !(flags & WINED3D_MAP_NOOVERWRITE))
1039 buffer->flags &= ~WINED3D_BUFFER_NOSYNC;
1042 else if(flags & WINED3D_MAP_NOOVERWRITE)
1044 buffer->flags |= WINED3D_BUFFER_NOSYNC;
1047 if (flags & WINED3D_MAP_DISCARD)
1049 buffer->flags |= WINED3D_BUFFER_DISCARD;
1054 *data = buffer->resource.allocatedMemory + offset;
1056 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, buffer->resource.allocatedMemory, offset);
1057 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1059 return WINED3D_OK;
1062 void CDECL wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1064 ULONG i;
1066 TRACE("buffer %p.\n", buffer);
1068 /* In the case that the number of Unmap calls > the
1069 * number of Map calls, d3d returns always D3D_OK.
1070 * This is also needed to prevent Map from returning garbage on
1071 * the next call (this will happen if the lock_count is < 0). */
1072 if (!buffer->resource.map_count)
1074 WARN("Unmap called without a previous map call.\n");
1075 return;
1078 if (--buffer->resource.map_count)
1080 /* Delay loading the buffer until everything is unlocked */
1081 TRACE("Ignoring unmap.\n");
1082 return;
1085 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER) && buffer->buffer_object)
1087 struct wined3d_device *device = buffer->resource.device;
1088 const struct wined3d_gl_info *gl_info;
1089 struct wined3d_context *context;
1091 context = context_acquire(device, NULL);
1092 gl_info = context->gl_info;
1094 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1095 context_invalidate_state(context, STATE_INDEXBUFFER);
1096 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
1098 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1100 for (i = 0; i < buffer->modified_areas; ++i)
1102 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1103 buffer->maps[i].offset, buffer->maps[i].size));
1104 checkGLcall("glFlushMappedBufferRange");
1107 else if (buffer->flags & WINED3D_BUFFER_FLUSH)
1109 for (i = 0; i < buffer->modified_areas; ++i)
1111 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1112 buffer->maps[i].offset, buffer->maps[i].size));
1113 checkGLcall("glFlushMappedBufferRangeAPPLE");
1117 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1118 if (wined3d_settings.strict_draw_ordering)
1119 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1120 context_release(context);
1122 buffer->resource.allocatedMemory = NULL;
1123 buffer_clear_dirty_areas(buffer);
1125 else if (buffer->flags & WINED3D_BUFFER_HASDESC)
1127 wined3d_buffer_preload(buffer);
1131 static const struct wined3d_resource_ops buffer_resource_ops =
1133 buffer_unload,
1136 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1137 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, GLenum bind_hint,
1138 const char *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1140 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1141 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1142 HRESULT hr;
1143 BOOL dynamic_buffer_ok;
1145 if (!size)
1147 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1148 return WINED3DERR_INVALIDCALL;
1151 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1152 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size,
1153 parent, parent_ops, &buffer_resource_ops);
1154 if (FAILED(hr))
1156 WARN("Failed to initialize resource, hr %#x\n", hr);
1157 return hr;
1159 buffer->buffer_type_hint = bind_hint;
1161 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1162 debug_d3dformat(buffer->resource.format->id), buffer->resource.allocatedMemory, buffer);
1164 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING)
1166 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1167 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1168 * Still use a VBO to support OpenGL 3 core contexts. */
1169 TRACE("Using doublebuffer mode because of software vertex processing\n");
1170 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
1173 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1175 /* Observations show that drawStridedSlow is faster on dynamic VBs than converting +
1176 * drawStridedFast (half-life 2 and others).
1178 * Basically converting the vertices in the buffer is quite expensive, and observations
1179 * show that drawStridedSlow is faster than converting + uploading + drawStridedFast.
1180 * Therefore do not create a VBO for WINED3DUSAGE_DYNAMIC buffers.
1182 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1184 TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
1186 else if(buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1188 TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
1190 else if(!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1192 TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
1194 else
1196 buffer->flags |= WINED3D_BUFFER_CREATEBO;
1199 if (data)
1201 BYTE *ptr;
1203 hr = wined3d_buffer_map(buffer, 0, size, &ptr, 0);
1204 if (FAILED(hr))
1206 ERR("Failed to map buffer, hr %#x\n", hr);
1207 buffer_unload(&buffer->resource);
1208 resource_cleanup(&buffer->resource);
1209 return hr;
1212 memcpy(ptr, data, size);
1214 wined3d_buffer_unmap(buffer);
1217 buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps));
1218 if (!buffer->maps)
1220 ERR("Out of memory\n");
1221 buffer_unload(&buffer->resource);
1222 resource_cleanup(&buffer->resource);
1223 return E_OUTOFMEMORY;
1225 buffer->maps_size = 1;
1227 return WINED3D_OK;
1230 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, struct wined3d_buffer_desc *desc, const void *data,
1231 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1233 struct wined3d_buffer *object;
1234 HRESULT hr;
1236 TRACE("device %p, desc %p, data %p, parent %p, buffer %p\n", device, desc, data, parent, buffer);
1238 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1239 if (!object)
1240 return E_OUTOFMEMORY;
1242 FIXME("Ignoring access flags (pool)\n");
1244 hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1245 WINED3D_POOL_MANAGED, GL_ARRAY_BUFFER_ARB, data, parent, parent_ops);
1246 if (FAILED(hr))
1248 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1249 HeapFree(GetProcessHeap(), 0, object);
1250 return hr;
1252 object->desc = *desc;
1254 TRACE("Created buffer %p.\n", object);
1256 *buffer = object;
1258 return WINED3D_OK;
1261 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1262 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1264 struct wined3d_buffer *object;
1265 HRESULT hr;
1267 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1268 device, size, usage, pool, parent, parent_ops, buffer);
1270 if (pool == WINED3D_POOL_SCRATCH)
1272 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1273 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1274 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1275 *buffer = NULL;
1276 return WINED3DERR_INVALIDCALL;
1279 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1280 if (!object)
1282 *buffer = NULL;
1283 return WINED3DERR_OUTOFVIDEOMEMORY;
1286 hr = buffer_init(object, device, size, usage, WINED3DFMT_VERTEXDATA,
1287 pool, GL_ARRAY_BUFFER_ARB, NULL, parent, parent_ops);
1288 if (FAILED(hr))
1290 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1291 HeapFree(GetProcessHeap(), 0, object);
1292 return hr;
1295 TRACE("Created buffer %p.\n", object);
1296 *buffer = object;
1298 return WINED3D_OK;
1301 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1302 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1304 struct wined3d_buffer *object;
1305 HRESULT hr;
1307 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1308 device, size, usage, pool, parent, parent_ops, buffer);
1310 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1311 if (!object)
1313 *buffer = NULL;
1314 return WINED3DERR_OUTOFVIDEOMEMORY;
1317 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1318 WINED3DFMT_UNKNOWN, pool, GL_ELEMENT_ARRAY_BUFFER_ARB, NULL,
1319 parent, parent_ops);
1320 if (FAILED(hr))
1322 WARN("Failed to initialize buffer, hr %#x\n", hr);
1323 HeapFree(GetProcessHeap(), 0, object);
1324 return hr;
1327 TRACE("Created buffer %p.\n", object);
1328 *buffer = object;
1330 return WINED3D_OK;