wined3d: Avoid accessing gl_info in wined3d_buffer_init().
[wine.git] / dlls / wined3d / buffer.c
blob35248ce9fa5ef50e5d52e32ed4965dd165048a49
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_gl *context_gl)
138 wined3d_context_gl_bind_bo(context_gl, buffer_gl->buffer_type_hint, buffer_gl->b.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_gl *context_gl)
145 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
146 struct wined3d_resource *resource = &buffer_gl->b.resource;
147 GLuint bo;
149 if (!buffer_gl->b.buffer_object)
150 return;
152 /* The stream source state handler might have read the memory of the
153 * vertex buffer already and got the memory in the vbo which is not
154 * valid any longer. Dirtify the stream source to force a reload. This
155 * happens only once per changed vertexbuffer and should occur rather
156 * rarely. */
157 if (resource->bind_count)
159 if (resource->bind_flags & WINED3D_BIND_VERTEX_BUFFER)
160 device_invalidate_state(resource->device, STATE_STREAMSRC);
161 if (resource->bind_flags & WINED3D_BIND_INDEX_BUFFER)
162 device_invalidate_state(resource->device, STATE_INDEXBUFFER);
163 if (resource->bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
165 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX));
166 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL));
167 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN));
168 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY));
169 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL));
170 device_invalidate_state(resource->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE));
172 if (resource->bind_flags & WINED3D_BIND_STREAM_OUTPUT)
174 device_invalidate_state(resource->device, STATE_STREAM_OUTPUT);
175 if (context_gl->c.transform_feedback_active)
177 /* We have to make sure that transform feedback is not active
178 * when deleting a potentially bound transform feedback buffer.
179 * This may happen when the device is being destroyed. */
180 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl);
181 wined3d_context_gl_end_transform_feedback(context_gl);
186 bo = buffer_gl->b.buffer_object;
187 GL_EXTCALL(glDeleteBuffers(1, &bo));
188 checkGLcall("glDeleteBuffers");
189 buffer_gl->b.buffer_object = 0;
191 if (buffer_gl->b.fence)
193 wined3d_fence_destroy(buffer_gl->b.fence);
194 buffer_gl->b.fence = NULL;
196 buffer_gl->b.flags &= ~WINED3D_BUFFER_APPLESYNC;
199 /* Context activation is done by the caller. */
200 static BOOL wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl *buffer_gl,
201 struct wined3d_context_gl *context_gl)
203 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
204 GLenum gl_usage = GL_STATIC_DRAW;
205 GLenum error;
206 GLuint bo;
208 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
209 buffer_gl, debug_d3dusage(buffer_gl->b.resource.usage));
211 /* Make sure that the gl error is cleared. Do not use checkGLcall
212 * here because checkGLcall just prints a fixme and continues. However,
213 * if an error during VBO creation occurs we can fall back to non-VBO operation
214 * with full functionality(but performance loss).
216 while (gl_info->gl_ops.gl.p_glGetError() != GL_NO_ERROR);
218 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
219 * The vertex declaration from the device determines how the data in the
220 * buffer is interpreted. This means that on each draw call the buffer has
221 * to be verified to check if the rhw and color values are in the correct
222 * format. */
224 GL_EXTCALL(glGenBuffers(1, &bo));
225 buffer_gl->b.buffer_object = bo;
226 error = gl_info->gl_ops.gl.p_glGetError();
227 if (!buffer_gl->b.buffer_object || error != GL_NO_ERROR)
229 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error), error);
230 goto fail;
233 wined3d_buffer_gl_bind(buffer_gl, context_gl);
234 error = gl_info->gl_ops.gl.p_glGetError();
235 if (error != GL_NO_ERROR)
237 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error), error);
238 goto fail;
241 if (buffer_gl->b.resource.usage & WINED3DUSAGE_DYNAMIC)
243 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
244 gl_usage = GL_STREAM_DRAW_ARB;
246 if (gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
248 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint,
249 GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
250 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint,
251 GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
252 checkGLcall("glBufferParameteriAPPLE");
253 buffer_gl->b.flags |= WINED3D_BUFFER_APPLESYNC;
255 /* No setup is needed here for GL_ARB_map_buffer_range. */
258 GL_EXTCALL(glBufferData(buffer_gl->buffer_type_hint, buffer_gl->b.resource.size, NULL, gl_usage));
259 error = gl_info->gl_ops.gl.p_glGetError();
260 if (error != GL_NO_ERROR)
262 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error), error);
263 goto fail;
266 buffer_gl->buffer_object_usage = gl_usage;
267 buffer_invalidate_bo_range(&buffer_gl->b, 0, 0);
269 return TRUE;
271 fail:
272 /* Clean up all BO init, but continue because we can work without a BO :-) */
273 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
274 buffer_gl->b.flags &= ~WINED3D_BUFFER_USE_BO;
275 wined3d_buffer_gl_destroy_buffer_object(buffer_gl, context_gl);
276 buffer_clear_dirty_areas(&buffer_gl->b);
277 return FALSE;
280 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *buffer,
281 const enum wined3d_buffer_conversion_type conversion_type,
282 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
284 const struct wined3d_format *format = attrib->format;
285 BOOL ret = FALSE;
286 unsigned int i;
287 DWORD_PTR data;
289 /* Check for some valid situations which cause us pain. One is if the buffer is used for
290 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
291 * with different strides. In the 2nd case we might have to drop conversion entirely,
292 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
294 if (!attrib->stride)
296 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
297 debug_d3dformat(format->id));
299 else if (attrib->stride != *stride_this_run && *stride_this_run)
301 FIXME("Got two concurrent strides, %d and %d.\n", attrib->stride, *stride_this_run);
303 else
305 *stride_this_run = attrib->stride;
306 if (buffer->stride != *stride_this_run)
308 /* We rely that this happens only on the first converted attribute that is found,
309 * if at all. See above check
311 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
312 buffer->stride = *stride_this_run;
313 heap_free(buffer->conversion_map);
314 buffer->conversion_map = heap_calloc(buffer->stride, sizeof(*buffer->conversion_map));
315 ret = TRUE;
319 data = ((DWORD_PTR)attrib->data.addr) % buffer->stride;
320 for (i = 0; i < format->byte_count; ++i)
322 DWORD_PTR idx = (data + i) % buffer->stride;
323 if (buffer->conversion_map[idx] != conversion_type)
325 TRACE("Byte %lu in vertex changed:\n", idx);
326 TRACE(" It was type %#x, is %#x now.\n", buffer->conversion_map[idx], conversion_type);
327 ret = TRUE;
328 buffer->conversion_map[idx] = conversion_type;
332 return ret;
335 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
336 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
338 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
339 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
341 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
342 enum wined3d_format_id format;
343 BOOL ret = FALSE;
345 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
346 * there, on nonexistent attribs the vbo is 0.
348 if (!(si->use_map & (1u << attrib_idx))
349 || state->streams[attrib->stream_idx].buffer != This)
350 return FALSE;
352 format = attrib->format->id;
353 /* Look for newly appeared conversion */
354 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
356 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
358 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
360 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
362 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
363 return FALSE;
366 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
368 else if (This->conversion_map)
370 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
373 return ret;
376 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
377 const struct wined3d_state *state, DWORD fixup_flags)
379 UINT stride_this_run = 0;
380 BOOL ret = FALSE;
382 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
383 * Once we have our declaration there is no need to look it up again. Index buffers also never need
384 * conversion, so once the (empty) conversion structure is created don't bother checking again
386 if (This->flags & WINED3D_BUFFER_HASDESC)
388 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
391 if (!fixup_flags)
393 TRACE("No fixup required.\n");
394 if(This->conversion_map)
396 heap_free(This->conversion_map);
397 This->conversion_map = NULL;
398 This->stride = 0;
399 return TRUE;
402 return FALSE;
405 TRACE("Finding vertex buffer conversion information\n");
406 /* Certain declaration types need some fixups before we can pass them to
407 * opengl. This means D3DCOLOR attributes with fixed function vertex
408 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
409 * GL_ARB_half_float_vertex is not supported.
411 * Note for d3d8 and d3d9:
412 * The vertex buffer FVF doesn't help with finding them, we have to use
413 * the decoded vertex declaration and pick the things that concern the
414 * current buffer. A problem with this is that this can change between
415 * draws, so we have to validate the information and reprocess the buffer
416 * if it changes, and avoid false positives for performance reasons.
417 * WineD3D doesn't even know the vertex buffer any more, it is managed
418 * by the client libraries and passed to SetStreamSource and ProcessVertices
419 * as needed.
421 * We have to distinguish between vertex shaders and fixed function to
422 * pick the way we access the strided vertex information.
424 * This code sets up a per-byte array with the size of the detected
425 * stride of the arrays in the buffer. For each byte we have a field
426 * that marks the conversion needed on this byte. For example, the
427 * following declaration with fixed function vertex processing:
429 * POSITIONT, FLOAT4
430 * NORMAL, FLOAT3
431 * DIFFUSE, FLOAT16_4
432 * SPECULAR, D3DCOLOR
434 * Will result in
435 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
436 * [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]
438 * Where in this example map P means 4 component position conversion, 0
439 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
440 * conversion (red / blue swizzle).
442 * If we're doing conversion and the stride changes we have to reconvert
443 * the whole buffer. Note that we do not mind if the semantic changes,
444 * we only care for the conversion type. So if the NORMAL is replaced
445 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
446 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
447 * conversion types depend on the semantic as well, for example a FLOAT4
448 * texcoord needs no conversion while a FLOAT4 positiont needs one
451 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
452 fixup_flags, &stride_this_run) || ret;
453 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
455 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDWEIGHT,
456 fixup_flags, &stride_this_run) || ret;
457 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDINDICES,
458 fixup_flags, &stride_this_run) || ret;
459 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
460 fixup_flags, &stride_this_run) || ret;
461 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
462 fixup_flags, &stride_this_run) || ret;
463 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
464 fixup_flags, &stride_this_run) || ret;
465 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
466 fixup_flags, &stride_this_run) || ret;
467 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
468 fixup_flags, &stride_this_run) || ret;
469 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
470 fixup_flags, &stride_this_run) || ret;
471 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
472 fixup_flags, &stride_this_run) || ret;
473 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
474 fixup_flags, &stride_this_run) || ret;
475 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
476 fixup_flags, &stride_this_run) || ret;
477 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
478 fixup_flags, &stride_this_run) || ret;
479 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
480 fixup_flags, &stride_this_run) || ret;
482 if (!stride_this_run && This->conversion_map)
484 /* Sanity test */
485 if (!ret)
486 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
487 heap_free(This->conversion_map);
488 This->conversion_map = NULL;
489 This->stride = 0;
492 if (ret) TRACE("Conversion information changed\n");
494 return ret;
497 static inline unsigned int fixup_d3dcolor(DWORD *dst_color)
499 DWORD src_color = *dst_color;
501 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
502 * endianness. If we want this to work on big-endian machines as well we
503 * have to consider more things.
505 * 0xff000000: Alpha mask
506 * 0x00ff0000: Blue mask
507 * 0x0000ff00: Green mask
508 * 0x000000ff: Red mask
510 *dst_color = 0;
511 *dst_color |= (src_color & 0xff00ff00u); /* Alpha Green */
512 *dst_color |= (src_color & 0x00ff0000u) >> 16; /* Red */
513 *dst_color |= (src_color & 0x000000ffu) << 16; /* Blue */
515 return sizeof(*dst_color);
518 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4 *p)
520 /* rhw conversion like in position_float4(). */
521 if (p->w != 1.0f && p->w != 0.0f)
523 float w = 1.0f / p->w;
524 p->x *= w;
525 p->y *= w;
526 p->z *= w;
527 p->w = w;
530 return sizeof(*p);
533 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
535 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
537 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
539 return refcount;
542 static void buffer_conversion_upload(struct wined3d_buffer *buffer, struct wined3d_context *context)
544 unsigned int i, j, range_idx, start, end, vertex_count;
545 BYTE *data;
547 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
549 ERR("Failed to load system memory.\n");
550 return;
552 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
554 /* Now for each vertex in the buffer that needs conversion. */
555 vertex_count = buffer->resource.size / buffer->stride;
557 if (!(data = heap_alloc(buffer->resource.size)))
559 ERR("Out of memory.\n");
560 return;
563 for (range_idx = 0; range_idx < buffer->modified_areas; ++range_idx)
565 start = buffer->maps[range_idx].offset;
566 end = start + buffer->maps[range_idx].size;
568 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
569 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertex_count); ++i)
571 for (j = 0; j < buffer->stride;)
573 switch (buffer->conversion_map[j])
575 case CONV_NONE:
576 /* Done already */
577 j += sizeof(DWORD);
578 break;
579 case CONV_D3DCOLOR:
580 j += fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
581 break;
582 case CONV_POSITIONT:
583 j += fixup_transformed_pos((struct wined3d_vec4 *) (data + i * buffer->stride + j));
584 break;
585 default:
586 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer->conversion_map[j]);
587 ++j;
593 buffer->buffer_ops->buffer_upload_ranges(buffer, context,
594 data, 0, buffer->modified_areas, buffer->maps);
596 heap_free(data);
599 static BOOL wined3d_buffer_prepare_location(struct wined3d_buffer *buffer,
600 struct wined3d_context *context, unsigned int location)
602 return buffer->buffer_ops->buffer_prepare_location(buffer, context, location);
605 static void wined3d_buffer_unload_location(struct wined3d_buffer *buffer,
606 struct wined3d_context *context, unsigned int location)
608 buffer->buffer_ops->buffer_unload_location(buffer, context, location);
611 BOOL wined3d_buffer_load_location(struct wined3d_buffer *buffer,
612 struct wined3d_context *context, DWORD location)
614 struct wined3d_map_range range;
616 TRACE("buffer %p, context %p, location %s.\n",
617 buffer, context, wined3d_debug_location(location));
619 if (buffer->locations & location)
621 TRACE("Location (%#x) is already up to date.\n", location);
622 return TRUE;
625 if (!buffer->locations)
627 ERR("Buffer %p does not have any up to date location.\n", buffer);
628 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_DISCARDED);
629 return wined3d_buffer_load_location(buffer, context, location);
632 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer->locations));
634 if (!wined3d_buffer_prepare_location(buffer, context, location))
635 return FALSE;
637 if (buffer->locations & WINED3D_LOCATION_DISCARDED)
639 TRACE("Buffer previously discarded, nothing to do.\n");
640 wined3d_buffer_validate_location(buffer, location);
641 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_DISCARDED);
642 return TRUE;
645 switch (location)
647 case WINED3D_LOCATION_SYSMEM:
648 range.offset = 0;
649 range.size = buffer->resource.size;
650 buffer->buffer_ops->buffer_download_ranges(buffer, context,
651 buffer->resource.heap_memory, 0, 1, &range);
652 break;
654 case WINED3D_LOCATION_BUFFER:
655 if (!buffer->conversion_map)
656 buffer->buffer_ops->buffer_upload_ranges(buffer, context,
657 buffer->resource.heap_memory, 0, buffer->modified_areas, buffer->maps);
658 else
659 buffer_conversion_upload(buffer, context);
660 break;
662 default:
663 ERR("Invalid location %s.\n", wined3d_debug_location(location));
664 return FALSE;
667 wined3d_buffer_validate_location(buffer, location);
668 if (buffer->resource.heap_memory && location == WINED3D_LOCATION_BUFFER
669 && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
670 wined3d_buffer_evict_sysmem(buffer);
672 return TRUE;
675 /* Context activation is done by the caller. */
676 BYTE *wined3d_buffer_load_sysmem(struct wined3d_buffer *buffer, struct wined3d_context *context)
678 if (wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
679 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
680 return buffer->resource.heap_memory;
683 DWORD wined3d_buffer_get_memory(struct wined3d_buffer *buffer,
684 struct wined3d_bo_address *data, DWORD locations)
686 TRACE("buffer %p, data %p, locations %s.\n",
687 buffer, data, wined3d_debug_location(locations));
689 if (locations & WINED3D_LOCATION_BUFFER)
691 data->buffer_object = buffer->buffer_object;
692 data->addr = NULL;
693 return WINED3D_LOCATION_BUFFER;
695 if (locations & WINED3D_LOCATION_SYSMEM)
697 data->buffer_object = 0;
698 data->addr = buffer->resource.heap_memory;
699 return WINED3D_LOCATION_SYSMEM;
702 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
703 data->buffer_object = 0;
704 data->addr = NULL;
705 return 0;
708 static void buffer_resource_unload(struct wined3d_resource *resource)
710 struct wined3d_buffer *buffer = buffer_from_resource(resource);
712 TRACE("buffer %p.\n", buffer);
714 if (buffer->buffer_object)
716 struct wined3d_context *context;
718 context = context_acquire(resource->device, NULL, 0);
720 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
721 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
722 wined3d_buffer_unload_location(buffer, context, WINED3D_LOCATION_BUFFER);
723 buffer_clear_dirty_areas(buffer);
725 context_release(context);
727 heap_free(buffer->conversion_map);
728 buffer->conversion_map = NULL;
729 buffer->stride = 0;
730 buffer->conversion_stride = 0;
731 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
734 resource_unload(resource);
737 static void wined3d_buffer_drop_bo(struct wined3d_buffer *buffer)
739 buffer->flags &= ~WINED3D_BUFFER_USE_BO;
740 buffer_resource_unload(&buffer->resource);
743 static void wined3d_buffer_destroy_object(void *object)
745 struct wined3d_buffer *buffer = object;
746 struct wined3d_context *context;
748 if (buffer->buffer_object)
750 context = context_acquire(buffer->resource.device, NULL, 0);
751 wined3d_buffer_unload_location(buffer, context, WINED3D_LOCATION_BUFFER);
752 context_release(context);
754 heap_free(buffer->conversion_map);
755 heap_free(buffer->maps);
758 void wined3d_buffer_cleanup(struct wined3d_buffer *buffer)
760 wined3d_cs_destroy_object(buffer->resource.device->cs, wined3d_buffer_destroy_object, buffer);
761 resource_cleanup(&buffer->resource);
764 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
766 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
768 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
770 if (!refcount)
772 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
773 buffer->resource.device->adapter->adapter_ops->adapter_destroy_buffer(buffer);
776 return refcount;
779 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
781 TRACE("buffer %p.\n", buffer);
783 return buffer->resource.parent;
786 /* The caller provides a context and binds the buffer */
787 static void wined3d_buffer_gl_sync_apple(struct wined3d_buffer_gl *buffer_gl,
788 uint32_t flags, struct wined3d_context_gl *context_gl)
790 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
791 enum wined3d_fence_result ret;
792 HRESULT hr;
794 /* No fencing needs to be done if the app promises not to overwrite
795 * existing data. */
796 if (flags & WINED3D_MAP_NOOVERWRITE)
797 return;
799 if (flags & WINED3D_MAP_DISCARD)
801 wined3d_buffer_gl_bind(buffer_gl, context_gl);
803 GL_EXTCALL(glBufferData(buffer_gl->buffer_type_hint, buffer_gl->b.resource.size,
804 NULL, buffer_gl->buffer_object_usage));
805 checkGLcall("glBufferData");
806 return;
809 if (!buffer_gl->b.fence)
811 TRACE("Creating fence for buffer %p.\n", buffer_gl);
813 if (FAILED(hr = wined3d_fence_create(buffer_gl->b.resource.device, &buffer_gl->b.fence)))
815 if (hr == WINED3DERR_NOTAVAILABLE)
816 FIXME("Fences not supported, dropping async buffer locks.\n");
817 else
818 ERR("Failed to create fence, hr %#x.\n", hr);
819 goto drop_fence;
822 /* Since we don't know about old draws a glFinish is needed once */
823 gl_info->gl_ops.gl.p_glFinish();
824 return;
827 TRACE("Synchronizing buffer %p.\n", buffer_gl);
828 ret = wined3d_fence_wait(buffer_gl->b.fence, buffer_gl->b.resource.device);
829 switch (ret)
831 case WINED3D_FENCE_NOT_STARTED:
832 case WINED3D_FENCE_OK:
833 /* All done */
834 return;
836 case WINED3D_FENCE_WRONG_THREAD:
837 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
838 goto drop_fence;
840 default:
841 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret);
842 goto drop_fence;
845 drop_fence:
846 if (buffer_gl->b.fence)
848 wined3d_fence_destroy(buffer_gl->b.fence);
849 buffer_gl->b.fence = NULL;
852 gl_info->gl_ops.gl.p_glFinish();
853 wined3d_buffer_gl_bind(buffer_gl, context_gl);
854 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
855 checkGLcall("glBufferParameteriAPPLE(buffer_gl->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
856 buffer_gl->b.flags &= ~WINED3D_BUFFER_APPLESYNC;
859 static void buffer_mark_used(struct wined3d_buffer *buffer)
861 buffer->flags &= ~WINED3D_BUFFER_DISCARD;
864 /* Context activation is done by the caller. */
865 void wined3d_buffer_load(struct wined3d_buffer *buffer, struct wined3d_context *context,
866 const struct wined3d_state *state)
868 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
869 BOOL decl_changed = FALSE;
871 TRACE("buffer %p.\n", buffer);
873 if (buffer->resource.map_count && buffer->map_ptr)
875 FIXME("Buffer is mapped through buffer object, not loading.\n");
876 return;
878 else if (buffer->resource.map_count)
880 WARN("Loading mapped buffer.\n");
883 buffer_mark_used(buffer);
885 /* TODO: Make converting independent from VBOs */
886 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
888 /* Not doing any conversion */
889 return;
892 if (!wined3d_buffer_prepare_location(buffer, context, WINED3D_LOCATION_BUFFER))
894 ERR("Failed to prepare buffer location.\n");
895 return;
898 /* Reading the declaration makes only sense if we have valid state information
899 * (i.e., if this function is called during draws). */
900 if (state)
902 DWORD fixup_flags = 0;
904 if (!use_vs(state))
906 if (!d3d_info->vertex_bgra && !d3d_info->ffp_generic_attributes)
907 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
908 if (!d3d_info->xyzrhw)
909 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
912 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
913 buffer->flags |= WINED3D_BUFFER_HASDESC;
916 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
918 ++buffer->draw_count;
919 if (buffer->draw_count > VB_RESETDECLCHANGE)
920 buffer->decl_change_count = 0;
921 if (buffer->draw_count > VB_RESETFULLCONVS)
922 buffer->full_conversion_count = 0;
923 return;
926 /* If applications change the declaration over and over, reconverting all the time is a huge
927 * performance hit. So count the declaration changes and release the VBO if there are too many
928 * of them (and thus stop converting)
930 if (decl_changed)
932 ++buffer->decl_change_count;
933 buffer->draw_count = 0;
935 if (buffer->decl_change_count > VB_MAXDECLCHANGES
936 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
938 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
939 wined3d_buffer_drop_bo(buffer);
940 return;
943 /* The declaration changed, reload the whole buffer. */
944 WARN("Reloading buffer because of a vertex declaration change.\n");
945 buffer_invalidate_bo_range(buffer, 0, 0);
947 else
949 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
950 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
951 * decl changes and reset the decl change count after a specific number of them
953 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
955 ++buffer->full_conversion_count;
956 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
958 FIXME("Too many full buffer conversions, stopping converting.\n");
959 wined3d_buffer_drop_bo(buffer);
960 return;
963 else
965 ++buffer->draw_count;
966 if (buffer->draw_count > VB_RESETDECLCHANGE)
967 buffer->decl_change_count = 0;
968 if (buffer->draw_count > VB_RESETFULLCONVS)
969 buffer->full_conversion_count = 0;
973 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
974 ERR("Failed to load buffer location.\n");
977 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
979 TRACE("buffer %p.\n", buffer);
981 return &buffer->resource;
984 static HRESULT buffer_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
985 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, uint32_t flags)
987 struct wined3d_buffer *buffer = buffer_from_resource(resource);
988 struct wined3d_device *device = resource->device;
989 struct wined3d_context *context;
990 unsigned int offset, size;
991 uint8_t *base;
992 LONG count;
994 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
995 resource, sub_resource_idx, map_desc, debug_box(box), flags);
997 if (sub_resource_idx)
999 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1000 return E_INVALIDARG;
1003 if (box)
1005 offset = box->left;
1006 size = box->right - box->left;
1008 else
1010 offset = size = 0;
1013 map_desc->row_pitch = map_desc->slice_pitch = resource->size;
1015 count = ++resource->map_count;
1017 if (buffer->buffer_object)
1019 unsigned int dirty_offset = offset, dirty_size = size;
1020 struct wined3d_bo_address addr;
1022 /* DISCARD invalidates the entire buffer, regardless of the specified
1023 * offset and size. Some applications also depend on the entire buffer
1024 * being uploaded in that case. Two such applications are Port Royale
1025 * and Darkstar One. */
1026 if (flags & WINED3D_MAP_DISCARD)
1028 dirty_offset = 0;
1029 dirty_size = 0;
1032 if (((flags & WINED3D_MAP_WRITE) && !(flags & (WINED3D_MAP_NOOVERWRITE | WINED3D_MAP_DISCARD)))
1033 || (!(flags & WINED3D_MAP_WRITE) && (buffer->locations & WINED3D_LOCATION_SYSMEM))
1034 || buffer->flags & WINED3D_BUFFER_PIN_SYSMEM)
1036 if (!(buffer->locations & WINED3D_LOCATION_SYSMEM))
1038 context = context_acquire(device, NULL, 0);
1039 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
1040 context_release(context);
1043 if (flags & WINED3D_MAP_WRITE)
1044 wined3d_buffer_invalidate_range(buffer, WINED3D_LOCATION_BUFFER, dirty_offset, dirty_size);
1046 else
1048 context = context_acquire(device, NULL, 0);
1050 if (flags & WINED3D_MAP_DISCARD)
1051 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_BUFFER);
1052 else
1053 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
1055 if (flags & WINED3D_MAP_WRITE)
1057 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_SYSMEM);
1058 buffer_invalidate_bo_range(buffer, dirty_offset, dirty_size);
1061 if ((flags & WINED3D_MAP_DISCARD) && resource->heap_memory)
1062 wined3d_buffer_evict_sysmem(buffer);
1064 if (count == 1)
1066 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1067 * multitexture fill rate test seems to depend on this. When
1068 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1069 * driver is free to discard the previous contents of the
1070 * buffer. The r600g driver only does this when the buffer is
1071 * currently in use, while the proprietary NVIDIA driver
1072 * appears to do this unconditionally. */
1073 if (buffer->flags & WINED3D_BUFFER_DISCARD)
1074 flags &= ~WINED3D_MAP_DISCARD;
1076 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1077 wined3d_buffer_gl_sync_apple(wined3d_buffer_gl(buffer), flags, wined3d_context_gl(context));
1079 addr.buffer_object = buffer->buffer_object;
1080 addr.addr = 0;
1081 buffer->map_ptr = wined3d_context_map_bo_address(context,
1082 &addr, resource->size, resource->bind_flags, flags);
1084 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
1086 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
1088 wined3d_context_unmap_bo_address(context, &addr, resource->bind_flags, 0, NULL);
1089 buffer->map_ptr = NULL;
1091 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1093 /* The extra copy is more expensive than not using VBOs
1094 * at all on the NVIDIA Linux driver, which is the
1095 * only driver that returns unaligned pointers. */
1096 TRACE("Dynamic buffer, dropping VBO.\n");
1097 wined3d_buffer_drop_bo(buffer);
1099 else
1101 TRACE("Falling back to doublebuffered operation.\n");
1102 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
1103 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1105 TRACE("New pointer is %p.\n", resource->heap_memory);
1109 context_release(context);
1112 if (flags & WINED3D_MAP_DISCARD)
1113 buffer->flags |= WINED3D_BUFFER_DISCARD;
1116 base = buffer->map_ptr ? buffer->map_ptr : resource->heap_memory;
1117 map_desc->data = base + offset;
1119 TRACE("Returning memory at %p (base %p, offset %u).\n", map_desc->data, base, offset);
1121 return WINED3D_OK;
1124 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1126 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1127 unsigned int range_count = buffer->modified_areas;
1128 struct wined3d_device *device = resource->device;
1129 struct wined3d_context *context;
1130 struct wined3d_bo_address addr;
1132 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1134 if (sub_resource_idx)
1136 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
1137 return E_INVALIDARG;
1140 if (!resource->map_count)
1142 WARN("Unmap called without a previous map call.\n");
1143 return WINED3D_OK;
1146 if (--resource->map_count)
1148 /* Delay loading the buffer until everything is unmapped. */
1149 TRACE("Ignoring unmap.\n");
1150 return WINED3D_OK;
1153 if (!buffer->map_ptr)
1154 return WINED3D_OK;
1156 context = context_acquire(device, NULL, 0);
1158 if (buffer->flags & WINED3D_BUFFER_APPLESYNC)
1160 struct wined3d_context_gl *context_gl;
1161 const struct wined3d_gl_info *gl_info;
1162 struct wined3d_buffer_gl *buffer_gl;
1163 unsigned int i;
1165 buffer_gl = wined3d_buffer_gl(buffer);
1166 context_gl = wined3d_context_gl(context);
1167 gl_info = context_gl->gl_info;
1169 wined3d_buffer_gl_bind(buffer_gl, context_gl);
1170 for (i = 0; i < range_count; ++i)
1172 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer_gl->buffer_type_hint,
1173 buffer->maps[i].offset, buffer->maps[i].size));
1174 checkGLcall("glFlushMappedBufferRangeAPPLE");
1176 range_count = 0;
1179 addr.buffer_object = buffer->buffer_object;
1180 addr.addr = 0;
1181 wined3d_context_unmap_bo_address(context, &addr, resource->bind_flags, range_count, buffer->maps);
1183 context_release(context);
1185 buffer_clear_dirty_areas(buffer);
1186 buffer->map_ptr = NULL;
1188 return WINED3D_OK;
1191 void wined3d_buffer_copy(struct wined3d_buffer *dst_buffer, unsigned int dst_offset,
1192 struct wined3d_buffer *src_buffer, unsigned int src_offset, unsigned int size)
1194 struct wined3d_bo_address dst, src;
1195 struct wined3d_context *context;
1196 DWORD dst_location;
1198 buffer_mark_used(dst_buffer);
1199 buffer_mark_used(src_buffer);
1201 dst_location = wined3d_buffer_get_memory(dst_buffer, &dst, dst_buffer->locations);
1202 dst.addr += dst_offset;
1204 wined3d_buffer_get_memory(src_buffer, &src, src_buffer->locations);
1205 src.addr += src_offset;
1207 context = context_acquire(dst_buffer->resource.device, NULL, 0);
1208 wined3d_context_copy_bo_address(context, &dst, dst_buffer->resource.bind_flags,
1209 &src, src_buffer->resource.bind_flags, size);
1210 context_release(context);
1212 wined3d_buffer_invalidate_range(dst_buffer, ~dst_location, dst_offset, size);
1215 void wined3d_buffer_upload_data(struct wined3d_buffer *buffer, struct wined3d_context *context,
1216 const struct wined3d_box *box, const void *data)
1218 struct wined3d_map_range range;
1220 if (box)
1222 range.offset = box->left;
1223 range.size = box->right - box->left;
1225 else
1227 range.offset = 0;
1228 range.size = buffer->resource.size;
1231 buffer->buffer_ops->buffer_upload_ranges(buffer, context, data, range.offset, 1, &range);
1234 static void wined3d_buffer_init_data(struct wined3d_buffer *buffer,
1235 struct wined3d_device *device, const struct wined3d_sub_resource_data *data)
1237 struct wined3d_resource *resource = &buffer->resource;
1238 struct wined3d_bo_address bo;
1239 struct wined3d_box box;
1241 if (buffer->flags & WINED3D_BUFFER_USE_BO)
1243 wined3d_box_set(&box, 0, 0, resource->size, 1, 0, 1);
1244 wined3d_cs_emit_update_sub_resource(device->cs, resource,
1245 0, &box, data->data, data->row_pitch, data->slice_pitch);
1247 else
1249 wined3d_buffer_get_memory(buffer, &bo, WINED3D_LOCATION_SYSMEM);
1250 memcpy(bo.addr, data->data, resource->size);
1251 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_SYSMEM);
1252 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_SYSMEM);
1256 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1258 return wined3d_buffer_incref(buffer_from_resource(resource));
1261 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1263 return wined3d_buffer_decref(buffer_from_resource(resource));
1266 static void buffer_resource_preload(struct wined3d_resource *resource)
1268 struct wined3d_context *context;
1270 context = context_acquire(resource->device, NULL, 0);
1271 wined3d_buffer_load(buffer_from_resource(resource), context, NULL);
1272 context_release(context);
1275 static const struct wined3d_resource_ops buffer_resource_ops =
1277 buffer_resource_incref,
1278 buffer_resource_decref,
1279 buffer_resource_preload,
1280 buffer_resource_unload,
1281 buffer_resource_sub_resource_map,
1282 buffer_resource_sub_resource_unmap,
1285 GLenum wined3d_buffer_gl_binding_from_bind_flags(const struct wined3d_gl_info *gl_info, uint32_t bind_flags)
1287 if (!bind_flags)
1288 return GL_PIXEL_UNPACK_BUFFER;
1290 if (bind_flags == WINED3D_BIND_INDEX_BUFFER)
1291 return GL_ELEMENT_ARRAY_BUFFER;
1293 if (bind_flags & (WINED3D_BIND_SHADER_RESOURCE | WINED3D_BIND_UNORDERED_ACCESS)
1294 && gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1295 return GL_TEXTURE_BUFFER;
1297 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
1298 return GL_UNIFORM_BUFFER;
1300 if (bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1301 return GL_TRANSFORM_FEEDBACK_BUFFER;
1303 if (bind_flags & ~(WINED3D_BIND_VERTEX_BUFFER | WINED3D_BIND_INDEX_BUFFER))
1304 FIXME("Unhandled bind flags %#x.\n", bind_flags);
1306 return GL_ARRAY_BUFFER;
1309 static HRESULT wined3d_buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1310 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1311 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_buffer_ops *buffer_ops)
1313 const struct wined3d_format *format = wined3d_get_format(device->adapter, WINED3DFMT_UNKNOWN, desc->bind_flags);
1314 struct wined3d_resource *resource = &buffer->resource;
1315 HRESULT hr;
1317 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1318 "access %s, data %p, parent %p, parent_ops %p.\n",
1319 buffer, device, desc->byte_width, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
1320 wined3d_debug_resource_access(desc->access), data, parent, parent_ops);
1322 if (!desc->byte_width)
1324 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1325 return E_INVALIDARG;
1328 if (desc->bind_flags & WINED3D_BIND_CONSTANT_BUFFER && desc->byte_width & (WINED3D_CONSTANT_BUFFER_ALIGNMENT - 1))
1330 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc->byte_width);
1331 return E_INVALIDARG;
1334 if (data && !data->data)
1336 WARN("Invalid sub-resource data specified.\n");
1337 return E_INVALIDARG;
1340 if (FAILED(hr = resource_init(resource, device, WINED3D_RTYPE_BUFFER, format,
1341 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->bind_flags, desc->access,
1342 desc->byte_width, 1, 1, desc->byte_width, parent, parent_ops, &buffer_resource_ops)))
1344 WARN("Failed to initialize resource, hr %#x.\n", hr);
1345 return hr;
1347 buffer->buffer_ops = buffer_ops;
1348 buffer->structure_byte_stride = desc->structure_byte_stride;
1349 buffer->locations = data ? WINED3D_LOCATION_DISCARDED : WINED3D_LOCATION_SYSMEM;
1351 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1352 buffer, buffer->resource.size, buffer->resource.usage, buffer->resource.heap_memory);
1354 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1355 || wined3d_resource_access_is_managed(desc->access))
1357 /* SWvp and managed buffers always return the same pointer in buffer
1358 * maps and retain data in DISCARD maps. Keep a system memory copy of
1359 * the buffer to provide the same behavior to the application. */
1360 TRACE("Pinning system memory.\n");
1361 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1362 buffer->locations = WINED3D_LOCATION_SYSMEM;
1365 if (buffer->locations & WINED3D_LOCATION_SYSMEM || !(buffer->flags & WINED3D_BUFFER_USE_BO))
1367 if (!wined3d_resource_prepare_sysmem(&buffer->resource))
1368 return E_OUTOFMEMORY;
1371 if (!(buffer->maps = heap_alloc(sizeof(*buffer->maps))))
1373 ERR("Out of memory.\n");
1374 buffer_resource_unload(resource);
1375 resource_cleanup(resource);
1376 wined3d_resource_wait_idle(resource);
1377 return E_OUTOFMEMORY;
1379 buffer->maps_size = 1;
1381 if (data)
1382 wined3d_buffer_init_data(buffer, device, data);
1384 return WINED3D_OK;
1387 static BOOL wined3d_buffer_no3d_prepare_location(struct wined3d_buffer *buffer,
1388 struct wined3d_context *context, unsigned int location)
1390 if (location == WINED3D_LOCATION_SYSMEM)
1391 return wined3d_resource_prepare_sysmem(&buffer->resource);
1393 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
1395 return FALSE;
1398 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer *buffer,
1399 struct wined3d_context *context, unsigned int location)
1401 TRACE("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1404 static void wined3d_buffer_no3d_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1405 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1407 FIXME("Not implemented.\n");
1410 static void wined3d_buffer_no3d_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1411 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1413 FIXME("Not implemented.\n");
1416 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops =
1418 wined3d_buffer_no3d_prepare_location,
1419 wined3d_buffer_no3d_unload_location,
1420 wined3d_buffer_no3d_upload_ranges,
1421 wined3d_buffer_no3d_download_ranges,
1424 HRESULT wined3d_buffer_no3d_init(struct wined3d_buffer *buffer_no3d, struct wined3d_device *device,
1425 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1426 void *parent, const struct wined3d_parent_ops *parent_ops)
1428 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1429 buffer_no3d, device, desc, data, parent, parent_ops);
1431 return wined3d_buffer_init(buffer_no3d, device, desc, data, parent, parent_ops, &wined3d_buffer_no3d_ops);
1434 static BOOL wined3d_buffer_gl_prepare_location(struct wined3d_buffer *buffer,
1435 struct wined3d_context *context, unsigned int location)
1437 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1438 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1440 switch (location)
1442 case WINED3D_LOCATION_SYSMEM:
1443 return wined3d_resource_prepare_sysmem(&buffer->resource);
1445 case WINED3D_LOCATION_BUFFER:
1446 if (buffer->buffer_object)
1447 return TRUE;
1449 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
1451 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer);
1452 return FALSE;
1454 return wined3d_buffer_gl_create_buffer_object(buffer_gl, context_gl);
1456 default:
1457 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1458 return FALSE;
1462 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer *buffer,
1463 struct wined3d_context *context, unsigned int location)
1465 TRACE("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1467 switch (location)
1469 case WINED3D_LOCATION_BUFFER:
1470 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer), wined3d_context_gl(context));
1471 break;
1473 default:
1474 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
1475 break;
1479 /* Context activation is done by the caller. */
1480 static void wined3d_buffer_gl_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1481 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1483 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1484 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1485 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1486 const struct wined3d_map_range *range;
1488 wined3d_buffer_gl_bind(buffer_gl, context_gl);
1490 while (range_count--)
1492 range = &ranges[range_count];
1493 GL_EXTCALL(glBufferSubData(buffer_gl->buffer_type_hint,
1494 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
1496 checkGLcall("buffer upload");
1499 /* Context activation is done by the caller. */
1500 static void wined3d_buffer_gl_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1501 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1503 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1504 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1505 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1506 const struct wined3d_map_range *range;
1508 wined3d_buffer_gl_bind(buffer_gl, context_gl);
1510 while (range_count--)
1512 range = &ranges[range_count];
1513 GL_EXTCALL(glGetBufferSubData(buffer_gl->buffer_type_hint,
1514 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
1516 checkGLcall("buffer download");
1519 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops =
1521 wined3d_buffer_gl_prepare_location,
1522 wined3d_buffer_gl_unload_location,
1523 wined3d_buffer_gl_upload_ranges,
1524 wined3d_buffer_gl_download_ranges,
1527 HRESULT wined3d_buffer_gl_init(struct wined3d_buffer_gl *buffer_gl, struct wined3d_device *device,
1528 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1529 void *parent, const struct wined3d_parent_ops *parent_ops)
1531 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1533 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1534 buffer_gl, device, desc, data, parent, parent_ops);
1536 /* Observations show that draw_primitive_immediate_mode() is faster on
1537 * dynamic vertex buffers than converting + draw_primitive_arrays().
1538 * (Half-Life 2 and others.) */
1539 if (!(desc->access & WINED3D_RESOURCE_ACCESS_GPU))
1540 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1541 else if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1542 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1543 else if (!(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE])
1544 && (desc->usage & WINED3DUSAGE_DYNAMIC))
1545 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1546 else
1547 buffer_gl->b.flags |= WINED3D_BUFFER_USE_BO;
1548 buffer_gl->buffer_type_hint = wined3d_buffer_gl_binding_from_bind_flags(gl_info, desc->bind_flags);
1550 return wined3d_buffer_init(&buffer_gl->b, device, desc, data, parent, parent_ops, &wined3d_buffer_gl_ops);
1553 static BOOL wined3d_buffer_vk_prepare_location(struct wined3d_buffer *buffer,
1554 struct wined3d_context *context, unsigned int location)
1556 switch (location)
1558 case WINED3D_LOCATION_SYSMEM:
1559 return wined3d_resource_prepare_sysmem(&buffer->resource);
1561 case WINED3D_LOCATION_BUFFER:
1562 /* The Vulkan buffer is created during resource creation. */
1563 return TRUE;
1565 default:
1566 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
1567 return FALSE;
1571 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer *buffer,
1572 struct wined3d_context *context, unsigned int location)
1574 FIXME("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1577 static void wined3d_buffer_vk_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1578 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1580 FIXME("Not implemented.\n");
1583 static void wined3d_buffer_vk_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1584 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_map_range *ranges)
1586 FIXME("Not implemented.\n");
1589 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops =
1591 wined3d_buffer_vk_prepare_location,
1592 wined3d_buffer_vk_unload_location,
1593 wined3d_buffer_vk_upload_ranges,
1594 wined3d_buffer_vk_download_ranges,
1597 HRESULT wined3d_buffer_vk_init(struct wined3d_buffer_vk *buffer_vk, struct wined3d_device *device,
1598 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1599 void *parent, const struct wined3d_parent_ops *parent_ops)
1601 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1602 buffer_vk, device, desc, data, parent, parent_ops);
1604 return wined3d_buffer_init(&buffer_vk->b, device, desc, data, parent, parent_ops, &wined3d_buffer_vk_ops);
1607 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1608 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops,
1609 struct wined3d_buffer **buffer)
1611 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1612 device, desc, data, parent, parent_ops, buffer);
1614 return device->adapter->adapter_ops->adapter_create_buffer(device, desc, data, parent, parent_ops, buffer);