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 "wined3d_private.h"
26 #include "wined3d_vk.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
30 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
31 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
33 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
34 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
35 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
36 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
38 #define SB_MIN_SIZE (512 * 1024) /* Minimum size of an allocated streaming buffer. */
40 struct wined3d_buffer_ops
42 BOOL (*buffer_prepare_location
)(struct wined3d_buffer
*buffer
,
43 struct wined3d_context
*context
, unsigned int location
);
44 void (*buffer_unload_location
)(struct wined3d_buffer
*buffer
,
45 struct wined3d_context
*context
, unsigned int location
);
48 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer
*buffer
)
50 if (buffer
->resource
.pin_sysmem
)
52 TRACE("Not evicting system memory for buffer %p.\n", buffer
);
56 TRACE("Evicting system memory for buffer %p.\n", buffer
);
57 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
58 wined3d_resource_free_sysmem(&buffer
->resource
);
61 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
63 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
66 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
68 WARN("Invalid range specified, invalidating entire buffer.\n");
72 if (!wined3d_array_reserve((void **)&buffer
->maps
, &buffer
->maps_size
,
73 buffer
->modified_areas
+ 1, sizeof(*buffer
->maps
)))
75 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
79 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
80 buffer
->maps
[buffer
->modified_areas
].size
= size
;
81 ++buffer
->modified_areas
;
85 buffer
->modified_areas
= 1;
86 buffer
->maps
[0].offset
= 0;
87 buffer
->maps
[0].size
= buffer
->resource
.size
;
90 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
92 This
->modified_areas
= 0;
95 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
97 return !!buffer
->modified_areas
;
100 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
102 return buffer
->modified_areas
== 1
103 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
106 void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, uint32_t location
)
108 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
110 if (location
& WINED3D_LOCATION_BUFFER
)
111 buffer_clear_dirty_areas(buffer
);
113 buffer
->locations
|= location
;
115 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
118 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
119 unsigned int offset
, unsigned int size
)
121 TRACE("buffer %p, location %s, offset %u, size %u.\n",
122 buffer
, wined3d_debug_location(location
), offset
, size
);
124 if (location
& WINED3D_LOCATION_BUFFER
)
125 buffer_invalidate_bo_range(buffer
, offset
, size
);
127 buffer
->locations
&= ~location
;
129 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
131 if (!buffer
->locations
)
132 ERR("Buffer %p does not have any up to date location.\n", buffer
);
135 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, uint32_t location
)
137 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
140 GLenum
wined3d_buffer_gl_binding_from_bind_flags(const struct wined3d_gl_info
*gl_info
, uint32_t bind_flags
)
143 return GL_PIXEL_UNPACK_BUFFER
;
145 /* We must always return GL_ELEMENT_ARRAY_BUFFER here;
146 * wined3d_device_gl_create_bo() checks the GL binding to see whether we
147 * can suballocate, and we cannot suballocate if this BO might be used for
148 * an index buffer. */
149 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
150 return GL_ELEMENT_ARRAY_BUFFER
;
152 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
153 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
154 return GL_TEXTURE_BUFFER
;
156 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
157 return GL_UNIFORM_BUFFER
;
159 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
160 return GL_TRANSFORM_FEEDBACK_BUFFER
;
162 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
163 && gl_info
->supported
[ARB_DRAW_INDIRECT
])
164 return GL_DRAW_INDIRECT_BUFFER
;
166 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
167 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
169 return GL_ARRAY_BUFFER
;
172 /* Context activation is done by the caller. */
173 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
174 struct wined3d_context_gl
*context_gl
)
176 struct wined3d_resource
*resource
= &buffer_gl
->b
.resource
;
177 struct wined3d_bo_gl
*bo_gl
;
179 if (!buffer_gl
->b
.buffer_object
)
181 bo_gl
= wined3d_bo_gl(buffer_gl
->b
.buffer_object
);
183 if (context_gl
->c
.transform_feedback_active
&& (resource
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
184 && wined3d_context_is_graphics_state_dirty(&context_gl
->c
, STATE_STREAM_OUTPUT
))
186 /* It's illegal to (un)bind GL_TRANSFORM_FEEDBACK_BUFFER while transform
187 * feedback is active. Deleting a buffer implicitly unbinds it, so we
188 * need to end transform feedback here if this buffer was bound.
190 * This should only be possible if STATE_STREAM_OUTPUT is dirty; if we
191 * do a draw call before destroying this buffer then the draw call will
192 * already rebind the GL target. */
193 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl
);
194 wined3d_context_gl_end_transform_feedback(context_gl
);
197 if (buffer_gl
->b
.bo_user
.valid
)
199 buffer_gl
->b
.bo_user
.valid
= false;
200 list_remove(&buffer_gl
->b
.bo_user
.entry
);
202 wined3d_context_gl_destroy_bo(context_gl
, bo_gl
);
204 buffer_gl
->b
.buffer_object
= NULL
;
207 /* Context activation is done by the caller. */
208 static BOOL
wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
209 struct wined3d_context_gl
*context_gl
)
211 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(buffer_gl
->b
.resource
.device
);
212 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
213 GLenum usage
= GL_STATIC_DRAW
;
214 GLbitfield gl_storage_flags
;
215 struct wined3d_bo_gl
*bo
;
216 bool coherent
= true;
220 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
221 buffer_gl
, debug_d3dusage(buffer_gl
->b
.resource
.usage
));
223 if (!(bo
= heap_alloc(sizeof(*bo
))))
226 size
= buffer_gl
->b
.resource
.size
;
227 binding
= wined3d_buffer_gl_binding_from_bind_flags(gl_info
, buffer_gl
->b
.resource
.bind_flags
);
228 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
230 usage
= GL_STREAM_DRAW_ARB
;
233 gl_storage_flags
= wined3d_resource_gl_storage_flags(&buffer_gl
->b
.resource
);
234 if (!wined3d_device_gl_create_bo(device_gl
, context_gl
, size
, binding
, usage
, coherent
, gl_storage_flags
, bo
))
236 ERR("Failed to create OpenGL buffer object.\n");
237 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_USE_BO
;
238 buffer_clear_dirty_areas(&buffer_gl
->b
);
243 buffer_gl
->b
.buffer_object
= &bo
->b
;
244 buffer_invalidate_bo_range(&buffer_gl
->b
, 0, 0);
249 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
250 const enum wined3d_buffer_conversion_type conversion_type
,
251 const struct wined3d_stream_info_element
*attrib
, UINT
*stride_this_run
)
253 const struct wined3d_format
*format
= attrib
->format
;
258 /* Check for some valid situations which cause us pain. One is if the buffer is used for
259 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
260 * with different strides. In the 2nd case we might have to drop conversion entirely,
261 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
265 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
266 debug_d3dformat(format
->id
));
268 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
270 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
274 *stride_this_run
= attrib
->stride
;
275 if (buffer
->stride
!= *stride_this_run
)
277 /* We rely that this happens only on the first converted attribute that is found,
278 * if at all. See above check
280 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
281 buffer
->stride
= *stride_this_run
;
282 heap_free(buffer
->conversion_map
);
283 buffer
->conversion_map
= heap_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
288 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
289 for (i
= 0; i
< format
->byte_count
; ++i
)
291 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
292 if (buffer
->conversion_map
[idx
] != conversion_type
)
294 TRACE("Byte %Iu in vertex changed:\n", idx
);
295 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
297 buffer
->conversion_map
[idx
] = conversion_type
;
304 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
305 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
307 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
308 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, UINT
*stride_this_run
)
310 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
311 enum wined3d_format_id format
;
314 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
315 * there, on nonexistent attribs the vbo is 0.
317 if (!(si
->use_map
& (1u << attrib_idx
))
318 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
321 format
= attrib
->format
->id
;
322 /* Look for newly appeared conversion */
323 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
325 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
327 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
329 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
331 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
335 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
337 else if (This
->conversion_map
)
339 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
345 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
346 const struct wined3d_state
*state
, DWORD fixup_flags
)
348 UINT stride_this_run
= 0;
351 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
352 * Once we have our declaration there is no need to look it up again. Index buffers also never need
353 * conversion, so once the (empty) conversion structure is created don't bother checking again
355 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
357 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
362 TRACE("No fixup required.\n");
363 if(This
->conversion_map
)
365 heap_free(This
->conversion_map
);
366 This
->conversion_map
= NULL
;
374 TRACE("Finding vertex buffer conversion information\n");
375 /* Certain declaration types need some fixups before we can pass them to
376 * opengl. This means D3DCOLOR attributes with fixed function vertex
377 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
378 * GL_ARB_half_float_vertex is not supported.
380 * Note for d3d8 and d3d9:
381 * The vertex buffer FVF doesn't help with finding them, we have to use
382 * the decoded vertex declaration and pick the things that concern the
383 * current buffer. A problem with this is that this can change between
384 * draws, so we have to validate the information and reprocess the buffer
385 * if it changes, and avoid false positives for performance reasons.
386 * WineD3D doesn't even know the vertex buffer any more, it is managed
387 * by the client libraries and passed to SetStreamSource and ProcessVertices
390 * We have to distinguish between vertex shaders and fixed function to
391 * pick the way we access the strided vertex information.
393 * This code sets up a per-byte array with the size of the detected
394 * stride of the arrays in the buffer. For each byte we have a field
395 * that marks the conversion needed on this byte. For example, the
396 * following declaration with fixed function vertex processing:
404 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
405 * [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]
407 * Where in this example map P means 4 component position conversion, 0
408 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
409 * conversion (red / blue swizzle).
411 * If we're doing conversion and the stride changes we have to reconvert
412 * the whole buffer. Note that we do not mind if the semantic changes,
413 * we only care for the conversion type. So if the NORMAL is replaced
414 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
415 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
416 * conversion types depend on the semantic as well, for example a FLOAT4
417 * texcoord needs no conversion while a FLOAT4 positiont needs one
420 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
421 fixup_flags
, &stride_this_run
) || ret
;
422 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
424 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
425 fixup_flags
, &stride_this_run
) || ret
;
426 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
427 fixup_flags
, &stride_this_run
) || ret
;
428 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
429 fixup_flags
, &stride_this_run
) || ret
;
430 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
431 fixup_flags
, &stride_this_run
) || ret
;
432 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
433 fixup_flags
, &stride_this_run
) || ret
;
434 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
435 fixup_flags
, &stride_this_run
) || ret
;
436 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
437 fixup_flags
, &stride_this_run
) || ret
;
438 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
439 fixup_flags
, &stride_this_run
) || ret
;
440 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
441 fixup_flags
, &stride_this_run
) || ret
;
442 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
443 fixup_flags
, &stride_this_run
) || ret
;
444 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
445 fixup_flags
, &stride_this_run
) || ret
;
446 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
447 fixup_flags
, &stride_this_run
) || ret
;
448 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
449 fixup_flags
, &stride_this_run
) || ret
;
451 if (!stride_this_run
&& This
->conversion_map
)
455 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
456 heap_free(This
->conversion_map
);
457 This
->conversion_map
= NULL
;
461 if (ret
) TRACE("Conversion information changed\n");
466 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
468 DWORD src_color
= *dst_color
;
470 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
471 * endianness. If we want this to work on big-endian machines as well we
472 * have to consider more things.
474 * 0xff000000: Alpha mask
475 * 0x00ff0000: Blue mask
476 * 0x0000ff00: Green mask
477 * 0x000000ff: Red mask
480 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
481 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
482 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
484 return sizeof(*dst_color
);
487 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
489 /* rhw conversion like in position_float4(). */
490 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
492 float w
= 1.0f
/ p
->w
;
502 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
504 unsigned int refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
506 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
511 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
513 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
514 struct wined3d_bo_address src
, dst
;
517 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
519 ERR("Failed to load system memory.\n");
522 buffer
->resource
.pin_sysmem
= 1;
524 /* Now for each vertex in the buffer that needs conversion. */
525 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
527 if (!(data
= heap_alloc(buffer
->resource
.size
)))
529 ERR("Out of memory.\n");
533 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
535 start
= buffer
->maps
[range_idx
].offset
;
536 end
= start
+ buffer
->maps
[range_idx
].size
;
538 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
539 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
541 for (j
= 0; j
< buffer
->stride
;)
543 switch (buffer
->conversion_map
[j
])
550 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
553 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
556 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
563 dst
.buffer_object
= buffer
->buffer_object
;
565 src
.buffer_object
= NULL
;
567 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
, WINED3D_MAP_WRITE
);
572 BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
573 struct wined3d_context
*context
, unsigned int location
)
575 return buffer
->buffer_ops
->buffer_prepare_location(buffer
, context
, location
);
578 static void wined3d_buffer_unload_location(struct wined3d_buffer
*buffer
,
579 struct wined3d_context
*context
, unsigned int location
)
581 buffer
->buffer_ops
->buffer_unload_location(buffer
, context
, location
);
584 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
585 struct wined3d_context
*context
, uint32_t location
)
587 struct wined3d_bo_address src
, dst
;
588 struct wined3d_range range
;
590 TRACE("buffer %p, context %p, location %s.\n",
591 buffer
, context
, wined3d_debug_location(location
));
593 if (buffer
->locations
& location
)
595 TRACE("Location (%#x) is already up to date.\n", location
);
599 if (!buffer
->locations
)
601 ERR("Buffer %p does not have any up to date location.\n", buffer
);
602 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
603 return wined3d_buffer_load_location(buffer
, context
, location
);
606 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
608 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
611 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
613 TRACE("Buffer previously discarded, nothing to do.\n");
614 wined3d_buffer_validate_location(buffer
, location
);
615 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
621 case WINED3D_LOCATION_SYSMEM
:
622 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
624 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
628 dst
.buffer_object
= NULL
;
629 dst
.addr
= buffer
->resource
.heap_memory
;
630 src
.buffer_object
= buffer
->buffer_object
;
633 range
.size
= buffer
->resource
.size
;
634 wined3d_context_copy_bo_address(context
, &dst
, &src
, 1, &range
, WINED3D_MAP_WRITE
);
638 case WINED3D_LOCATION_BUFFER
:
639 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
641 /* FIXME: Clear the buffer on the GPU if possible. */
642 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
644 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
647 dst
.buffer_object
= buffer
->buffer_object
;
649 src
.buffer_object
= NULL
;
650 src
.addr
= buffer
->resource
.heap_memory
;
652 if (!buffer
->conversion_map
)
654 uint32_t map_flags
= WINED3D_MAP_WRITE
;
656 if (buffer
->modified_areas
== 1 && !buffer
->maps
[0].offset
657 && buffer
->maps
[0].size
== buffer
->resource
.size
)
658 map_flags
|= WINED3D_MAP_DISCARD
;
660 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
, map_flags
);
664 buffer_conversion_upload(buffer
, context
);
669 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
673 wined3d_buffer_validate_location(buffer
, location
);
674 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
675 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
676 wined3d_buffer_evict_sysmem(buffer
);
681 /* Context activation is done by the caller. */
682 void *wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
684 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
685 buffer
->resource
.pin_sysmem
= 1;
686 return buffer
->resource
.heap_memory
;
689 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
690 struct wined3d_bo_address
*data
)
692 unsigned int locations
= buffer
->locations
;
694 TRACE("buffer %p, context %p, data %p, locations %s.\n",
695 buffer
, context
, data
, wined3d_debug_location(locations
));
697 if (locations
& (WINED3D_LOCATION_DISCARDED
| WINED3D_LOCATION_CLEARED
))
699 locations
= ((buffer
->flags
& WINED3D_BUFFER_USE_BO
) ? WINED3D_LOCATION_BUFFER
: WINED3D_LOCATION_SYSMEM
);
700 if (!wined3d_buffer_load_location(buffer
, context
, locations
))
702 data
->buffer_object
= 0;
707 if (locations
& WINED3D_LOCATION_BUFFER
)
709 data
->buffer_object
= buffer
->buffer_object
;
711 return WINED3D_LOCATION_BUFFER
;
713 if (locations
& WINED3D_LOCATION_SYSMEM
)
715 data
->buffer_object
= 0;
716 data
->addr
= buffer
->resource
.heap_memory
;
717 return WINED3D_LOCATION_SYSMEM
;
720 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
721 data
->buffer_object
= 0;
726 static void buffer_resource_unload(struct wined3d_resource
*resource
)
728 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
730 TRACE("buffer %p.\n", buffer
);
732 if (buffer
->buffer_object
)
734 struct wined3d_context
*context
;
736 context
= context_acquire(resource
->device
, NULL
, 0);
738 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
739 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
740 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
741 buffer_clear_dirty_areas(buffer
);
743 context_release(context
);
745 heap_free(buffer
->conversion_map
);
746 buffer
->conversion_map
= NULL
;
748 buffer
->conversion_stride
= 0;
749 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
752 resource_unload(resource
);
755 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
757 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
758 buffer_resource_unload(&buffer
->resource
);
761 static void wined3d_buffer_destroy_object(void *object
)
763 struct wined3d_buffer
*buffer
= object
;
764 struct wined3d_context
*context
;
766 TRACE("buffer %p.\n", buffer
);
768 if (buffer
->buffer_object
)
770 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
771 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
772 context_release(context
);
774 heap_free(buffer
->conversion_map
);
775 heap_free(buffer
->maps
);
778 void wined3d_buffer_cleanup(struct wined3d_buffer
*buffer
)
780 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
781 resource_cleanup(&buffer
->resource
);
784 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
786 unsigned int refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
788 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
792 wined3d_mutex_lock();
793 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
794 buffer
->resource
.device
->adapter
->adapter_ops
->adapter_destroy_buffer(buffer
);
795 wined3d_mutex_unlock();
801 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
803 TRACE("buffer %p.\n", buffer
);
805 return buffer
->resource
.parent
;
808 /* Context activation is done by the caller. */
809 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
810 const struct wined3d_state
*state
)
812 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
813 BOOL decl_changed
= FALSE
;
815 TRACE("buffer %p.\n", buffer
);
817 if (buffer
->resource
.map_count
&& buffer
->map_ptr
)
819 FIXME("Buffer is mapped through buffer object, not loading.\n");
822 else if (buffer
->resource
.map_count
)
824 WARN("Loading mapped buffer.\n");
827 /* TODO: Make converting independent from VBOs */
828 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
830 /* Not doing any conversion */
834 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
836 ERR("Failed to prepare buffer location.\n");
840 /* Reading the declaration makes only sense if we have valid state information
841 * (i.e., if this function is called during draws). */
844 DWORD fixup_flags
= 0;
848 if (!d3d_info
->vertex_bgra
&& !d3d_info
->ffp_generic_attributes
)
849 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
850 if (!d3d_info
->xyzrhw
)
851 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
854 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
855 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
858 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
860 ++buffer
->draw_count
;
861 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
862 buffer
->decl_change_count
= 0;
863 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
864 buffer
->full_conversion_count
= 0;
868 /* If applications change the declaration over and over, reconverting all the time is a huge
869 * performance hit. So count the declaration changes and release the VBO if there are too many
870 * of them (and thus stop converting)
874 ++buffer
->decl_change_count
;
875 buffer
->draw_count
= 0;
877 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
878 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
880 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
881 wined3d_buffer_drop_bo(buffer
);
885 /* The declaration changed, reload the whole buffer. */
886 WARN("Reloading buffer because of a vertex declaration change.\n");
887 buffer_invalidate_bo_range(buffer
, 0, 0);
891 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
892 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
893 * decl changes and reset the decl change count after a specific number of them
895 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
897 ++buffer
->full_conversion_count
;
898 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
900 FIXME("Too many full buffer conversions, stopping converting.\n");
901 wined3d_buffer_drop_bo(buffer
);
907 ++buffer
->draw_count
;
908 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
909 buffer
->decl_change_count
= 0;
910 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
911 buffer
->full_conversion_count
= 0;
915 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
916 ERR("Failed to load buffer location.\n");
919 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
921 TRACE("buffer %p.\n", buffer
);
923 return &buffer
->resource
;
926 static HRESULT
buffer_resource_sub_resource_get_desc(struct wined3d_resource
*resource
,
927 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
929 if (sub_resource_idx
)
931 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
935 desc
->format
= WINED3DFMT_R8_UNORM
;
936 desc
->multisample_type
= WINED3D_MULTISAMPLE_NONE
;
937 desc
->multisample_quality
= 0;
938 desc
->usage
= resource
->usage
;
939 desc
->bind_flags
= resource
->bind_flags
;
940 desc
->access
= resource
->access
;
941 desc
->width
= resource
->size
;
944 desc
->size
= resource
->size
;
948 static void buffer_resource_sub_resource_get_map_pitch(struct wined3d_resource
*resource
,
949 unsigned int sub_resource_idx
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
951 *row_pitch
= *slice_pitch
= resource
->size
;
954 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
955 void **map_ptr
, const struct wined3d_box
*box
, uint32_t flags
)
957 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
958 unsigned int offset
, size
, dirty_offset
, dirty_size
;
959 struct wined3d_device
*device
= resource
->device
;
960 struct wined3d_context
*context
;
961 struct wined3d_bo_address addr
;
965 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
966 resource
, sub_resource_idx
, map_ptr
, debug_box(box
), flags
);
968 dirty_offset
= offset
= box
->left
;
969 dirty_size
= size
= box
->right
- box
->left
;
971 count
= ++resource
->map_count
;
973 /* DISCARD invalidates the entire buffer, regardless of the specified
974 * offset and size. Some applications also depend on the entire buffer
975 * being uploaded in that case. Two such applications are Port Royale
976 * and Darkstar One. */
977 if (flags
& WINED3D_MAP_DISCARD
)
983 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
984 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
985 || buffer
->resource
.pin_sysmem
986 || !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
988 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
990 context
= context_acquire(device
, NULL
, 0);
991 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
992 context_release(context
);
995 if (flags
& WINED3D_MAP_WRITE
)
996 wined3d_buffer_invalidate_range(buffer
, ~WINED3D_LOCATION_SYSMEM
, dirty_offset
, dirty_size
);
1000 context
= context_acquire(device
, NULL
, 0);
1002 if (flags
& WINED3D_MAP_DISCARD
)
1004 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
1006 context_release(context
);
1007 return E_OUTOFMEMORY
;
1009 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1013 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1016 if (flags
& WINED3D_MAP_WRITE
)
1018 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1019 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1022 if ((flags
& WINED3D_MAP_DISCARD
) && resource
->heap_memory
)
1023 wined3d_buffer_evict_sysmem(buffer
);
1027 addr
.buffer_object
= buffer
->buffer_object
;
1029 buffer
->map_ptr
= wined3d_context_map_bo_address(context
, &addr
, resource
->size
, flags
);
1031 /* We are accessing buffer->resource.client from the CS thread,
1032 * but it's safe because the client thread will wait for the
1033 * map to return, thus completely serializing this call with
1034 * other client code. */
1035 if (context
->d3d_info
->persistent_map
)
1036 buffer
->resource
.client
.addr
= addr
;
1038 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1040 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1042 wined3d_context_unmap_bo_address(context
, &addr
, 0, NULL
);
1043 buffer
->map_ptr
= NULL
;
1045 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1047 /* The extra copy is more expensive than not using VBOs
1048 * at all on the NVIDIA Linux driver, which is the
1049 * only driver that returns unaligned pointers. */
1050 TRACE("Dynamic buffer, dropping VBO.\n");
1051 wined3d_buffer_drop_bo(buffer
);
1055 TRACE("Falling back to doublebuffered operation.\n");
1056 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1057 buffer
->resource
.pin_sysmem
= 1;
1059 TRACE("New pointer is %p.\n", resource
->heap_memory
);
1063 context_release(context
);
1066 base
= buffer
->map_ptr
? buffer
->map_ptr
: resource
->heap_memory
;
1067 *map_ptr
= base
+ offset
;
1069 TRACE("Returning memory at %p (base %p, offset %u).\n", *map_ptr
, base
, offset
);
1074 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1076 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1077 unsigned int range_count
= buffer
->modified_areas
;
1078 struct wined3d_device
*device
= resource
->device
;
1079 struct wined3d_context
*context
;
1080 struct wined3d_bo_address addr
;
1082 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1084 if (sub_resource_idx
)
1086 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1087 return E_INVALIDARG
;
1090 if (!resource
->map_count
)
1092 WARN("Unmap called without a previous map call.\n");
1096 if (--resource
->map_count
)
1098 /* Delay loading the buffer until everything is unmapped. */
1099 TRACE("Ignoring unmap.\n");
1103 if (!buffer
->map_ptr
)
1106 context
= context_acquire(device
, NULL
, 0);
1108 addr
.buffer_object
= buffer
->buffer_object
;
1110 wined3d_context_unmap_bo_address(context
, &addr
, range_count
, buffer
->maps
);
1112 context_release(context
);
1114 buffer_clear_dirty_areas(buffer
);
1115 buffer
->map_ptr
= NULL
;
1120 static void wined3d_buffer_set_bo(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
, struct wined3d_bo
*bo
)
1122 struct wined3d_bo
*prev_bo
= buffer
->buffer_object
;
1124 TRACE("buffer %p, context %p, bo %p.\n", buffer
, context
, bo
);
1128 struct wined3d_bo_user
*bo_user
;
1130 LIST_FOR_EACH_ENTRY(bo_user
, &prev_bo
->users
, struct wined3d_bo_user
, entry
)
1131 bo_user
->valid
= false;
1132 list_init(&prev_bo
->users
);
1134 assert(list_empty(&bo
->users
));
1136 wined3d_context_destroy_bo(context
, prev_bo
);
1140 buffer
->buffer_object
= bo
;
1143 void wined3d_buffer_copy_bo_address(struct wined3d_buffer
*dst_buffer
, struct wined3d_context
*context
,
1144 unsigned int dst_offset
, const struct wined3d_const_bo_address
*src_addr
, unsigned int size
)
1146 uint32_t map_flags
= WINED3D_MAP_WRITE
;
1147 struct wined3d_bo_address dst_addr
;
1148 struct wined3d_range range
;
1151 if (!dst_offset
&& size
== dst_buffer
->resource
.size
)
1152 map_flags
|= WINED3D_MAP_DISCARD
;
1154 dst_location
= wined3d_buffer_get_memory(dst_buffer
, context
, &dst_addr
);
1155 dst_addr
.addr
+= dst_offset
;
1159 wined3d_context_copy_bo_address(context
, &dst_addr
, (const struct wined3d_bo_address
*)src_addr
, 1, &range
, map_flags
);
1160 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1163 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1164 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1166 struct wined3d_context
*context
;
1167 struct wined3d_bo_address src
;
1169 TRACE("dst_buffer %p, dst_offset %u, src_buffer %p, src_offset %u, size %u.\n",
1170 dst_buffer
, dst_offset
, src_buffer
, src_offset
, size
);
1172 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1174 wined3d_buffer_get_memory(src_buffer
, context
, &src
);
1175 src
.addr
+= src_offset
;
1177 wined3d_buffer_copy_bo_address(dst_buffer
, context
, dst_offset
, wined3d_const_bo_address(&src
), size
);
1179 context_release(context
);
1182 void wined3d_buffer_update_sub_resource(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1183 const struct upload_bo
*upload_bo
, unsigned int offset
, unsigned int size
)
1185 if (upload_bo
->flags
& UPLOAD_BO_RENAME_ON_UNMAP
)
1187 wined3d_buffer_set_bo(buffer
, context
, upload_bo
->addr
.buffer_object
);
1188 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1189 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1192 if (upload_bo
->addr
.buffer_object
&& upload_bo
->addr
.buffer_object
== buffer
->buffer_object
)
1194 struct wined3d_range range
;
1196 /* We need to flush changes, which is implicitly done by
1197 * wined3d_context_unmap_bo_address() even if we aren't actually going
1200 * We would also like to free up virtual address space used by this BO
1201 * if it's at a premium—note that this BO was allocated for an
1202 * accelerated map. Hence we unmap the BO instead of merely flushing it;
1203 * if we don't care about unmapping BOs then
1204 * wined3d_context_unmap_bo_address() will flush and return.
1206 range
.offset
= offset
;
1208 if (upload_bo
->addr
.buffer_object
->map_ptr
)
1209 wined3d_context_unmap_bo_address(context
, (const struct wined3d_bo_address
*)&upload_bo
->addr
, 1, &range
);
1213 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &upload_bo
->addr
, size
);
1217 static void wined3d_buffer_init_data(struct wined3d_buffer
*buffer
,
1218 struct wined3d_device
*device
, const struct wined3d_sub_resource_data
*data
)
1220 struct wined3d_resource
*resource
= &buffer
->resource
;
1221 struct wined3d_box box
;
1223 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
1225 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1226 wined3d_device_context_emit_update_sub_resource(&device
->cs
->c
, resource
,
1227 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1231 memcpy(buffer
->resource
.heap_memory
, data
->data
, resource
->size
);
1232 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
1233 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_SYSMEM
);
1237 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1239 return wined3d_buffer_incref(buffer_from_resource(resource
));
1242 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1244 return wined3d_buffer_decref(buffer_from_resource(resource
));
1247 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1249 struct wined3d_context
*context
;
1251 context
= context_acquire(resource
->device
, NULL
, 0);
1252 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1253 context_release(context
);
1256 static const struct wined3d_resource_ops buffer_resource_ops
=
1258 buffer_resource_incref
,
1259 buffer_resource_decref
,
1260 buffer_resource_preload
,
1261 buffer_resource_unload
,
1262 buffer_resource_sub_resource_get_desc
,
1263 buffer_resource_sub_resource_get_map_pitch
,
1264 buffer_resource_sub_resource_map
,
1265 buffer_resource_sub_resource_unmap
,
1268 static HRESULT
wined3d_buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1269 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1270 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_buffer_ops
*buffer_ops
)
1272 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, WINED3DFMT_R8_UNORM
, desc
->bind_flags
);
1273 struct wined3d_resource
*resource
= &buffer
->resource
;
1274 unsigned int access
;
1277 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1278 "access %s, data %p, parent %p, parent_ops %p.\n",
1279 buffer
, device
, desc
->byte_width
, debug_d3dusage(desc
->usage
), wined3d_debug_bind_flags(desc
->bind_flags
),
1280 wined3d_debug_resource_access(desc
->access
), data
, parent
, parent_ops
);
1282 if (!desc
->byte_width
)
1284 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1285 return E_INVALIDARG
;
1288 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& desc
->byte_width
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1290 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc
->byte_width
);
1291 return E_INVALIDARG
;
1294 if (data
&& !data
->data
)
1296 WARN("Invalid sub-resource data specified.\n");
1297 return E_INVALIDARG
;
1300 access
= desc
->access
;
1301 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& wined3d_settings
.cb_access_map_w
)
1302 access
|= WINED3D_RESOURCE_ACCESS_MAP_W
;
1304 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1305 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->bind_flags
, access
,
1306 desc
->byte_width
, 1, 1, desc
->byte_width
, parent
, parent_ops
, &buffer_resource_ops
)))
1308 WARN("Failed to initialize resource, hr %#lx.\n", hr
);
1311 buffer
->buffer_ops
= buffer_ops
;
1312 buffer
->structure_byte_stride
= desc
->structure_byte_stride
;
1313 buffer
->locations
= WINED3D_LOCATION_CLEARED
;
1315 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1316 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
, buffer
->resource
.heap_memory
);
1318 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1319 || (desc
->usage
& WINED3DUSAGE_MANAGED
))
1321 /* SWvp and managed buffers always return the same pointer in buffer
1322 * maps and retain data in DISCARD maps. Keep a system memory copy of
1323 * the buffer to provide the same behavior to the application. */
1324 TRACE("Pinning system memory.\n");
1325 buffer
->resource
.pin_sysmem
= 1;
1326 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1329 if (buffer
->locations
& WINED3D_LOCATION_SYSMEM
|| !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1331 if (!wined3d_resource_prepare_sysmem(&buffer
->resource
))
1332 return E_OUTOFMEMORY
;
1335 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1337 ERR("Out of memory.\n");
1338 buffer_resource_unload(resource
);
1339 resource_cleanup(resource
);
1340 wined3d_resource_wait_idle(resource
);
1341 return E_OUTOFMEMORY
;
1343 buffer
->maps_size
= 1;
1345 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
1346 buffer
->resource
.client
.addr
.buffer_object
= CLIENT_BO_DISCARDED
;
1349 wined3d_buffer_init_data(buffer
, device
, data
);
1354 static BOOL
wined3d_buffer_no3d_prepare_location(struct wined3d_buffer
*buffer
,
1355 struct wined3d_context
*context
, unsigned int location
)
1357 if (location
== WINED3D_LOCATION_SYSMEM
)
1358 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1360 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1365 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer
*buffer
,
1366 struct wined3d_context
*context
, unsigned int location
)
1368 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1371 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops
=
1373 wined3d_buffer_no3d_prepare_location
,
1374 wined3d_buffer_no3d_unload_location
,
1377 HRESULT
wined3d_buffer_no3d_init(struct wined3d_buffer
*buffer_no3d
, struct wined3d_device
*device
,
1378 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1379 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1381 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1382 buffer_no3d
, device
, desc
, data
, parent
, parent_ops
);
1384 return wined3d_buffer_init(buffer_no3d
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_no3d_ops
);
1387 static BOOL
wined3d_buffer_gl_prepare_location(struct wined3d_buffer
*buffer
,
1388 struct wined3d_context
*context
, unsigned int location
)
1390 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
1391 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
1395 case WINED3D_LOCATION_SYSMEM
:
1396 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1398 case WINED3D_LOCATION_BUFFER
:
1399 if (buffer
->buffer_object
)
1402 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1404 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
1407 return wined3d_buffer_gl_create_buffer_object(buffer_gl
, context_gl
);
1410 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1415 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer
*buffer
,
1416 struct wined3d_context
*context
, unsigned int location
)
1418 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1422 case WINED3D_LOCATION_BUFFER
:
1423 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer
), wined3d_context_gl(context
));
1427 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1432 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops
=
1434 wined3d_buffer_gl_prepare_location
,
1435 wined3d_buffer_gl_unload_location
,
1438 HRESULT
wined3d_buffer_gl_init(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_device
*device
,
1439 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1440 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1442 const struct wined3d_gl_info
*gl_info
= &wined3d_adapter_gl(device
->adapter
)->gl_info
;
1444 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1445 buffer_gl
, device
, desc
, data
, parent
, parent_ops
);
1447 /* Observations show that draw_primitive_immediate_mode() is faster on
1448 * dynamic vertex buffers than converting + draw_primitive_arrays().
1449 * (Half-Life 2 and others.) */
1450 if (!(desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
1451 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1452 else if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1453 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1454 else if (!(gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1455 && (desc
->usage
& WINED3DUSAGE_DYNAMIC
))
1456 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1458 buffer_gl
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1460 return wined3d_buffer_init(&buffer_gl
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_gl_ops
);
1463 VkBufferUsageFlags
vk_buffer_usage_from_bind_flags(uint32_t bind_flags
)
1465 VkBufferUsageFlags usage
;
1467 usage
= VK_BUFFER_USAGE_TRANSFER_SRC_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT
;
1468 if (bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
1469 usage
|= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
;
1470 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
1471 usage
|= VK_BUFFER_USAGE_INDEX_BUFFER_BIT
;
1472 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1473 usage
|= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
;
1474 if (bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
1475 usage
|= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
;
1476 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1477 usage
|= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT
;
1478 if (bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
)
1479 usage
|= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
;
1480 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
)
1481 usage
|= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
;
1482 if (bind_flags
& (WINED3D_BIND_RENDER_TARGET
| WINED3D_BIND_DEPTH_STENCIL
))
1483 FIXME("Ignoring some bind flags %#x.\n", bind_flags
);
1487 VkMemoryPropertyFlags
vk_memory_type_from_access_flags(uint32_t access
, uint32_t usage
)
1489 VkMemoryPropertyFlags memory_type
= 0;
1491 if (access
& WINED3D_RESOURCE_ACCESS_MAP_R
)
1492 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_CACHED_BIT
;
1493 else if (access
& WINED3D_RESOURCE_ACCESS_MAP_W
)
1494 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
;
1495 else if (!(usage
& WINED3DUSAGE_DYNAMIC
))
1496 memory_type
|= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
;
1500 static BOOL
wined3d_buffer_vk_create_buffer_object(struct wined3d_buffer_vk
*buffer_vk
,
1501 struct wined3d_context_vk
*context_vk
)
1503 struct wined3d_resource
*resource
= &buffer_vk
->b
.resource
;
1504 struct wined3d_bo_vk
*bo_vk
;
1506 if (!(bo_vk
= heap_alloc(sizeof(*bo_vk
))))
1509 if (!(wined3d_context_vk_create_bo(context_vk
, resource
->size
,
1510 vk_buffer_usage_from_bind_flags(resource
->bind_flags
),
1511 vk_memory_type_from_access_flags(resource
->access
, resource
->usage
), bo_vk
)))
1513 WARN("Failed to create Vulkan buffer.\n");
1518 buffer_vk
->b
.buffer_object
= &bo_vk
->b
;
1519 buffer_invalidate_bo_range(&buffer_vk
->b
, 0, 0);
1524 const VkDescriptorBufferInfo
*wined3d_buffer_vk_get_buffer_info(struct wined3d_buffer_vk
*buffer_vk
)
1526 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1528 if (buffer_vk
->b
.bo_user
.valid
)
1529 return &buffer_vk
->buffer_info
;
1531 buffer_vk
->buffer_info
.buffer
= bo
->vk_buffer
;
1532 buffer_vk
->buffer_info
.offset
= bo
->b
.buffer_offset
;
1533 buffer_vk
->buffer_info
.range
= buffer_vk
->b
.resource
.size
;
1534 wined3d_buffer_validate_user(&buffer_vk
->b
);
1536 return &buffer_vk
->buffer_info
;
1539 static BOOL
wined3d_buffer_vk_prepare_location(struct wined3d_buffer
*buffer
,
1540 struct wined3d_context
*context
, unsigned int location
)
1544 case WINED3D_LOCATION_SYSMEM
:
1545 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1547 case WINED3D_LOCATION_BUFFER
:
1548 if (buffer
->buffer_object
)
1551 return wined3d_buffer_vk_create_buffer_object(wined3d_buffer_vk(buffer
), wined3d_context_vk(context
));
1554 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1559 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer
*buffer
,
1560 struct wined3d_context
*context
, unsigned int location
)
1562 struct wined3d_context_vk
*context_vk
= wined3d_context_vk(context
);
1563 struct wined3d_bo_vk
*bo_vk
= wined3d_bo_vk(buffer
->buffer_object
);
1565 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1569 case WINED3D_LOCATION_BUFFER
:
1570 if (buffer
->bo_user
.valid
)
1572 buffer
->bo_user
.valid
= false;
1573 list_remove(&buffer
->bo_user
.entry
);
1575 wined3d_context_vk_destroy_bo(context_vk
, bo_vk
);
1577 buffer
->buffer_object
= NULL
;
1581 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1586 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops
=
1588 wined3d_buffer_vk_prepare_location
,
1589 wined3d_buffer_vk_unload_location
,
1592 HRESULT
wined3d_buffer_vk_init(struct wined3d_buffer_vk
*buffer_vk
, struct wined3d_device
*device
,
1593 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1594 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1596 const struct wined3d_vk_info
*vk_info
= &wined3d_adapter_vk(device
->adapter
)->vk_info
;
1598 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1599 buffer_vk
, device
, desc
, data
, parent
, parent_ops
);
1601 if ((desc
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1602 && !vk_info
->supported
[WINED3D_VK_EXT_TRANSFORM_FEEDBACK
])
1604 WARN("The Vulkan implementation does not support transform feedback.\n");
1605 return WINED3DERR_INVALIDCALL
;
1608 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
1609 buffer_vk
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1611 return wined3d_buffer_init(&buffer_vk
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_vk_ops
);
1614 void wined3d_buffer_vk_barrier(struct wined3d_buffer_vk
*buffer_vk
,
1615 struct wined3d_context_vk
*context_vk
, uint32_t bind_mask
)
1617 uint32_t src_bind_mask
= 0;
1619 TRACE("buffer_vk %p, context_vk %p, bind_mask %s.\n",
1620 buffer_vk
, context_vk
, wined3d_debug_bind_flags(bind_mask
));
1622 if (bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
)
1624 src_bind_mask
= buffer_vk
->bind_mask
& WINED3D_READ_ONLY_BIND_MASK
;
1626 src_bind_mask
= buffer_vk
->bind_mask
;
1628 buffer_vk
->bind_mask
= bind_mask
;
1630 else if ((buffer_vk
->bind_mask
& bind_mask
) != bind_mask
)
1632 src_bind_mask
= buffer_vk
->bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
;
1633 buffer_vk
->bind_mask
|= bind_mask
;
1638 const struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1639 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1640 VkBufferMemoryBarrier vk_barrier
;
1642 TRACE(" %s -> %s.\n",
1643 wined3d_debug_bind_flags(src_bind_mask
), wined3d_debug_bind_flags(bind_mask
));
1645 wined3d_context_vk_end_current_render_pass(context_vk
);
1647 vk_barrier
.sType
= VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER
;
1648 vk_barrier
.pNext
= NULL
;
1649 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(src_bind_mask
);
1650 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(bind_mask
);
1651 vk_barrier
.srcQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1652 vk_barrier
.dstQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1653 vk_barrier
.buffer
= bo
->vk_buffer
;
1654 vk_barrier
.offset
= bo
->b
.buffer_offset
;
1655 vk_barrier
.size
= buffer_vk
->b
.resource
.size
;
1656 VK_CALL(vkCmdPipelineBarrier(wined3d_context_vk_get_command_buffer(context_vk
),
1657 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask
),
1658 vk_pipeline_stage_mask_from_bind_flags(bind_mask
),
1659 0, 0, NULL
, 1, &vk_barrier
, 0, NULL
));
1663 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1664 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1665 struct wined3d_buffer
**buffer
)
1667 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1668 device
, desc
, data
, parent
, parent_ops
, buffer
);
1670 return device
->adapter
->adapter_ops
->adapter_create_buffer(device
, desc
, data
, parent
, parent_ops
, buffer
);
1673 static HRESULT
wined3d_streaming_buffer_prepare(struct wined3d_device
*device
,
1674 struct wined3d_streaming_buffer
*buffer
, unsigned int min_size
)
1676 struct wined3d_buffer
*wined3d_buffer
;
1677 struct wined3d_buffer_desc desc
;
1678 unsigned int old_size
= 0;
1684 old_size
= buffer
->buffer
->resource
.size
;
1685 if (old_size
>= min_size
)
1689 size
= max(SB_MIN_SIZE
, max(old_size
* 2, min_size
));
1690 TRACE("Growing buffer to %u bytes.\n", size
);
1692 desc
.byte_width
= size
;
1693 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
1694 desc
.bind_flags
= buffer
->bind_flags
;
1695 desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1696 desc
.misc_flags
= 0;
1697 desc
.structure_byte_stride
= 0;
1699 if (SUCCEEDED(hr
= wined3d_buffer_create(device
, &desc
, NULL
, NULL
, &wined3d_null_parent_ops
, &wined3d_buffer
)))
1702 wined3d_buffer_decref(buffer
->buffer
);
1703 buffer
->buffer
= wined3d_buffer
;
1709 HRESULT CDECL
wined3d_streaming_buffer_map(struct wined3d_device
*device
,
1710 struct wined3d_streaming_buffer
*buffer
, unsigned int size
, unsigned int stride
,
1711 unsigned int *ret_pos
, void **ret_data
)
1713 unsigned int map_flags
= WINED3D_MAP_WRITE
;
1714 struct wined3d_resource
*resource
;
1715 struct wined3d_map_desc map_desc
;
1716 unsigned int pos
, align
;
1717 struct wined3d_box box
;
1720 TRACE("device %p, buffer %p, size %u, stride %u, ret_pos %p, ret_data %p.\n",
1721 device
, buffer
, size
, stride
, ret_pos
, ret_data
);
1723 if (FAILED(hr
= wined3d_streaming_buffer_prepare(device
, buffer
, size
)))
1725 resource
= &buffer
->buffer
->resource
;
1728 if ((align
= pos
% stride
))
1729 align
= stride
- align
;
1730 if (pos
+ size
+ align
> resource
->size
)
1733 map_flags
|= WINED3D_MAP_DISCARD
;
1738 map_flags
|= WINED3D_MAP_NOOVERWRITE
;
1741 wined3d_box_set(&box
, pos
, 0, pos
+ size
, 1, 0, 1);
1742 if (SUCCEEDED(hr
= wined3d_resource_map(resource
, 0, &map_desc
, &box
, map_flags
)))
1745 *ret_data
= map_desc
.data
;
1746 buffer
->pos
= pos
+ size
;
1751 void CDECL
wined3d_streaming_buffer_unmap(struct wined3d_streaming_buffer
*buffer
)
1753 wined3d_resource_unmap(&buffer
->buffer
->resource
, 0);
1756 HRESULT CDECL
wined3d_streaming_buffer_upload(struct wined3d_device
*device
, struct wined3d_streaming_buffer
*buffer
,
1757 const void *data
, unsigned int size
, unsigned int stride
, unsigned int *ret_pos
)
1762 if (SUCCEEDED(hr
= wined3d_streaming_buffer_map(device
, buffer
, size
, stride
, ret_pos
, &dst_data
)))
1764 memcpy(dst_data
, data
, size
);
1765 wined3d_streaming_buffer_unmap(buffer
);