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