wined3d: Remove dead statement.
[wine/zf.git] / dlls / wined3d / buffer.c
blob8fca81ec19ca13067b7f141728d912c36cdc49e5
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. */
36 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
37 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
38 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
39 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
41 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer *buffer)
43 if (buffer->flags & WINED3D_BUFFER_PIN_SYSMEM)
45 TRACE("Not evicting system memory for buffer %p.\n", buffer);
46 return;
49 TRACE("Evicting system memory for buffer %p.\n", buffer);
50 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_SYSMEM);
51 wined3d_resource_free_sysmem(&buffer->resource);
54 static void buffer_invalidate_bo_range(struct wined3d_buffer *buffer, unsigned int offset, unsigned int size)
56 if (!offset && (!size || size == buffer->resource.size))
57 goto invalidate_all;
59 if (offset > buffer->resource.size || size > buffer->resource.size - offset)
61 WARN("Invalid range specified, invalidating entire buffer.\n");
62 goto invalidate_all;
65 if (!wined3d_array_reserve((void **)&buffer->maps, &buffer->maps_size,
66 buffer->modified_areas + 1, sizeof(*buffer->maps)))
68 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
69 goto invalidate_all;
72 buffer->maps[buffer->modified_areas].offset = offset;
73 buffer->maps[buffer->modified_areas].size = size;
74 ++buffer->modified_areas;
75 return;
77 invalidate_all:
78 buffer->modified_areas = 1;
79 buffer->maps[0].offset = 0;
80 buffer->maps[0].size = buffer->resource.size;
83 static inline void buffer_clear_dirty_areas(struct wined3d_buffer *This)
85 This->modified_areas = 0;
88 static BOOL buffer_is_dirty(const struct wined3d_buffer *buffer)
90 return !!buffer->modified_areas;
93 static BOOL buffer_is_fully_dirty(const struct wined3d_buffer *buffer)
95 return buffer->modified_areas == 1
96 && !buffer->maps->offset && buffer->maps->size == buffer->resource.size;
99 static void wined3d_buffer_validate_location(struct wined3d_buffer *buffer, DWORD location)
101 TRACE("buffer %p, location %s.\n", buffer, wined3d_debug_location(location));
103 if (location & WINED3D_LOCATION_BUFFER)
104 buffer_clear_dirty_areas(buffer);
106 buffer->locations |= location;
108 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer->locations));
111 static void wined3d_buffer_invalidate_range(struct wined3d_buffer *buffer, DWORD location,
112 unsigned int offset, unsigned int size)
114 TRACE("buffer %p, location %s, offset %u, size %u.\n",
115 buffer, wined3d_debug_location(location), offset, size);
117 if (location & WINED3D_LOCATION_BUFFER)
118 buffer_invalidate_bo_range(buffer, offset, size);
120 buffer->locations &= ~location;
122 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer->locations));
124 if (!buffer->locations)
125 ERR("Buffer %p does not have any up to date location.\n", buffer);
128 void wined3d_buffer_invalidate_location(struct wined3d_buffer *buffer, DWORD location)
130 wined3d_buffer_invalidate_range(buffer, location, 0, 0);
133 /* Context activation is done by the caller. */
134 static void wined3d_buffer_gl_bind(struct wined3d_buffer_gl *buffer_gl, struct wined3d_context_gl *context_gl)
136 wined3d_context_gl_bind_bo(context_gl, buffer_gl->bo.binding, buffer_gl->bo.id);
139 /* Context activation is done by the caller. */
140 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl *buffer_gl,
141 struct wined3d_context_gl *context_gl)
143 struct wined3d_resource *resource = &buffer_gl->b.resource;
145 if (!buffer_gl->b.buffer_object)
146 return;
148 if (context_gl->c.transform_feedback_active && resource->bind_count
149 && resource->bind_flags & WINED3D_BIND_STREAM_OUTPUT)
151 /* We have to make sure that transform feedback is not active
152 * when deleting a potentially bound transform feedback buffer.
153 * This may happen when the device is being destroyed. */
154 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl);
155 wined3d_context_gl_end_transform_feedback(context_gl);
158 buffer_gl->bo_user.valid = false;
159 list_remove(&buffer_gl->bo_user.entry);
160 wined3d_context_gl_destroy_bo(context_gl, &buffer_gl->bo);
161 buffer_gl->b.buffer_object = 0;
164 /* Context activation is done by the caller. */
165 static BOOL wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl *buffer_gl,
166 struct wined3d_context_gl *context_gl)
168 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
169 GLenum usage = GL_STATIC_DRAW;
170 GLbitfield gl_storage_flags;
171 struct wined3d_bo_gl *bo;
172 bool coherent = true;
173 GLsizeiptr size;
174 GLenum binding;
176 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
177 buffer_gl, debug_d3dusage(buffer_gl->b.resource.usage));
179 size = buffer_gl->b.resource.size;
180 binding = wined3d_buffer_gl_binding_from_bind_flags(gl_info, buffer_gl->b.resource.bind_flags);
181 if (buffer_gl->b.resource.usage & WINED3DUSAGE_DYNAMIC)
183 usage = GL_STREAM_DRAW_ARB;
184 coherent = false;
186 gl_storage_flags = wined3d_resource_gl_storage_flags(&buffer_gl->b.resource);
187 bo = &buffer_gl->bo;
188 if (!wined3d_context_gl_create_bo(context_gl, size, binding, usage, coherent, gl_storage_flags, bo))
190 ERR("Failed to create OpenGL buffer object.\n");
191 buffer_gl->b.flags &= ~WINED3D_BUFFER_USE_BO;
192 buffer_clear_dirty_areas(&buffer_gl->b);
193 return FALSE;
196 list_add_head(&buffer_gl->bo.users, &buffer_gl->bo_user.entry);
197 buffer_gl->b.buffer_object = (uintptr_t)bo;
198 buffer_invalidate_bo_range(&buffer_gl->b, 0, 0);
200 return TRUE;
203 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *buffer,
204 const enum wined3d_buffer_conversion_type conversion_type,
205 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
207 const struct wined3d_format *format = attrib->format;
208 BOOL ret = FALSE;
209 unsigned int i;
210 DWORD_PTR data;
212 /* Check for some valid situations which cause us pain. One is if the buffer is used for
213 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
214 * with different strides. In the 2nd case we might have to drop conversion entirely,
215 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
217 if (!attrib->stride)
219 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
220 debug_d3dformat(format->id));
222 else if (attrib->stride != *stride_this_run && *stride_this_run)
224 FIXME("Got two concurrent strides, %d and %d.\n", attrib->stride, *stride_this_run);
226 else
228 *stride_this_run = attrib->stride;
229 if (buffer->stride != *stride_this_run)
231 /* We rely that this happens only on the first converted attribute that is found,
232 * if at all. See above check
234 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
235 buffer->stride = *stride_this_run;
236 heap_free(buffer->conversion_map);
237 buffer->conversion_map = heap_calloc(buffer->stride, sizeof(*buffer->conversion_map));
238 ret = TRUE;
242 data = ((DWORD_PTR)attrib->data.addr) % buffer->stride;
243 for (i = 0; i < format->byte_count; ++i)
245 DWORD_PTR idx = (data + i) % buffer->stride;
246 if (buffer->conversion_map[idx] != conversion_type)
248 TRACE("Byte %lu in vertex changed:\n", idx);
249 TRACE(" It was type %#x, is %#x now.\n", buffer->conversion_map[idx], conversion_type);
250 ret = TRUE;
251 buffer->conversion_map[idx] = conversion_type;
255 return ret;
258 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
259 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
261 static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
262 const struct wined3d_state *state, UINT attrib_idx, DWORD fixup_flags, DWORD *stride_this_run)
264 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
265 enum wined3d_format_id format;
266 BOOL ret = FALSE;
268 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
269 * there, on nonexistent attribs the vbo is 0.
271 if (!(si->use_map & (1u << attrib_idx))
272 || state->streams[attrib->stream_idx].buffer != This)
273 return FALSE;
275 format = attrib->format->id;
276 /* Look for newly appeared conversion */
277 if (fixup_flags & WINED3D_BUFFER_FIXUP_D3DCOLOR && format == WINED3DFMT_B8G8R8A8_UNORM)
279 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
281 else if (fixup_flags & WINED3D_BUFFER_FIXUP_XYZRHW && si->position_transformed)
283 if (format != WINED3DFMT_R32G32B32A32_FLOAT)
285 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format));
286 return FALSE;
289 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
291 else if (This->conversion_map)
293 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
296 return ret;
299 static BOOL buffer_find_decl(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
300 const struct wined3d_state *state, DWORD fixup_flags)
302 UINT stride_this_run = 0;
303 BOOL ret = FALSE;
305 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
306 * Once we have our declaration there is no need to look it up again. Index buffers also never need
307 * conversion, so once the (empty) conversion structure is created don't bother checking again
309 if (This->flags & WINED3D_BUFFER_HASDESC)
311 if(This->resource.usage & WINED3DUSAGE_STATICDECL) return FALSE;
314 if (!fixup_flags)
316 TRACE("No fixup required.\n");
317 if(This->conversion_map)
319 heap_free(This->conversion_map);
320 This->conversion_map = NULL;
321 This->stride = 0;
322 return TRUE;
325 return FALSE;
328 TRACE("Finding vertex buffer conversion information\n");
329 /* Certain declaration types need some fixups before we can pass them to
330 * opengl. This means D3DCOLOR attributes with fixed function vertex
331 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
332 * GL_ARB_half_float_vertex is not supported.
334 * Note for d3d8 and d3d9:
335 * The vertex buffer FVF doesn't help with finding them, we have to use
336 * the decoded vertex declaration and pick the things that concern the
337 * current buffer. A problem with this is that this can change between
338 * draws, so we have to validate the information and reprocess the buffer
339 * if it changes, and avoid false positives for performance reasons.
340 * WineD3D doesn't even know the vertex buffer any more, it is managed
341 * by the client libraries and passed to SetStreamSource and ProcessVertices
342 * as needed.
344 * We have to distinguish between vertex shaders and fixed function to
345 * pick the way we access the strided vertex information.
347 * This code sets up a per-byte array with the size of the detected
348 * stride of the arrays in the buffer. For each byte we have a field
349 * that marks the conversion needed on this byte. For example, the
350 * following declaration with fixed function vertex processing:
352 * POSITIONT, FLOAT4
353 * NORMAL, FLOAT3
354 * DIFFUSE, FLOAT16_4
355 * SPECULAR, D3DCOLOR
357 * Will result in
358 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
359 * [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]
361 * Where in this example map P means 4 component position conversion, 0
362 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
363 * conversion (red / blue swizzle).
365 * If we're doing conversion and the stride changes we have to reconvert
366 * the whole buffer. Note that we do not mind if the semantic changes,
367 * we only care for the conversion type. So if the NORMAL is replaced
368 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
369 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
370 * conversion types depend on the semantic as well, for example a FLOAT4
371 * texcoord needs no conversion while a FLOAT4 positiont needs one
374 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_POSITION,
375 fixup_flags, &stride_this_run) || ret;
376 fixup_flags &= ~WINED3D_BUFFER_FIXUP_XYZRHW;
378 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDWEIGHT,
379 fixup_flags, &stride_this_run) || ret;
380 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_BLENDINDICES,
381 fixup_flags, &stride_this_run) || ret;
382 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_NORMAL,
383 fixup_flags, &stride_this_run) || ret;
384 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_DIFFUSE,
385 fixup_flags, &stride_this_run) || ret;
386 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_SPECULAR,
387 fixup_flags, &stride_this_run) || ret;
388 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD0,
389 fixup_flags, &stride_this_run) || ret;
390 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD1,
391 fixup_flags, &stride_this_run) || ret;
392 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD2,
393 fixup_flags, &stride_this_run) || ret;
394 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD3,
395 fixup_flags, &stride_this_run) || ret;
396 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD4,
397 fixup_flags, &stride_this_run) || ret;
398 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD5,
399 fixup_flags, &stride_this_run) || ret;
400 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD6,
401 fixup_flags, &stride_this_run) || ret;
402 ret = buffer_check_attribute(This, si, state, WINED3D_FFP_TEXCOORD7,
403 fixup_flags, &stride_this_run) || ret;
405 if (!stride_this_run && This->conversion_map)
407 /* Sanity test */
408 if (!ret)
409 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
410 heap_free(This->conversion_map);
411 This->conversion_map = NULL;
412 This->stride = 0;
415 if (ret) TRACE("Conversion information changed\n");
417 return ret;
420 static inline unsigned int fixup_d3dcolor(DWORD *dst_color)
422 DWORD src_color = *dst_color;
424 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
425 * endianness. If we want this to work on big-endian machines as well we
426 * have to consider more things.
428 * 0xff000000: Alpha mask
429 * 0x00ff0000: Blue mask
430 * 0x0000ff00: Green mask
431 * 0x000000ff: Red mask
433 *dst_color = 0;
434 *dst_color |= (src_color & 0xff00ff00u); /* Alpha Green */
435 *dst_color |= (src_color & 0x00ff0000u) >> 16; /* Red */
436 *dst_color |= (src_color & 0x000000ffu) << 16; /* Blue */
438 return sizeof(*dst_color);
441 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4 *p)
443 /* rhw conversion like in position_float4(). */
444 if (p->w != 1.0f && p->w != 0.0f)
446 float w = 1.0f / p->w;
447 p->x *= w;
448 p->y *= w;
449 p->z *= w;
450 p->w = w;
453 return sizeof(*p);
456 ULONG CDECL wined3d_buffer_incref(struct wined3d_buffer *buffer)
458 ULONG refcount = InterlockedIncrement(&buffer->resource.ref);
460 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
462 return refcount;
465 static void buffer_conversion_upload(struct wined3d_buffer *buffer, struct wined3d_context *context)
467 unsigned int i, j, range_idx, start, end, vertex_count;
468 BYTE *data;
470 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
472 ERR("Failed to load system memory.\n");
473 return;
475 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
477 /* Now for each vertex in the buffer that needs conversion. */
478 vertex_count = buffer->resource.size / buffer->stride;
480 if (!(data = heap_alloc(buffer->resource.size)))
482 ERR("Out of memory.\n");
483 return;
486 for (range_idx = 0; range_idx < buffer->modified_areas; ++range_idx)
488 start = buffer->maps[range_idx].offset;
489 end = start + buffer->maps[range_idx].size;
491 memcpy(data + start, (BYTE *)buffer->resource.heap_memory + start, end - start);
492 for (i = start / buffer->stride; i < min((end / buffer->stride) + 1, vertex_count); ++i)
494 for (j = 0; j < buffer->stride;)
496 switch (buffer->conversion_map[j])
498 case CONV_NONE:
499 /* Done already */
500 j += sizeof(DWORD);
501 break;
502 case CONV_D3DCOLOR:
503 j += fixup_d3dcolor((DWORD *) (data + i * buffer->stride + j));
504 break;
505 case CONV_POSITIONT:
506 j += fixup_transformed_pos((struct wined3d_vec4 *) (data + i * buffer->stride + j));
507 break;
508 default:
509 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer->conversion_map[j]);
510 ++j;
516 buffer->buffer_ops->buffer_upload_ranges(buffer, context,
517 data, 0, buffer->modified_areas, buffer->maps);
519 heap_free(data);
522 BOOL wined3d_buffer_prepare_location(struct wined3d_buffer *buffer,
523 struct wined3d_context *context, unsigned int location)
525 return buffer->buffer_ops->buffer_prepare_location(buffer, context, location);
528 static void wined3d_buffer_unload_location(struct wined3d_buffer *buffer,
529 struct wined3d_context *context, unsigned int location)
531 buffer->buffer_ops->buffer_unload_location(buffer, context, location);
534 BOOL wined3d_buffer_load_location(struct wined3d_buffer *buffer,
535 struct wined3d_context *context, DWORD location)
537 struct wined3d_range range;
539 TRACE("buffer %p, context %p, location %s.\n",
540 buffer, context, wined3d_debug_location(location));
542 if (buffer->locations & location)
544 TRACE("Location (%#x) is already up to date.\n", location);
545 return TRUE;
548 if (!buffer->locations)
550 ERR("Buffer %p does not have any up to date location.\n", buffer);
551 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_DISCARDED);
552 return wined3d_buffer_load_location(buffer, context, location);
555 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer->locations));
557 if (!wined3d_buffer_prepare_location(buffer, context, location))
558 return FALSE;
560 if (buffer->locations & WINED3D_LOCATION_DISCARDED)
562 TRACE("Buffer previously discarded, nothing to do.\n");
563 wined3d_buffer_validate_location(buffer, location);
564 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_DISCARDED);
565 return TRUE;
568 switch (location)
570 case WINED3D_LOCATION_SYSMEM:
571 range.offset = 0;
572 range.size = buffer->resource.size;
573 buffer->buffer_ops->buffer_download_ranges(buffer, context,
574 buffer->resource.heap_memory, 0, 1, &range);
575 break;
577 case WINED3D_LOCATION_BUFFER:
578 if (!buffer->conversion_map)
579 buffer->buffer_ops->buffer_upload_ranges(buffer, context,
580 buffer->resource.heap_memory, 0, buffer->modified_areas, buffer->maps);
581 else
582 buffer_conversion_upload(buffer, context);
583 break;
585 default:
586 ERR("Invalid location %s.\n", wined3d_debug_location(location));
587 return FALSE;
590 wined3d_buffer_validate_location(buffer, location);
591 if (buffer->resource.heap_memory && location == WINED3D_LOCATION_BUFFER
592 && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
593 wined3d_buffer_evict_sysmem(buffer);
595 return TRUE;
598 /* Context activation is done by the caller. */
599 BYTE *wined3d_buffer_load_sysmem(struct wined3d_buffer *buffer, struct wined3d_context *context)
601 if (wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM))
602 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
603 return buffer->resource.heap_memory;
606 DWORD wined3d_buffer_get_memory(struct wined3d_buffer *buffer,
607 struct wined3d_bo_address *data, DWORD locations)
609 TRACE("buffer %p, data %p, locations %s.\n",
610 buffer, data, wined3d_debug_location(locations));
612 if (locations & WINED3D_LOCATION_BUFFER)
614 data->buffer_object = buffer->buffer_object;
615 data->addr = NULL;
616 return WINED3D_LOCATION_BUFFER;
618 if (locations & WINED3D_LOCATION_SYSMEM)
620 data->buffer_object = 0;
621 data->addr = buffer->resource.heap_memory;
622 return WINED3D_LOCATION_SYSMEM;
625 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
626 data->buffer_object = 0;
627 data->addr = NULL;
628 return 0;
631 static void buffer_resource_unload(struct wined3d_resource *resource)
633 struct wined3d_buffer *buffer = buffer_from_resource(resource);
635 TRACE("buffer %p.\n", buffer);
637 if (buffer->buffer_object)
639 struct wined3d_context *context;
641 context = context_acquire(resource->device, NULL, 0);
643 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
644 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
645 wined3d_buffer_unload_location(buffer, context, WINED3D_LOCATION_BUFFER);
646 buffer_clear_dirty_areas(buffer);
648 context_release(context);
650 heap_free(buffer->conversion_map);
651 buffer->conversion_map = NULL;
652 buffer->stride = 0;
653 buffer->conversion_stride = 0;
654 buffer->flags &= ~WINED3D_BUFFER_HASDESC;
657 resource_unload(resource);
660 static void wined3d_buffer_drop_bo(struct wined3d_buffer *buffer)
662 buffer->flags &= ~WINED3D_BUFFER_USE_BO;
663 buffer_resource_unload(&buffer->resource);
666 static void wined3d_buffer_destroy_object(void *object)
668 struct wined3d_buffer *buffer = object;
669 struct wined3d_context *context;
671 TRACE("buffer %p.\n", buffer);
673 if (buffer->buffer_object)
675 context = context_acquire(buffer->resource.device, NULL, 0);
676 wined3d_buffer_unload_location(buffer, context, WINED3D_LOCATION_BUFFER);
677 context_release(context);
679 heap_free(buffer->conversion_map);
680 heap_free(buffer->maps);
683 void wined3d_buffer_cleanup(struct wined3d_buffer *buffer)
685 wined3d_cs_destroy_object(buffer->resource.device->cs, wined3d_buffer_destroy_object, buffer);
686 resource_cleanup(&buffer->resource);
689 ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
691 ULONG refcount = InterlockedDecrement(&buffer->resource.ref);
693 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
695 if (!refcount)
697 buffer->resource.parent_ops->wined3d_object_destroyed(buffer->resource.parent);
698 buffer->resource.device->adapter->adapter_ops->adapter_destroy_buffer(buffer);
701 return refcount;
704 void * CDECL wined3d_buffer_get_parent(const struct wined3d_buffer *buffer)
706 TRACE("buffer %p.\n", buffer);
708 return buffer->resource.parent;
711 /* Context activation is done by the caller. */
712 void wined3d_buffer_load(struct wined3d_buffer *buffer, struct wined3d_context *context,
713 const struct wined3d_state *state)
715 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
716 BOOL decl_changed = FALSE;
718 TRACE("buffer %p.\n", buffer);
720 if (buffer->resource.map_count && buffer->map_ptr)
722 FIXME("Buffer is mapped through buffer object, not loading.\n");
723 return;
725 else if (buffer->resource.map_count)
727 WARN("Loading mapped buffer.\n");
730 /* TODO: Make converting independent from VBOs */
731 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
733 /* Not doing any conversion */
734 return;
737 if (!wined3d_buffer_prepare_location(buffer, context, WINED3D_LOCATION_BUFFER))
739 ERR("Failed to prepare buffer location.\n");
740 return;
743 /* Reading the declaration makes only sense if we have valid state information
744 * (i.e., if this function is called during draws). */
745 if (state)
747 DWORD fixup_flags = 0;
749 if (!use_vs(state))
751 if (!d3d_info->vertex_bgra && !d3d_info->ffp_generic_attributes)
752 fixup_flags |= WINED3D_BUFFER_FIXUP_D3DCOLOR;
753 if (!d3d_info->xyzrhw)
754 fixup_flags |= WINED3D_BUFFER_FIXUP_XYZRHW;
757 decl_changed = buffer_find_decl(buffer, &context->stream_info, state, fixup_flags);
758 buffer->flags |= WINED3D_BUFFER_HASDESC;
761 if (!decl_changed && !(buffer->flags & WINED3D_BUFFER_HASDESC && buffer_is_dirty(buffer)))
763 ++buffer->draw_count;
764 if (buffer->draw_count > VB_RESETDECLCHANGE)
765 buffer->decl_change_count = 0;
766 if (buffer->draw_count > VB_RESETFULLCONVS)
767 buffer->full_conversion_count = 0;
768 return;
771 /* If applications change the declaration over and over, reconverting all the time is a huge
772 * performance hit. So count the declaration changes and release the VBO if there are too many
773 * of them (and thus stop converting)
775 if (decl_changed)
777 ++buffer->decl_change_count;
778 buffer->draw_count = 0;
780 if (buffer->decl_change_count > VB_MAXDECLCHANGES
781 || (buffer->conversion_map && (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)))
783 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
784 wined3d_buffer_drop_bo(buffer);
785 return;
788 /* The declaration changed, reload the whole buffer. */
789 WARN("Reloading buffer because of a vertex declaration change.\n");
790 buffer_invalidate_bo_range(buffer, 0, 0);
792 else
794 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
795 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
796 * decl changes and reset the decl change count after a specific number of them
798 if (buffer->conversion_map && buffer_is_fully_dirty(buffer))
800 ++buffer->full_conversion_count;
801 if (buffer->full_conversion_count > VB_MAXFULLCONVERSIONS)
803 FIXME("Too many full buffer conversions, stopping converting.\n");
804 wined3d_buffer_drop_bo(buffer);
805 return;
808 else
810 ++buffer->draw_count;
811 if (buffer->draw_count > VB_RESETDECLCHANGE)
812 buffer->decl_change_count = 0;
813 if (buffer->draw_count > VB_RESETFULLCONVS)
814 buffer->full_conversion_count = 0;
818 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
819 ERR("Failed to load buffer location.\n");
822 struct wined3d_resource * CDECL wined3d_buffer_get_resource(struct wined3d_buffer *buffer)
824 TRACE("buffer %p.\n", buffer);
826 return &buffer->resource;
829 static HRESULT buffer_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
830 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, uint32_t flags)
832 struct wined3d_buffer *buffer = buffer_from_resource(resource);
833 struct wined3d_device *device = resource->device;
834 struct wined3d_context *context;
835 unsigned int offset, size;
836 uint8_t *base;
837 LONG count;
839 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
840 resource, sub_resource_idx, map_desc, debug_box(box), flags);
842 if (sub_resource_idx)
844 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
845 return E_INVALIDARG;
848 if (box)
850 offset = box->left;
851 size = box->right - box->left;
853 else
855 offset = size = 0;
858 map_desc->row_pitch = map_desc->slice_pitch = resource->size;
860 count = ++resource->map_count;
862 if (buffer->buffer_object)
864 unsigned int dirty_offset = offset, dirty_size = size;
865 struct wined3d_bo_address addr;
867 /* DISCARD invalidates the entire buffer, regardless of the specified
868 * offset and size. Some applications also depend on the entire buffer
869 * being uploaded in that case. Two such applications are Port Royale
870 * and Darkstar One. */
871 if (flags & WINED3D_MAP_DISCARD)
873 dirty_offset = 0;
874 dirty_size = 0;
877 if (((flags & WINED3D_MAP_WRITE) && !(flags & (WINED3D_MAP_NOOVERWRITE | WINED3D_MAP_DISCARD)))
878 || (!(flags & WINED3D_MAP_WRITE) && (buffer->locations & WINED3D_LOCATION_SYSMEM))
879 || buffer->flags & WINED3D_BUFFER_PIN_SYSMEM)
881 if (!(buffer->locations & WINED3D_LOCATION_SYSMEM))
883 context = context_acquire(device, NULL, 0);
884 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
885 context_release(context);
888 if (flags & WINED3D_MAP_WRITE)
889 wined3d_buffer_invalidate_range(buffer, WINED3D_LOCATION_BUFFER, dirty_offset, dirty_size);
891 else
893 context = context_acquire(device, NULL, 0);
895 if (flags & WINED3D_MAP_DISCARD)
896 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_BUFFER);
897 else
898 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
900 if (flags & WINED3D_MAP_WRITE)
902 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_SYSMEM);
903 buffer_invalidate_bo_range(buffer, dirty_offset, dirty_size);
906 if ((flags & WINED3D_MAP_DISCARD) && resource->heap_memory)
907 wined3d_buffer_evict_sysmem(buffer);
909 if (count == 1)
911 addr.buffer_object = buffer->buffer_object;
912 addr.addr = 0;
913 buffer->map_ptr = wined3d_context_map_bo_address(context, &addr, resource->size, flags);
915 if (((DWORD_PTR)buffer->map_ptr) & (RESOURCE_ALIGNMENT - 1))
917 WARN("Pointer %p is not %u byte aligned.\n", buffer->map_ptr, RESOURCE_ALIGNMENT);
919 wined3d_context_unmap_bo_address(context, &addr, 0, NULL);
920 buffer->map_ptr = NULL;
922 if (resource->usage & WINED3DUSAGE_DYNAMIC)
924 /* The extra copy is more expensive than not using VBOs
925 * at all on the NVIDIA Linux driver, which is the
926 * only driver that returns unaligned pointers. */
927 TRACE("Dynamic buffer, dropping VBO.\n");
928 wined3d_buffer_drop_bo(buffer);
930 else
932 TRACE("Falling back to doublebuffered operation.\n");
933 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_SYSMEM);
934 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
936 TRACE("New pointer is %p.\n", resource->heap_memory);
940 context_release(context);
944 base = buffer->map_ptr ? buffer->map_ptr : resource->heap_memory;
945 map_desc->data = base + offset;
947 TRACE("Returning memory at %p (base %p, offset %u).\n", map_desc->data, base, offset);
949 return WINED3D_OK;
952 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
954 struct wined3d_buffer *buffer = buffer_from_resource(resource);
955 unsigned int range_count = buffer->modified_areas;
956 struct wined3d_device *device = resource->device;
957 struct wined3d_context *context;
958 struct wined3d_bo_address addr;
960 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
962 if (sub_resource_idx)
964 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
965 return E_INVALIDARG;
968 if (!resource->map_count)
970 WARN("Unmap called without a previous map call.\n");
971 return WINED3D_OK;
974 if (--resource->map_count)
976 /* Delay loading the buffer until everything is unmapped. */
977 TRACE("Ignoring unmap.\n");
978 return WINED3D_OK;
981 if (!buffer->map_ptr)
982 return WINED3D_OK;
984 context = context_acquire(device, NULL, 0);
986 addr.buffer_object = buffer->buffer_object;
987 addr.addr = 0;
988 wined3d_context_unmap_bo_address(context, &addr, range_count, buffer->maps);
990 context_release(context);
992 buffer_clear_dirty_areas(buffer);
993 buffer->map_ptr = NULL;
995 return WINED3D_OK;
998 void wined3d_buffer_copy(struct wined3d_buffer *dst_buffer, unsigned int dst_offset,
999 struct wined3d_buffer *src_buffer, unsigned int src_offset, unsigned int size)
1001 struct wined3d_bo_address dst, src;
1002 struct wined3d_context *context;
1003 DWORD dst_location;
1005 TRACE("dst_buffer %p, dst_offset %u, src_buffer %p, src_offset %u, size %u.\n",
1006 dst_buffer, dst_offset, src_buffer, src_offset, size);
1008 dst_location = wined3d_buffer_get_memory(dst_buffer, &dst, dst_buffer->locations);
1009 dst.addr += dst_offset;
1011 wined3d_buffer_get_memory(src_buffer, &src, src_buffer->locations);
1012 src.addr += src_offset;
1014 context = context_acquire(dst_buffer->resource.device, NULL, 0);
1015 wined3d_context_copy_bo_address(context, &dst, &src, size);
1016 context_release(context);
1018 wined3d_buffer_invalidate_range(dst_buffer, ~dst_location, dst_offset, size);
1021 void wined3d_buffer_upload_data(struct wined3d_buffer *buffer, struct wined3d_context *context,
1022 const struct wined3d_box *box, const void *data)
1024 struct wined3d_range range;
1026 if (box)
1028 range.offset = box->left;
1029 range.size = box->right - box->left;
1031 else
1033 range.offset = 0;
1034 range.size = buffer->resource.size;
1037 buffer->buffer_ops->buffer_upload_ranges(buffer, context, data, range.offset, 1, &range);
1040 static void wined3d_buffer_init_data(struct wined3d_buffer *buffer,
1041 struct wined3d_device *device, const struct wined3d_sub_resource_data *data)
1043 struct wined3d_resource *resource = &buffer->resource;
1044 struct wined3d_bo_address bo;
1045 struct wined3d_box box;
1047 if (buffer->flags & WINED3D_BUFFER_USE_BO)
1049 wined3d_box_set(&box, 0, 0, resource->size, 1, 0, 1);
1050 wined3d_cs_emit_update_sub_resource(device->cs, resource,
1051 0, &box, data->data, data->row_pitch, data->slice_pitch);
1053 else
1055 wined3d_buffer_get_memory(buffer, &bo, WINED3D_LOCATION_SYSMEM);
1056 memcpy(bo.addr, data->data, resource->size);
1057 wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_SYSMEM);
1058 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_SYSMEM);
1062 static ULONG buffer_resource_incref(struct wined3d_resource *resource)
1064 return wined3d_buffer_incref(buffer_from_resource(resource));
1067 static ULONG buffer_resource_decref(struct wined3d_resource *resource)
1069 return wined3d_buffer_decref(buffer_from_resource(resource));
1072 static void buffer_resource_preload(struct wined3d_resource *resource)
1074 struct wined3d_context *context;
1076 context = context_acquire(resource->device, NULL, 0);
1077 wined3d_buffer_load(buffer_from_resource(resource), context, NULL);
1078 context_release(context);
1081 static const struct wined3d_resource_ops buffer_resource_ops =
1083 buffer_resource_incref,
1084 buffer_resource_decref,
1085 buffer_resource_preload,
1086 buffer_resource_unload,
1087 buffer_resource_sub_resource_map,
1088 buffer_resource_sub_resource_unmap,
1091 GLenum wined3d_buffer_gl_binding_from_bind_flags(const struct wined3d_gl_info *gl_info, uint32_t bind_flags)
1093 if (!bind_flags)
1094 return GL_PIXEL_UNPACK_BUFFER;
1096 if (bind_flags == WINED3D_BIND_INDEX_BUFFER)
1097 return GL_ELEMENT_ARRAY_BUFFER;
1099 if (bind_flags & (WINED3D_BIND_SHADER_RESOURCE | WINED3D_BIND_UNORDERED_ACCESS)
1100 && gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1101 return GL_TEXTURE_BUFFER;
1103 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
1104 return GL_UNIFORM_BUFFER;
1106 if (bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1107 return GL_TRANSFORM_FEEDBACK_BUFFER;
1109 if (bind_flags & WINED3D_BIND_INDIRECT_BUFFER
1110 && gl_info->supported[ARB_DRAW_INDIRECT])
1111 return GL_DRAW_INDIRECT_BUFFER;
1113 if (bind_flags & ~(WINED3D_BIND_VERTEX_BUFFER | WINED3D_BIND_INDEX_BUFFER))
1114 FIXME("Unhandled bind flags %#x.\n", bind_flags);
1116 return GL_ARRAY_BUFFER;
1119 static HRESULT wined3d_buffer_init(struct wined3d_buffer *buffer, struct wined3d_device *device,
1120 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1121 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_buffer_ops *buffer_ops)
1123 const struct wined3d_format *format = wined3d_get_format(device->adapter, WINED3DFMT_UNKNOWN, desc->bind_flags);
1124 struct wined3d_resource *resource = &buffer->resource;
1125 HRESULT hr;
1127 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1128 "access %s, data %p, parent %p, parent_ops %p.\n",
1129 buffer, device, desc->byte_width, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
1130 wined3d_debug_resource_access(desc->access), data, parent, parent_ops);
1132 if (!desc->byte_width)
1134 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1135 return E_INVALIDARG;
1138 if (desc->bind_flags & WINED3D_BIND_CONSTANT_BUFFER && desc->byte_width & (WINED3D_CONSTANT_BUFFER_ALIGNMENT - 1))
1140 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc->byte_width);
1141 return E_INVALIDARG;
1144 if (data && !data->data)
1146 WARN("Invalid sub-resource data specified.\n");
1147 return E_INVALIDARG;
1150 if (FAILED(hr = resource_init(resource, device, WINED3D_RTYPE_BUFFER, format,
1151 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->bind_flags, desc->access,
1152 desc->byte_width, 1, 1, desc->byte_width, parent, parent_ops, &buffer_resource_ops)))
1154 WARN("Failed to initialize resource, hr %#x.\n", hr);
1155 return hr;
1157 buffer->buffer_ops = buffer_ops;
1158 buffer->structure_byte_stride = desc->structure_byte_stride;
1159 buffer->locations = data ? WINED3D_LOCATION_DISCARDED : WINED3D_LOCATION_SYSMEM;
1161 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1162 buffer, buffer->resource.size, buffer->resource.usage, buffer->resource.heap_memory);
1164 if (device->create_parms.flags & WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1165 || wined3d_resource_access_is_managed(desc->access))
1167 /* SWvp and managed buffers always return the same pointer in buffer
1168 * maps and retain data in DISCARD maps. Keep a system memory copy of
1169 * the buffer to provide the same behavior to the application. */
1170 TRACE("Pinning system memory.\n");
1171 buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
1172 buffer->locations = WINED3D_LOCATION_SYSMEM;
1175 if (buffer->locations & WINED3D_LOCATION_SYSMEM || !(buffer->flags & WINED3D_BUFFER_USE_BO))
1177 if (!wined3d_resource_prepare_sysmem(&buffer->resource))
1178 return E_OUTOFMEMORY;
1181 if (!(buffer->maps = heap_alloc(sizeof(*buffer->maps))))
1183 ERR("Out of memory.\n");
1184 buffer_resource_unload(resource);
1185 resource_cleanup(resource);
1186 wined3d_resource_wait_idle(resource);
1187 return E_OUTOFMEMORY;
1189 buffer->maps_size = 1;
1191 if (data)
1192 wined3d_buffer_init_data(buffer, device, data);
1194 return WINED3D_OK;
1197 static BOOL wined3d_buffer_no3d_prepare_location(struct wined3d_buffer *buffer,
1198 struct wined3d_context *context, unsigned int location)
1200 if (location == WINED3D_LOCATION_SYSMEM)
1201 return wined3d_resource_prepare_sysmem(&buffer->resource);
1203 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
1205 return FALSE;
1208 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer *buffer,
1209 struct wined3d_context *context, unsigned int location)
1211 TRACE("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1214 static void wined3d_buffer_no3d_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1215 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1217 FIXME("Not implemented.\n");
1220 static void wined3d_buffer_no3d_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1221 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1223 FIXME("Not implemented.\n");
1226 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops =
1228 wined3d_buffer_no3d_prepare_location,
1229 wined3d_buffer_no3d_unload_location,
1230 wined3d_buffer_no3d_upload_ranges,
1231 wined3d_buffer_no3d_download_ranges,
1234 HRESULT wined3d_buffer_no3d_init(struct wined3d_buffer *buffer_no3d, struct wined3d_device *device,
1235 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1236 void *parent, const struct wined3d_parent_ops *parent_ops)
1238 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1239 buffer_no3d, device, desc, data, parent, parent_ops);
1241 return wined3d_buffer_init(buffer_no3d, device, desc, data, parent, parent_ops, &wined3d_buffer_no3d_ops);
1244 static BOOL wined3d_buffer_gl_prepare_location(struct wined3d_buffer *buffer,
1245 struct wined3d_context *context, unsigned int location)
1247 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1248 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1250 switch (location)
1252 case WINED3D_LOCATION_SYSMEM:
1253 return wined3d_resource_prepare_sysmem(&buffer->resource);
1255 case WINED3D_LOCATION_BUFFER:
1256 if (buffer->buffer_object)
1257 return TRUE;
1259 if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
1261 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer);
1262 return FALSE;
1264 return wined3d_buffer_gl_create_buffer_object(buffer_gl, context_gl);
1266 default:
1267 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1268 return FALSE;
1272 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer *buffer,
1273 struct wined3d_context *context, unsigned int location)
1275 TRACE("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1277 switch (location)
1279 case WINED3D_LOCATION_BUFFER:
1280 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer), wined3d_context_gl(context));
1281 break;
1283 default:
1284 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
1285 break;
1289 /* Context activation is done by the caller. */
1290 static void wined3d_buffer_gl_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1291 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1293 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1294 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1295 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1296 const struct wined3d_range *range;
1298 TRACE("buffer %p, context %p, data %p, data_offset %u, range_count %u, ranges %p.\n",
1299 buffer, context, data, data_offset, range_count, ranges);
1301 wined3d_buffer_gl_bind(buffer_gl, context_gl);
1303 while (range_count--)
1305 range = &ranges[range_count];
1306 GL_EXTCALL(glBufferSubData(buffer_gl->bo.binding,
1307 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
1309 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
1310 checkGLcall("buffer upload");
1313 /* Context activation is done by the caller. */
1314 static void wined3d_buffer_gl_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1315 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1317 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1318 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(buffer);
1319 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1320 const struct wined3d_range *range;
1322 TRACE("buffer %p, context %p, data %p, data_offset %u, range_count %u, ranges %p.\n",
1323 buffer, context, data, data_offset, range_count, ranges);
1325 wined3d_buffer_gl_bind(buffer_gl, context_gl);
1327 while (range_count--)
1329 range = &ranges[range_count];
1330 GL_EXTCALL(glGetBufferSubData(buffer_gl->bo.binding,
1331 range->offset, range->size, (BYTE *)data + range->offset - data_offset));
1333 checkGLcall("buffer download");
1336 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops =
1338 wined3d_buffer_gl_prepare_location,
1339 wined3d_buffer_gl_unload_location,
1340 wined3d_buffer_gl_upload_ranges,
1341 wined3d_buffer_gl_download_ranges,
1344 HRESULT wined3d_buffer_gl_init(struct wined3d_buffer_gl *buffer_gl, struct wined3d_device *device,
1345 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1346 void *parent, const struct wined3d_parent_ops *parent_ops)
1348 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1350 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1351 buffer_gl, device, desc, data, parent, parent_ops);
1353 /* Observations show that draw_primitive_immediate_mode() is faster on
1354 * dynamic vertex buffers than converting + draw_primitive_arrays().
1355 * (Half-Life 2 and others.) */
1356 if (!(desc->access & WINED3D_RESOURCE_ACCESS_GPU))
1357 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1358 else if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
1359 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1360 else if (!(gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE])
1361 && (desc->usage & WINED3DUSAGE_DYNAMIC))
1362 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1363 else
1364 buffer_gl->b.flags |= WINED3D_BUFFER_USE_BO;
1366 return wined3d_buffer_init(&buffer_gl->b, device, desc, data, parent, parent_ops, &wined3d_buffer_gl_ops);
1369 static BOOL wined3d_buffer_vk_create_buffer_object(struct wined3d_buffer_vk *buffer_vk,
1370 struct wined3d_context_vk *context_vk)
1372 struct wined3d_resource *resource = &buffer_vk->b.resource;
1373 uint32_t bind_flags = resource->bind_flags;
1374 VkMemoryPropertyFlags memory_type;
1375 VkBufferUsageFlags usage;
1377 usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
1378 if (bind_flags & WINED3D_BIND_VERTEX_BUFFER)
1379 usage |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
1380 if (bind_flags & WINED3D_BIND_INDEX_BUFFER)
1381 usage |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
1382 if (bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
1383 usage |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
1384 if (bind_flags & WINED3D_BIND_SHADER_RESOURCE)
1385 usage |= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT;
1386 if (bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1387 usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT;
1388 if (bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
1389 usage |= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
1390 if (bind_flags & WINED3D_BIND_INDIRECT_BUFFER)
1391 usage |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
1392 if (bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL))
1393 FIXME("Ignoring some bind flags %#x.\n", bind_flags);
1395 memory_type = 0;
1396 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
1397 memory_type |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1398 if (resource->access & WINED3D_RESOURCE_ACCESS_MAP_R)
1399 memory_type |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
1400 else if (resource->access & WINED3D_RESOURCE_ACCESS_MAP_W)
1401 memory_type |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
1403 if (!(wined3d_context_vk_create_bo(context_vk, resource->size, usage, memory_type, &buffer_vk->bo)))
1405 WARN("Failed to create Vulkan buffer.\n");
1406 return FALSE;
1409 list_init(&buffer_vk->bo_user.entry);
1410 list_add_head(&buffer_vk->bo.users, &buffer_vk->bo_user.entry);
1411 buffer_vk->b.buffer_object = (uintptr_t)&buffer_vk->bo;
1412 buffer_invalidate_bo_range(&buffer_vk->b, 0, 0);
1414 return TRUE;
1417 const VkDescriptorBufferInfo *wined3d_buffer_vk_get_buffer_info(struct wined3d_buffer_vk *buffer_vk)
1419 if (buffer_vk->bo_user.valid)
1420 return &buffer_vk->buffer_info;
1422 buffer_vk->buffer_info.buffer = buffer_vk->bo.vk_buffer;
1423 buffer_vk->buffer_info.offset = buffer_vk->bo.buffer_offset;
1424 buffer_vk->buffer_info.range = buffer_vk->b.resource.size;
1425 buffer_vk->bo_user.valid = true;
1427 return &buffer_vk->buffer_info;
1430 static BOOL wined3d_buffer_vk_prepare_location(struct wined3d_buffer *buffer,
1431 struct wined3d_context *context, unsigned int location)
1433 switch (location)
1435 case WINED3D_LOCATION_SYSMEM:
1436 return wined3d_resource_prepare_sysmem(&buffer->resource);
1438 case WINED3D_LOCATION_BUFFER:
1439 if (buffer->buffer_object)
1440 return TRUE;
1442 return wined3d_buffer_vk_create_buffer_object(wined3d_buffer_vk(buffer), wined3d_context_vk(context));
1444 default:
1445 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
1446 return FALSE;
1450 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer *buffer,
1451 struct wined3d_context *context, unsigned int location)
1453 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
1454 struct wined3d_buffer_vk *buffer_vk = wined3d_buffer_vk(buffer);
1456 TRACE("buffer %p, context %p, location %s.\n", buffer, context, wined3d_debug_location(location));
1458 switch (location)
1460 case WINED3D_LOCATION_BUFFER:
1461 buffer_vk->bo_user.valid = false;
1462 list_remove(&buffer_vk->bo_user.entry);
1463 wined3d_context_vk_destroy_bo(context_vk, &buffer_vk->bo);
1464 buffer_vk->bo.vk_buffer = VK_NULL_HANDLE;
1465 buffer_vk->bo.memory = NULL;
1466 buffer_vk->b.buffer_object = 0u;
1467 break;
1469 default:
1470 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
1471 break;
1475 static void wined3d_buffer_vk_upload_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1476 const void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1478 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
1479 struct wined3d_resource *resource = &buffer->resource;
1480 struct wined3d_bo_address src, dst;
1481 const struct wined3d_range *range;
1482 struct wined3d_bo_vk *dst_bo;
1483 unsigned int i = range_count;
1484 uint32_t flags;
1485 void *map_ptr;
1487 if (!range_count)
1488 return;
1490 dst.buffer_object = buffer->buffer_object;
1491 dst.addr = NULL;
1493 flags = WINED3D_MAP_WRITE;
1494 if (!ranges->offset && ranges->size == resource->size)
1495 flags |= WINED3D_MAP_DISCARD;
1497 dst_bo = &wined3d_buffer_vk(buffer)->bo;
1498 if (!(dst_bo->memory_type & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) || (!(flags & WINED3D_MAP_DISCARD)
1499 && dst_bo->command_buffer_id > context_vk->completed_command_buffer_id))
1501 src.buffer_object = 0;
1502 while (range_count--)
1504 range = &ranges[range_count];
1506 src.addr = (uint8_t *)data + range->offset - data_offset;
1507 dst.addr = (void *)(uintptr_t)range->offset;
1508 wined3d_context_copy_bo_address(context, &dst, &src, range->size);
1511 return;
1514 if (!(map_ptr = wined3d_context_map_bo_address(context, &dst, resource->size, flags)))
1516 FIXME("Failed to map buffer.\n");
1517 return;
1520 while (i--)
1522 range = &ranges[i];
1523 memcpy((uint8_t *)map_ptr + range->offset, (uint8_t *)data + range->offset - data_offset, range->size);
1526 wined3d_context_unmap_bo_address(context, &dst, range_count, ranges);
1529 static void wined3d_buffer_vk_download_ranges(struct wined3d_buffer *buffer, struct wined3d_context *context,
1530 void *data, unsigned int data_offset, unsigned int range_count, const struct wined3d_range *ranges)
1532 FIXME("Not implemented.\n");
1535 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops =
1537 wined3d_buffer_vk_prepare_location,
1538 wined3d_buffer_vk_unload_location,
1539 wined3d_buffer_vk_upload_ranges,
1540 wined3d_buffer_vk_download_ranges,
1543 HRESULT wined3d_buffer_vk_init(struct wined3d_buffer_vk *buffer_vk, struct wined3d_device *device,
1544 const struct wined3d_buffer_desc *desc, const struct wined3d_sub_resource_data *data,
1545 void *parent, const struct wined3d_parent_ops *parent_ops)
1547 const struct wined3d_vk_info *vk_info = &wined3d_adapter_vk(device->adapter)->vk_info;
1549 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1550 buffer_vk, device, desc, data, parent, parent_ops);
1552 if ((desc->bind_flags & WINED3D_BIND_STREAM_OUTPUT)
1553 && !vk_info->supported[WINED3D_VK_EXT_TRANSFORM_FEEDBACK])
1555 WARN("The Vulkan implementation does not support transform feedback.\n");
1556 return WINED3DERR_INVALIDCALL;
1559 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
1560 buffer_vk->b.flags |= WINED3D_BUFFER_USE_BO;
1562 return wined3d_buffer_init(&buffer_vk->b, device, desc, data, parent, parent_ops, &wined3d_buffer_vk_ops);
1565 void wined3d_buffer_vk_barrier(struct wined3d_buffer_vk *buffer_vk,
1566 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
1568 TRACE("buffer_vk %p, context_vk %p, bind_mask %s.\n",
1569 buffer_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
1571 if (buffer_vk->bind_mask && buffer_vk->bind_mask != bind_mask)
1573 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1574 VkBufferMemoryBarrier vk_barrier;
1576 TRACE(" %s -> %s.\n",
1577 wined3d_debug_bind_flags(buffer_vk->bind_mask), wined3d_debug_bind_flags(bind_mask));
1579 wined3d_context_vk_end_current_render_pass(context_vk);
1581 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
1582 vk_barrier.pNext = NULL;
1583 vk_barrier.srcAccessMask = vk_access_mask_from_bind_flags(buffer_vk->bind_mask);
1584 vk_barrier.dstAccessMask = vk_access_mask_from_bind_flags(bind_mask);
1585 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1586 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1587 vk_barrier.buffer = buffer_vk->bo.vk_buffer;
1588 vk_barrier.offset = buffer_vk->bo.buffer_offset;
1589 vk_barrier.size = buffer_vk->b.resource.size;
1590 VK_CALL(vkCmdPipelineBarrier(wined3d_context_vk_get_command_buffer(context_vk),
1591 vk_pipeline_stage_mask_from_bind_flags(buffer_vk->bind_mask),
1592 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
1593 0, 0, NULL, 1, &vk_barrier, 0, NULL));
1595 buffer_vk->bind_mask = bind_mask;
1598 HRESULT CDECL wined3d_buffer_create(struct wined3d_device *device, const struct wined3d_buffer_desc *desc,
1599 const struct wined3d_sub_resource_data *data, void *parent, const struct wined3d_parent_ops *parent_ops,
1600 struct wined3d_buffer **buffer)
1602 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1603 device, desc, data, parent, parent_ops, buffer);
1605 return device->adapter->adapter_ops->adapter_create_buffer(device, desc, data, parent, parent_ops, buffer);