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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
32 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
33 #define WINED3D_BUFFER_PIN_SYSMEM 0x04 /* Keep a system memory copy for this buffer. */
35 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
36 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
37 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
38 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
40 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer
*buffer
)
42 if (buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
44 TRACE("Not evicting system memory for buffer %p.\n", buffer
);
48 TRACE("Evicting system memory for buffer %p.\n", buffer
);
49 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
50 wined3d_resource_free_sysmem(&buffer
->resource
);
53 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
55 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
58 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
60 WARN("Invalid range specified, invalidating entire buffer.\n");
64 if (!wined3d_array_reserve((void **)&buffer
->maps
, &buffer
->maps_size
,
65 buffer
->modified_areas
+ 1, sizeof(*buffer
->maps
)))
67 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
71 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
72 buffer
->maps
[buffer
->modified_areas
].size
= size
;
73 ++buffer
->modified_areas
;
77 buffer
->modified_areas
= 1;
78 buffer
->maps
[0].offset
= 0;
79 buffer
->maps
[0].size
= buffer
->resource
.size
;
82 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
84 This
->modified_areas
= 0;
87 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
89 return !!buffer
->modified_areas
;
92 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
94 return buffer
->modified_areas
== 1
95 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
98 static void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, DWORD location
)
100 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
102 if (location
& WINED3D_LOCATION_BUFFER
)
103 buffer_clear_dirty_areas(buffer
);
105 buffer
->locations
|= location
;
107 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
110 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
111 unsigned int offset
, unsigned int size
)
113 TRACE("buffer %p, location %s, offset %u, size %u.\n",
114 buffer
, wined3d_debug_location(location
), offset
, size
);
116 if (location
& WINED3D_LOCATION_BUFFER
)
117 buffer_invalidate_bo_range(buffer
, offset
, size
);
119 buffer
->locations
&= ~location
;
121 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
123 if (!buffer
->locations
)
124 ERR("Buffer %p does not have any up to date location.\n", buffer
);
127 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, DWORD location
)
129 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
132 /* Context activation is done by the caller. */
133 static void wined3d_buffer_gl_bind(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_context_gl
*context_gl
)
135 const struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(buffer_gl
->b
.buffer_object
);
137 wined3d_context_gl_bind_bo(context_gl
, bo_gl
->binding
, bo_gl
->id
);
140 static 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 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
146 return GL_ELEMENT_ARRAY_BUFFER
;
148 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
149 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
150 return GL_TEXTURE_BUFFER
;
152 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
153 return GL_UNIFORM_BUFFER
;
155 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
156 return GL_TRANSFORM_FEEDBACK_BUFFER
;
158 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
159 && gl_info
->supported
[ARB_DRAW_INDIRECT
])
160 return GL_DRAW_INDIRECT_BUFFER
;
162 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
163 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
165 return GL_ARRAY_BUFFER
;
168 /* Context activation is done by the caller. */
169 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
170 struct wined3d_context_gl
*context_gl
)
172 struct wined3d_resource
*resource
= &buffer_gl
->b
.resource
;
173 struct wined3d_bo_gl
*bo_gl
;
175 if (!buffer_gl
->b
.buffer_object
)
177 bo_gl
= wined3d_bo_gl(buffer_gl
->b
.buffer_object
);
179 if (context_gl
->c
.transform_feedback_active
&& (resource
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
180 && wined3d_context_is_graphics_state_dirty(&context_gl
->c
, STATE_STREAM_OUTPUT
))
182 /* It's illegal to (un)bind GL_TRANSFORM_FEEDBACK_BUFFER while transform
183 * feedback is active. Deleting a buffer implicitly unbinds it, so we
184 * need to end transform feedback here if this buffer was bound.
186 * This should only be possible if STATE_STREAM_OUTPUT is dirty; if we
187 * do a draw call before destroying this buffer then the draw call will
188 * already rebind the GL target. */
189 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl
);
190 wined3d_context_gl_end_transform_feedback(context_gl
);
193 buffer_gl
->b
.bo_user
.valid
= false;
194 list_remove(&buffer_gl
->b
.bo_user
.entry
);
195 wined3d_context_gl_destroy_bo(context_gl
, bo_gl
);
197 buffer_gl
->b
.buffer_object
= NULL
;
200 /* Context activation is done by the caller. */
201 static BOOL
wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
202 struct wined3d_context_gl
*context_gl
)
204 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
205 GLenum usage
= GL_STATIC_DRAW
;
206 GLbitfield gl_storage_flags
;
207 struct wined3d_bo_gl
*bo
;
208 bool coherent
= true;
212 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
213 buffer_gl
, debug_d3dusage(buffer_gl
->b
.resource
.usage
));
215 if (!(bo
= heap_alloc(sizeof(*bo
))))
218 size
= buffer_gl
->b
.resource
.size
;
219 binding
= wined3d_buffer_gl_binding_from_bind_flags(gl_info
, buffer_gl
->b
.resource
.bind_flags
);
220 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
222 usage
= GL_STREAM_DRAW_ARB
;
225 gl_storage_flags
= wined3d_resource_gl_storage_flags(&buffer_gl
->b
.resource
);
226 if (!wined3d_context_gl_create_bo(context_gl
, size
, binding
, usage
, coherent
, gl_storage_flags
, bo
))
228 ERR("Failed to create OpenGL buffer object.\n");
229 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_USE_BO
;
230 buffer_clear_dirty_areas(&buffer_gl
->b
);
235 list_add_head(&bo
->b
.users
, &buffer_gl
->b
.bo_user
.entry
);
236 buffer_gl
->b
.buffer_object
= &bo
->b
;
237 buffer_invalidate_bo_range(&buffer_gl
->b
, 0, 0);
242 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
243 const enum wined3d_buffer_conversion_type conversion_type
,
244 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
246 const struct wined3d_format
*format
= attrib
->format
;
251 /* Check for some valid situations which cause us pain. One is if the buffer is used for
252 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
253 * with different strides. In the 2nd case we might have to drop conversion entirely,
254 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
258 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
259 debug_d3dformat(format
->id
));
261 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
263 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
267 *stride_this_run
= attrib
->stride
;
268 if (buffer
->stride
!= *stride_this_run
)
270 /* We rely that this happens only on the first converted attribute that is found,
271 * if at all. See above check
273 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
274 buffer
->stride
= *stride_this_run
;
275 heap_free(buffer
->conversion_map
);
276 buffer
->conversion_map
= heap_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
281 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
282 for (i
= 0; i
< format
->byte_count
; ++i
)
284 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
285 if (buffer
->conversion_map
[idx
] != conversion_type
)
287 TRACE("Byte %lu in vertex changed:\n", idx
);
288 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
290 buffer
->conversion_map
[idx
] = conversion_type
;
297 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
298 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
300 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
301 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
303 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
304 enum wined3d_format_id format
;
307 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
308 * there, on nonexistent attribs the vbo is 0.
310 if (!(si
->use_map
& (1u << attrib_idx
))
311 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
314 format
= attrib
->format
->id
;
315 /* Look for newly appeared conversion */
316 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
318 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
320 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
322 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
324 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
328 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
330 else if (This
->conversion_map
)
332 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
338 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
339 const struct wined3d_state
*state
, DWORD fixup_flags
)
341 UINT stride_this_run
= 0;
344 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
345 * Once we have our declaration there is no need to look it up again. Index buffers also never need
346 * conversion, so once the (empty) conversion structure is created don't bother checking again
348 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
350 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
355 TRACE("No fixup required.\n");
356 if(This
->conversion_map
)
358 heap_free(This
->conversion_map
);
359 This
->conversion_map
= NULL
;
367 TRACE("Finding vertex buffer conversion information\n");
368 /* Certain declaration types need some fixups before we can pass them to
369 * opengl. This means D3DCOLOR attributes with fixed function vertex
370 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
371 * GL_ARB_half_float_vertex is not supported.
373 * Note for d3d8 and d3d9:
374 * The vertex buffer FVF doesn't help with finding them, we have to use
375 * the decoded vertex declaration and pick the things that concern the
376 * current buffer. A problem with this is that this can change between
377 * draws, so we have to validate the information and reprocess the buffer
378 * if it changes, and avoid false positives for performance reasons.
379 * WineD3D doesn't even know the vertex buffer any more, it is managed
380 * by the client libraries and passed to SetStreamSource and ProcessVertices
383 * We have to distinguish between vertex shaders and fixed function to
384 * pick the way we access the strided vertex information.
386 * This code sets up a per-byte array with the size of the detected
387 * stride of the arrays in the buffer. For each byte we have a field
388 * that marks the conversion needed on this byte. For example, the
389 * following declaration with fixed function vertex processing:
397 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
398 * [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]
400 * Where in this example map P means 4 component position conversion, 0
401 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
402 * conversion (red / blue swizzle).
404 * If we're doing conversion and the stride changes we have to reconvert
405 * the whole buffer. Note that we do not mind if the semantic changes,
406 * we only care for the conversion type. So if the NORMAL is replaced
407 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
408 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
409 * conversion types depend on the semantic as well, for example a FLOAT4
410 * texcoord needs no conversion while a FLOAT4 positiont needs one
413 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
414 fixup_flags
, &stride_this_run
) || ret
;
415 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
417 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
418 fixup_flags
, &stride_this_run
) || ret
;
419 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
420 fixup_flags
, &stride_this_run
) || ret
;
421 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
422 fixup_flags
, &stride_this_run
) || ret
;
423 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
424 fixup_flags
, &stride_this_run
) || ret
;
425 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
426 fixup_flags
, &stride_this_run
) || ret
;
427 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
428 fixup_flags
, &stride_this_run
) || ret
;
429 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
430 fixup_flags
, &stride_this_run
) || ret
;
431 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
432 fixup_flags
, &stride_this_run
) || ret
;
433 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
434 fixup_flags
, &stride_this_run
) || ret
;
435 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
436 fixup_flags
, &stride_this_run
) || ret
;
437 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
438 fixup_flags
, &stride_this_run
) || ret
;
439 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
440 fixup_flags
, &stride_this_run
) || ret
;
441 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
442 fixup_flags
, &stride_this_run
) || ret
;
444 if (!stride_this_run
&& This
->conversion_map
)
448 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
449 heap_free(This
->conversion_map
);
450 This
->conversion_map
= NULL
;
454 if (ret
) TRACE("Conversion information changed\n");
459 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
461 DWORD src_color
= *dst_color
;
463 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
464 * endianness. If we want this to work on big-endian machines as well we
465 * have to consider more things.
467 * 0xff000000: Alpha mask
468 * 0x00ff0000: Blue mask
469 * 0x0000ff00: Green mask
470 * 0x000000ff: Red mask
473 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
474 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
475 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
477 return sizeof(*dst_color
);
480 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
482 /* rhw conversion like in position_float4(). */
483 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
485 float w
= 1.0f
/ p
->w
;
495 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
497 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
499 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
504 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
506 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
509 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
511 ERR("Failed to load system memory.\n");
514 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
516 /* Now for each vertex in the buffer that needs conversion. */
517 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
519 if (!(data
= heap_alloc(buffer
->resource
.size
)))
521 ERR("Out of memory.\n");
525 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
527 start
= buffer
->maps
[range_idx
].offset
;
528 end
= start
+ buffer
->maps
[range_idx
].size
;
530 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
531 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
533 for (j
= 0; j
< buffer
->stride
;)
535 switch (buffer
->conversion_map
[j
])
542 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
545 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
548 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
555 buffer
->buffer_ops
->buffer_upload_ranges(buffer
, context
,
556 data
, 0, buffer
->modified_areas
, buffer
->maps
);
561 BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
562 struct wined3d_context
*context
, unsigned int location
)
564 return buffer
->buffer_ops
->buffer_prepare_location(buffer
, context
, location
);
567 static void wined3d_buffer_unload_location(struct wined3d_buffer
*buffer
,
568 struct wined3d_context
*context
, unsigned int location
)
570 buffer
->buffer_ops
->buffer_unload_location(buffer
, context
, location
);
573 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
574 struct wined3d_context
*context
, DWORD location
)
576 struct wined3d_range range
;
578 TRACE("buffer %p, context %p, location %s.\n",
579 buffer
, context
, wined3d_debug_location(location
));
581 if (buffer
->locations
& location
)
583 TRACE("Location (%#x) is already up to date.\n", location
);
587 if (!buffer
->locations
)
589 ERR("Buffer %p does not have any up to date location.\n", buffer
);
590 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
591 return wined3d_buffer_load_location(buffer
, context
, location
);
594 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
596 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
599 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
601 TRACE("Buffer previously discarded, nothing to do.\n");
602 wined3d_buffer_validate_location(buffer
, location
);
603 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
609 case WINED3D_LOCATION_SYSMEM
:
611 range
.size
= buffer
->resource
.size
;
612 buffer
->buffer_ops
->buffer_download_ranges(buffer
, context
,
613 buffer
->resource
.heap_memory
, 0, 1, &range
);
616 case WINED3D_LOCATION_BUFFER
:
617 if (!buffer
->conversion_map
)
618 buffer
->buffer_ops
->buffer_upload_ranges(buffer
, context
,
619 buffer
->resource
.heap_memory
, 0, buffer
->modified_areas
, buffer
->maps
);
621 buffer_conversion_upload(buffer
, context
);
625 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
629 wined3d_buffer_validate_location(buffer
, location
);
630 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
631 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
632 wined3d_buffer_evict_sysmem(buffer
);
637 /* Context activation is done by the caller. */
638 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
640 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
641 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
642 return buffer
->resource
.heap_memory
;
645 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
646 struct wined3d_bo_address
*data
)
648 unsigned int locations
= buffer
->locations
;
650 TRACE("buffer %p, context %p, data %p, locations %s.\n",
651 buffer
, context
, data
, wined3d_debug_location(locations
));
653 if (locations
& WINED3D_LOCATION_DISCARDED
)
655 locations
= ((buffer
->flags
& WINED3D_BUFFER_USE_BO
) ? WINED3D_LOCATION_BUFFER
: WINED3D_LOCATION_SYSMEM
);
656 if (!wined3d_buffer_prepare_location(buffer
, context
, locations
))
658 data
->buffer_object
= 0;
662 wined3d_buffer_validate_location(buffer
, locations
);
663 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
665 if (locations
& WINED3D_LOCATION_BUFFER
)
667 data
->buffer_object
= buffer
->buffer_object
;
669 return WINED3D_LOCATION_BUFFER
;
671 if (locations
& WINED3D_LOCATION_SYSMEM
)
673 data
->buffer_object
= 0;
674 data
->addr
= buffer
->resource
.heap_memory
;
675 return WINED3D_LOCATION_SYSMEM
;
678 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
679 data
->buffer_object
= 0;
684 static void buffer_resource_unload(struct wined3d_resource
*resource
)
686 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
688 TRACE("buffer %p.\n", buffer
);
690 if (buffer
->buffer_object
)
692 struct wined3d_context
*context
;
694 context
= context_acquire(resource
->device
, NULL
, 0);
696 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
697 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
698 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
699 buffer_clear_dirty_areas(buffer
);
701 context_release(context
);
703 heap_free(buffer
->conversion_map
);
704 buffer
->conversion_map
= NULL
;
706 buffer
->conversion_stride
= 0;
707 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
710 resource_unload(resource
);
713 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
715 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
716 buffer_resource_unload(&buffer
->resource
);
719 static void wined3d_buffer_destroy_object(void *object
)
721 struct wined3d_buffer
*buffer
= object
;
722 struct wined3d_context
*context
;
724 TRACE("buffer %p.\n", buffer
);
726 if (buffer
->buffer_object
)
728 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
729 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
730 context_release(context
);
732 heap_free(buffer
->conversion_map
);
733 heap_free(buffer
->maps
);
736 void wined3d_buffer_cleanup(struct wined3d_buffer
*buffer
)
738 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
739 resource_cleanup(&buffer
->resource
);
742 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
744 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
746 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
750 wined3d_mutex_lock();
751 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
752 buffer
->resource
.device
->adapter
->adapter_ops
->adapter_destroy_buffer(buffer
);
753 wined3d_mutex_unlock();
759 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
761 TRACE("buffer %p.\n", buffer
);
763 return buffer
->resource
.parent
;
766 /* Context activation is done by the caller. */
767 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
768 const struct wined3d_state
*state
)
770 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
771 BOOL decl_changed
= FALSE
;
773 TRACE("buffer %p.\n", buffer
);
775 if (buffer
->resource
.map_count
&& buffer
->map_ptr
)
777 FIXME("Buffer is mapped through buffer object, not loading.\n");
780 else if (buffer
->resource
.map_count
)
782 WARN("Loading mapped buffer.\n");
785 /* TODO: Make converting independent from VBOs */
786 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
788 /* Not doing any conversion */
792 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
794 ERR("Failed to prepare buffer location.\n");
798 /* Reading the declaration makes only sense if we have valid state information
799 * (i.e., if this function is called during draws). */
802 DWORD fixup_flags
= 0;
806 if (!d3d_info
->vertex_bgra
&& !d3d_info
->ffp_generic_attributes
)
807 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
808 if (!d3d_info
->xyzrhw
)
809 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
812 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
813 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
816 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
818 ++buffer
->draw_count
;
819 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
820 buffer
->decl_change_count
= 0;
821 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
822 buffer
->full_conversion_count
= 0;
826 /* If applications change the declaration over and over, reconverting all the time is a huge
827 * performance hit. So count the declaration changes and release the VBO if there are too many
828 * of them (and thus stop converting)
832 ++buffer
->decl_change_count
;
833 buffer
->draw_count
= 0;
835 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
836 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
838 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
839 wined3d_buffer_drop_bo(buffer
);
843 /* The declaration changed, reload the whole buffer. */
844 WARN("Reloading buffer because of a vertex declaration change.\n");
845 buffer_invalidate_bo_range(buffer
, 0, 0);
849 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
850 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
851 * decl changes and reset the decl change count after a specific number of them
853 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
855 ++buffer
->full_conversion_count
;
856 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
858 FIXME("Too many full buffer conversions, stopping converting.\n");
859 wined3d_buffer_drop_bo(buffer
);
865 ++buffer
->draw_count
;
866 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
867 buffer
->decl_change_count
= 0;
868 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
869 buffer
->full_conversion_count
= 0;
873 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
874 ERR("Failed to load buffer location.\n");
877 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
879 TRACE("buffer %p.\n", buffer
);
881 return &buffer
->resource
;
884 static HRESULT
buffer_resource_sub_resource_get_desc(struct wined3d_resource
*resource
,
885 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
887 if (sub_resource_idx
)
889 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
893 desc
->format
= WINED3DFMT_R8_UNORM
;
894 desc
->multisample_type
= WINED3D_MULTISAMPLE_NONE
;
895 desc
->multisample_quality
= 0;
896 desc
->usage
= resource
->usage
;
897 desc
->bind_flags
= resource
->bind_flags
;
898 desc
->access
= resource
->access
;
899 desc
->width
= resource
->size
;
902 desc
->size
= resource
->size
;
906 static void buffer_resource_sub_resource_get_map_pitch(struct wined3d_resource
*resource
,
907 unsigned int sub_resource_idx
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
909 *row_pitch
= *slice_pitch
= resource
->size
;
912 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
913 void **map_ptr
, const struct wined3d_box
*box
, uint32_t flags
)
915 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
916 struct wined3d_device
*device
= resource
->device
;
917 struct wined3d_context
*context
;
918 unsigned int offset
, size
;
922 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
923 resource
, sub_resource_idx
, map_ptr
, debug_box(box
), flags
);
926 size
= box
->right
- box
->left
;
928 count
= ++resource
->map_count
;
930 if (buffer
->buffer_object
)
932 unsigned int dirty_offset
= offset
, dirty_size
= size
;
933 struct wined3d_bo_address addr
;
935 /* DISCARD invalidates the entire buffer, regardless of the specified
936 * offset and size. Some applications also depend on the entire buffer
937 * being uploaded in that case. Two such applications are Port Royale
938 * and Darkstar One. */
939 if (flags
& WINED3D_MAP_DISCARD
)
945 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
946 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
947 || buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
949 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
951 context
= context_acquire(device
, NULL
, 0);
952 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
953 context_release(context
);
956 if (flags
& WINED3D_MAP_WRITE
)
957 wined3d_buffer_invalidate_range(buffer
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
961 context
= context_acquire(device
, NULL
, 0);
963 if (flags
& WINED3D_MAP_DISCARD
)
964 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
966 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
968 if (flags
& WINED3D_MAP_WRITE
)
970 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
971 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
974 if ((flags
& WINED3D_MAP_DISCARD
) && resource
->heap_memory
)
975 wined3d_buffer_evict_sysmem(buffer
);
979 addr
.buffer_object
= buffer
->buffer_object
;
981 buffer
->map_ptr
= wined3d_context_map_bo_address(context
, &addr
, resource
->size
, flags
);
982 /* We are accessing buffer->resource.client from the CS thread,
983 * but it's safe because the client thread will wait for the
984 * map to return, thus completely serializing this call with
985 * other client code. */
986 buffer
->resource
.client
.addr
= addr
;
988 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
990 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
992 wined3d_context_unmap_bo_address(context
, &addr
, 0, NULL
);
993 buffer
->map_ptr
= NULL
;
995 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
997 /* The extra copy is more expensive than not using VBOs
998 * at all on the NVIDIA Linux driver, which is the
999 * only driver that returns unaligned pointers. */
1000 TRACE("Dynamic buffer, dropping VBO.\n");
1001 wined3d_buffer_drop_bo(buffer
);
1005 TRACE("Falling back to doublebuffered operation.\n");
1006 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1007 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1009 TRACE("New pointer is %p.\n", resource
->heap_memory
);
1013 context_release(context
);
1017 base
= buffer
->map_ptr
? buffer
->map_ptr
: resource
->heap_memory
;
1018 *map_ptr
= base
+ offset
;
1020 TRACE("Returning memory at %p (base %p, offset %u).\n", *map_ptr
, base
, offset
);
1025 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1027 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1028 unsigned int range_count
= buffer
->modified_areas
;
1029 struct wined3d_device
*device
= resource
->device
;
1030 struct wined3d_context
*context
;
1031 struct wined3d_bo_address addr
;
1033 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1035 if (sub_resource_idx
)
1037 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1038 return E_INVALIDARG
;
1041 if (!resource
->map_count
)
1043 WARN("Unmap called without a previous map call.\n");
1047 if (--resource
->map_count
)
1049 /* Delay loading the buffer until everything is unmapped. */
1050 TRACE("Ignoring unmap.\n");
1054 if (!buffer
->map_ptr
)
1057 context
= context_acquire(device
, NULL
, 0);
1059 addr
.buffer_object
= buffer
->buffer_object
;
1061 wined3d_context_unmap_bo_address(context
, &addr
, range_count
, buffer
->maps
);
1063 context_release(context
);
1065 buffer_clear_dirty_areas(buffer
);
1066 buffer
->map_ptr
= NULL
;
1071 static void wined3d_buffer_set_bo(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
, struct wined3d_bo
*bo
)
1073 struct wined3d_bo
*prev_bo
= buffer
->buffer_object
;
1075 TRACE("buffer %p, context %p, bo %p.\n", buffer
, context
, bo
);
1079 struct wined3d_bo_user
*bo_user
;
1081 LIST_FOR_EACH_ENTRY(bo_user
, &prev_bo
->users
, struct wined3d_bo_user
, entry
)
1082 bo_user
->valid
= false;
1083 assert(list_empty(&bo
->users
));
1084 list_move_head(&bo
->users
, &prev_bo
->users
);
1086 wined3d_context_destroy_bo(context
, prev_bo
);
1091 list_add_head(&bo
->users
, &buffer
->bo_user
.entry
);
1094 buffer
->buffer_object
= bo
;
1097 void wined3d_buffer_copy_bo_address(struct wined3d_buffer
*dst_buffer
, struct wined3d_context
*context
,
1098 unsigned int dst_offset
, const struct wined3d_const_bo_address
*src_addr
, unsigned int size
)
1100 struct wined3d_bo_address dst_addr
;
1103 dst_location
= wined3d_buffer_get_memory(dst_buffer
, context
, &dst_addr
);
1104 dst_addr
.addr
+= dst_offset
;
1106 wined3d_context_copy_bo_address(context
, &dst_addr
, (const struct wined3d_bo_address
*)src_addr
, size
);
1107 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1110 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1111 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1113 struct wined3d_context
*context
;
1114 struct wined3d_bo_address src
;
1116 TRACE("dst_buffer %p, dst_offset %u, src_buffer %p, src_offset %u, size %u.\n",
1117 dst_buffer
, dst_offset
, src_buffer
, src_offset
, size
);
1119 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1121 wined3d_buffer_get_memory(src_buffer
, context
, &src
);
1122 src
.addr
+= src_offset
;
1124 wined3d_buffer_copy_bo_address(dst_buffer
, context
, dst_offset
, wined3d_const_bo_address(&src
), size
);
1126 context_release(context
);
1129 void wined3d_buffer_update_sub_resource(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1130 const struct upload_bo
*upload_bo
, unsigned int offset
, unsigned int size
)
1132 if (upload_bo
->flags
& UPLOAD_BO_RENAME_ON_UNMAP
)
1134 wined3d_buffer_set_bo(buffer
, context
, upload_bo
->addr
.buffer_object
);
1135 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1136 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1139 if (upload_bo
->addr
.buffer_object
&& upload_bo
->addr
.buffer_object
== buffer
->buffer_object
)
1140 wined3d_context_flush_bo_address(context
, &upload_bo
->addr
, size
);
1142 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &upload_bo
->addr
, size
);
1145 static void wined3d_buffer_init_data(struct wined3d_buffer
*buffer
,
1146 struct wined3d_device
*device
, const struct wined3d_sub_resource_data
*data
)
1148 struct wined3d_resource
*resource
= &buffer
->resource
;
1149 struct wined3d_box box
;
1151 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
1153 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1154 wined3d_device_context_emit_update_sub_resource(&device
->cs
->c
, resource
,
1155 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1159 memcpy(buffer
->resource
.heap_memory
, data
->data
, resource
->size
);
1160 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
1161 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_SYSMEM
);
1165 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1167 return wined3d_buffer_incref(buffer_from_resource(resource
));
1170 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1172 return wined3d_buffer_decref(buffer_from_resource(resource
));
1175 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1177 struct wined3d_context
*context
;
1179 context
= context_acquire(resource
->device
, NULL
, 0);
1180 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1181 context_release(context
);
1184 static const struct wined3d_resource_ops buffer_resource_ops
=
1186 buffer_resource_incref
,
1187 buffer_resource_decref
,
1188 buffer_resource_preload
,
1189 buffer_resource_unload
,
1190 buffer_resource_sub_resource_get_desc
,
1191 buffer_resource_sub_resource_get_map_pitch
,
1192 buffer_resource_sub_resource_map
,
1193 buffer_resource_sub_resource_unmap
,
1196 static HRESULT
wined3d_buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1197 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1198 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_buffer_ops
*buffer_ops
)
1200 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, WINED3DFMT_R8_UNORM
, desc
->bind_flags
);
1201 struct wined3d_resource
*resource
= &buffer
->resource
;
1202 unsigned int access
;
1205 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1206 "access %s, data %p, parent %p, parent_ops %p.\n",
1207 buffer
, device
, desc
->byte_width
, debug_d3dusage(desc
->usage
), wined3d_debug_bind_flags(desc
->bind_flags
),
1208 wined3d_debug_resource_access(desc
->access
), data
, parent
, parent_ops
);
1210 if (!desc
->byte_width
)
1212 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1213 return E_INVALIDARG
;
1216 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& desc
->byte_width
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1218 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc
->byte_width
);
1219 return E_INVALIDARG
;
1222 if (data
&& !data
->data
)
1224 WARN("Invalid sub-resource data specified.\n");
1225 return E_INVALIDARG
;
1228 access
= desc
->access
;
1229 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& wined3d_settings
.cb_access_map_w
)
1230 access
|= WINED3D_RESOURCE_ACCESS_MAP_W
;
1232 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1233 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->bind_flags
, access
,
1234 desc
->byte_width
, 1, 1, desc
->byte_width
, parent
, parent_ops
, &buffer_resource_ops
)))
1236 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1239 buffer
->buffer_ops
= buffer_ops
;
1240 buffer
->structure_byte_stride
= desc
->structure_byte_stride
;
1241 buffer
->locations
= data
? WINED3D_LOCATION_DISCARDED
: WINED3D_LOCATION_SYSMEM
;
1243 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1244 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
, buffer
->resource
.heap_memory
);
1246 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1247 || wined3d_resource_access_is_managed(access
))
1249 /* SWvp and managed buffers always return the same pointer in buffer
1250 * maps and retain data in DISCARD maps. Keep a system memory copy of
1251 * the buffer to provide the same behavior to the application. */
1252 TRACE("Pinning system memory.\n");
1253 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1254 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1257 if (buffer
->locations
& WINED3D_LOCATION_SYSMEM
|| !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1259 if (!wined3d_resource_prepare_sysmem(&buffer
->resource
))
1260 return E_OUTOFMEMORY
;
1263 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1265 ERR("Out of memory.\n");
1266 buffer_resource_unload(resource
);
1267 resource_cleanup(resource
);
1268 wined3d_resource_wait_idle(resource
);
1269 return E_OUTOFMEMORY
;
1271 buffer
->maps_size
= 1;
1274 wined3d_buffer_init_data(buffer
, device
, data
);
1279 static BOOL
wined3d_buffer_no3d_prepare_location(struct wined3d_buffer
*buffer
,
1280 struct wined3d_context
*context
, unsigned int location
)
1282 if (location
== WINED3D_LOCATION_SYSMEM
)
1283 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1285 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1290 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer
*buffer
,
1291 struct wined3d_context
*context
, unsigned int location
)
1293 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1296 static void wined3d_buffer_no3d_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1297 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1299 FIXME("Not implemented.\n");
1302 static void wined3d_buffer_no3d_download_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1303 void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1305 FIXME("Not implemented.\n");
1308 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops
=
1310 wined3d_buffer_no3d_prepare_location
,
1311 wined3d_buffer_no3d_unload_location
,
1312 wined3d_buffer_no3d_upload_ranges
,
1313 wined3d_buffer_no3d_download_ranges
,
1316 HRESULT
wined3d_buffer_no3d_init(struct wined3d_buffer
*buffer_no3d
, struct wined3d_device
*device
,
1317 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1318 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1320 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1321 buffer_no3d
, device
, desc
, data
, parent
, parent_ops
);
1323 return wined3d_buffer_init(buffer_no3d
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_no3d_ops
);
1326 static BOOL
wined3d_buffer_gl_prepare_location(struct wined3d_buffer
*buffer
,
1327 struct wined3d_context
*context
, unsigned int location
)
1329 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
1330 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
1334 case WINED3D_LOCATION_SYSMEM
:
1335 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1337 case WINED3D_LOCATION_BUFFER
:
1338 if (buffer
->buffer_object
)
1341 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1343 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
1346 return wined3d_buffer_gl_create_buffer_object(buffer_gl
, context_gl
);
1349 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1354 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer
*buffer
,
1355 struct wined3d_context
*context
, unsigned int location
)
1357 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1361 case WINED3D_LOCATION_BUFFER
:
1362 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer
), wined3d_context_gl(context
));
1366 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1371 /* Context activation is done by the caller. */
1372 static void wined3d_buffer_gl_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1373 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1375 struct wined3d_bo_address src
, dst
;
1377 TRACE("buffer %p, context %p, data %p, data_offset %u, range_count %u, ranges %p.\n",
1378 buffer
, context
, data
, data_offset
, range_count
, ranges
);
1380 dst
.buffer_object
= buffer
->buffer_object
;
1381 src
.buffer_object
= NULL
;
1383 while (range_count
--)
1385 const struct wined3d_range
*range
= &ranges
[range_count
];
1387 src
.addr
= (uint8_t *)data
+ range
->offset
- data_offset
;
1388 dst
.addr
= (void *)(uintptr_t)range
->offset
;
1389 wined3d_context_copy_bo_address(context
, &dst
, &src
, range
->size
);
1393 /* Context activation is done by the caller. */
1394 static void wined3d_buffer_gl_download_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1395 void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1397 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
1398 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(buffer
->buffer_object
);
1399 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
1400 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1401 const struct wined3d_range
*range
;
1403 TRACE("buffer %p, context %p, data %p, data_offset %u, range_count %u, ranges %p.\n",
1404 buffer
, context
, data
, data_offset
, range_count
, ranges
);
1406 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
1408 while (range_count
--)
1410 range
= &ranges
[range_count
];
1411 GL_EXTCALL(glGetBufferSubData(bo_gl
->binding
, bo_gl
->b
.buffer_offset
+ range
->offset
,
1412 range
->size
, (BYTE
*)data
+ range
->offset
- data_offset
));
1414 checkGLcall("buffer download");
1417 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops
=
1419 wined3d_buffer_gl_prepare_location
,
1420 wined3d_buffer_gl_unload_location
,
1421 wined3d_buffer_gl_upload_ranges
,
1422 wined3d_buffer_gl_download_ranges
,
1425 HRESULT
wined3d_buffer_gl_init(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_device
*device
,
1426 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1427 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1429 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1431 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1432 buffer_gl
, device
, desc
, data
, parent
, parent_ops
);
1434 /* Observations show that draw_primitive_immediate_mode() is faster on
1435 * dynamic vertex buffers than converting + draw_primitive_arrays().
1436 * (Half-Life 2 and others.) */
1437 if (!(desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
1438 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1439 else if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1440 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1441 else if (!(gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1442 && (desc
->usage
& WINED3DUSAGE_DYNAMIC
))
1443 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1445 buffer_gl
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1447 return wined3d_buffer_init(&buffer_gl
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_gl_ops
);
1450 VkBufferUsageFlags
vk_buffer_usage_from_bind_flags(uint32_t bind_flags
)
1452 VkBufferUsageFlags usage
;
1454 usage
= VK_BUFFER_USAGE_TRANSFER_SRC_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT
;
1455 if (bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
1456 usage
|= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
;
1457 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
1458 usage
|= VK_BUFFER_USAGE_INDEX_BUFFER_BIT
;
1459 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1460 usage
|= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
;
1461 if (bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
1462 usage
|= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
;
1463 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1464 usage
|= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT
;
1465 if (bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
)
1466 usage
|= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
;
1467 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
)
1468 usage
|= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
;
1469 if (bind_flags
& (WINED3D_BIND_RENDER_TARGET
| WINED3D_BIND_DEPTH_STENCIL
))
1470 FIXME("Ignoring some bind flags %#x.\n", bind_flags
);
1474 VkMemoryPropertyFlags
vk_memory_type_from_access_flags(uint32_t access
, uint32_t usage
)
1476 VkMemoryPropertyFlags memory_type
= 0;
1478 if (access
& WINED3D_RESOURCE_ACCESS_MAP_R
)
1479 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_CACHED_BIT
;
1480 else if (access
& WINED3D_RESOURCE_ACCESS_MAP_W
)
1481 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
;
1482 else if (!(usage
& WINED3DUSAGE_DYNAMIC
))
1483 memory_type
|= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
;
1487 static BOOL
wined3d_buffer_vk_create_buffer_object(struct wined3d_buffer_vk
*buffer_vk
,
1488 struct wined3d_context_vk
*context_vk
)
1490 struct wined3d_resource
*resource
= &buffer_vk
->b
.resource
;
1491 struct wined3d_bo_vk
*bo_vk
;
1493 if (!(bo_vk
= heap_alloc(sizeof(*bo_vk
))))
1496 if (!(wined3d_context_vk_create_bo(context_vk
, resource
->size
,
1497 vk_buffer_usage_from_bind_flags(resource
->bind_flags
),
1498 vk_memory_type_from_access_flags(resource
->access
, resource
->usage
), bo_vk
)))
1500 WARN("Failed to create Vulkan buffer.\n");
1505 list_init(&buffer_vk
->b
.bo_user
.entry
);
1506 list_add_head(&bo_vk
->b
.users
, &buffer_vk
->b
.bo_user
.entry
);
1507 buffer_vk
->b
.buffer_object
= &bo_vk
->b
;
1508 buffer_invalidate_bo_range(&buffer_vk
->b
, 0, 0);
1513 const VkDescriptorBufferInfo
*wined3d_buffer_vk_get_buffer_info(struct wined3d_buffer_vk
*buffer_vk
)
1515 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1517 if (buffer_vk
->b
.bo_user
.valid
)
1518 return &buffer_vk
->buffer_info
;
1520 buffer_vk
->buffer_info
.buffer
= bo
->vk_buffer
;
1521 buffer_vk
->buffer_info
.offset
= bo
->b
.buffer_offset
;
1522 buffer_vk
->buffer_info
.range
= buffer_vk
->b
.resource
.size
;
1523 buffer_vk
->b
.bo_user
.valid
= true;
1525 return &buffer_vk
->buffer_info
;
1528 static BOOL
wined3d_buffer_vk_prepare_location(struct wined3d_buffer
*buffer
,
1529 struct wined3d_context
*context
, unsigned int location
)
1533 case WINED3D_LOCATION_SYSMEM
:
1534 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1536 case WINED3D_LOCATION_BUFFER
:
1537 if (buffer
->buffer_object
)
1540 return wined3d_buffer_vk_create_buffer_object(wined3d_buffer_vk(buffer
), wined3d_context_vk(context
));
1543 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1548 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer
*buffer
,
1549 struct wined3d_context
*context
, unsigned int location
)
1551 struct wined3d_context_vk
*context_vk
= wined3d_context_vk(context
);
1552 struct wined3d_bo_vk
*bo_vk
= wined3d_bo_vk(buffer
->buffer_object
);
1554 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1558 case WINED3D_LOCATION_BUFFER
:
1559 buffer
->bo_user
.valid
= false;
1560 list_remove(&buffer
->bo_user
.entry
);
1561 wined3d_context_vk_destroy_bo(context_vk
, bo_vk
);
1563 buffer
->buffer_object
= NULL
;
1567 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1572 static void wined3d_buffer_vk_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1573 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1575 struct wined3d_context_vk
*context_vk
= wined3d_context_vk(context
);
1576 struct wined3d_resource
*resource
= &buffer
->resource
;
1577 struct wined3d_bo_address src
, dst
;
1578 const struct wined3d_range
*range
;
1579 struct wined3d_bo_vk
*dst_bo
;
1580 unsigned int i
= range_count
;
1587 dst
.buffer_object
= buffer
->buffer_object
;
1590 flags
= WINED3D_MAP_WRITE
;
1591 if (!ranges
->offset
&& ranges
->size
== resource
->size
)
1592 flags
|= WINED3D_MAP_DISCARD
;
1594 dst_bo
= wined3d_bo_vk(buffer
->buffer_object
);
1595 if (!(dst_bo
->memory_type
& VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
) || (!(flags
& WINED3D_MAP_DISCARD
)
1596 && dst_bo
->command_buffer_id
> context_vk
->completed_command_buffer_id
))
1598 src
.buffer_object
= 0;
1599 while (range_count
--)
1601 range
= &ranges
[range_count
];
1603 src
.addr
= (uint8_t *)data
+ range
->offset
- data_offset
;
1604 dst
.addr
= (void *)(uintptr_t)range
->offset
;
1605 wined3d_context_copy_bo_address(context
, &dst
, &src
, range
->size
);
1611 if (!(map_ptr
= wined3d_context_map_bo_address(context
, &dst
, resource
->size
, flags
)))
1613 FIXME("Failed to map buffer.\n");
1620 memcpy((uint8_t *)map_ptr
+ range
->offset
, (uint8_t *)data
+ range
->offset
- data_offset
, range
->size
);
1623 wined3d_context_unmap_bo_address(context
, &dst
, range_count
, ranges
);
1626 static void wined3d_buffer_vk_download_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1627 void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_range
*ranges
)
1629 FIXME("Not implemented.\n");
1632 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops
=
1634 wined3d_buffer_vk_prepare_location
,
1635 wined3d_buffer_vk_unload_location
,
1636 wined3d_buffer_vk_upload_ranges
,
1637 wined3d_buffer_vk_download_ranges
,
1640 HRESULT
wined3d_buffer_vk_init(struct wined3d_buffer_vk
*buffer_vk
, struct wined3d_device
*device
,
1641 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1642 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1644 const struct wined3d_vk_info
*vk_info
= &wined3d_adapter_vk(device
->adapter
)->vk_info
;
1646 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1647 buffer_vk
, device
, desc
, data
, parent
, parent_ops
);
1649 if ((desc
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1650 && !vk_info
->supported
[WINED3D_VK_EXT_TRANSFORM_FEEDBACK
])
1652 WARN("The Vulkan implementation does not support transform feedback.\n");
1653 return WINED3DERR_INVALIDCALL
;
1656 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
1657 buffer_vk
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1659 return wined3d_buffer_init(&buffer_vk
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_vk_ops
);
1662 void wined3d_buffer_vk_barrier(struct wined3d_buffer_vk
*buffer_vk
,
1663 struct wined3d_context_vk
*context_vk
, uint32_t bind_mask
)
1665 uint32_t src_bind_mask
= 0;
1667 TRACE("buffer_vk %p, context_vk %p, bind_mask %s.\n",
1668 buffer_vk
, context_vk
, wined3d_debug_bind_flags(bind_mask
));
1670 if (bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
)
1672 src_bind_mask
= buffer_vk
->bind_mask
& WINED3D_READ_ONLY_BIND_MASK
;
1674 src_bind_mask
= buffer_vk
->bind_mask
;
1676 buffer_vk
->bind_mask
= bind_mask
;
1678 else if ((buffer_vk
->bind_mask
& bind_mask
) != bind_mask
)
1680 src_bind_mask
= buffer_vk
->bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
;
1681 buffer_vk
->bind_mask
|= bind_mask
;
1686 const struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1687 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1688 VkBufferMemoryBarrier vk_barrier
;
1690 TRACE(" %s -> %s.\n",
1691 wined3d_debug_bind_flags(src_bind_mask
), wined3d_debug_bind_flags(bind_mask
));
1693 wined3d_context_vk_end_current_render_pass(context_vk
);
1695 vk_barrier
.sType
= VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER
;
1696 vk_barrier
.pNext
= NULL
;
1697 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(src_bind_mask
);
1698 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(bind_mask
);
1699 vk_barrier
.srcQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1700 vk_barrier
.dstQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1701 vk_barrier
.buffer
= bo
->vk_buffer
;
1702 vk_barrier
.offset
= bo
->b
.buffer_offset
;
1703 vk_barrier
.size
= buffer_vk
->b
.resource
.size
;
1704 VK_CALL(vkCmdPipelineBarrier(wined3d_context_vk_get_command_buffer(context_vk
),
1705 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask
),
1706 vk_pipeline_stage_mask_from_bind_flags(bind_mask
),
1707 0, 0, NULL
, 1, &vk_barrier
, 0, NULL
));
1711 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1712 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1713 struct wined3d_buffer
**buffer
)
1715 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1716 device
, desc
, data
, parent
, parent_ops
, buffer
);
1718 return device
->adapter
->adapter_ops
->adapter_create_buffer(device
, desc
, data
, parent
, parent_ops
, buffer
);