* Sync up to trunk head (r64377).
[reactos.git] / dll / directx / wine / wined3d / buffer.c
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2007-2011, 2013-2014 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 */
24
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28
29 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
30 #define WINED3D_BUFFER_CREATEBO 0x02 /* Create a buffer object for this buffer. */
31 #define WINED3D_BUFFER_DOUBLEBUFFER 0x04 /* Keep both a buffer object and a system memory copy for this buffer. */
32 #define WINED3D_BUFFER_FLUSH 0x08 /* Manual unmap flushing. */
33 #define WINED3D_BUFFER_DISCARD 0x10 /* A DISCARD lock has occurred since the last preload. */
34 #define WINED3D_BUFFER_SYNC 0x20 /* There has been at least one synchronized map since the last preload. */
35 #define WINED3D_BUFFER_APPLESYNC 0x40 /* Using sync as in GL_APPLE_flush_buffer_range. */
36
37 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
38 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
39 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
40 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
41
42 static void buffer_invalidate_bo_range(struct wined3d_buffer *buffer, UINT offset, UINT size)
43 {
44 if (!offset && !size)
45 goto invalidate_all;
46
47 if (offset > buffer->resource.size || offset + size > buffer->resource.size)
48 {
49 WARN("Invalid range specified, invalidating entire buffer.\n");
50 goto invalidate_all;
51 }
52
53 if (buffer->modified_areas >= buffer->maps_size)
54 {
55 struct wined3d_map_range *new;
56
57 if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer->maps, 2 * buffer->maps_size * sizeof(*buffer->maps))))
58 {
59 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
60 goto invalidate_all;
61 }
62
63 buffer->maps = new;
64 buffer->maps_size *= 2;
65 }
66
67 buffer->maps[buffer->modified_areas].offset = offset;
68 buffer->maps[buffer->modified_areas].size = size;
69 ++buffer->modified_areas;
70 return;
71
72 invalidate_all:
73 buffer->modified_areas = 1;
74 buffer->maps[0].offset = 0;
75 buffer->maps[0].size = buffer->resource.size;
76 }
77
78 static inline void buffer_clear_dirty_areas(struct wined3d_buffer *This)
79 {
80 This->modified_areas = 0;
81 }
82
83 static BOOL buffer_is_dirty(const struct wined3d_buffer *buffer)
84 {
85 return !!buffer->modified_areas;
86 }
87
88 static BOOL buffer_is_fully_dirty(const struct wined3d_buffer *buffer)
89 {
90 unsigned int i;
91
92 for (i = 0; i < buffer->modified_areas; ++i)
93 {
94 if (!buffer->maps[i].offset && buffer->maps[i].size == buffer->resource.size)
95 return TRUE;
96 }
97 return FALSE;
98 }
99
100 /* Context activation is done by the caller */
101 static void delete_gl_buffer(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info)
102 {
103 if(!This->buffer_object) return;
104
105 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
106 checkGLcall("glDeleteBuffersARB");
107 This->buffer_object = 0;
108
109 if(This->query)
110 {
111 wined3d_event_query_destroy(This->query);
112 This->query = NULL;
113 }
114 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
115 }
116
117 /* Context activation is done by the caller. */
118 static void buffer_create_buffer_object(struct wined3d_buffer *This, struct wined3d_context *context)
119 {
120 GLenum gl_usage = GL_STATIC_DRAW_ARB;
121 GLenum error;
122 const struct wined3d_gl_info *gl_info = context->gl_info;
123
124 TRACE("Creating an OpenGL vertex buffer object for wined3d_buffer %p with usage %s.\n",
125 This, debug_d3dusage(This->resource.usage));
126
127 /* Make sure that the gl error is cleared. Do not use checkGLcall
128 * here because checkGLcall just prints a fixme and continues. However,
129 * if an error during VBO creation occurs we can fall back to non-vbo operation
130 * with full functionality(but performance loss)
131 */
132 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
133
134 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
135 * The vertex declaration from the device determines how the data in the
136 * buffer is interpreted. This means that on each draw call the buffer has
137 * to be verified to check if the rhw and color values are in the correct
138 * format. */
139
140 GL_EXTCALL(glGenBuffersARB(1, &This->buffer_object));
141 error = gl_info->gl_ops.gl.p_glGetError();
142 if (!This->buffer_object || error != GL_NO_ERROR)
143 {
144 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
145 goto fail;
146 }
147
148 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
149 context_invalidate_state(context, STATE_INDEXBUFFER);
150 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
151 error = gl_info->gl_ops.gl.p_glGetError();
152 if (error != GL_NO_ERROR)
153 {
154 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
155 goto fail;
156 }
157
158 if (This->resource.usage & WINED3DUSAGE_DYNAMIC)
159 {
160 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
161 gl_usage = GL_STREAM_DRAW_ARB;
162
163 if(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
164 {
165 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
166 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
167 This->flags |= WINED3D_BUFFER_FLUSH;
168
169 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
170 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
171 This->flags |= WINED3D_BUFFER_APPLESYNC;
172 }
173 /* No setup is needed here for GL_ARB_map_buffer_range */
174 }
175
176 /* Reserve memory for the buffer. The amount of data won't change
177 * so we are safe with calling glBufferData once and
178 * calling glBufferSubData on updates. Upload the actual data in case
179 * we're not double buffering, so we can release the heap mem afterwards
180 */
181 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, This->resource.heap_memory, gl_usage));
182 error = gl_info->gl_ops.gl.p_glGetError();
183 if (error != GL_NO_ERROR)
184 {
185 ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
186 goto fail;
187 }
188
189 This->buffer_object_usage = gl_usage;
190
191 if (This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
192 buffer_invalidate_bo_range(This, 0, 0);
193 else
194 wined3d_resource_free_sysmem(&This->resource);
195
196 return;
197
198 fail:
199 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
200 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
201 delete_gl_buffer(This, gl_info);
202 buffer_clear_dirty_areas(This);
203 }
204
205 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
206 const enum wined3d_buffer_conversion_type conversion_type,
207 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
208 {
209 DWORD attrib_size;
210 BOOL ret = FALSE;
211 unsigned int i;
212 DWORD_PTR data;
213
214 /* Check for some valid situations which cause us pain. One is if the buffer is used for
215 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
216 * with different strides. In the 2nd case we might have to drop conversion entirely,
217 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
218 */
219 if (!attrib->stride)
220 {
221 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
222 debug_d3dformat(attrib->format->id));
223 }
224 else if(attrib->stride != *stride_this_run && *stride_this_run)
225 {
226 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
227 }
228 else
229 {
230 *stride_this_run = attrib->stride;
231 if (This->stride != *stride_this_run)
232 {
233 /* We rely that this happens only on the first converted attribute that is found,
234 * if at all. See above check
235 */
236 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
237 This->stride = *stride_this_run;
238 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
239 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
240 sizeof(*This->conversion_map) * This->stride);
241 ret = TRUE;
242 }
243 }
244
245 data = ((DWORD_PTR)attrib->data.addr) % This->stride;
246 attrib_size = attrib->format->component_count * attrib->format->component_size;
247 for (i = 0; i < attrib_size; ++i)
248 {
249 DWORD_PTR idx = (data + i) % This->stride;
250 if (This->conversion_map[idx] != conversion_type)
251 {
252 TRACE("Byte %ld in vertex changed\n", idx);
253 TRACE("It was type %d, is %d now\n", This->conversion_map[idx], conversion_type);
254 ret = TRUE;
255 This->conversion_map[idx] = conversion_type;
256 }
257 }
258
259 return ret;
260 }
261
262 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
263 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
264
265 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
266 UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
267 {
268 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
269 enum wined3d_format_id format;
270 BOOL ret = FALSE;
271
272 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
273 * there, on nonexistent attribs the vbo is 0.
274 */
275 if (!(si->use_map & (1 << attrib_idx))
276 || attrib->data.buffer_object != This->buffer_object)
277 return FALSE;
278
279 format = attrib->format->id;
280 /* Look for newly appeared conversion */
281 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
282 {
283 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
284 }
285 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
286 {
287 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
288 {
289 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
290 return FALSE;
291 }
292
293 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
294 }
295 else if (This->conversion_map)
296 {
297 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
298 }
299
300 return ret;
301 }
302
303 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
304 DWORD fixup_flags)
305 {
306 UINT stride_this_run = 0;
307 BOOL ret = FALSE;
308
309 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
310 * Once we have our declaration there is no need to look it up again. Index buffers also never need
311 * conversion, so once the (empty) conversion structure is created don't bother checking again
312 */
313 if (This->flags & WINED3D_BUFFER_HASDESC)
314 {
315 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
316 }
317
318 if (!fixup_flags)
319 {
320 TRACE("No fixup required.\n");
321 if(This->conversion_map)
322 {
323 HeapFree(GetProcessHeap(), 0, This->conversion_map);
324 This->conversion_map = NULL;
325 This->stride = 0;
326 return TRUE;
327 }
328
329 return FALSE;
330 }
331
332 TRACE("Finding vertex buffer conversion information\n");
333 /* Certain declaration types need some fixups before we can pass them to
334 * opengl. This means D3DCOLOR attributes with fixed function vertex
335 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
336 * GL_ARB_half_float_vertex is not supported.
337 *
338 * Note for d3d8 and d3d9:
339 * The vertex buffer FVF doesn't help with finding them, we have to use
340 * the decoded vertex declaration and pick the things that concern the
341 * current buffer. A problem with this is that this can change between
342 * draws, so we have to validate the information and reprocess the buffer
343 * if it changes, and avoid false positives for performance reasons.
344 * WineD3D doesn't even know the vertex buffer any more, it is managed
345 * by the client libraries and passed to SetStreamSource and ProcessVertices
346 * as needed.
347 *
348 * We have to distinguish between vertex shaders and fixed function to
349 * pick the way we access the strided vertex information.
350 *
351 * This code sets up a per-byte array with the size of the detected
352 * stride of the arrays in the buffer. For each byte we have a field
353 * that marks the conversion needed on this byte. For example, the
354 * following declaration with fixed function vertex processing:
355 *
356 * POSITIONT, FLOAT4
357 * NORMAL, FLOAT3
358 * DIFFUSE, FLOAT16_4
359 * SPECULAR, D3DCOLOR
360 *
361 * Will result in
362 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
363 * [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]
364 *
365 * Where in this example map P means 4 component position conversion, 0
366 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
367 * conversion (red / blue swizzle).
368 *
369 * If we're doing conversion and the stride changes we have to reconvert
370 * the whole buffer. Note that we do not mind if the semantic changes,
371 * we only care for the conversion type. So if the NORMAL is replaced
372 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
373 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
374 * conversion types depend on the semantic as well, for example a FLOAT4
375 * texcoord needs no conversion while a FLOAT4 positiont needs one
376 */
377
378 ret = buffer_check_attribute(This, si, WINED3D_FFP_POSITION,
379 fixup_flags, &stride_this_run) || ret;
380 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
381
382 ret = buffer_check_attribute(This, si, WINED3D_FFP_NORMAL,
383 fixup_flags, &stride_this_run) || ret;
384 ret = buffer_check_attribute(This, si, WINED3D_FFP_DIFFUSE,
385 fixup_flags, &stride_this_run) || ret;
386 ret = buffer_check_attribute(This, si, WINED3D_FFP_SPECULAR,
387 fixup_flags, &stride_this_run) || ret;
388 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD0,
389 fixup_flags, &stride_this_run) || ret;
390 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD1,
391 fixup_flags, &stride_this_run) || ret;
392 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD2,
393 fixup_flags, &stride_this_run) || ret;
394 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD3,
395 fixup_flags, &stride_this_run) || ret;
396 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD4,
397 fixup_flags, &stride_this_run) || ret;
398 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD5,
399 fixup_flags, &stride_this_run) || ret;
400 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD6,
401 fixup_flags, &stride_this_run) || ret;
402 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD7,
403 fixup_flags, &stride_this_run) || ret;
404
405 if (!stride_this_run && This->conversion_map)
406 {
407 /* Sanity test */
408 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
409 HeapFree(GetProcessHeap(), 0, This->conversion_map);
410 This->conversion_map = NULL;
411 This->stride = 0;
412 }
413
414 if (ret) TRACE("Conversion information changed\n");
415
416 return ret;
417 }
418
419 static inline void fixup_d3dcolor(DWORD *dst_color)
420 {
421 DWORD src_color = *dst_color;
422
423 /* Color conversion like in drawStridedSlow. watch out for little endianity
424 * If we want that stuff to work on big endian machines too we have to consider more things
425 *
426 * 0xff000000: Alpha mask
427 * 0x00ff0000: Blue mask
428 * 0x0000ff00: Green mask
429 * 0x000000ff: Red mask
430 */
431 *dst_color = 0;
432 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
433 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
434 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
435 }
436
437 static inline void fixup_transformed_pos(float *p)
438 {
439 /* rhw conversion like in position_float4(). */
440 if (p[3] != 1.0f && p[3] != 0.0f)
441 {
442 float w = 1.0f / p[3];
443 p[0] *= w;
444 p[1] *= w;
445 p[2] *= w;
446 p[3] = w;
447 }
448 }
449
450 /* Context activation is done by the caller. */
451 void buffer_get_memory(struct wined3d_buffer *buffer, struct wined3d_context *context,
452 struct wined3d_bo_address *data)
453 {
454 data->buffer_object = buffer->buffer_object;
455 if (!buffer->buffer_object)
456 {
457 if ((buffer->flags & WINED3D_BUFFER_CREATEBO) && !buffer->resource.map_count)
458 {
459 buffer_create_buffer_object(buffer, context);
460 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
461 if (buffer->buffer_object)
462 {
463 data->buffer_object = buffer->buffer_object;
464 data->addr = NULL;
465 return;
466 }
467 }
468 data->addr = buffer->resource.heap_memory;
469 }
470 else
471 {
472 data->addr = NULL;
473 }
474 }
475
476 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
477 {
478 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
479
480 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
481
482 return refcount;
483 }
484
485 /* Context activation is done by the caller. */
486 BYTE *buffer_get_sysmem(struct wined3d_buffer *This, struct wined3d_context *context)
487 {
488 const struct wined3d_gl_info *gl_info = context->gl_info;
489
490 /* Heap_memory exists if the buffer is double buffered or has no buffer object at all. */
491 if (This->resource.heap_memory)
492 return This->resource.heap_memory;
493
494 if (!wined3d_resource_allocate_sysmem(&This->resource))
495 ERR("Failed to allocate system memory.\n");
496
497 if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
498 context_invalidate_state(context, STATE_INDEXBUFFER);
499
500 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
501 GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.heap_memory));
502 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
503
504 return This->resource.heap_memory;
505 }
506
507 static void buffer_unload(struct wined3d_resource *resource)
508 {
509 struct wined3d_buffer *buffer = buffer_from_resource(resource);
510
511 TRACE("buffer %p.\n", buffer);
512
513 if (buffer->buffer_object)
514 {
515 struct wined3d_device *device = resource->device;
516 struct wined3d_context *context;
517
518 context = context_acquire(device, NULL);
519
520 /* Download the buffer, but don't permanently enable double buffering */
521 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
522 {
523 buffer_get_sysmem(buffer, context);
524 buffer->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
525 }
526
527 delete_gl_buffer(buffer, context->gl_info);
528 buffer->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
529 buffer_clear_dirty_areas(buffer);
530
531 context_release(context);
532
533 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
534 buffer->conversion_map = NULL;
535 buffer->stride = 0;
536 buffer->conversion_stride = 0;
537 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
538 }
539
540 resource_unload(resource);
541 }
542
543 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
544 {
545 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
546 struct wined3d_context *context;
547
548 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
549
550 if (!refcount)
551 {
552 if (buffer->buffer_object)
553 {
554 context = context_acquire(buffer->resource.device, NULL);
555 delete_gl_buffer(buffer, context->gl_info);
556 context_release(context);
557
558 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
559 }
560
561 resource_cleanup(&buffer->resource);
562 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
563 HeapFree(GetProcessHeap(), 0, buffer->maps);
564 HeapFree(GetProcessHeap(), 0, buffer);
565 }
566
567 return refcount;
568 }
569
570 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
571 {
572 TRACE("buffer %p.\n", buffer);
573
574 return buffer->resource.parent;
575 }
576
577 /* The caller provides a context and binds the buffer */
578 static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
579 {
580 enum wined3d_event_query_result ret;
581
582 /* No fencing needs to be done if the app promises not to overwrite
583 * existing data. */
584 if (flags & WINED3D_MAP_NOOVERWRITE)
585 return;
586
587 if (flags & WINED3D_MAP_DISCARD)
588 {
589 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
590 checkGLcall("glBufferDataARB\n");
591 return;
592 }
593
594 if(!This->query)
595 {
596 TRACE("Creating event query for buffer %p\n", This);
597
598 if (!wined3d_event_query_supported(gl_info))
599 {
600 FIXME("Event queries not supported, dropping async buffer locks.\n");
601 goto drop_query;
602 }
603
604 This->query = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->query));
605 if (!This->query)
606 {
607 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
608 goto drop_query;
609 }
610
611 /* Since we don't know about old draws a glFinish is needed once */
612 gl_info->gl_ops.gl.p_glFinish();
613 return;
614 }
615 TRACE("Synchronizing buffer %p\n", This);
616 ret = wined3d_event_query_finish(This->query, This->resource.device);
617 switch(ret)
618 {
619 case WINED3D_EVENT_QUERY_NOT_STARTED:
620 case WINED3D_EVENT_QUERY_OK:
621 /* All done */
622 return;
623
624 case WINED3D_EVENT_QUERY_WRONG_THREAD:
625 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
626 goto drop_query;
627
628 default:
629 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret);
630 goto drop_query;
631 }
632
633 drop_query:
634 if(This->query)
635 {
636 wined3d_event_query_destroy(This->query);
637 This->query = NULL;
638 }
639
640 gl_info->gl_ops.gl.p_glFinish();
641 GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
642 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
643 This->flags &= ~WINED3D_BUFFER_APPLESYNC;
644 }
645
646 /* The caller provides a GL context */
647 static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
648 {
649 BYTE *map;
650 UINT start = 0, len = 0;
651
652 /* This potentially invalidates the element array buffer binding, but the
653 * caller always takes care of this. */
654 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
655 checkGLcall("glBindBufferARB");
656 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
657 {
658 GLbitfield mapflags;
659 mapflags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
660 if (flags & WINED3D_BUFFER_DISCARD)
661 mapflags |= GL_MAP_INVALIDATE_BUFFER_BIT;
662 else if (!(flags & WINED3D_BUFFER_SYNC))
663 mapflags |= GL_MAP_UNSYNCHRONIZED_BIT;
664 map = GL_EXTCALL(glMapBufferRange(This->buffer_type_hint, 0,
665 This->resource.size, mapflags));
666 checkGLcall("glMapBufferRange");
667 }
668 else
669 {
670 if (This->flags & WINED3D_BUFFER_APPLESYNC)
671 {
672 DWORD syncflags = 0;
673 if (flags & WINED3D_BUFFER_DISCARD)
674 syncflags |= WINED3D_MAP_DISCARD;
675 else if (!(flags & WINED3D_BUFFER_SYNC))
676 syncflags |= WINED3D_MAP_NOOVERWRITE;
677 buffer_sync_apple(This, syncflags, gl_info);
678 }
679 map = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_WRITE_ONLY_ARB));
680 checkGLcall("glMapBufferARB");
681 }
682 if (!map)
683 {
684 ERR("Failed to map opengl buffer\n");
685 return;
686 }
687
688 while (This->modified_areas)
689 {
690 This->modified_areas--;
691 start = This->maps[This->modified_areas].offset;
692 len = This->maps[This->modified_areas].size;
693
694 memcpy(map + start, (BYTE *)This->resource.heap_memory + start, len);
695
696 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
697 {
698 GL_EXTCALL(glFlushMappedBufferRange(This->buffer_type_hint, start, len));
699 checkGLcall("glFlushMappedBufferRange");
700 }
701 else if (This->flags & WINED3D_BUFFER_FLUSH)
702 {
703 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This->buffer_type_hint, start, len));
704 checkGLcall("glFlushMappedBufferRangeAPPLE");
705 }
706 }
707 GL_EXTCALL(glUnmapBufferARB(This->buffer_type_hint));
708 checkGLcall("glUnmapBufferARB");
709 }
710
711 void buffer_mark_used(struct wined3d_buffer *buffer)
712 {
713 buffer->flags &= ~(WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
714 }
715
716 /* Context activation is done by the caller. */
717 void buffer_internal_preload(struct wined3d_buffer *buffer, struct wined3d_context *context,
718 const struct wined3d_state *state)
719 {
720 DWORD flags = buffer->flags & (WINED3D_BUFFER_SYNC | WINED3D_BUFFER_DISCARD);
721 struct wined3d_device *device = buffer->resource.device;
722 UINT start = 0, end = 0, len = 0, vertices;
723 const struct wined3d_gl_info *gl_info;
724 BOOL decl_changed = FALSE;
725 unsigned int i, j;
726 BYTE *data;
727
728 TRACE("buffer %p.\n", buffer);
729
730 if (buffer->resource.map_count)
731 {
732 WARN("Buffer is mapped, skipping preload.\n");
733 return;
734 }
735
736 buffer_mark_used(buffer);
737
738 if (!buffer->buffer_object)
739 {
740 /* TODO: Make converting independent from VBOs */
741 if (buffer->flags & WINED3D_BUFFER_CREATEBO)
742 {
743 buffer_create_buffer_object(buffer, context);
744 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
745 }
746 else
747 {
748 /* Not doing any conversion */
749 return;
750 }
751 }
752
753 /* Reading the declaration makes only sense if we have valid state information
754 * (i.e., if this function is called during draws). */
755 if (state)
756 {
757 DWORD fixup_flags = 0;
758
759 if (!use_vs(state))
760 {
761 if (!context->gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
762 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
763 if (!context->d3d_info->xyzrhw)
764 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
765 }
766
767 decl_changed = buffer_find_decl(buffer, &context->stream_info, fixup_flags);
768 buffer->flags |= WINED3D_BUFFER_HASDESC;
769 }
770
771 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
772 {
773 ++buffer->draw_count;
774 if (buffer->draw_count > VB_RESETDECLCHANGE)
775 buffer->decl_change_count = 0;
776 if (buffer->draw_count > VB_RESETFULLCONVS)
777 buffer->full_conversion_count = 0;
778 return;
779 }
780
781 /* If applications change the declaration over and over, reconverting all the time is a huge
782 * performance hit. So count the declaration changes and release the VBO if there are too many
783 * of them (and thus stop converting)
784 */
785 if (decl_changed)
786 {
787 ++buffer->decl_change_count;
788 buffer->draw_count = 0;
789
790 if (buffer->decl_change_count > VB_MAXDECLCHANGES
791 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
792 {
793 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
794
795 buffer_unload(&buffer->resource);
796 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
797
798 /* The stream source state handler might have read the memory of
799 * the vertex buffer already and got the memory in the vbo which
800 * is not valid any longer. Dirtify the stream source to force a
801 * reload. This happens only once per changed vertexbuffer and
802 * should occur rather rarely. */
803 device_invalidate_state(device, STATE_STREAMSRC);
804 return;
805 }
806
807 /* The declaration changed, reload the whole buffer. */
808 WARN("Reloading buffer because of a vertex declaration change.\n");
809 buffer_invalidate_bo_range(buffer, 0, 0);
810
811 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
812 * cleared for unsynchronized updates
813 */
814 flags = 0;
815 }
816 else
817 {
818 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
819 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
820 * decl changes and reset the decl change count after a specific number of them
821 */
822 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
823 {
824 ++buffer->full_conversion_count;
825 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
826 {
827 FIXME("Too many full buffer conversions, stopping converting.\n");
828 buffer_unload(&buffer->resource);
829 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
830 if (buffer->resource.bind_count)
831 device_invalidate_state(device, STATE_STREAMSRC);
832 return;
833 }
834 }
835 else
836 {
837 ++buffer->draw_count;
838 if (buffer->draw_count > VB_RESETDECLCHANGE)
839 buffer->decl_change_count = 0;
840 if (buffer->draw_count > VB_RESETFULLCONVS)
841 buffer->full_conversion_count = 0;
842 }
843 }
844
845 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
846 device_invalidate_state(device, STATE_INDEXBUFFER);
847
848 if (!buffer->conversion_map)
849 {
850 /* That means that there is nothing to fixup. Just upload from
851 * buffer->resource.heap_memory directly into the vbo. Do not
852 * free the system memory copy because drawPrimitive may need it if
853 * the stride is 0, for instancing emulation, vertex blending
854 * emulation or shader emulation. */
855 TRACE("No conversion needed.\n");
856
857 /* Nothing to do because we locked directly into the vbo */
858 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
859 {
860 return;
861 }
862
863 buffer_direct_upload(buffer, context->gl_info, flags);
864
865 return;
866 }
867
868 gl_info = context->gl_info;
869
870 if(!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
871 {
872 buffer_get_sysmem(buffer, context);
873 }
874
875 /* Now for each vertex in the buffer that needs conversion */
876 vertices = buffer->resource.size / buffer->stride;
877
878 data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size);
879
880 while(buffer->modified_areas)
881 {
882 buffer->modified_areas--;
883 start = buffer->maps[buffer->modified_areas].offset;
884 len = buffer->maps[buffer->modified_areas].size;
885 end = start + len;
886
887 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
888 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertices); ++i)
889 {
890 for (j = 0; j < buffer->stride; ++j)
891 {
892 switch (buffer->conversion_map[j])
893 {
894 case CONV_NONE:
895 /* Done already */
896 j += 3;
897 break;
898 case CONV_D3DCOLOR:
899 fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
900 j += 3;
901 break;
902
903 case CONV_POSITIONT:
904 fixup_transformed_pos((float *) (data + i * buffer->stride + j));
905 j += 15;
906 break;
907 default:
908 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer->conversion_map[j]);
909 }
910 }
911 }
912
913 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
914 checkGLcall("glBindBufferARB");
915 GL_EXTCALL(glBufferSubDataARB(buffer->buffer_type_hint, start, len, data + start));
916 checkGLcall("glBufferSubDataARB");
917 }
918
919 HeapFree(GetProcessHeap(), 0, data);
920 }
921
922 void CDECL wined3d_buffer_preload(struct wined3d_buffer *buffer)
923 {
924 struct wined3d_context *context;
925 context = context_acquire(buffer->resource.device, NULL);
926 buffer_internal_preload(buffer, context, NULL);
927 context_release(context);
928 }
929
930 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
931 {
932 TRACE("buffer %p.\n", buffer);
933
934 return &buffer->resource;
935 }
936
937 HRESULT CDECL wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
938 {
939 LONG count;
940 BYTE *base;
941
942 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x\n", buffer, offset, size, data, flags);
943
944 flags = wined3d_resource_sanitize_map_flags(&buffer->resource, flags);
945 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
946 * fill rate test seems to depend on this. When we map a buffer with
947 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
948 * previous contents of the buffer. The r600g driver only does this when
949 * the buffer is currently in use, while the proprietary NVIDIA driver
950 * appears to do this unconditionally. */
951 if (buffer->flags & WINED3D_BUFFER_DISCARD)
952 flags &= ~WINED3D_MAP_DISCARD;
953 count = ++buffer->resource.map_count;
954
955 if (buffer->buffer_object)
956 {
957 /* DISCARD invalidates the entire buffer, regardless of the specified
958 * offset and size. Some applications also depend on the entire buffer
959 * being uploaded in that case. Two such applications are Port Royale
960 * and Darkstar One. */
961 if (flags & WINED3D_MAP_DISCARD)
962 buffer_invalidate_bo_range(buffer, 0, 0);
963 else if (!(flags & WINED3D_MAP_READONLY))
964 buffer_invalidate_bo_range(buffer, offset, size);
965
966 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER))
967 {
968 if (count == 1)
969 {
970 struct wined3d_device *device = buffer->resource.device;
971 struct wined3d_context *context;
972 const struct wined3d_gl_info *gl_info;
973
974 context = context_acquire(device, NULL);
975 gl_info = context->gl_info;
976
977 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
978 context_invalidate_state(context, STATE_INDEXBUFFER);
979 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
980
981 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
982 {
983 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
984 buffer->map_ptr = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
985 0, buffer->resource.size, mapflags));
986 checkGLcall("glMapBufferRange");
987 }
988 else
989 {
990 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
991 buffer_sync_apple(buffer, flags, gl_info);
992 buffer->map_ptr = GL_EXTCALL(glMapBufferARB(buffer->buffer_type_hint,
993 GL_READ_WRITE_ARB));
994 checkGLcall("glMapBufferARB");
995 }
996
997 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
998 {
999 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
1000
1001 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1002 checkGLcall("glUnmapBufferARB");
1003 buffer->map_ptr = NULL;
1004
1005 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1006 {
1007 /* The extra copy is more expensive than not using VBOs at
1008 * all on the Nvidia Linux driver, which is the only driver
1009 * that returns unaligned pointers
1010 */
1011 TRACE("Dynamic buffer, dropping VBO\n");
1012 buffer_unload(&buffer->resource);
1013 buffer->flags &= ~WINED3D_BUFFER_CREATEBO;
1014 if (buffer->resource.bind_count)
1015 device_invalidate_state(device, STATE_STREAMSRC);
1016 }
1017 else
1018 {
1019 TRACE("Falling back to doublebuffered operation\n");
1020 buffer_get_sysmem(buffer, context);
1021 }
1022 TRACE("New pointer is %p.\n", buffer->resource.heap_memory);
1023 buffer->map_ptr = NULL;
1024 }
1025 context_release(context);
1026 }
1027 }
1028
1029 if (flags & WINED3D_MAP_DISCARD)
1030 buffer->flags |= WINED3D_BUFFER_DISCARD;
1031 else if (!(flags & WINED3D_MAP_NOOVERWRITE))
1032 buffer->flags |= WINED3D_BUFFER_SYNC;
1033 }
1034
1035 base = buffer->map_ptr ? buffer->map_ptr : buffer->resource.heap_memory;
1036 *data = base + offset;
1037
1038 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, base, offset);
1039 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1040
1041 return WINED3D_OK;
1042 }
1043
1044 void CDECL wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1045 {
1046 ULONG i;
1047
1048 TRACE("buffer %p.\n", buffer);
1049
1050 /* In the case that the number of Unmap calls > the
1051 * number of Map calls, d3d returns always D3D_OK.
1052 * This is also needed to prevent Map from returning garbage on
1053 * the next call (this will happen if the lock_count is < 0). */
1054 if (!buffer->resource.map_count)
1055 {
1056 WARN("Unmap called without a previous map call.\n");
1057 return;
1058 }
1059
1060 if (--buffer->resource.map_count)
1061 {
1062 /* Delay loading the buffer until everything is unlocked */
1063 TRACE("Ignoring unmap.\n");
1064 return;
1065 }
1066
1067 if (!(buffer->flags & WINED3D_BUFFER_DOUBLEBUFFER) && buffer->buffer_object)
1068 {
1069 struct wined3d_device *device = buffer->resource.device;
1070 const struct wined3d_gl_info *gl_info;
1071 struct wined3d_context *context;
1072
1073 context = context_acquire(device, NULL);
1074 gl_info = context->gl_info;
1075
1076 if (buffer->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1077 context_invalidate_state(context, STATE_INDEXBUFFER);
1078 GL_EXTCALL(glBindBufferARB(buffer->buffer_type_hint, buffer->buffer_object));
1079
1080 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1081 {
1082 for (i = 0; i < buffer->modified_areas; ++i)
1083 {
1084 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1085 buffer->maps[i].offset, buffer->maps[i].size));
1086 checkGLcall("glFlushMappedBufferRange");
1087 }
1088 }
1089 else if (buffer->flags & WINED3D_BUFFER_FLUSH)
1090 {
1091 for (i = 0; i < buffer->modified_areas; ++i)
1092 {
1093 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1094 buffer->maps[i].offset, buffer->maps[i].size));
1095 checkGLcall("glFlushMappedBufferRangeAPPLE");
1096 }
1097 }
1098
1099 GL_EXTCALL(glUnmapBufferARB(buffer->buffer_type_hint));
1100 if (wined3d_settings.strict_draw_ordering)
1101 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1102 context_release(context);
1103
1104 buffer_clear_dirty_areas(buffer);
1105 buffer->map_ptr = NULL;
1106 }
1107 else if (buffer->flags & WINED3D_BUFFER_HASDESC)
1108 {
1109 wined3d_buffer_preload(buffer);
1110 }
1111 }
1112
1113 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1114 {
1115 return wined3d_buffer_incref(buffer_from_resource(resource));
1116 }
1117
1118 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1119 {
1120 return wined3d_buffer_decref(buffer_from_resource(resource));
1121 }
1122
1123 static const struct wined3d_resource_ops buffer_resource_ops =
1124 {
1125 buffer_resource_incref,
1126 buffer_resource_decref,
1127 buffer_unload,
1128 };
1129
1130 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1131 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, GLenum bind_hint,
1132 const char *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1133 {
1134 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1135 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1136 HRESULT hr;
1137 BOOL dynamic_buffer_ok;
1138
1139 if (!size)
1140 {
1141 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1142 return WINED3DERR_INVALIDCALL;
1143 }
1144
1145 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1146 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size,
1147 parent, parent_ops, &buffer_resource_ops);
1148 if (FAILED(hr))
1149 {
1150 WARN("Failed to initialize resource, hr %#x\n", hr);
1151 return hr;
1152 }
1153 buffer->buffer_type_hint = bind_hint;
1154
1155 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1156 debug_d3dformat(buffer->resource.format->id), buffer->resource.heap_memory, buffer);
1157
1158 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING)
1159 {
1160 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1161 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1162 * Still use a VBO to support OpenGL 3 core contexts. */
1163 TRACE("Using doublebuffer mode because of software vertex processing\n");
1164 buffer->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
1165 }
1166
1167 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1168
1169 /* Observations show that drawStridedSlow is faster on dynamic VBs than converting +
1170 * drawStridedFast (half-life 2 and others).
1171 *
1172 * Basically converting the vertices in the buffer is quite expensive, and observations
1173 * show that drawStridedSlow is faster than converting + uploading + drawStridedFast.
1174 * Therefore do not create a VBO for WINED3DUSAGE_DYNAMIC buffers.
1175 */
1176 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1177 {
1178 TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
1179 }
1180 else if(buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1181 {
1182 TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
1183 }
1184 else if(!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1185 {
1186 TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
1187 }
1188 else
1189 {
1190 buffer->flags |= WINED3D_BUFFER_CREATEBO;
1191 }
1192
1193 if (data)
1194 {
1195 BYTE *ptr;
1196
1197 hr = wined3d_buffer_map(buffer, 0, size, &ptr, 0);
1198 if (FAILED(hr))
1199 {
1200 ERR("Failed to map buffer, hr %#x\n", hr);
1201 buffer_unload(&buffer->resource);
1202 resource_cleanup(&buffer->resource);
1203 return hr;
1204 }
1205
1206 memcpy(ptr, data, size);
1207
1208 wined3d_buffer_unmap(buffer);
1209 }
1210
1211 buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps));
1212 if (!buffer->maps)
1213 {
1214 ERR("Out of memory\n");
1215 buffer_unload(&buffer->resource);
1216 resource_cleanup(&buffer->resource);
1217 return E_OUTOFMEMORY;
1218 }
1219 buffer->maps_size = 1;
1220
1221 return WINED3D_OK;
1222 }
1223
1224 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1225 const void *data, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1226 {
1227 struct wined3d_buffer *object;
1228 HRESULT hr;
1229
1230 TRACE("device %p, desc %p, data %p, parent %p, buffer %p\n", device, desc, data, parent, buffer);
1231
1232 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1233 if (!object)
1234 return E_OUTOFMEMORY;
1235
1236 FIXME("Ignoring access flags (pool)\n");
1237
1238 hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1239 WINED3D_POOL_MANAGED, GL_ARRAY_BUFFER_ARB, data, parent, parent_ops);
1240 if (FAILED(hr))
1241 {
1242 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1243 HeapFree(GetProcessHeap(), 0, object);
1244 return hr;
1245 }
1246 object->desc = *desc;
1247
1248 TRACE("Created buffer %p.\n", object);
1249
1250 *buffer = object;
1251
1252 return WINED3D_OK;
1253 }
1254
1255 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1256 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1257 {
1258 struct wined3d_buffer *object;
1259 HRESULT hr;
1260
1261 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1262 device, size, usage, pool, parent, parent_ops, buffer);
1263
1264 if (pool == WINED3D_POOL_SCRATCH)
1265 {
1266 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1267 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1268 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1269 *buffer = NULL;
1270 return WINED3DERR_INVALIDCALL;
1271 }
1272
1273 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1274 if (!object)
1275 {
1276 *buffer = NULL;
1277 return WINED3DERR_OUTOFVIDEOMEMORY;
1278 }
1279
1280 hr = buffer_init(object, device, size, usage, WINED3DFMT_VERTEXDATA,
1281 pool, GL_ARRAY_BUFFER_ARB, NULL, parent, parent_ops);
1282 if (FAILED(hr))
1283 {
1284 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1285 HeapFree(GetProcessHeap(), 0, object);
1286 return hr;
1287 }
1288
1289 TRACE("Created buffer %p.\n", object);
1290 *buffer = object;
1291
1292 return WINED3D_OK;
1293 }
1294
1295 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1296 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1297 {
1298 struct wined3d_buffer *object;
1299 HRESULT hr;
1300
1301 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1302 device, size, usage, pool, parent, parent_ops, buffer);
1303
1304 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1305 if (!object)
1306 {
1307 *buffer = NULL;
1308 return WINED3DERR_OUTOFVIDEOMEMORY;
1309 }
1310
1311 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1312 WINED3DFMT_UNKNOWN, pool, GL_ELEMENT_ARRAY_BUFFER_ARB, NULL,
1313 parent, parent_ops);
1314 if (FAILED(hr))
1315 {
1316 WARN("Failed to initialize buffer, hr %#x\n", hr);
1317 HeapFree(GetProcessHeap(), 0, object);
1318 return hr;
1319 }
1320
1321 TRACE("Created buffer %p.\n", object);
1322 *buffer = object;
1323
1324 return WINED3D_OK;
1325 }