wined3d: Merge drawStridedFast() and drawStridedInstanced().
[wine.git] / dlls / wined3d / buffer.c
blobbf2e58dd139f6485de66ae5238b5813f57ce73bf
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, unsigned int offset, unsigned int size)
47 if (!offset && (!size || size == buffer->resource.size))
48 goto invalidate_all;
50 if (offset > buffer->resource.size || size > buffer->resource.size - offset)
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 buffer_bind(struct wined3d_buffer *buffer, struct wined3d_context *context)
106 const struct wined3d_gl_info *gl_info = context->gl_info;
108 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER)
109 context_invalidate_state(context, STATE_INDEXBUFFER);
111 GL_EXTCALL(glBindBuffer(buffer->buffer_type_hint, buffer->buffer_object));
114 /* Context activation is done by the caller */
115 static void delete_gl_buffer(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
117 if(!This->buffer_object) return;
119 GL_EXTCALL(glDeleteBuffers(1, &This->buffer_object));
120 checkGLcall("glDeleteBuffers");
121 This->buffer_object = 0;
123 if(This->query)
125 wined3d_event_query_destroy(This->query);
126 This->query = NULL;
128 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
131 /* Context activation is done by the caller. */
132 static void buffer_create_buffer_object(struct wined3d_buffer *This, struct wined3d_context *context)
134 GLenum gl_usage = GL_STATIC_DRAW_ARB;
135 GLenum error;
136 const struct wined3d_gl_info *gl_info = context->gl_info;
138 TRACE("Creating an OpenGL vertex buffer object for wined3d_buffer %p with usage %s.\n",
139 This, debug_d3dusage(This->resource.usage));
141 /* Make sure that the gl error is cleared. Do not use checkGLcall
142 * here because checkGLcall just prints a fixme and continues. However,
143 * if an error during VBO creation occurs we can fall back to non-vbo operation
144 * with full functionality(but performance loss)
146 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
148 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
149 * The vertex declaration from the device determines how the data in the
150 * buffer is interpreted. This means that on each draw call the buffer has
151 * to be verified to check if the rhw and color values are in the correct
152 * format. */
154 GL_EXTCALL(glGenBuffers(1, &This->buffer_object));
155 error = gl_info->gl_ops.gl.p_glGetError();
156 if (!This->buffer_object || error != GL_NO_ERROR)
158 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
159 goto fail;
162 buffer_bind(This, context);
163 error = gl_info->gl_ops.gl.p_glGetError();
164 if (error != GL_NO_ERROR)
166 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
167 goto fail;
170 if (This->resource.usage & WINED3DUSAGE_DYNAMIC)
172 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
173 gl_usage = GL_STREAM_DRAW_ARB;
175 if(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
177 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
178 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
179 This->flags |= WINED3D_BUFFER_FLUSH;
181 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
182 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
183 This->flags |= WINED3D_BUFFER_APPLESYNC;
185 /* No setup is needed here for GL_ARB_map_buffer_range */
188 /* Reserve memory for the buffer. The amount of data won't change
189 * so we are safe with calling glBufferData once and
190 * calling glBufferSubData on updates. Upload the actual data in case
191 * we're not double buffering, so we can release the heap mem afterwards
193 GL_EXTCALL(glBufferData(This->buffer_type_hint, This->resource.size, This->resource.heap_memory, gl_usage));
194 error = gl_info->gl_ops.gl.p_glGetError();
195 if (error != GL_NO_ERROR)
197 ERR("glBufferData failed with error %s (%#x)\n", debug_glerror(error), error);
198 goto fail;
201 This->buffer_object_usage = gl_usage;
203 if (This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
204 buffer_invalidate_bo_range(This, 0, 0);
205 else
206 wined3d_resource_free_sysmem(&This->resource);
208 return;
210 fail:
211 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
212 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
213 delete_gl_buffer(This, gl_info);
214 buffer_clear_dirty_areas(This);
217 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
218 const enum wined3d_buffer_conversion_type conversion_type,
219 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
221 DWORD attrib_size;
222 BOOL ret = FALSE;
223 unsigned int i;
224 DWORD_PTR data;
226 /* Check for some valid situations which cause us pain. One is if the buffer is used for
227 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
228 * with different strides. In the 2nd case we might have to drop conversion entirely,
229 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
231 if (!attrib->stride)
233 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
234 debug_d3dformat(attrib->format->id));
236 else if(attrib->stride != *stride_this_run && *stride_this_run)
238 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
240 else
242 *stride_this_run = attrib->stride;
243 if (This->stride != *stride_this_run)
245 /* We rely that this happens only on the first converted attribute that is found,
246 * if at all. See above check
248 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
249 This->stride = *stride_this_run;
250 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
251 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
252 sizeof(*This->conversion_map) * This->stride);
253 ret = TRUE;
257 data = ((DWORD_PTR)attrib->data.addr) % This->stride;
258 attrib_size = attrib->format->component_count * attrib->format->component_size;
259 for (i = 0; i < attrib_size; ++i)
261 DWORD_PTR idx = (data + i) % This->stride;
262 if (This->conversion_map[idx] != conversion_type)
264 TRACE("Byte %ld in vertex changed\n", idx);
265 TRACE("It was type %d, is %d now\n", This->conversion_map[idx], conversion_type);
266 ret = TRUE;
267 This->conversion_map[idx] = conversion_type;
271 return ret;
274 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
275 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
277 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
278 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
280 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
281 enum wined3d_format_id format;
282 BOOL ret = FALSE;
284 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
285 * there, on nonexistent attribs the vbo is 0.
287 if (!(si->use_map & (1u << attrib_idx))
288 || state->streams[attrib->stream_idx].buffer != This)
289 return FALSE;
291 format = attrib->format->id;
292 /* Look for newly appeared conversion */
293 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
295 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
297 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
299 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
301 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
302 return FALSE;
305 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
307 else if (This->conversion_map)
309 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
312 return ret;
315 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
316 const struct wined3d_state *state, DWORD fixup_flags)
318 UINT stride_this_run = 0;
319 BOOL ret = FALSE;
321 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
322 * Once we have our declaration there is no need to look it up again. Index buffers also never need
323 * conversion, so once the (empty) conversion structure is created don't bother checking again
325 if (This->flags & WINED3D_BUFFER_HASDESC)
327 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
330 if (!fixup_flags)
332 TRACE("No fixup required.\n");
333 if(This->conversion_map)
335 HeapFree(GetProcessHeap(), 0, This->conversion_map);
336 This->conversion_map = NULL;
337 This->stride = 0;
338 return TRUE;
341 return FALSE;
344 TRACE("Finding vertex buffer conversion information\n");
345 /* Certain declaration types need some fixups before we can pass them to
346 * opengl. This means D3DCOLOR attributes with fixed function vertex
347 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
348 * GL_ARB_half_float_vertex is not supported.
350 * Note for d3d8 and d3d9:
351 * The vertex buffer FVF doesn't help with finding them, we have to use
352 * the decoded vertex declaration and pick the things that concern the
353 * current buffer. A problem with this is that this can change between
354 * draws, so we have to validate the information and reprocess the buffer
355 * if it changes, and avoid false positives for performance reasons.
356 * WineD3D doesn't even know the vertex buffer any more, it is managed
357 * by the client libraries and passed to SetStreamSource and ProcessVertices
358 * as needed.
360 * We have to distinguish between vertex shaders and fixed function to
361 * pick the way we access the strided vertex information.
363 * This code sets up a per-byte array with the size of the detected
364 * stride of the arrays in the buffer. For each byte we have a field
365 * that marks the conversion needed on this byte. For example, the
366 * following declaration with fixed function vertex processing:
368 * POSITIONT, FLOAT4
369 * NORMAL, FLOAT3
370 * DIFFUSE, FLOAT16_4
371 * SPECULAR, D3DCOLOR
373 * Will result in
374 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
375 * [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]
377 * Where in this example map P means 4 component position conversion, 0
378 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
379 * conversion (red / blue swizzle).
381 * If we're doing conversion and the stride changes we have to reconvert
382 * the whole buffer. Note that we do not mind if the semantic changes,
383 * we only care for the conversion type. So if the NORMAL is replaced
384 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
385 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
386 * conversion types depend on the semantic as well, for example a FLOAT4
387 * texcoord needs no conversion while a FLOAT4 positiont needs one
390 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
391 fixup_flags, &stride_this_run) || ret;
392 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
394 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
395 fixup_flags, &stride_this_run) || ret;
396 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
397 fixup_flags, &stride_this_run) || ret;
398 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
399 fixup_flags, &stride_this_run) || ret;
400 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
401 fixup_flags, &stride_this_run) || ret;
402 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
403 fixup_flags, &stride_this_run) || ret;
404 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
405 fixup_flags, &stride_this_run) || ret;
406 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
407 fixup_flags, &stride_this_run) || ret;
408 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
409 fixup_flags, &stride_this_run) || ret;
410 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
411 fixup_flags, &stride_this_run) || ret;
412 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
413 fixup_flags, &stride_this_run) || ret;
414 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
415 fixup_flags, &stride_this_run) || ret;
417 if (!stride_this_run && This->conversion_map)
419 /* Sanity test */
420 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
421 HeapFree(GetProcessHeap(), 0, This->conversion_map);
422 This->conversion_map = NULL;
423 This->stride = 0;
426 if (ret) TRACE("Conversion information changed\n");
428 return ret;
431 static inline void fixup_d3dcolor(DWORD *dst_color)
433 DWORD src_color = *dst_color;
435 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
436 * endianness. If we want this to work on big-endian machines as well we
437 * have to consider more things.
439 * 0xff000000: Alpha mask
440 * 0x00ff0000: Blue mask
441 * 0x0000ff00: Green mask
442 * 0x000000ff: Red mask
444 *dst_color = 0;
445 *dst_color |= (src_color & 0xff00ff00u); /* Alpha Green */
446 *dst_color |= (src_color & 0x00ff0000u) >> 16; /* Red */
447 *dst_color |= (src_color & 0x000000ffu) << 16; /* Blue */
450 static inline void fixup_transformed_pos(float *p)
452 /* rhw conversion like in position_float4(). */
453 if (p[3] != 1.0f && p[3] != 0.0f)
455 float w = 1.0f / p[3];
456 p[0] *= w;
457 p[1] *= w;
458 p[2] *= w;
459 p[3] = w;
463 /* Context activation is done by the caller. */
464 void buffer_get_memory(struct wined3d_buffer *buffer, struct wined3d_context *context,
465 struct wined3d_bo_address *data)
467 data->buffer_object = buffer->buffer_object;
468 if (!buffer->buffer_object)
470 if ((buffer->flags & WINED3D_BUFFER_CREATEBO) && !buffer->resource.map_count)
472 buffer_create_buffer_object(buffer, context);
473 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
474 if (buffer->buffer_object)
476 data->buffer_object = buffer->buffer_object;
477 data->addr = NULL;
478 return;
481 data->addr = buffer->resource.heap_memory;
483 else
485 data->addr = NULL;
489 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
491 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
493 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
495 return refcount;
498 /* Context activation is done by the caller. */
499 BYTE *buffer_get_sysmem(struct wined3d_buffer *buffer, struct wined3d_context *context)
501 const struct wined3d_gl_info *gl_info = context->gl_info;
503 /* Heap_memory exists if the buffer is double buffered or has no buffer object at all. */
504 if (buffer->resource.heap_memory)
505 return buffer->resource.heap_memory;
507 if (!wined3d_resource_allocate_sysmem(&buffer->resource))
508 ERR("Failed to allocate system memory.\n");
510 buffer_bind(buffer, context);
511 GL_EXTCALL(glGetBufferSubData(buffer->buffer_type_hint, 0, buffer->resource.size, buffer->resource.heap_memory));
512 checkGLcall("buffer download");
513 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
515 return buffer->resource.heap_memory;
518 static void buffer_unload(struct wined3d_resource *resource)
520 struct wined3d_buffer *buffer = buffer_from_resource(resource);
522 TRACE("buffer %p.\n", buffer);
524 if (buffer->buffer_object)
526 struct wined3d_device *device = resource->device;
527 struct wined3d_context *context;
529 context = context_acquire(device, NULL);
531 /* Download the buffer, but don't permanently enable double buffering */
532 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
534 buffer_get_sysmem(buffer, context);
535 buffer->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
538 delete_gl_buffer(buffer, context->gl_info);
539 buffer->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
540 buffer_clear_dirty_areas(buffer);
542 context_release(context);
544 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
545 buffer->conversion_map = NULL;
546 buffer->stride = 0;
547 buffer->conversion_stride = 0;
548 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
551 resource_unload(resource);
554 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
556 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
557 struct wined3d_context *context;
559 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
561 if (!refcount)
563 if (buffer->buffer_object)
565 context = context_acquire(buffer->resource.device, NULL);
566 delete_gl_buffer(buffer, context->gl_info);
567 context_release(context);
569 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
572 resource_cleanup(&buffer->resource);
573 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
574 HeapFree(GetProcessHeap(), 0, buffer->maps);
575 HeapFree(GetProcessHeap(), 0, buffer);
578 return refcount;
581 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
583 TRACE("buffer %p.\n", buffer);
585 return buffer->resource.parent;
588 /* The caller provides a context and binds the buffer */
589 static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
591 enum wined3d_event_query_result ret;
593 /* No fencing needs to be done if the app promises not to overwrite
594 * existing data. */
595 if (flags & WINED3D_MAP_NOOVERWRITE)
596 return;
598 if (flags & WINED3D_MAP_DISCARD)
600 GL_EXTCALL(glBufferData(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
601 checkGLcall("glBufferData");
602 return;
605 if(!This->query)
607 TRACE("Creating event query for buffer %p\n", This);
609 if (!wined3d_event_query_supported(gl_info))
611 FIXME("Event queries not supported, dropping async buffer locks.\n");
612 goto drop_query;
615 This->query = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->query));
616 if (!This->query)
618 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
619 goto drop_query;
622 /* Since we don't know about old draws a glFinish is needed once */
623 gl_info->gl_ops.gl.p_glFinish();
624 return;
626 TRACE("Synchronizing buffer %p\n", This);
627 ret = wined3d_event_query_finish(This->query, This->resource.device);
628 switch(ret)
630 case WINED3D_EVENT_QUERY_NOT_STARTED:
631 case WINED3D_EVENT_QUERY_OK:
632 /* All done */
633 return;
635 case WINED3D_EVENT_QUERY_WRONG_THREAD:
636 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
637 goto drop_query;
639 default:
640 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret);
641 goto drop_query;
644 drop_query:
645 if(This->query)
647 wined3d_event_query_destroy(This->query);
648 This->query = NULL;
651 gl_info->gl_ops.gl.p_glFinish();
652 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
653 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
654 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
657 /* The caller provides a GL context */
658 static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
660 BYTE *map;
661 UINT start, len;
663 /* This potentially invalidates the element array buffer binding, but the
664 * caller always takes care of this. */
665 GL_EXTCALL(glBindBuffer(This->buffer_type_hint, This->buffer_object));
666 checkGLcall("glBindBuffer");
667 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
669 GLbitfield mapflags;
670 mapflags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
671 if (flags & WINED3D_BUFFER_DISCARD)
672 mapflags |= GL_MAP_INVALIDATE_BUFFER_BIT;
673 else if (!(flags & WINED3D_BUFFER_SYNC))
674 mapflags |= GL_MAP_UNSYNCHRONIZED_BIT;
675 map = GL_EXTCALL(glMapBufferRange(This->buffer_type_hint, 0,
676 This->resource.size, mapflags));
677 checkGLcall("glMapBufferRange");
679 else
681 if (This->flags & WINED3D_BUFFER_APPLESYNC)
683 DWORD syncflags = 0;
684 if (flags & WINED3D_BUFFER_DISCARD)
685 syncflags |= WINED3D_MAP_DISCARD;
686 else if (!(flags & WINED3D_BUFFER_SYNC))
687 syncflags |= WINED3D_MAP_NOOVERWRITE;
688 buffer_sync_apple(This, syncflags, gl_info);
690 map = GL_EXTCALL(glMapBuffer(This->buffer_type_hint, GL_WRITE_ONLY));
691 checkGLcall("glMapBuffer");
693 if (!map)
695 ERR("Failed to map opengl buffer\n");
696 return;
699 while (This->modified_areas)
701 This->modified_areas--;
702 start = This->maps[This->modified_areas].offset;
703 len = This->maps[This->modified_areas].size;
705 memcpy(map + start, (BYTE *)This->resource.heap_memory + start, len);
707 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
709 GL_EXTCALL(glFlushMappedBufferRange(This->buffer_type_hint, start, len));
710 checkGLcall("glFlushMappedBufferRange");
712 else if (This->flags & WINED3D_BUFFER_FLUSH)
714 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This->buffer_type_hint, start, len));
715 checkGLcall("glFlushMappedBufferRangeAPPLE");
718 GL_EXTCALL(glUnmapBuffer(This->buffer_type_hint));
719 checkGLcall("glUnmapBuffer");
722 void buffer_mark_used(struct wined3d_buffer *buffer)
724 buffer->flags &= ~(WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
727 /* Context activation is done by the caller. */
728 void buffer_internal_preload(struct wined3d_buffer *buffer, struct wined3d_context *context,
729 const struct wined3d_state *state)
731 DWORD flags = buffer->flags & (WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
732 struct wined3d_device *device = buffer->resource.device;
733 UINT start, end, len, vertices;
734 const struct wined3d_gl_info *gl_info;
735 BOOL decl_changed = FALSE;
736 unsigned int i, j;
737 BYTE *data;
739 TRACE("buffer %p.\n", buffer);
741 if (buffer->resource.map_count)
743 WARN("Buffer is mapped, skipping preload.\n");
744 return;
747 buffer_mark_used(buffer);
749 if (!buffer->buffer_object)
751 /* TODO: Make converting independent from VBOs */
752 if (buffer->flags & WINED3D_BUFFER_CREATEBO)
754 buffer_create_buffer_object(buffer, context);
755 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
757 else
759 /* Not doing any conversion */
760 return;
764 /* Reading the declaration makes only sense if we have valid state information
765 * (i.e., if this function is called during draws). */
766 if (state)
768 DWORD fixup_flags = 0;
770 if (!use_vs(state))
772 if (!context->gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
773 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
774 if (!context->d3d_info->xyzrhw)
775 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
778 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
779 buffer->flags |= WINED3D_BUFFER_HASDESC;
782 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
784 ++buffer->draw_count;
785 if (buffer->draw_count > VB_RESETDECLCHANGE)
786 buffer->decl_change_count = 0;
787 if (buffer->draw_count > VB_RESETFULLCONVS)
788 buffer->full_conversion_count = 0;
789 return;
792 /* If applications change the declaration over and over, reconverting all the time is a huge
793 * performance hit. So count the declaration changes and release the VBO if there are too many
794 * of them (and thus stop converting)
796 if (decl_changed)
798 ++buffer->decl_change_count;
799 buffer->draw_count = 0;
801 if (buffer->decl_change_count > VB_MAXDECLCHANGES
802 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
804 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
806 buffer_unload(&buffer->resource);
807 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
809 /* The stream source state handler might have read the memory of
810 * the vertex buffer already and got the memory in the vbo which
811 * is not valid any longer. Dirtify the stream source to force a
812 * reload. This happens only once per changed vertexbuffer and
813 * should occur rather rarely. */
814 device_invalidate_state(device, STATE_STREAMSRC);
815 return;
818 /* The declaration changed, reload the whole buffer. */
819 WARN("Reloading buffer because of a vertex declaration change.\n");
820 buffer_invalidate_bo_range(buffer, 0, 0);
822 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
823 * cleared for unsynchronized updates
825 flags = 0;
827 else
829 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
830 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
831 * decl changes and reset the decl change count after a specific number of them
833 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
835 ++buffer->full_conversion_count;
836 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
838 FIXME("Too many full buffer conversions, stopping converting.\n");
839 buffer_unload(&buffer->resource);
840 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
841 if (buffer->resource.bind_count)
842 device_invalidate_state(device, STATE_STREAMSRC);
843 return;
846 else
848 ++buffer->draw_count;
849 if (buffer->draw_count > VB_RESETDECLCHANGE)
850 buffer->decl_change_count = 0;
851 if (buffer->draw_count > VB_RESETFULLCONVS)
852 buffer->full_conversion_count = 0;
856 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
857 device_invalidate_state(device, STATE_INDEXBUFFER);
859 if (!buffer->conversion_map)
861 /* That means that there is nothing to fixup. Just upload from
862 * buffer->resource.heap_memory directly into the vbo. Do not
863 * free the system memory copy because drawPrimitive may need it if
864 * the stride is 0, for instancing emulation, vertex blending
865 * emulation or shader emulation. */
866 TRACE("No conversion needed.\n");
868 /* Nothing to do because we locked directly into the vbo */
869 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
871 return;
874 buffer_direct_upload(buffer, context->gl_info, flags);
876 return;
879 gl_info = context->gl_info;
881 if(!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
883 buffer_get_sysmem(buffer, context);
886 /* Now for each vertex in the buffer that needs conversion */
887 vertices = buffer->resource.size / buffer->stride;
889 data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size);
891 while(buffer->modified_areas)
893 buffer->modified_areas--;
894 start = buffer->maps[buffer->modified_areas].offset;
895 len = buffer->maps[buffer->modified_areas].size;
896 end = start + len;
898 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
899 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertices); ++i)
901 for (j = 0; j < buffer->stride; ++j)
903 switch (buffer->conversion_map[j])
905 case CONV_NONE:
906 /* Done already */
907 j += 3;
908 break;
909 case CONV_D3DCOLOR:
910 fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
911 j += 3;
912 break;
914 case CONV_POSITIONT:
915 fixup_transformed_pos((float *) (data + i * buffer->stride + j));
916 j += 15;
917 break;
918 default:
919 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer->conversion_map[j]);
924 GL_EXTCALL(glBindBuffer(buffer->buffer_type_hint, buffer->buffer_object));
925 checkGLcall("glBindBuffer");
926 GL_EXTCALL(glBufferSubData(buffer->buffer_type_hint, start, len, data + start));
927 checkGLcall("glBufferSubData");
930 HeapFree(GetProcessHeap(), 0, data);
933 void CDECL wined3d_buffer_preload(struct wined3d_buffer *buffer)
935 struct wined3d_context *context;
936 context = context_acquire(buffer->resource.device, NULL);
937 buffer_internal_preload(buffer, context, NULL);
938 context_release(context);
941 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
943 TRACE("buffer %p.\n", buffer);
945 return &buffer->resource;
948 HRESULT CDECL wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
950 LONG count;
951 BYTE *base;
953 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x\n", buffer, offset, size, data, flags);
955 flags = wined3d_resource_sanitize_map_flags(&buffer->resource, flags);
956 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
957 * fill rate test seems to depend on this. When we map a buffer with
958 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
959 * previous contents of the buffer. The r600g driver only does this when
960 * the buffer is currently in use, while the proprietary NVIDIA driver
961 * appears to do this unconditionally. */
962 if (buffer->flags & WINED3D_BUFFER_DISCARD)
963 flags &= ~WINED3D_MAP_DISCARD;
964 count = ++buffer->resource.map_count;
966 if (buffer->buffer_object)
968 /* DISCARD invalidates the entire buffer, regardless of the specified
969 * offset and size. Some applications also depend on the entire buffer
970 * being uploaded in that case. Two such applications are Port Royale
971 * and Darkstar One. */
972 if (flags & WINED3D_MAP_DISCARD)
973 buffer_invalidate_bo_range(buffer, 0, 0);
974 else if (!(flags & WINED3D_MAP_READONLY))
975 buffer_invalidate_bo_range(buffer, offset, size);
977 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
979 if (count == 1)
981 struct wined3d_device *device = buffer->resource.device;
982 struct wined3d_context *context;
983 const struct wined3d_gl_info *gl_info;
985 context = context_acquire(device, NULL);
986 gl_info = context->gl_info;
988 buffer_bind(buffer, context);
990 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
992 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
993 buffer->map_ptr = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
994 0, buffer->resource.size, mapflags));
995 checkGLcall("glMapBufferRange");
997 else
999 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1000 buffer_sync_apple(buffer, flags, gl_info);
1001 buffer->map_ptr = GL_EXTCALL(glMapBuffer(buffer->buffer_type_hint,
1002 GL_READ_WRITE));
1003 checkGLcall("glMapBuffer");
1006 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
1008 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
1010 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1011 checkGLcall("glUnmapBuffer");
1012 buffer->map_ptr = NULL;
1014 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1016 /* The extra copy is more expensive than not using VBOs at
1017 * all on the Nvidia Linux driver, which is the only driver
1018 * that returns unaligned pointers
1020 TRACE("Dynamic buffer, dropping VBO\n");
1021 buffer_unload(&buffer->resource);
1022 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
1023 if (buffer->resource.bind_count)
1024 device_invalidate_state(device, STATE_STREAMSRC);
1026 else
1028 TRACE("Falling back to doublebuffered operation\n");
1029 buffer_get_sysmem(buffer, context);
1031 TRACE("New pointer is %p.\n", buffer->resource.heap_memory);
1032 buffer->map_ptr = NULL;
1034 context_release(context);
1038 if (flags & WINED3D_MAP_DISCARD)
1039 buffer->flags |= WINED3D_BUFFER_DISCARD;
1040 else if (!(flags & WINED3D_MAP_NOOVERWRITE))
1041 buffer->flags |= WINED3D_BUFFER_SYNC;
1044 base = buffer->map_ptr ? buffer->map_ptr : buffer->resource.heap_memory;
1045 *data = base + offset;
1047 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, base, offset);
1048 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1050 return WINED3D_OK;
1053 void CDECL wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1055 ULONG i;
1057 TRACE("buffer %p.\n", buffer);
1059 /* In the case that the number of Unmap calls > the
1060 * number of Map calls, d3d returns always D3D_OK.
1061 * This is also needed to prevent Map from returning garbage on
1062 * the next call (this will happen if the lock_count is < 0). */
1063 if (!buffer->resource.map_count)
1065 WARN("Unmap called without a previous map call.\n");
1066 return;
1069 if (--buffer->resource.map_count)
1071 /* Delay loading the buffer until everything is unlocked */
1072 TRACE("Ignoring unmap.\n");
1073 return;
1076 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER) && buffer->buffer_object)
1078 struct wined3d_device *device = buffer->resource.device;
1079 const struct wined3d_gl_info *gl_info;
1080 struct wined3d_context *context;
1082 context = context_acquire(device, NULL);
1083 gl_info = context->gl_info;
1085 buffer_bind(buffer, context);
1087 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1089 for (i = 0; i < buffer->modified_areas; ++i)
1091 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1092 buffer->maps[i].offset, buffer->maps[i].size));
1093 checkGLcall("glFlushMappedBufferRange");
1096 else if (buffer->flags & WINED3D_BUFFER_FLUSH)
1098 for (i = 0; i < buffer->modified_areas; ++i)
1100 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1101 buffer->maps[i].offset, buffer->maps[i].size));
1102 checkGLcall("glFlushMappedBufferRangeAPPLE");
1106 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1107 if (wined3d_settings.strict_draw_ordering)
1108 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1109 context_release(context);
1111 buffer_clear_dirty_areas(buffer);
1112 buffer->map_ptr = NULL;
1114 else if (buffer->flags & WINED3D_BUFFER_HASDESC)
1116 wined3d_buffer_preload(buffer);
1120 HRESULT wined3d_buffer_copy(struct wined3d_buffer *dst_buffer, unsigned int dst_offset,
1121 struct wined3d_buffer *src_buffer, unsigned int src_offset, unsigned int size)
1123 BYTE *dst_buffer_mem, *src_buffer_mem, *dst_ptr, *src_ptr;
1124 struct wined3d_bo_address dst_bo_address, src_bo_address;
1125 const struct wined3d_gl_info *gl_info;
1126 struct wined3d_context *context;
1127 struct wined3d_device *device;
1128 HRESULT hr;
1130 device = dst_buffer->resource.device;
1132 context = context_acquire(device, NULL);
1133 gl_info = context->gl_info;
1135 buffer_get_memory(dst_buffer, context, &dst_bo_address);
1136 buffer_get_memory(src_buffer, context, &src_bo_address);
1138 dst_buffer_mem = dst_buffer->resource.heap_memory;
1139 src_buffer_mem = src_buffer->resource.heap_memory;
1141 if (!dst_buffer_mem && !src_buffer_mem)
1143 if (gl_info->supported[ARB_COPY_BUFFER])
1145 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo_address.buffer_object));
1146 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo_address.buffer_object));
1147 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, src_offset, dst_offset, size));
1148 checkGLcall("direct buffer copy");
1150 else
1152 if (FAILED(hr = wined3d_buffer_map(dst_buffer, dst_offset, size, &dst_ptr, 0)))
1154 WARN("Failed to map dst_buffer, hr %#x.\n", hr);
1155 context_release(context);
1156 return WINED3DERR_INVALIDCALL;
1158 if (FAILED(hr = wined3d_buffer_map(src_buffer, src_offset, size, &src_ptr, WINED3D_MAP_READONLY)))
1160 WARN("Failed to map src_buffer, hr %#x.\n", hr);
1161 wined3d_buffer_unmap(dst_buffer);
1162 context_release(context);
1163 return WINED3DERR_INVALIDCALL;
1166 memcpy(dst_ptr, src_ptr, size);
1168 wined3d_buffer_unmap(src_buffer);
1169 wined3d_buffer_unmap(dst_buffer);
1172 else if (dst_buffer_mem && !src_buffer_mem)
1174 buffer_bind(src_buffer, context);
1175 GL_EXTCALL(glGetBufferSubData(src_buffer->buffer_type_hint, src_offset, size, dst_buffer_mem + dst_offset));
1176 checkGLcall("buffer download");
1178 else if (!dst_buffer_mem && src_buffer_mem)
1180 buffer_bind(dst_buffer, context);
1181 GL_EXTCALL(glBufferSubData(dst_buffer->buffer_type_hint, dst_offset, size, src_buffer_mem + src_offset));
1182 checkGLcall("buffer upload");
1184 else
1186 memcpy(dst_buffer_mem + dst_offset, src_buffer_mem + src_offset, size);
1189 if (dst_buffer_mem)
1190 buffer_invalidate_bo_range(dst_buffer, dst_offset, size);
1192 context_release(context);
1193 return WINED3D_OK;
1196 HRESULT wined3d_buffer_upload_data(struct wined3d_buffer *buffer,
1197 const struct wined3d_box *box, const void *data)
1199 UINT offset, size;
1200 HRESULT hr;
1201 BYTE *ptr;
1203 if (box)
1205 offset = box->left;
1206 size = box->right - box->left;
1208 else
1210 offset = 0;
1211 size = buffer->resource.size;
1214 if (FAILED(hr = wined3d_buffer_map(buffer, offset, size, &ptr, 0)))
1215 return hr;
1217 memcpy(ptr, data, size);
1219 wined3d_buffer_unmap(buffer);
1220 return WINED3D_OK;
1223 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1225 return wined3d_buffer_incref(buffer_from_resource(resource));
1228 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1230 return wined3d_buffer_decref(buffer_from_resource(resource));
1233 static HRESULT buffer_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1234 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1236 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1237 UINT offset, size;
1239 if (sub_resource_idx)
1241 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1242 return E_INVALIDARG;
1245 if (box)
1247 offset = box->left;
1248 size = box->right - box->left;
1250 else
1252 offset = size = 0;
1255 map_desc->row_pitch = map_desc->slice_pitch = buffer->desc.byte_width;
1256 return wined3d_buffer_map(buffer, offset, size, (BYTE **)&map_desc->data, flags);
1259 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1261 if (sub_resource_idx)
1263 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1264 return E_INVALIDARG;
1267 wined3d_buffer_unmap(buffer_from_resource(resource));
1268 return WINED3D_OK;
1271 static const struct wined3d_resource_ops buffer_resource_ops =
1273 buffer_resource_incref,
1274 buffer_resource_decref,
1275 buffer_unload,
1276 buffer_resource_sub_resource_map,
1277 buffer_resource_sub_resource_unmap,
1280 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1281 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, GLenum bind_hint,
1282 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1284 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1285 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1286 HRESULT hr;
1287 BOOL dynamic_buffer_ok;
1289 if (!size)
1291 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1292 return WINED3DERR_INVALIDCALL;
1295 if (data && !data->data)
1297 WARN("Invalid sub-resource data specified.\n");
1298 return E_INVALIDARG;
1301 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1302 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size, parent, parent_ops, &buffer_resource_ops);
1303 if (FAILED(hr))
1305 WARN("Failed to initialize resource, hr %#x\n", hr);
1306 return hr;
1308 buffer->buffer_type_hint = bind_hint;
1310 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1311 debug_d3dformat(buffer->resource.format->id), buffer->resource.heap_memory, buffer);
1313 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING || pool == WINED3D_POOL_MANAGED)
1315 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1316 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1317 * Still use a VBO to support OpenGL 3 core contexts. */
1318 TRACE("Using doublebuffer mode because of software vertex processing\n");
1319 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
1322 /* Observations show that draw_primitive_immediate_mode() is faster on
1323 * dynamic vertex buffers than converting + draw_primitive_arrays().
1324 * (Half-Life 2 and others.) */
1325 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1327 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1329 TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
1331 else if(buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1333 TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
1335 else if(!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1337 TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
1339 else
1341 buffer->flags |= WINED3D_BUFFER_CREATEBO;
1344 if (data)
1346 if (FAILED(hr = wined3d_buffer_upload_data(buffer, NULL, data->data)))
1348 ERR("Failed to upload data, hr %#x.\n", hr);
1349 buffer_unload(&buffer->resource);
1350 resource_cleanup(&buffer->resource);
1351 return hr;
1355 buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps));
1356 if (!buffer->maps)
1358 ERR("Out of memory\n");
1359 buffer_unload(&buffer->resource);
1360 resource_cleanup(&buffer->resource);
1361 return E_OUTOFMEMORY;
1363 buffer->maps_size = 1;
1365 return WINED3D_OK;
1368 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1369 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops,
1370 struct wined3d_buffer **buffer)
1372 struct wined3d_buffer *object;
1373 HRESULT hr;
1375 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p\n",
1376 device, desc, data, parent, parent_ops, buffer);
1378 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1379 if (!object)
1380 return E_OUTOFMEMORY;
1382 FIXME("Ignoring access flags (pool)\n");
1384 hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1385 WINED3D_POOL_MANAGED, GL_ARRAY_BUFFER_ARB, data, parent, parent_ops);
1386 if (FAILED(hr))
1388 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1389 HeapFree(GetProcessHeap(), 0, object);
1390 return hr;
1392 object->desc = *desc;
1394 TRACE("Created buffer %p.\n", object);
1396 *buffer = object;
1398 return WINED3D_OK;
1401 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1402 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1404 struct wined3d_buffer *object;
1405 HRESULT hr;
1407 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1408 device, size, usage, pool, parent, parent_ops, buffer);
1410 if (pool == WINED3D_POOL_SCRATCH)
1412 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1413 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1414 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1415 *buffer = NULL;
1416 return WINED3DERR_INVALIDCALL;
1419 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1420 if (!object)
1422 *buffer = NULL;
1423 return WINED3DERR_OUTOFVIDEOMEMORY;
1426 hr = buffer_init(object, device, size, usage, WINED3DFMT_VERTEXDATA,
1427 pool, GL_ARRAY_BUFFER_ARB, NULL, parent, parent_ops);
1428 if (FAILED(hr))
1430 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1431 HeapFree(GetProcessHeap(), 0, object);
1432 return hr;
1435 TRACE("Created buffer %p.\n", object);
1436 *buffer = object;
1438 return WINED3D_OK;
1441 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1442 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1444 struct wined3d_buffer *object;
1445 HRESULT hr;
1447 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1448 device, size, usage, pool, parent, parent_ops, buffer);
1450 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1451 if (!object)
1453 *buffer = NULL;
1454 return WINED3DERR_OUTOFVIDEOMEMORY;
1457 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1458 WINED3DFMT_UNKNOWN, pool, GL_ELEMENT_ARRAY_BUFFER_ARB, NULL,
1459 parent, parent_ops);
1460 if (FAILED(hr))
1462 WARN("Failed to initialize buffer, hr %#x\n", hr);
1463 HeapFree(GetProcessHeap(), 0, object);
1464 return hr;
1467 TRACE("Created buffer %p.\n", object);
1468 *buffer = object;
1470 return WINED3D_OK;