mshtml: Make IEventTarget_{add,remove}EventListener_hook() static.
[wine.git] / dlls / wined3d / buffer.c
blobe953e9ad215c0828780cdb52af6580941c132688
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2007-2011, 2013-2014 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
33 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
34 #define WINED3D_BUFFER_PIN_SYSMEM 0x04 /* Keep a system memory copy for this buffer. */
35 #define WINED3D_BUFFER_DISCARD 0x08 /* A DISCARD lock has occurred since the last preload. */
36 #define WINED3D_BUFFER_APPLESYNC 0x10 /* Using sync as in GL_APPLE_flush_buffer_range. */
38 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
39 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
40 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
41 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
43 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer *buffer)
45 if (buffer->flags & WINED3D_BUFFER_PIN_SYSMEM)
47 TRACE("Not evicting system memory for buffer %p.\n", buffer);
48 return;
51 TRACE("Evicting system memory for buffer %p.\n", buffer);
52 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_SYSMEM);
53 wined3d_resource_free_sysmem(&buffer->resource);
56 static void buffer_invalidate_bo_range(struct wined3d_buffer *buffer, unsigned int offset, unsigned int size)
58 if (!offset && (!size || size == buffer->resource.size))
59 goto invalidate_all;
61 if (offset > buffer->resource.size || size > buffer->resource.size - offset)
63 WARN("Invalid range specified, invalidating entire buffer.\n");
64 goto invalidate_all;
67 if (!wined3d_array_reserve((void **)&buffer->maps, &buffer->maps_size,
68 buffer->modified_areas + 1, sizeof(*buffer->maps)))
70 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
71 goto invalidate_all;
74 buffer->maps[buffer->modified_areas].offset = offset;
75 buffer->maps[buffer->modified_areas].size = size;
76 ++buffer->modified_areas;
77 return;
79 invalidate_all:
80 buffer->modified_areas = 1;
81 buffer->maps[0].offset = 0;
82 buffer->maps[0].size = buffer->resource.size;
85 static inline void buffer_clear_dirty_areas(struct wined3d_buffer *This)
87 This->modified_areas = 0;
90 static BOOL buffer_is_dirty(const struct wined3d_buffer *buffer)
92 return !!buffer->modified_areas;
95 static BOOL buffer_is_fully_dirty(const struct wined3d_buffer *buffer)
97 return buffer->modified_areas == 1
98 && !buffer->maps->offset && buffer->maps->size == buffer->resource.size;
101 static void wined3d_buffer_validate_location(struct wined3d_buffer *buffer, DWORD location)
103 TRACE("buffer %p, location %s.\n", buffer, wined3d_debug_location(location));
105 if (location & WINED3D_LOCATION_BUFFER)
106 buffer_clear_dirty_areas(buffer);
108 buffer->locations |= location;
110 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer->locations));
113 static void wined3d_buffer_invalidate_range(struct wined3d_buffer *buffer, DWORD location,
114 unsigned int offset, unsigned int size)
116 TRACE("buffer %p, location %s, offset %u, size %u.\n",
117 buffer, wined3d_debug_location(location), offset, size);
119 if (location & WINED3D_LOCATION_BUFFER)
120 buffer_invalidate_bo_range(buffer, offset, size);
122 buffer->locations &= ~location;
124 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer->locations));
126 if (!buffer->locations)
127 ERR("Buffer %p does not have any up to date location.\n", buffer);
130 void wined3d_buffer_invalidate_location(struct wined3d_buffer *buffer, DWORD location)
132 wined3d_buffer_invalidate_range(buffer, location, 0, 0);
135 /* Context activation is done by the caller. */
136 static void buffer_bind(struct wined3d_buffer *buffer, struct wined3d_context *context)
138 context_bind_bo(context, buffer->buffer_type_hint, buffer->buffer_object);
141 /* Context activation is done by the caller. */
142 static void buffer_destroy_buffer_object(struct wined3d_buffer *buffer, struct wined3d_context *context)
144 const struct wined3d_gl_info *gl_info = context->gl_info;
145 struct wined3d_resource *resource = &buffer->resource;
147 if (!buffer->buffer_object)
148 return;
150 /* The stream source state handler might have read the memory of the
151 * vertex buffer already and got the memory in the vbo which is not
152 * valid any longer. Dirtify the stream source to force a reload. This
153 * happens only once per changed vertexbuffer and should occur rather
154 * rarely. */
155 if (resource->bind_count)
157 if (buffer->bind_flags & WINED3D_BIND_VERTEX_BUFFER)
158 device_invalidate_state(resource->device, STATE_STREAMSRC);
159 if (buffer->bind_flags & WINED3D_BIND_INDEX_BUFFER)
160 device_invalidate_state(resource->device, STATE_INDEXBUFFER);
161 if (buffer->bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
163 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX));
164 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL));
165 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN));
166 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY));
167 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL));
168 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE));
170 if (buffer->bind_flags & WINED3D_BIND_STREAM_OUTPUT)
172 device_invalidate_state(resource->device, STATE_STREAM_OUTPUT);
173 if (context->transform_feedback_active)
175 /* We have to make sure that transform feedback is not active
176 * when deleting a potentially bound transform feedback buffer.
177 * This may happen when the device is being destroyed. */
178 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer);
179 context_end_transform_feedback(context);
184 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
185 checkGLcall("glDeleteBuffers");
186 buffer->buffer_object = 0;
188 if (buffer->fence)
190 wined3d_fence_destroy(buffer->fence);
191 buffer->fence = NULL;
193 buffer->flags &= ~WINED3D_BUFFER_APPLESYNC;
196 /* Context activation is done by the caller. */
197 static BOOL buffer_create_buffer_object(struct wined3d_buffer *buffer, struct wined3d_context *context)
199 const struct wined3d_gl_info *gl_info = context->gl_info;
200 GLenum gl_usage = GL_STATIC_DRAW;
201 GLenum error;
203 TRACE("Creating an OpenGL buffer object for wined3d_buffer %p with usage %s.\n",
204 buffer, debug_d3dusage(buffer->resource.usage));
206 /* Make sure that the gl error is cleared. Do not use checkGLcall
207 * here because checkGLcall just prints a fixme and continues. However,
208 * if an error during VBO creation occurs we can fall back to non-VBO operation
209 * with full functionality(but performance loss).
211 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
213 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
214 * The vertex declaration from the device determines how the data in the
215 * buffer is interpreted. This means that on each draw call the buffer has
216 * to be verified to check if the rhw and color values are in the correct
217 * format. */
219 GL_EXTCALL(glGenBuffers(1, &buffer->buffer_object));
220 error = gl_info->gl_ops.gl.p_glGetError();
221 if (!buffer->buffer_object || error != GL_NO_ERROR)
223 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error), error);
224 goto fail;
227 buffer_bind(buffer, context);
228 error = gl_info->gl_ops.gl.p_glGetError();
229 if (error != GL_NO_ERROR)
231 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error), error);
232 goto fail;
235 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
237 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
238 gl_usage = GL_STREAM_DRAW_ARB;
240 if (gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
242 GL_EXTCALL(glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
243 GL_EXTCALL(glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
244 checkGLcall("glBufferParameteriAPPLE");
245 buffer->flags |= WINED3D_BUFFER_APPLESYNC;
247 /* No setup is needed here for GL_ARB_map_buffer_range. */
250 GL_EXTCALL(glBufferData(buffer->buffer_type_hint, buffer->resource.size, NULL, gl_usage));
251 error = gl_info->gl_ops.gl.p_glGetError();
252 if (error != GL_NO_ERROR)
254 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error), error);
255 goto fail;
258 buffer->buffer_object_usage = gl_usage;
259 buffer_invalidate_bo_range(buffer, 0, 0);
261 return TRUE;
263 fail:
264 /* Clean up all BO init, but continue because we can work without a BO :-) */
265 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
266 buffer->flags &= ~WINED3D_BUFFER_USE_BO;
267 buffer_destroy_buffer_object(buffer, context);
268 buffer_clear_dirty_areas(buffer);
269 return FALSE;
272 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *buffer,
273 const enum wined3d_buffer_conversion_type conversion_type,
274 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
276 const struct wined3d_format *format = attrib->format;
277 BOOL ret = FALSE;
278 unsigned int i;
279 DWORD_PTR data;
281 /* Check for some valid situations which cause us pain. One is if the buffer is used for
282 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
283 * with different strides. In the 2nd case we might have to drop conversion entirely,
284 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
286 if (!attrib->stride)
288 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
289 debug_d3dformat(format->id));
291 else if (attrib->stride != *stride_this_run && *stride_this_run)
293 FIXME("Got two concurrent strides, %d and %d.\n", attrib->stride, *stride_this_run);
295 else
297 *stride_this_run = attrib->stride;
298 if (buffer->stride != *stride_this_run)
300 /* We rely that this happens only on the first converted attribute that is found,
301 * if at all. See above check
303 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
304 buffer->stride = *stride_this_run;
305 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer->conversion_map);
306 buffer->conversion_map = wined3d_calloc(buffer->stride, sizeof(*buffer->conversion_map));
307 ret = TRUE;
311 data = ((DWORD_PTR)attrib->data.addr) % buffer->stride;
312 for (i = 0; i < format->attribute_size; ++i)
314 DWORD_PTR idx = (data + i) % buffer->stride;
315 if (buffer->conversion_map[idx] != conversion_type)
317 TRACE("Byte %lu in vertex changed:\n", idx);
318 TRACE(" It was type %#x, is %#x now.\n", buffer->conversion_map[idx], conversion_type);
319 ret = TRUE;
320 buffer->conversion_map[idx] = conversion_type;
324 return ret;
327 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
328 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
330 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
331 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
333 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
334 enum wined3d_format_id format;
335 BOOL ret = FALSE;
337 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
338 * there, on nonexistent attribs the vbo is 0.
340 if (!(si->use_map & (1u << attrib_idx))
341 || state->streams[attrib->stream_idx].buffer != This)
342 return FALSE;
344 format = attrib->format->id;
345 /* Look for newly appeared conversion */
346 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
348 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
350 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
352 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
354 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
355 return FALSE;
358 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
360 else if (This->conversion_map)
362 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
365 return ret;
368 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
369 const struct wined3d_state *state, DWORD fixup_flags)
371 UINT stride_this_run = 0;
372 BOOL ret = FALSE;
374 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
375 * Once we have our declaration there is no need to look it up again. Index buffers also never need
376 * conversion, so once the (empty) conversion structure is created don't bother checking again
378 if (This->flags & WINED3D_BUFFER_HASDESC)
380 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
383 if (!fixup_flags)
385 TRACE("No fixup required.\n");
386 if(This->conversion_map)
388 HeapFree(GetProcessHeap(), 0, This->conversion_map);
389 This->conversion_map = NULL;
390 This->stride = 0;
391 return TRUE;
394 return FALSE;
397 TRACE("Finding vertex buffer conversion information\n");
398 /* Certain declaration types need some fixups before we can pass them to
399 * opengl. This means D3DCOLOR attributes with fixed function vertex
400 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
401 * GL_ARB_half_float_vertex is not supported.
403 * Note for d3d8 and d3d9:
404 * The vertex buffer FVF doesn't help with finding them, we have to use
405 * the decoded vertex declaration and pick the things that concern the
406 * current buffer. A problem with this is that this can change between
407 * draws, so we have to validate the information and reprocess the buffer
408 * if it changes, and avoid false positives for performance reasons.
409 * WineD3D doesn't even know the vertex buffer any more, it is managed
410 * by the client libraries and passed to SetStreamSource and ProcessVertices
411 * as needed.
413 * We have to distinguish between vertex shaders and fixed function to
414 * pick the way we access the strided vertex information.
416 * This code sets up a per-byte array with the size of the detected
417 * stride of the arrays in the buffer. For each byte we have a field
418 * that marks the conversion needed on this byte. For example, the
419 * following declaration with fixed function vertex processing:
421 * POSITIONT, FLOAT4
422 * NORMAL, FLOAT3
423 * DIFFUSE, FLOAT16_4
424 * SPECULAR, D3DCOLOR
426 * Will result in
427 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
428 * [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]
430 * Where in this example map P means 4 component position conversion, 0
431 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
432 * conversion (red / blue swizzle).
434 * If we're doing conversion and the stride changes we have to reconvert
435 * the whole buffer. Note that we do not mind if the semantic changes,
436 * we only care for the conversion type. So if the NORMAL is replaced
437 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
438 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
439 * conversion types depend on the semantic as well, for example a FLOAT4
440 * texcoord needs no conversion while a FLOAT4 positiont needs one
443 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
444 fixup_flags, &stride_this_run) || ret;
445 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
447 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDWEIGHT,
448 fixup_flags, &stride_this_run) || ret;
449 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDINDICES,
450 fixup_flags, &stride_this_run) || ret;
451 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
452 fixup_flags, &stride_this_run) || ret;
453 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
454 fixup_flags, &stride_this_run) || ret;
455 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
456 fixup_flags, &stride_this_run) || ret;
457 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
458 fixup_flags, &stride_this_run) || ret;
459 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
460 fixup_flags, &stride_this_run) || ret;
461 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
462 fixup_flags, &stride_this_run) || ret;
463 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
464 fixup_flags, &stride_this_run) || ret;
465 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
466 fixup_flags, &stride_this_run) || ret;
467 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
468 fixup_flags, &stride_this_run) || ret;
469 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
470 fixup_flags, &stride_this_run) || ret;
471 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
472 fixup_flags, &stride_this_run) || ret;
474 if (!stride_this_run && This->conversion_map)
476 /* Sanity test */
477 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
478 HeapFree(GetProcessHeap(), 0, This->conversion_map);
479 This->conversion_map = NULL;
480 This->stride = 0;
483 if (ret) TRACE("Conversion information changed\n");
485 return ret;
488 static inline unsigned int fixup_d3dcolor(DWORD *dst_color)
490 DWORD src_color = *dst_color;
492 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
493 * endianness. If we want this to work on big-endian machines as well we
494 * have to consider more things.
496 * 0xff000000: Alpha mask
497 * 0x00ff0000: Blue mask
498 * 0x0000ff00: Green mask
499 * 0x000000ff: Red mask
501 *dst_color = 0;
502 *dst_color |= (src_color & 0xff00ff00u); /* Alpha Green */
503 *dst_color |= (src_color & 0x00ff0000u) >> 16; /* Red */
504 *dst_color |= (src_color & 0x000000ffu) << 16; /* Blue */
506 return sizeof(*dst_color);
509 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4 *p)
511 /* rhw conversion like in position_float4(). */
512 if (p->w != 1.0f && p->w != 0.0f)
514 float w = 1.0f / p->w;
515 p->x *= w;
516 p->y *= w;
517 p->z *= w;
518 p->w = w;
521 return sizeof(*p);
524 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
526 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
528 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
530 return refcount;
533 /* Context activation is done by the caller. */
534 static void wined3d_buffer_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
535 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
537 const struct wined3d_gl_info *gl_info = context->gl_info;
538 const struct wined3d_map_range *range;
540 buffer_bind(buffer, context);
542 while (range_count--)
544 range = &ranges[range_count];
545 GL_EXTCALL(glBufferSubData(buffer->buffer_type_hint,
546 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
548 checkGLcall("glBufferSubData");
551 static void buffer_conversion_upload(struct wined3d_buffer *buffer, struct wined3d_context *context)
553 unsigned int i, j, range_idx, start, end, vertex_count;
554 BYTE *data;
556 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
558 ERR("Failed to load system memory.\n");
559 return;
561 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
563 /* Now for each vertex in the buffer that needs conversion. */
564 vertex_count = buffer->resource.size / buffer->stride;
566 if (!(data = HeapAlloc(GetProcessHeap(), 0, buffer->resource.size)))
568 ERR("Out of memory.\n");
569 return;
572 for (range_idx = 0; range_idx < buffer->modified_areas; ++range_idx)
574 start = buffer->maps[range_idx].offset;
575 end = start + buffer->maps[range_idx].size;
577 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
578 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertex_count); ++i)
580 for (j = 0; j < buffer->stride;)
582 switch (buffer->conversion_map[j])
584 case CONV_NONE:
585 /* Done already */
586 j += sizeof(DWORD);
587 break;
588 case CONV_D3DCOLOR:
589 j += fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
590 break;
591 case CONV_POSITIONT:
592 j += fixup_transformed_pos((struct wined3d_vec4 *) (data + i * buffer->stride + j));
593 break;
594 default:
595 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer->conversion_map[j]);
596 ++j;
602 wined3d_buffer_upload_ranges(buffer, context, data, 0, buffer->modified_areas, buffer->maps);
604 HeapFree(GetProcessHeap(), 0, data);
607 static BOOL wined3d_buffer_prepare_location(struct wined3d_buffer *buffer,
608 struct wined3d_context *context, DWORD location)
610 switch (location)
612 case WINED3D_LOCATION_SYSMEM:
613 if (buffer->resource.heap_memory)
614 return TRUE;
616 if (!wined3d_resource_allocate_sysmem(&buffer->resource))
618 ERR("Failed to allocate system memory.\n");
619 return FALSE;
621 return TRUE;
623 case WINED3D_LOCATION_BUFFER:
624 if (buffer->buffer_object)
625 return TRUE;
627 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
629 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer);
630 return FALSE;
632 return buffer_create_buffer_object(buffer, context);
634 default:
635 ERR("Invalid location %s.\n", wined3d_debug_location(location));
636 return FALSE;
640 BOOL wined3d_buffer_load_location(struct wined3d_buffer *buffer,
641 struct wined3d_context *context, DWORD location)
643 const struct wined3d_gl_info *gl_info = context->gl_info;
645 TRACE("buffer %p, context %p, location %s.\n",
646 buffer, context, wined3d_debug_location(location));
648 if (buffer->locations & location)
650 TRACE("Location (%#x) is already up to date.\n", location);
651 return TRUE;
654 if (!buffer->locations)
656 ERR("Buffer %p does not have any up to date location.\n", buffer);
657 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_DISCARDED);
658 return wined3d_buffer_load_location(buffer, context, location);
661 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer->locations));
663 if (!wined3d_buffer_prepare_location(buffer, context, location))
664 return FALSE;
666 if (buffer->locations & WINED3D_LOCATION_DISCARDED)
668 TRACE("Buffer previously discarded, nothing to do.\n");
669 wined3d_buffer_validate_location(buffer, location);
670 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_DISCARDED);
671 return TRUE;
674 switch (location)
676 case WINED3D_LOCATION_SYSMEM:
677 buffer_bind(buffer, context);
678 GL_EXTCALL(glGetBufferSubData(buffer->buffer_type_hint, 0, buffer->resource.size,
679 buffer->resource.heap_memory));
680 checkGLcall("buffer download");
681 break;
683 case WINED3D_LOCATION_BUFFER:
684 if (!buffer->conversion_map)
685 wined3d_buffer_upload_ranges(buffer, context, buffer->resource.heap_memory,
686 0, buffer->modified_areas, buffer->maps);
687 else
688 buffer_conversion_upload(buffer, context);
689 break;
691 default:
692 ERR("Invalid location %s.\n", wined3d_debug_location(location));
693 return FALSE;
696 wined3d_buffer_validate_location(buffer, location);
697 if (buffer->resource.heap_memory && location == WINED3D_LOCATION_BUFFER
698 && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
699 wined3d_buffer_evict_sysmem(buffer);
701 return TRUE;
704 /* Context activation is done by the caller. */
705 BYTE *wined3d_buffer_load_sysmem(struct wined3d_buffer *buffer, struct wined3d_context *context)
707 if (wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
708 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
709 return buffer->resource.heap_memory;
712 DWORD wined3d_buffer_get_memory(struct wined3d_buffer *buffer,
713 struct wined3d_bo_address *data, DWORD locations)
715 TRACE("buffer %p, data %p, locations %s.\n",
716 buffer, data, wined3d_debug_location(locations));
718 if (locations & WINED3D_LOCATION_BUFFER)
720 data->buffer_object = buffer->buffer_object;
721 data->addr = NULL;
722 return WINED3D_LOCATION_BUFFER;
724 if (locations & WINED3D_LOCATION_SYSMEM)
726 data->buffer_object = 0;
727 data->addr = buffer->resource.heap_memory;
728 return WINED3D_LOCATION_SYSMEM;
731 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
732 data->buffer_object = 0;
733 data->addr = NULL;
734 return 0;
737 static void buffer_unload(struct wined3d_resource *resource)
739 struct wined3d_buffer *buffer = buffer_from_resource(resource);
741 TRACE("buffer %p.\n", buffer);
743 if (buffer->buffer_object)
745 struct wined3d_context *context;
747 context = context_acquire(resource->device, NULL, 0);
749 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
750 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
751 buffer_destroy_buffer_object(buffer, context);
752 buffer_clear_dirty_areas(buffer);
754 context_release(context);
756 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
757 buffer->conversion_map = NULL;
758 buffer->stride = 0;
759 buffer->conversion_stride = 0;
760 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
763 resource_unload(resource);
766 static void wined3d_buffer_drop_bo(struct wined3d_buffer *buffer)
768 buffer->flags &= ~WINED3D_BUFFER_USE_BO;
769 buffer_unload(&buffer->resource);
772 static void wined3d_buffer_destroy_object(void *object)
774 struct wined3d_buffer *buffer = object;
775 struct wined3d_context *context;
777 if (buffer->buffer_object)
779 context = context_acquire(buffer->resource.device, NULL, 0);
780 buffer_destroy_buffer_object(buffer, context);
781 context_release(context);
783 HeapFree(GetProcessHeap(), 0, buffer->conversion_map);
786 HeapFree(GetProcessHeap(), 0, buffer->maps);
787 HeapFree(GetProcessHeap(), 0, buffer);
790 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
792 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
794 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
796 if (!refcount)
798 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
799 resource_cleanup(&buffer->resource);
800 wined3d_cs_destroy_object(buffer->resource.device->cs, wined3d_buffer_destroy_object, buffer);
803 return refcount;
806 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
808 TRACE("buffer %p.\n", buffer);
810 return buffer->resource.parent;
813 /* The caller provides a context and binds the buffer */
814 static void buffer_sync_apple(struct wined3d_buffer *buffer, DWORD flags, const struct wined3d_gl_info *gl_info)
816 enum wined3d_fence_result ret;
817 HRESULT hr;
819 /* No fencing needs to be done if the app promises not to overwrite
820 * existing data. */
821 if (flags & WINED3D_MAP_NOOVERWRITE)
822 return;
824 if (flags & WINED3D_MAP_DISCARD)
826 GL_EXTCALL(glBufferData(buffer->buffer_type_hint, buffer->resource.size, NULL, buffer->buffer_object_usage));
827 checkGLcall("glBufferData");
828 return;
831 if (!buffer->fence)
833 TRACE("Creating fence for buffer %p.\n", buffer);
835 if (FAILED(hr = wined3d_fence_create(buffer->resource.device, &buffer->fence)))
837 if (hr == WINED3DERR_NOTAVAILABLE)
838 FIXME("Fences not supported, dropping async buffer locks.\n");
839 else
840 ERR("Failed to create fence, hr %#x.\n", hr);
841 goto drop_fence;
844 /* Since we don't know about old draws a glFinish is needed once */
845 gl_info->gl_ops.gl.p_glFinish();
846 return;
849 TRACE("Synchronizing buffer %p.\n", buffer);
850 ret = wined3d_fence_wait(buffer->fence, buffer->resource.device);
851 switch (ret)
853 case WINED3D_FENCE_NOT_STARTED:
854 case WINED3D_FENCE_OK:
855 /* All done */
856 return;
858 case WINED3D_FENCE_WRONG_THREAD:
859 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
860 goto drop_fence;
862 default:
863 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret);
864 goto drop_fence;
867 drop_fence:
868 if (buffer->fence)
870 wined3d_fence_destroy(buffer->fence);
871 buffer->fence = NULL;
874 gl_info->gl_ops.gl.p_glFinish();
875 GL_EXTCALL(glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
876 checkGLcall("glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
877 buffer->flags &= ~WINED3D_BUFFER_APPLESYNC;
880 static void buffer_mark_used(struct wined3d_buffer *buffer)
882 buffer->flags &= ~WINED3D_BUFFER_DISCARD;
885 /* Context activation is done by the caller. */
886 void wined3d_buffer_load(struct wined3d_buffer *buffer, struct wined3d_context *context,
887 const struct wined3d_state *state)
889 const struct wined3d_gl_info *gl_info = context->gl_info;
890 BOOL decl_changed = FALSE;
892 TRACE("buffer %p.\n", buffer);
894 if (buffer->resource.map_count)
896 WARN("Buffer is mapped, skipping preload.\n");
897 return;
900 buffer_mark_used(buffer);
902 /* TODO: Make converting independent from VBOs */
903 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
905 /* Not doing any conversion */
906 return;
909 if (!wined3d_buffer_prepare_location(buffer, context, WINED3D_LOCATION_BUFFER))
911 ERR("Failed to prepare buffer location.\n");
912 return;
915 /* Reading the declaration makes only sense if we have valid state information
916 * (i.e., if this function is called during draws). */
917 if (state)
919 DWORD fixup_flags = 0;
921 if (!use_vs(state))
923 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && !context->d3d_info->ffp_generic_attributes)
924 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
925 if (!context->d3d_info->xyzrhw)
926 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
929 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
930 buffer->flags |= WINED3D_BUFFER_HASDESC;
933 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
935 ++buffer->draw_count;
936 if (buffer->draw_count > VB_RESETDECLCHANGE)
937 buffer->decl_change_count = 0;
938 if (buffer->draw_count > VB_RESETFULLCONVS)
939 buffer->full_conversion_count = 0;
940 return;
943 /* If applications change the declaration over and over, reconverting all the time is a huge
944 * performance hit. So count the declaration changes and release the VBO if there are too many
945 * of them (and thus stop converting)
947 if (decl_changed)
949 ++buffer->decl_change_count;
950 buffer->draw_count = 0;
952 if (buffer->decl_change_count > VB_MAXDECLCHANGES
953 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
955 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
956 wined3d_buffer_drop_bo(buffer);
957 return;
960 /* The declaration changed, reload the whole buffer. */
961 WARN("Reloading buffer because of a vertex declaration change.\n");
962 buffer_invalidate_bo_range(buffer, 0, 0);
964 else
966 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
967 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
968 * decl changes and reset the decl change count after a specific number of them
970 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
972 ++buffer->full_conversion_count;
973 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
975 FIXME("Too many full buffer conversions, stopping converting.\n");
976 wined3d_buffer_drop_bo(buffer);
977 return;
980 else
982 ++buffer->draw_count;
983 if (buffer->draw_count > VB_RESETDECLCHANGE)
984 buffer->decl_change_count = 0;
985 if (buffer->draw_count > VB_RESETFULLCONVS)
986 buffer->full_conversion_count = 0;
990 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
991 ERR("Failed to load buffer location.\n");
994 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
996 TRACE("buffer %p.\n", buffer);
998 return &buffer->resource;
1001 static HRESULT wined3d_buffer_map(struct wined3d_buffer *buffer, UINT offset, UINT size, BYTE **data, DWORD flags)
1003 struct wined3d_device *device = buffer->resource.device;
1004 struct wined3d_context *context;
1005 LONG count;
1006 BYTE *base;
1008 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer, offset, size, data, flags);
1010 count = ++buffer->resource.map_count;
1012 if (buffer->buffer_object)
1014 unsigned int dirty_offset = offset, dirty_size = size;
1016 /* DISCARD invalidates the entire buffer, regardless of the specified
1017 * offset and size. Some applications also depend on the entire buffer
1018 * being uploaded in that case. Two such applications are Port Royale
1019 * and Darkstar One. */
1020 if (flags & WINED3D_MAP_DISCARD)
1022 dirty_offset = 0;
1023 dirty_size = 0;
1026 if (!(flags & (WINED3D_MAP_NOOVERWRITE | WINED3D_MAP_DISCARD | WINED3D_MAP_READONLY))
1027 || ((flags & WINED3D_MAP_READONLY) && (buffer->locations & WINED3D_LOCATION_SYSMEM))
1028 || buffer->flags & WINED3D_BUFFER_PIN_SYSMEM)
1030 if (!(buffer->locations & WINED3D_LOCATION_SYSMEM))
1032 context = context_acquire(device, NULL, 0);
1033 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
1034 context_release(context);
1037 if (!(flags & WINED3D_MAP_READONLY))
1038 wined3d_buffer_invalidate_range(buffer, WINED3D_LOCATION_BUFFER, dirty_offset, dirty_size);
1040 else
1042 const struct wined3d_gl_info *gl_info;
1044 context = context_acquire(device, NULL, 0);
1045 gl_info = context->gl_info;
1047 if (flags & WINED3D_MAP_DISCARD)
1048 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_BUFFER);
1049 else
1050 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
1052 if (!(flags & WINED3D_MAP_READONLY))
1053 buffer_invalidate_bo_range(buffer, dirty_offset, dirty_size);
1055 if ((flags & WINED3D_MAP_DISCARD) && buffer->resource.heap_memory)
1056 wined3d_buffer_evict_sysmem(buffer);
1058 if (count == 1)
1060 buffer_bind(buffer, context);
1062 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1063 * multitexture fill rate test seems to depend on this. When
1064 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1065 * driver is free to discard the previous contents of the
1066 * buffer. The r600g driver only does this when the buffer is
1067 * currently in use, while the proprietary NVIDIA driver
1068 * appears to do this unconditionally. */
1069 if (buffer->flags & WINED3D_BUFFER_DISCARD)
1070 flags &= ~WINED3D_MAP_DISCARD;
1072 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1074 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
1075 buffer->map_ptr = GL_EXTCALL(glMapBufferRange(buffer->buffer_type_hint,
1076 0, buffer->resource.size, mapflags));
1077 checkGLcall("glMapBufferRange");
1079 else
1081 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1082 buffer_sync_apple(buffer, flags, gl_info);
1083 buffer->map_ptr = GL_EXTCALL(glMapBuffer(buffer->buffer_type_hint,
1084 GL_READ_WRITE));
1085 checkGLcall("glMapBuffer");
1088 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
1090 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
1092 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1093 checkGLcall("glUnmapBuffer");
1094 buffer->map_ptr = NULL;
1096 if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
1098 /* The extra copy is more expensive than not using VBOs at
1099 * all on the Nvidia Linux driver, which is the only driver
1100 * that returns unaligned pointers.
1102 TRACE("Dynamic buffer, dropping VBO.\n");
1103 wined3d_buffer_drop_bo(buffer);
1105 else
1107 TRACE("Falling back to doublebuffered operation.\n");
1108 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
1109 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1111 TRACE("New pointer is %p.\n", buffer->resource.heap_memory);
1115 context_release(context);
1118 if (flags & WINED3D_MAP_DISCARD)
1119 buffer->flags |= WINED3D_BUFFER_DISCARD;
1122 base = buffer->map_ptr ? buffer->map_ptr : buffer->resource.heap_memory;
1123 *data = base + offset;
1125 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, base, offset);
1126 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1128 return WINED3D_OK;
1131 static void wined3d_buffer_unmap(struct wined3d_buffer *buffer)
1133 ULONG i;
1135 TRACE("buffer %p.\n", buffer);
1137 /* In the case that the number of Unmap calls > the
1138 * number of Map calls, d3d returns always D3D_OK.
1139 * This is also needed to prevent Map from returning garbage on
1140 * the next call (this will happen if the lock_count is < 0). */
1141 if (!buffer->resource.map_count)
1143 WARN("Unmap called without a previous map call.\n");
1144 return;
1147 if (--buffer->resource.map_count)
1149 /* Delay loading the buffer until everything is unlocked */
1150 TRACE("Ignoring unmap.\n");
1151 return;
1154 if (buffer->map_ptr)
1156 struct wined3d_device *device = buffer->resource.device;
1157 const struct wined3d_gl_info *gl_info;
1158 struct wined3d_context *context;
1160 context = context_acquire(device, NULL, 0);
1161 gl_info = context->gl_info;
1163 buffer_bind(buffer, context);
1165 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1167 for (i = 0; i < buffer->modified_areas; ++i)
1169 GL_EXTCALL(glFlushMappedBufferRange(buffer->buffer_type_hint,
1170 buffer->maps[i].offset, buffer->maps[i].size));
1171 checkGLcall("glFlushMappedBufferRange");
1174 else if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1176 for (i = 0; i < buffer->modified_areas; ++i)
1178 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer->buffer_type_hint,
1179 buffer->maps[i].offset, buffer->maps[i].size));
1180 checkGLcall("glFlushMappedBufferRangeAPPLE");
1184 GL_EXTCALL(glUnmapBuffer(buffer->buffer_type_hint));
1185 if (wined3d_settings.strict_draw_ordering)
1186 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1187 context_release(context);
1189 buffer_clear_dirty_areas(buffer);
1190 buffer->map_ptr = NULL;
1194 void wined3d_buffer_copy(struct wined3d_buffer *dst_buffer, unsigned int dst_offset,
1195 struct wined3d_buffer *src_buffer, unsigned int src_offset, unsigned int size)
1197 struct wined3d_bo_address dst, src;
1198 struct wined3d_context *context;
1199 DWORD dst_location;
1201 buffer_mark_used(dst_buffer);
1202 buffer_mark_used(src_buffer);
1204 dst_location = wined3d_buffer_get_memory(dst_buffer, &dst, dst_buffer->locations);
1205 dst.addr += dst_offset;
1207 wined3d_buffer_get_memory(src_buffer, &src, src_buffer->locations);
1208 src.addr += src_offset;
1210 context = context_acquire(dst_buffer->resource.device, NULL, 0);
1211 context_copy_bo_address(context, &dst, dst_buffer->buffer_type_hint,
1212 &src, src_buffer->buffer_type_hint, size);
1213 context_release(context);
1215 wined3d_buffer_invalidate_range(dst_buffer, ~dst_location, dst_offset, size);
1218 void wined3d_buffer_upload_data(struct wined3d_buffer *buffer, struct wined3d_context *context,
1219 const struct wined3d_box *box, const void *data)
1221 struct wined3d_map_range range;
1223 if (box)
1225 range.offset = box->left;
1226 range.size = box->right - box->left;
1228 else
1230 range.offset = 0;
1231 range.size = buffer->resource.size;
1234 wined3d_buffer_upload_ranges(buffer, context, data, range.offset, 1, &range);
1237 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1239 return wined3d_buffer_incref(buffer_from_resource(resource));
1242 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1244 return wined3d_buffer_decref(buffer_from_resource(resource));
1247 static void buffer_resource_preload(struct wined3d_resource *resource)
1249 struct wined3d_context *context;
1251 context = context_acquire(resource->device, NULL, 0);
1252 wined3d_buffer_load(buffer_from_resource(resource), context, NULL);
1253 context_release(context);
1256 static HRESULT buffer_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1257 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1259 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1260 UINT offset, size;
1262 if (sub_resource_idx)
1264 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1265 return E_INVALIDARG;
1268 if (box)
1270 offset = box->left;
1271 size = box->right - box->left;
1273 else
1275 offset = size = 0;
1278 map_desc->row_pitch = map_desc->slice_pitch = buffer->desc.byte_width;
1279 return wined3d_buffer_map(buffer, offset, size, (BYTE **)&map_desc->data, flags);
1282 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1284 if (sub_resource_idx)
1286 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1287 return E_INVALIDARG;
1290 wined3d_buffer_unmap(buffer_from_resource(resource));
1291 return WINED3D_OK;
1294 static const struct wined3d_resource_ops buffer_resource_ops =
1296 buffer_resource_incref,
1297 buffer_resource_decref,
1298 buffer_resource_preload,
1299 buffer_unload,
1300 buffer_resource_sub_resource_map,
1301 buffer_resource_sub_resource_unmap,
1304 static GLenum buffer_type_hint_from_bind_flags(const struct wined3d_gl_info *gl_info,
1305 unsigned int bind_flags)
1307 if (bind_flags == WINED3D_BIND_INDEX_BUFFER)
1308 return GL_ELEMENT_ARRAY_BUFFER;
1310 if (bind_flags & (WINED3D_BIND_SHADER_RESOURCE | WINED3D_BIND_UNORDERED_ACCESS)
1311 && gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1312 return GL_TEXTURE_BUFFER;
1314 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
1315 return GL_UNIFORM_BUFFER;
1317 if (bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1318 return GL_TRANSFORM_FEEDBACK_BUFFER;
1320 if (bind_flags & ~(WINED3D_BIND_VERTEX_BUFFER | WINED3D_BIND_INDEX_BUFFER))
1321 FIXME("Unhandled bind flags %#x.\n", bind_flags);
1323 return GL_ARRAY_BUFFER;
1326 static HRESULT buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1327 UINT size, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, unsigned int bind_flags,
1328 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops)
1330 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1331 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, usage);
1332 BOOL dynamic_buffer_ok;
1333 HRESULT hr;
1335 if (!size)
1337 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1338 return E_INVALIDARG;
1341 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER && size & (WINED3D_CONSTANT_BUFFER_ALIGNMENT - 1))
1343 WARN("Size %#x is not suitably aligned for constant buffers.\n", size);
1344 return E_INVALIDARG;
1347 if (data && !data->data)
1349 WARN("Invalid sub-resource data specified.\n");
1350 return E_INVALIDARG;
1353 hr = resource_init(&buffer->resource, device, WINED3D_RTYPE_BUFFER, format,
1354 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, size, 1, 1, size, parent, parent_ops, &buffer_resource_ops);
1355 if (FAILED(hr))
1357 WARN("Failed to initialize resource, hr %#x.\n", hr);
1358 return hr;
1360 buffer->buffer_type_hint = buffer_type_hint_from_bind_flags(gl_info, bind_flags);
1361 buffer->bind_flags = bind_flags;
1362 buffer->locations = WINED3D_LOCATION_SYSMEM;
1364 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1365 buffer, buffer->resource.size, buffer->resource.usage,
1366 debug_d3dformat(buffer->resource.format->id), buffer->resource.heap_memory);
1368 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING || pool == WINED3D_POOL_MANAGED)
1370 /* SWvp and managed buffers always return the same pointer in buffer
1371 * maps and retain data in DISCARD maps. Keep a system memory copy of
1372 * the buffer to provide the same behavior to the application. */
1373 TRACE("Using doublebuffer mode.\n");
1374 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1377 /* Observations show that draw_primitive_immediate_mode() is faster on
1378 * dynamic vertex buffers than converting + draw_primitive_arrays().
1379 * (Half-Life 2 and others.) */
1380 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1382 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1384 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1386 else if (buffer->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1388 TRACE("Not creating a BO because the buffer is in system memory.\n");
1390 else if (!dynamic_buffer_ok && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
1392 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1394 else
1396 buffer->flags |= WINED3D_BUFFER_USE_BO;
1399 if (!(buffer->maps = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->maps))))
1401 ERR("Out of memory.\n");
1402 buffer_unload(&buffer->resource);
1403 resource_cleanup(&buffer->resource);
1404 wined3d_resource_wait_idle(&buffer->resource);
1405 return E_OUTOFMEMORY;
1407 buffer->maps_size = 1;
1409 if (data)
1410 wined3d_device_update_sub_resource(device, &buffer->resource,
1411 0, NULL, data->data, data->row_pitch, data->slice_pitch);
1413 return WINED3D_OK;
1416 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1417 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops,
1418 struct wined3d_buffer **buffer)
1420 struct wined3d_buffer *object;
1421 HRESULT hr;
1423 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1424 device, desc, data, parent, parent_ops, buffer);
1426 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1427 return E_OUTOFMEMORY;
1429 FIXME("Ignoring access flags (pool).\n");
1431 if (FAILED(hr = buffer_init(object, device, desc->byte_width, desc->usage, WINED3DFMT_UNKNOWN,
1432 WINED3D_POOL_MANAGED, desc->bind_flags, data, parent, parent_ops)))
1434 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1435 HeapFree(GetProcessHeap(), 0, object);
1436 return hr;
1438 object->desc = *desc;
1440 TRACE("Created buffer %p.\n", object);
1442 *buffer = object;
1444 return WINED3D_OK;
1447 HRESULT CDECL wined3d_buffer_create_vb(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1448 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1450 struct wined3d_buffer *object;
1451 HRESULT hr;
1453 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1454 device, size, usage, pool, parent, parent_ops, buffer);
1456 if (pool == WINED3D_POOL_SCRATCH)
1458 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1459 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1460 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1461 *buffer = NULL;
1462 return WINED3DERR_INVALIDCALL;
1465 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1466 if (!object)
1468 *buffer = NULL;
1469 return WINED3DERR_OUTOFVIDEOMEMORY;
1472 hr = buffer_init(object, device, size, usage, WINED3DFMT_UNKNOWN,
1473 pool, WINED3D_BIND_VERTEX_BUFFER, NULL, parent, parent_ops);
1474 if (FAILED(hr))
1476 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1477 HeapFree(GetProcessHeap(), 0, object);
1478 return hr;
1481 TRACE("Created buffer %p.\n", object);
1482 *buffer = object;
1484 return WINED3D_OK;
1487 HRESULT CDECL wined3d_buffer_create_ib(struct wined3d_device *device, UINT size, DWORD usage, enum wined3d_pool pool,
1488 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_buffer **buffer)
1490 struct wined3d_buffer *object;
1491 HRESULT hr;
1493 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1494 device, size, usage, pool, parent, parent_ops, buffer);
1496 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1497 if (!object)
1499 *buffer = NULL;
1500 return WINED3DERR_OUTOFVIDEOMEMORY;
1503 hr = buffer_init(object, device, size, usage | WINED3DUSAGE_STATICDECL,
1504 WINED3DFMT_UNKNOWN, pool, WINED3D_BIND_INDEX_BUFFER, NULL,
1505 parent, parent_ops);
1506 if (FAILED(hr))
1508 WARN("Failed to initialize buffer, hr %#x\n", hr);
1509 HeapFree(GetProcessHeap(), 0, object);
1510 return hr;
1513 TRACE("Created buffer %p.\n", object);
1514 *buffer = object;
1516 return WINED3D_OK;