wined3d: Store resource bind flags in the wined3d_resource structure.
[wine.git] / dlls / wined3d / buffer.c
blob4c90d820a890fd4334454d560457569ed65cc17a
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 wined3d_buffer_gl_bind(struct wined3d_buffer_gl *buffer_gl, struct wined3d_context *context)
138 context_bind_bo(context, buffer_gl->buffer_type_hint, buffer_gl->buffer_object);
141 /* Context activation is done by the caller. */
142 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl *buffer_gl,
143 struct wined3d_context *context)
145 struct wined3d_resource *resource = &buffer_gl->b.resource;
146 const struct wined3d_gl_info *gl_info = context->gl_info;
148 if (!buffer_gl->buffer_object)
149 return;
151 /* The stream source state handler might have read the memory of the
152 * vertex buffer already and got the memory in the vbo which is not
153 * valid any longer. Dirtify the stream source to force a reload. This
154 * happens only once per changed vertexbuffer and should occur rather
155 * rarely. */
156 if (resource->bind_count)
158 if (resource->bind_flags & WINED3D_BIND_VERTEX_BUFFER)
159 device_invalidate_state(resource->device, STATE_STREAMSRC);
160 if (resource->bind_flags & WINED3D_BIND_INDEX_BUFFER)
161 device_invalidate_state(resource->device, STATE_INDEXBUFFER);
162 if (resource->bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
164 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX));
165 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL));
166 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN));
167 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY));
168 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL));
169 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE));
171 if (resource->bind_flags & WINED3D_BIND_STREAM_OUTPUT)
173 device_invalidate_state(resource->device, STATE_STREAM_OUTPUT);
174 if (context->transform_feedback_active)
176 /* We have to make sure that transform feedback is not active
177 * when deleting a potentially bound transform feedback buffer.
178 * This may happen when the device is being destroyed. */
179 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl);
180 context_end_transform_feedback(context);
185 GL_EXTCALL(glDeleteBuffers(1, &buffer_gl->buffer_object));
186 checkGLcall("glDeleteBuffers");
187 buffer_gl->buffer_object = 0;
189 if (buffer_gl->b.fence)
191 wined3d_fence_destroy(buffer_gl->b.fence);
192 buffer_gl->b.fence = NULL;
194 buffer_gl->b.flags &= ~WINED3D_BUFFER_APPLESYNC;
197 /* Context activation is done by the caller. */
198 static BOOL wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl *buffer_gl, struct wined3d_context *context)
200 const struct wined3d_gl_info *gl_info = context->gl_info;
201 GLenum gl_usage = GL_STATIC_DRAW;
202 GLenum error;
204 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
205 buffer_gl, debug_d3dusage(buffer_gl->b.resource.usage));
207 /* Make sure that the gl error is cleared. Do not use checkGLcall
208 * here because checkGLcall just prints a fixme and continues. However,
209 * if an error during VBO creation occurs we can fall back to non-VBO operation
210 * with full functionality(but performance loss).
212 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
214 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
215 * The vertex declaration from the device determines how the data in the
216 * buffer is interpreted. This means that on each draw call the buffer has
217 * to be verified to check if the rhw and color values are in the correct
218 * format. */
220 GL_EXTCALL(glGenBuffers(1, &buffer_gl->buffer_object));
221 error = gl_info->gl_ops.gl.p_glGetError();
222 if (!buffer_gl->buffer_object || error != GL_NO_ERROR)
224 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error), error);
225 goto fail;
228 wined3d_buffer_gl_bind(buffer_gl, context);
229 error = gl_info->gl_ops.gl.p_glGetError();
230 if (error != GL_NO_ERROR)
232 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error), error);
233 goto fail;
236 if (buffer_gl->b.resource.usage & WINED3DUSAGE_DYNAMIC)
238 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
239 gl_usage = GL_STREAM_DRAW_ARB;
241 if (gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
243 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint,
244 GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
245 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint,
246 GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
247 checkGLcall("glBufferParameteriAPPLE");
248 buffer_gl->b.flags |= WINED3D_BUFFER_APPLESYNC;
250 /* No setup is needed here for GL_ARB_map_buffer_range. */
253 GL_EXTCALL(glBufferData(buffer_gl->buffer_type_hint, buffer_gl->b.resource.size, NULL, gl_usage));
254 error = gl_info->gl_ops.gl.p_glGetError();
255 if (error != GL_NO_ERROR)
257 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error), error);
258 goto fail;
261 buffer_gl->buffer_object_usage = gl_usage;
262 buffer_invalidate_bo_range(&buffer_gl->b, 0, 0);
264 return TRUE;
266 fail:
267 /* Clean up all BO init, but continue because we can work without a BO :-) */
268 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
269 buffer_gl->b.flags &= ~WINED3D_BUFFER_USE_BO;
270 wined3d_buffer_gl_destroy_buffer_object(buffer_gl, context);
271 buffer_clear_dirty_areas(&buffer_gl->b);
272 return FALSE;
275 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *buffer,
276 const enum wined3d_buffer_conversion_type conversion_type,
277 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
279 const struct wined3d_format *format = attrib->format;
280 BOOL ret = FALSE;
281 unsigned int i;
282 DWORD_PTR data;
284 /* Check for some valid situations which cause us pain. One is if the buffer is used for
285 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
286 * with different strides. In the 2nd case we might have to drop conversion entirely,
287 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
289 if (!attrib->stride)
291 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
292 debug_d3dformat(format->id));
294 else if (attrib->stride != *stride_this_run && *stride_this_run)
296 FIXME("Got two concurrent strides, %d and %d.\n", attrib->stride, *stride_this_run);
298 else
300 *stride_this_run = attrib->stride;
301 if (buffer->stride != *stride_this_run)
303 /* We rely that this happens only on the first converted attribute that is found,
304 * if at all. See above check
306 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
307 buffer->stride = *stride_this_run;
308 heap_free(buffer->conversion_map);
309 buffer->conversion_map = heap_calloc(buffer->stride, sizeof(*buffer->conversion_map));
310 ret = TRUE;
314 data = ((DWORD_PTR)attrib->data.addr) % buffer->stride;
315 for (i = 0; i < format->byte_count; ++i)
317 DWORD_PTR idx = (data + i) % buffer->stride;
318 if (buffer->conversion_map[idx] != conversion_type)
320 TRACE("Byte %lu in vertex changed:\n", idx);
321 TRACE(" It was type %#x, is %#x now.\n", buffer->conversion_map[idx], conversion_type);
322 ret = TRUE;
323 buffer->conversion_map[idx] = conversion_type;
327 return ret;
330 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
331 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
333 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
334 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
336 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
337 enum wined3d_format_id format;
338 BOOL ret = FALSE;
340 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
341 * there, on nonexistent attribs the vbo is 0.
343 if (!(si->use_map & (1u << attrib_idx))
344 || state->streams[attrib->stream_idx].buffer != This)
345 return FALSE;
347 format = attrib->format->id;
348 /* Look for newly appeared conversion */
349 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
351 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
353 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
355 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
357 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
358 return FALSE;
361 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
363 else if (This->conversion_map)
365 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
368 return ret;
371 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
372 const struct wined3d_state *state, DWORD fixup_flags)
374 UINT stride_this_run = 0;
375 BOOL ret = FALSE;
377 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
378 * Once we have our declaration there is no need to look it up again. Index buffers also never need
379 * conversion, so once the (empty) conversion structure is created don't bother checking again
381 if (This->flags & WINED3D_BUFFER_HASDESC)
383 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
386 if (!fixup_flags)
388 TRACE("No fixup required.\n");
389 if(This->conversion_map)
391 heap_free(This->conversion_map);
392 This->conversion_map = NULL;
393 This->stride = 0;
394 return TRUE;
397 return FALSE;
400 TRACE("Finding vertex buffer conversion information\n");
401 /* Certain declaration types need some fixups before we can pass them to
402 * opengl. This means D3DCOLOR attributes with fixed function vertex
403 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
404 * GL_ARB_half_float_vertex is not supported.
406 * Note for d3d8 and d3d9:
407 * The vertex buffer FVF doesn't help with finding them, we have to use
408 * the decoded vertex declaration and pick the things that concern the
409 * current buffer. A problem with this is that this can change between
410 * draws, so we have to validate the information and reprocess the buffer
411 * if it changes, and avoid false positives for performance reasons.
412 * WineD3D doesn't even know the vertex buffer any more, it is managed
413 * by the client libraries and passed to SetStreamSource and ProcessVertices
414 * as needed.
416 * We have to distinguish between vertex shaders and fixed function to
417 * pick the way we access the strided vertex information.
419 * This code sets up a per-byte array with the size of the detected
420 * stride of the arrays in the buffer. For each byte we have a field
421 * that marks the conversion needed on this byte. For example, the
422 * following declaration with fixed function vertex processing:
424 * POSITIONT, FLOAT4
425 * NORMAL, FLOAT3
426 * DIFFUSE, FLOAT16_4
427 * SPECULAR, D3DCOLOR
429 * Will result in
430 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
431 * [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]
433 * Where in this example map P means 4 component position conversion, 0
434 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
435 * conversion (red / blue swizzle).
437 * If we're doing conversion and the stride changes we have to reconvert
438 * the whole buffer. Note that we do not mind if the semantic changes,
439 * we only care for the conversion type. So if the NORMAL is replaced
440 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
441 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
442 * conversion types depend on the semantic as well, for example a FLOAT4
443 * texcoord needs no conversion while a FLOAT4 positiont needs one
446 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
447 fixup_flags, &stride_this_run) || ret;
448 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
450 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDWEIGHT,
451 fixup_flags, &stride_this_run) || ret;
452 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDINDICES,
453 fixup_flags, &stride_this_run) || ret;
454 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
455 fixup_flags, &stride_this_run) || ret;
456 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
457 fixup_flags, &stride_this_run) || ret;
458 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
459 fixup_flags, &stride_this_run) || ret;
460 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
461 fixup_flags, &stride_this_run) || ret;
462 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
463 fixup_flags, &stride_this_run) || ret;
464 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
465 fixup_flags, &stride_this_run) || ret;
466 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
467 fixup_flags, &stride_this_run) || ret;
468 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
469 fixup_flags, &stride_this_run) || ret;
470 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
471 fixup_flags, &stride_this_run) || ret;
472 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
473 fixup_flags, &stride_this_run) || ret;
474 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
475 fixup_flags, &stride_this_run) || ret;
477 if (!stride_this_run && This->conversion_map)
479 /* Sanity test */
480 if (!ret)
481 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
482 heap_free(This->conversion_map);
483 This->conversion_map = NULL;
484 This->stride = 0;
487 if (ret) TRACE("Conversion information changed\n");
489 return ret;
492 static inline unsigned int fixup_d3dcolor(DWORD *dst_color)
494 DWORD src_color = *dst_color;
496 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
497 * endianness. If we want this to work on big-endian machines as well we
498 * have to consider more things.
500 * 0xff000000: Alpha mask
501 * 0x00ff0000: Blue mask
502 * 0x0000ff00: Green mask
503 * 0x000000ff: Red mask
505 *dst_color = 0;
506 *dst_color |= (src_color & 0xff00ff00u); /* Alpha Green */
507 *dst_color |= (src_color & 0x00ff0000u) >> 16; /* Red */
508 *dst_color |= (src_color & 0x000000ffu) << 16; /* Blue */
510 return sizeof(*dst_color);
513 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4 *p)
515 /* rhw conversion like in position_float4(). */
516 if (p->w != 1.0f && p->w != 0.0f)
518 float w = 1.0f / p->w;
519 p->x *= w;
520 p->y *= w;
521 p->z *= w;
522 p->w = w;
525 return sizeof(*p);
528 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
530 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
532 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
534 return refcount;
537 /* Context activation is done by the caller. */
538 static void wined3d_buffer_gl_upload_ranges(struct wined3d_buffer_gl *buffer_gl, struct wined3d_context *context,
539 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
541 const struct wined3d_gl_info *gl_info = context->gl_info;
542 const struct wined3d_map_range *range;
544 wined3d_buffer_gl_bind(buffer_gl, context);
546 while (range_count--)
548 range = &ranges[range_count];
549 GL_EXTCALL(glBufferSubData(buffer_gl->buffer_type_hint,
550 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
552 checkGLcall("glBufferSubData");
555 static void buffer_conversion_upload(struct wined3d_buffer *buffer, struct wined3d_context *context)
557 unsigned int i, j, range_idx, start, end, vertex_count;
558 BYTE *data;
560 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
562 ERR("Failed to load system memory.\n");
563 return;
565 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
567 /* Now for each vertex in the buffer that needs conversion. */
568 vertex_count = buffer->resource.size / buffer->stride;
570 if (!(data = heap_alloc(buffer->resource.size)))
572 ERR("Out of memory.\n");
573 return;
576 for (range_idx = 0; range_idx < buffer->modified_areas; ++range_idx)
578 start = buffer->maps[range_idx].offset;
579 end = start + buffer->maps[range_idx].size;
581 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
582 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertex_count); ++i)
584 for (j = 0; j < buffer->stride;)
586 switch (buffer->conversion_map[j])
588 case CONV_NONE:
589 /* Done already */
590 j += sizeof(DWORD);
591 break;
592 case CONV_D3DCOLOR:
593 j += fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
594 break;
595 case CONV_POSITIONT:
596 j += fixup_transformed_pos((struct wined3d_vec4 *) (data + i * buffer->stride + j));
597 break;
598 default:
599 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer->conversion_map[j]);
600 ++j;
606 wined3d_buffer_gl_upload_ranges(wined3d_buffer_gl(buffer),
607 context, data, 0, buffer->modified_areas, buffer->maps);
609 heap_free(data);
612 static BOOL wined3d_buffer_prepare_location(struct wined3d_buffer *buffer,
613 struct wined3d_context *context, DWORD location)
615 switch (location)
617 case WINED3D_LOCATION_SYSMEM:
618 if (buffer->resource.heap_memory)
619 return TRUE;
621 if (!wined3d_resource_allocate_sysmem(&buffer->resource))
622 return FALSE;
623 return TRUE;
625 case WINED3D_LOCATION_BUFFER:
626 if (wined3d_buffer_gl(buffer)->buffer_object)
627 return TRUE;
629 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
631 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer);
632 return FALSE;
634 return wined3d_buffer_gl_create_buffer_object(wined3d_buffer_gl(buffer), context);
636 default:
637 ERR("Invalid location %s.\n", wined3d_debug_location(location));
638 return FALSE;
642 BOOL wined3d_buffer_load_location(struct wined3d_buffer *buffer,
643 struct wined3d_context *context, DWORD location)
645 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
646 const struct wined3d_gl_info *gl_info = context->gl_info;
648 TRACE("buffer %p, context %p, location %s.\n",
649 buffer, context, wined3d_debug_location(location));
651 if (buffer->locations & location)
653 TRACE("Location (%#x) is already up to date.\n", location);
654 return TRUE;
657 if (!buffer->locations)
659 ERR("Buffer %p does not have any up to date location.\n", buffer);
660 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_DISCARDED);
661 return wined3d_buffer_load_location(buffer, context, location);
664 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer->locations));
666 if (!wined3d_buffer_prepare_location(buffer, context, location))
667 return FALSE;
669 if (buffer->locations & WINED3D_LOCATION_DISCARDED)
671 TRACE("Buffer previously discarded, nothing to do.\n");
672 wined3d_buffer_validate_location(buffer, location);
673 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_DISCARDED);
674 return TRUE;
677 switch (location)
679 case WINED3D_LOCATION_SYSMEM:
680 wined3d_buffer_gl_bind(buffer_gl, context);
681 GL_EXTCALL(glGetBufferSubData(buffer_gl->buffer_type_hint, 0, buffer->resource.size,
682 buffer->resource.heap_memory));
683 checkGLcall("buffer download");
684 break;
686 case WINED3D_LOCATION_BUFFER:
687 if (!buffer->conversion_map)
688 wined3d_buffer_gl_upload_ranges(buffer_gl, context, buffer->resource.heap_memory,
689 0, buffer->modified_areas, buffer->maps);
690 else
691 buffer_conversion_upload(buffer, context);
692 break;
694 default:
695 ERR("Invalid location %s.\n", wined3d_debug_location(location));
696 return FALSE;
699 wined3d_buffer_validate_location(buffer, location);
700 if (buffer->resource.heap_memory && location == WINED3D_LOCATION_BUFFER
701 && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
702 wined3d_buffer_evict_sysmem(buffer);
704 return TRUE;
707 /* Context activation is done by the caller. */
708 BYTE *wined3d_buffer_load_sysmem(struct wined3d_buffer *buffer, struct wined3d_context *context)
710 if (wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
711 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
712 return buffer->resource.heap_memory;
715 DWORD wined3d_buffer_get_memory(struct wined3d_buffer *buffer,
716 struct wined3d_bo_address *data, DWORD locations)
718 TRACE("buffer %p, data %p, locations %s.\n",
719 buffer, data, wined3d_debug_location(locations));
721 if (locations & WINED3D_LOCATION_BUFFER)
723 data->buffer_object = wined3d_buffer_gl(buffer)->buffer_object;
724 data->addr = NULL;
725 return WINED3D_LOCATION_BUFFER;
727 if (locations & WINED3D_LOCATION_SYSMEM)
729 data->buffer_object = 0;
730 data->addr = buffer->resource.heap_memory;
731 return WINED3D_LOCATION_SYSMEM;
734 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
735 data->buffer_object = 0;
736 data->addr = NULL;
737 return 0;
740 static void buffer_unload(struct wined3d_resource *resource)
742 struct wined3d_buffer *buffer = buffer_from_resource(resource);
744 TRACE("buffer %p.\n", buffer);
746 if (wined3d_buffer_gl(buffer)->buffer_object)
748 struct wined3d_context *context;
750 context = context_acquire(resource->device, NULL, 0);
752 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
753 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
754 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer), context);
755 buffer_clear_dirty_areas(buffer);
757 context_release(context);
759 heap_free(buffer->conversion_map);
760 buffer->conversion_map = NULL;
761 buffer->stride = 0;
762 buffer->conversion_stride = 0;
763 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
766 resource_unload(resource);
769 static void wined3d_buffer_drop_bo(struct wined3d_buffer *buffer)
771 buffer->flags &= ~WINED3D_BUFFER_USE_BO;
772 buffer_unload(&buffer->resource);
775 static void wined3d_buffer_gl_destroy_object(void *object)
777 struct wined3d_buffer_gl *buffer_gl = object;
778 struct wined3d_context *context;
780 if (buffer_gl->buffer_object)
782 context = context_acquire(buffer_gl->b.resource.device, NULL, 0);
783 wined3d_buffer_gl_destroy_buffer_object(buffer_gl, context);
784 context_release(context);
786 heap_free(buffer_gl->b.conversion_map);
789 heap_free(buffer_gl->b.maps);
790 heap_free(buffer_gl);
793 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
795 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
797 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
799 if (!refcount)
801 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
802 resource_cleanup(&buffer->resource);
803 wined3d_cs_destroy_object(buffer->resource.device->cs,
804 wined3d_buffer_gl_destroy_object, wined3d_buffer_gl(buffer));
807 return refcount;
810 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
812 TRACE("buffer %p.\n", buffer);
814 return buffer->resource.parent;
817 /* The caller provides a context and binds the buffer */
818 static void wined3d_buffer_gl_sync_apple(struct wined3d_buffer_gl *buffer_gl,
819 DWORD flags, const struct wined3d_gl_info *gl_info)
821 enum wined3d_fence_result ret;
822 HRESULT hr;
824 /* No fencing needs to be done if the app promises not to overwrite
825 * existing data. */
826 if (flags & WINED3D_MAP_NOOVERWRITE)
827 return;
829 if (flags & WINED3D_MAP_DISCARD)
831 GL_EXTCALL(glBufferData(buffer_gl->buffer_type_hint, buffer_gl->b.resource.size,
832 NULL, buffer_gl->buffer_object_usage));
833 checkGLcall("glBufferData");
834 return;
837 if (!buffer_gl->b.fence)
839 TRACE("Creating fence for buffer %p.\n", buffer_gl);
841 if (FAILED(hr = wined3d_fence_create(buffer_gl->b.resource.device, &buffer_gl->b.fence)))
843 if (hr == WINED3DERR_NOTAVAILABLE)
844 FIXME("Fences not supported, dropping async buffer locks.\n");
845 else
846 ERR("Failed to create fence, hr %#x.\n", hr);
847 goto drop_fence;
850 /* Since we don't know about old draws a glFinish is needed once */
851 gl_info->gl_ops.gl.p_glFinish();
852 return;
855 TRACE("Synchronizing buffer %p.\n", buffer_gl);
856 ret = wined3d_fence_wait(buffer_gl->b.fence, buffer_gl->b.resource.device);
857 switch (ret)
859 case WINED3D_FENCE_NOT_STARTED:
860 case WINED3D_FENCE_OK:
861 /* All done */
862 return;
864 case WINED3D_FENCE_WRONG_THREAD:
865 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
866 goto drop_fence;
868 default:
869 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret);
870 goto drop_fence;
873 drop_fence:
874 if (buffer_gl->b.fence)
876 wined3d_fence_destroy(buffer_gl->b.fence);
877 buffer_gl->b.fence = NULL;
880 gl_info->gl_ops.gl.p_glFinish();
881 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
882 checkGLcall("glBufferParameteriAPPLE(buffer_gl->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
883 buffer_gl->b.flags &= ~WINED3D_BUFFER_APPLESYNC;
886 static void buffer_mark_used(struct wined3d_buffer *buffer)
888 buffer->flags &= ~WINED3D_BUFFER_DISCARD;
891 /* Context activation is done by the caller. */
892 void wined3d_buffer_load(struct wined3d_buffer *buffer, struct wined3d_context *context,
893 const struct wined3d_state *state)
895 const struct wined3d_gl_info *gl_info = context->gl_info;
896 BOOL decl_changed = FALSE;
898 TRACE("buffer %p.\n", buffer);
900 if (buffer->resource.map_count)
902 WARN("Buffer is mapped, skipping preload.\n");
903 return;
906 buffer_mark_used(buffer);
908 /* TODO: Make converting independent from VBOs */
909 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
911 /* Not doing any conversion */
912 return;
915 if (!wined3d_buffer_prepare_location(buffer, context, WINED3D_LOCATION_BUFFER))
917 ERR("Failed to prepare buffer location.\n");
918 return;
921 /* Reading the declaration makes only sense if we have valid state information
922 * (i.e., if this function is called during draws). */
923 if (state)
925 DWORD fixup_flags = 0;
927 if (!use_vs(state))
929 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && !context->d3d_info->ffp_generic_attributes)
930 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
931 if (!context->d3d_info->xyzrhw)
932 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
935 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
936 buffer->flags |= WINED3D_BUFFER_HASDESC;
939 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
941 ++buffer->draw_count;
942 if (buffer->draw_count > VB_RESETDECLCHANGE)
943 buffer->decl_change_count = 0;
944 if (buffer->draw_count > VB_RESETFULLCONVS)
945 buffer->full_conversion_count = 0;
946 return;
949 /* If applications change the declaration over and over, reconverting all the time is a huge
950 * performance hit. So count the declaration changes and release the VBO if there are too many
951 * of them (and thus stop converting)
953 if (decl_changed)
955 ++buffer->decl_change_count;
956 buffer->draw_count = 0;
958 if (buffer->decl_change_count > VB_MAXDECLCHANGES
959 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
961 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
962 wined3d_buffer_drop_bo(buffer);
963 return;
966 /* The declaration changed, reload the whole buffer. */
967 WARN("Reloading buffer because of a vertex declaration change.\n");
968 buffer_invalidate_bo_range(buffer, 0, 0);
970 else
972 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
973 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
974 * decl changes and reset the decl change count after a specific number of them
976 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
978 ++buffer->full_conversion_count;
979 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
981 FIXME("Too many full buffer conversions, stopping converting.\n");
982 wined3d_buffer_drop_bo(buffer);
983 return;
986 else
988 ++buffer->draw_count;
989 if (buffer->draw_count > VB_RESETDECLCHANGE)
990 buffer->decl_change_count = 0;
991 if (buffer->draw_count > VB_RESETFULLCONVS)
992 buffer->full_conversion_count = 0;
996 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
997 ERR("Failed to load buffer location.\n");
1000 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
1002 TRACE("buffer %p.\n", buffer);
1004 return &buffer->resource;
1007 static HRESULT wined3d_buffer_gl_map(struct wined3d_buffer_gl *buffer_gl,
1008 unsigned int offset, unsigned int size, BYTE **data, DWORD flags)
1010 struct wined3d_device *device = buffer_gl->b.resource.device;
1011 struct wined3d_context *context;
1012 LONG count;
1013 BYTE *base;
1015 TRACE("buffer_gl %p, offset %u, size %u, data %p, flags %#x.\n", buffer_gl, offset, size, data, flags);
1017 count = ++buffer_gl->b.resource.map_count;
1019 if (buffer_gl->buffer_object)
1021 unsigned int dirty_offset = offset, dirty_size = size;
1023 /* DISCARD invalidates the entire buffer, regardless of the specified
1024 * offset and size. Some applications also depend on the entire buffer
1025 * being uploaded in that case. Two such applications are Port Royale
1026 * and Darkstar One. */
1027 if (flags & WINED3D_MAP_DISCARD)
1029 dirty_offset = 0;
1030 dirty_size = 0;
1033 if (((flags & WINED3D_MAP_WRITE) && !(flags & (WINED3D_MAP_NOOVERWRITE | WINED3D_MAP_DISCARD)))
1034 || (!(flags & WINED3D_MAP_WRITE) && (buffer_gl->b.locations & WINED3D_LOCATION_SYSMEM))
1035 || buffer_gl->b.flags & WINED3D_BUFFER_PIN_SYSMEM)
1037 if (!(buffer_gl->b.locations & WINED3D_LOCATION_SYSMEM))
1039 context = context_acquire(device, NULL, 0);
1040 wined3d_buffer_load_location(&buffer_gl->b, context, WINED3D_LOCATION_SYSMEM);
1041 context_release(context);
1044 if (flags & WINED3D_MAP_WRITE)
1045 wined3d_buffer_invalidate_range(&buffer_gl->b, WINED3D_LOCATION_BUFFER, dirty_offset, dirty_size);
1047 else
1049 const struct wined3d_gl_info *gl_info;
1051 context = context_acquire(device, NULL, 0);
1052 gl_info = context->gl_info;
1054 if (flags & WINED3D_MAP_DISCARD)
1055 wined3d_buffer_validate_location(&buffer_gl->b, WINED3D_LOCATION_BUFFER);
1056 else
1057 wined3d_buffer_load_location(&buffer_gl->b, context, WINED3D_LOCATION_BUFFER);
1059 if (flags & WINED3D_MAP_WRITE)
1060 buffer_invalidate_bo_range(&buffer_gl->b, dirty_offset, dirty_size);
1062 if ((flags & WINED3D_MAP_DISCARD) && buffer_gl->b.resource.heap_memory)
1063 wined3d_buffer_evict_sysmem(&buffer_gl->b);
1065 if (count == 1)
1067 wined3d_buffer_gl_bind(buffer_gl, context);
1069 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1070 * multitexture fill rate test seems to depend on this. When
1071 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1072 * driver is free to discard the previous contents of the
1073 * buffer. The r600g driver only does this when the buffer is
1074 * currently in use, while the proprietary NVIDIA driver
1075 * appears to do this unconditionally. */
1076 if (buffer_gl->b.flags & WINED3D_BUFFER_DISCARD)
1077 flags &= ~WINED3D_MAP_DISCARD;
1079 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1081 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
1082 buffer_gl->b.map_ptr = GL_EXTCALL(glMapBufferRange(buffer_gl->buffer_type_hint,
1083 0, buffer_gl->b.resource.size, mapflags));
1084 checkGLcall("glMapBufferRange");
1086 else
1088 if (buffer_gl->b.flags & WINED3D_BUFFER_APPLESYNC)
1089 wined3d_buffer_gl_sync_apple(buffer_gl, flags, gl_info);
1090 buffer_gl->b.map_ptr = GL_EXTCALL(glMapBuffer(buffer_gl->buffer_type_hint,
1091 GL_READ_WRITE));
1092 checkGLcall("glMapBuffer");
1095 if (((DWORD_PTR)buffer_gl->b.map_ptr) & (RESOURCE_ALIGNMENT - 1))
1097 WARN("Pointer %p is not %u byte aligned.\n", buffer_gl->b.map_ptr, RESOURCE_ALIGNMENT);
1099 GL_EXTCALL(glUnmapBuffer(buffer_gl->buffer_type_hint));
1100 checkGLcall("glUnmapBuffer");
1101 buffer_gl->b.map_ptr = NULL;
1103 if (buffer_gl->b.resource.usage & WINED3DUSAGE_DYNAMIC)
1105 /* The extra copy is more expensive than not using VBOs at
1106 * all on the Nvidia Linux driver, which is the only driver
1107 * that returns unaligned pointers.
1109 TRACE("Dynamic buffer, dropping VBO.\n");
1110 wined3d_buffer_drop_bo(&buffer_gl->b);
1112 else
1114 TRACE("Falling back to doublebuffered operation.\n");
1115 wined3d_buffer_load_location(&buffer_gl->b, context, WINED3D_LOCATION_SYSMEM);
1116 buffer_gl->b.flags |= WINED3D_BUFFER_PIN_SYSMEM;
1118 TRACE("New pointer is %p.\n", buffer_gl->b.resource.heap_memory);
1122 context_release(context);
1125 if (flags & WINED3D_MAP_DISCARD)
1126 buffer_gl->b.flags |= WINED3D_BUFFER_DISCARD;
1129 base = buffer_gl->b.map_ptr ? buffer_gl->b.map_ptr : buffer_gl->b.resource.heap_memory;
1130 *data = base + offset;
1132 TRACE("Returning memory at %p (base %p, offset %u).\n", *data, base, offset);
1133 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1135 return WINED3D_OK;
1138 static void wined3d_buffer_gl_unmap(struct wined3d_buffer_gl *buffer_gl)
1140 ULONG i;
1142 TRACE("buffer_gl %p.\n", buffer_gl);
1144 /* In the case that the number of Unmap calls > the
1145 * number of Map calls, d3d returns always D3D_OK.
1146 * This is also needed to prevent Map from returning garbage on
1147 * the next call (this will happen if the lock_count is < 0). */
1148 if (!buffer_gl->b.resource.map_count)
1150 WARN("Unmap called without a previous map call.\n");
1151 return;
1154 if (--buffer_gl->b.resource.map_count)
1156 /* Delay loading the buffer until everything is unlocked */
1157 TRACE("Ignoring unmap.\n");
1158 return;
1161 if (buffer_gl->b.map_ptr)
1163 struct wined3d_device *device = buffer_gl->b.resource.device;
1164 const struct wined3d_gl_info *gl_info;
1165 struct wined3d_context *context;
1167 context = context_acquire(device, NULL, 0);
1168 gl_info = context->gl_info;
1170 wined3d_buffer_gl_bind(buffer_gl, context);
1172 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1174 for (i = 0; i < buffer_gl->b.modified_areas; ++i)
1176 GL_EXTCALL(glFlushMappedBufferRange(buffer_gl->buffer_type_hint,
1177 buffer_gl->b.maps[i].offset, buffer_gl->b.maps[i].size));
1178 checkGLcall("glFlushMappedBufferRange");
1181 else if (buffer_gl->b.flags & WINED3D_BUFFER_APPLESYNC)
1183 for (i = 0; i < buffer_gl->b.modified_areas; ++i)
1185 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer_gl->buffer_type_hint,
1186 buffer_gl->b.maps[i].offset, buffer_gl->b.maps[i].size));
1187 checkGLcall("glFlushMappedBufferRangeAPPLE");
1191 GL_EXTCALL(glUnmapBuffer(buffer_gl->buffer_type_hint));
1192 context_release(context);
1194 buffer_clear_dirty_areas(&buffer_gl->b);
1195 buffer_gl->b.map_ptr = NULL;
1199 void wined3d_buffer_copy(struct wined3d_buffer *dst_buffer, unsigned int dst_offset,
1200 struct wined3d_buffer *src_buffer, unsigned int src_offset, unsigned int size)
1202 struct wined3d_bo_address dst, src;
1203 struct wined3d_context *context;
1204 DWORD dst_location;
1206 buffer_mark_used(dst_buffer);
1207 buffer_mark_used(src_buffer);
1209 dst_location = wined3d_buffer_get_memory(dst_buffer, &dst, dst_buffer->locations);
1210 dst.addr += dst_offset;
1212 wined3d_buffer_get_memory(src_buffer, &src, src_buffer->locations);
1213 src.addr += src_offset;
1215 context = context_acquire(dst_buffer->resource.device, NULL, 0);
1216 context_copy_bo_address(context, &dst, wined3d_buffer_gl(dst_buffer)->buffer_type_hint,
1217 &src, wined3d_buffer_gl(src_buffer)->buffer_type_hint, size);
1218 context_release(context);
1220 wined3d_buffer_invalidate_range(dst_buffer, ~dst_location, dst_offset, size);
1223 void wined3d_buffer_upload_data(struct wined3d_buffer *buffer, struct wined3d_context *context,
1224 const struct wined3d_box *box, const void *data)
1226 struct wined3d_map_range range;
1228 if (box)
1230 range.offset = box->left;
1231 range.size = box->right - box->left;
1233 else
1235 range.offset = 0;
1236 range.size = buffer->resource.size;
1239 wined3d_buffer_gl_upload_ranges(wined3d_buffer_gl(buffer), context, data, range.offset, 1, &range);
1242 static void wined3d_buffer_init_data(struct wined3d_buffer *buffer,
1243 struct wined3d_device *device, const struct wined3d_sub_resource_data *data)
1245 struct wined3d_resource *resource = &buffer->resource;
1246 struct wined3d_bo_address bo;
1247 struct wined3d_box box;
1249 if (buffer->flags & WINED3D_BUFFER_USE_BO)
1251 wined3d_box_set(&box, 0, 0, resource->size, 1, 0, 1);
1252 wined3d_cs_emit_update_sub_resource(device->cs, resource,
1253 0, &box, data->data, data->row_pitch, data->slice_pitch);
1255 else
1257 wined3d_buffer_get_memory(buffer, &bo, WINED3D_LOCATION_SYSMEM);
1258 memcpy(bo.addr, data->data, resource->size);
1259 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_SYSMEM);
1260 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_SYSMEM);
1264 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1266 return wined3d_buffer_incref(buffer_from_resource(resource));
1269 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1271 return wined3d_buffer_decref(buffer_from_resource(resource));
1274 static void buffer_resource_preload(struct wined3d_resource *resource)
1276 struct wined3d_context *context;
1278 context = context_acquire(resource->device, NULL, 0);
1279 wined3d_buffer_load(buffer_from_resource(resource), context, NULL);
1280 context_release(context);
1283 static HRESULT buffer_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1284 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1286 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer_from_resource(resource));
1287 UINT offset, size;
1289 if (sub_resource_idx)
1291 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1292 return E_INVALIDARG;
1295 if (box)
1297 offset = box->left;
1298 size = box->right - box->left;
1300 else
1302 offset = size = 0;
1305 map_desc->row_pitch = map_desc->slice_pitch = resource->size;
1306 return wined3d_buffer_gl_map(buffer_gl, offset, size, (BYTE **)&map_desc->data, flags);
1309 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1311 if (sub_resource_idx)
1313 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1314 return E_INVALIDARG;
1317 wined3d_buffer_gl_unmap(wined3d_buffer_gl(buffer_from_resource(resource)));
1318 return WINED3D_OK;
1321 static const struct wined3d_resource_ops buffer_resource_ops =
1323 buffer_resource_incref,
1324 buffer_resource_decref,
1325 buffer_resource_preload,
1326 buffer_unload,
1327 buffer_resource_sub_resource_map,
1328 buffer_resource_sub_resource_unmap,
1331 static GLenum buffer_type_hint_from_bind_flags(const struct wined3d_gl_info *gl_info,
1332 unsigned int bind_flags)
1334 if (bind_flags == WINED3D_BIND_INDEX_BUFFER)
1335 return GL_ELEMENT_ARRAY_BUFFER;
1337 if (bind_flags & (WINED3D_BIND_SHADER_RESOURCE | WINED3D_BIND_UNORDERED_ACCESS)
1338 && gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1339 return GL_TEXTURE_BUFFER;
1341 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
1342 return GL_UNIFORM_BUFFER;
1344 if (bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1345 return GL_TRANSFORM_FEEDBACK_BUFFER;
1347 if (bind_flags & ~(WINED3D_BIND_VERTEX_BUFFER | WINED3D_BIND_INDEX_BUFFER))
1348 FIXME("Unhandled bind flags %#x.\n", bind_flags);
1350 return GL_ARRAY_BUFFER;
1353 static HRESULT wined3d_buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1354 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1355 void *parent, const struct wined3d_parent_ops *parent_ops)
1357 const struct wined3d_format *format = wined3d_get_format(device->adapter, WINED3DFMT_UNKNOWN, desc->usage);
1358 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1359 struct wined3d_resource *resource = &buffer->resource;
1360 BOOL dynamic_buffer_ok;
1361 HRESULT hr;
1363 if (!desc->byte_width)
1365 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1366 return E_INVALIDARG;
1369 if (desc->bind_flags & WINED3D_BIND_CONSTANT_BUFFER && desc->byte_width & (WINED3D_CONSTANT_BUFFER_ALIGNMENT - 1))
1371 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc->byte_width);
1372 return E_INVALIDARG;
1375 if (data && !data->data)
1377 WARN("Invalid sub-resource data specified.\n");
1378 return E_INVALIDARG;
1381 if (FAILED(hr = resource_init(resource, device, WINED3D_RTYPE_BUFFER, format,
1382 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->bind_flags, desc->access,
1383 desc->byte_width, 1, 1, desc->byte_width, parent, parent_ops, &buffer_resource_ops)))
1385 WARN("Failed to initialize resource, hr %#x.\n", hr);
1386 return hr;
1388 buffer->structure_byte_stride = desc->structure_byte_stride;
1389 buffer->locations = data ? WINED3D_LOCATION_DISCARDED : WINED3D_LOCATION_SYSMEM;
1391 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1392 buffer, buffer->resource.size, buffer->resource.usage,
1393 debug_d3dformat(buffer->resource.format->id), buffer->resource.heap_memory);
1395 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1396 || wined3d_resource_access_is_managed(desc->access))
1398 /* SWvp and managed buffers always return the same pointer in buffer
1399 * maps and retain data in DISCARD maps. Keep a system memory copy of
1400 * the buffer to provide the same behavior to the application. */
1401 TRACE("Pinning system memory.\n");
1402 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1403 buffer->locations = WINED3D_LOCATION_SYSMEM;
1406 /* Observations show that draw_primitive_immediate_mode() is faster on
1407 * dynamic vertex buffers than converting + draw_primitive_arrays().
1408 * (Half-Life 2 and others.) */
1409 dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
1411 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1413 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1415 else if (!(desc->access & WINED3D_RESOURCE_ACCESS_GPU))
1417 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1419 else if (!dynamic_buffer_ok && (resource->usage & WINED3DUSAGE_DYNAMIC))
1421 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1423 else
1425 buffer->flags |= WINED3D_BUFFER_USE_BO;
1428 if (buffer->locations & WINED3D_LOCATION_SYSMEM || !(buffer->flags & WINED3D_BUFFER_USE_BO))
1430 if (!wined3d_resource_allocate_sysmem(&buffer->resource))
1431 return E_OUTOFMEMORY;
1434 if (!(buffer->maps = heap_alloc(sizeof(*buffer->maps))))
1436 ERR("Out of memory.\n");
1437 buffer_unload(resource);
1438 resource_cleanup(resource);
1439 wined3d_resource_wait_idle(resource);
1440 return E_OUTOFMEMORY;
1442 buffer->maps_size = 1;
1444 if (data)
1445 wined3d_buffer_init_data(buffer, device, data);
1447 return WINED3D_OK;
1450 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1451 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops,
1452 struct wined3d_buffer **buffer)
1454 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1455 struct wined3d_buffer_gl *object;
1456 HRESULT hr;
1458 TRACE("device %p, desc byte_width %u, usage %s, bind_flags %s, access %s, data %p, parent %p, "
1459 "parent_ops %p, buffer %p.\n",
1460 device, desc->byte_width, debug_d3dusage(desc->usage),
1461 wined3d_debug_bind_flags(desc->bind_flags), wined3d_debug_resource_access(desc->access),
1462 data, parent, parent_ops, buffer);
1464 if (!(object = heap_alloc_zero(sizeof(*object))))
1465 return E_OUTOFMEMORY;
1467 object->buffer_type_hint = buffer_type_hint_from_bind_flags(gl_info, desc->bind_flags);
1469 if (FAILED(hr = wined3d_buffer_init(&object->b, device, desc, data, parent, parent_ops)))
1471 WARN("Failed to initialize buffer, hr %#x.\n", hr);
1472 heap_free(object);
1473 return hr;
1476 TRACE("Created buffer %p.\n", object);
1478 *buffer = &object->b;
1480 return WINED3D_OK;