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"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
29 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
30 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
32 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
33 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
34 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
35 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
37 struct wined3d_buffer_ops
39 BOOL (*buffer_prepare_location
)(struct wined3d_buffer
*buffer
,
40 struct wined3d_context
*context
, unsigned int location
);
41 void (*buffer_unload_location
)(struct wined3d_buffer
*buffer
,
42 struct wined3d_context
*context
, unsigned int location
);
45 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer
*buffer
)
47 if (buffer
->resource
.pin_sysmem
)
49 TRACE("Not evicting system memory for buffer %p.\n", buffer
);
53 TRACE("Evicting system memory for buffer %p.\n", buffer
);
54 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
55 wined3d_resource_free_sysmem(&buffer
->resource
);
58 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
60 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
63 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
65 WARN("Invalid range specified, invalidating entire buffer.\n");
69 if (!wined3d_array_reserve((void **)&buffer
->maps
, &buffer
->maps_size
,
70 buffer
->modified_areas
+ 1, sizeof(*buffer
->maps
)))
72 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
76 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
77 buffer
->maps
[buffer
->modified_areas
].size
= size
;
78 ++buffer
->modified_areas
;
82 buffer
->modified_areas
= 1;
83 buffer
->maps
[0].offset
= 0;
84 buffer
->maps
[0].size
= buffer
->resource
.size
;
87 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
89 This
->modified_areas
= 0;
92 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
94 return !!buffer
->modified_areas
;
97 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
99 return buffer
->modified_areas
== 1
100 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
103 static void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, DWORD location
)
105 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
107 if (location
& WINED3D_LOCATION_BUFFER
)
108 buffer_clear_dirty_areas(buffer
);
110 buffer
->locations
|= location
;
112 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
115 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
116 unsigned int offset
, unsigned int size
)
118 TRACE("buffer %p, location %s, offset %u, size %u.\n",
119 buffer
, wined3d_debug_location(location
), offset
, size
);
121 if (location
& WINED3D_LOCATION_BUFFER
)
122 buffer_invalidate_bo_range(buffer
, offset
, size
);
124 buffer
->locations
&= ~location
;
126 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
128 if (!buffer
->locations
)
129 ERR("Buffer %p does not have any up to date location.\n", buffer
);
132 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, DWORD location
)
134 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
137 GLenum
wined3d_buffer_gl_binding_from_bind_flags(const struct wined3d_gl_info
*gl_info
, uint32_t bind_flags
)
140 return GL_PIXEL_UNPACK_BUFFER
;
142 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
143 return GL_ELEMENT_ARRAY_BUFFER
;
145 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
146 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
147 return GL_TEXTURE_BUFFER
;
149 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
150 return GL_UNIFORM_BUFFER
;
152 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
153 return GL_TRANSFORM_FEEDBACK_BUFFER
;
155 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
156 && gl_info
->supported
[ARB_DRAW_INDIRECT
])
157 return GL_DRAW_INDIRECT_BUFFER
;
159 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
160 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
162 return GL_ARRAY_BUFFER
;
165 /* Context activation is done by the caller. */
166 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
167 struct wined3d_context_gl
*context_gl
)
169 struct wined3d_resource
*resource
= &buffer_gl
->b
.resource
;
170 struct wined3d_bo_gl
*bo_gl
;
172 if (!buffer_gl
->b
.buffer_object
)
174 bo_gl
= wined3d_bo_gl(buffer_gl
->b
.buffer_object
);
176 if (context_gl
->c
.transform_feedback_active
&& (resource
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
177 && wined3d_context_is_graphics_state_dirty(&context_gl
->c
, STATE_STREAM_OUTPUT
))
179 /* It's illegal to (un)bind GL_TRANSFORM_FEEDBACK_BUFFER while transform
180 * feedback is active. Deleting a buffer implicitly unbinds it, so we
181 * need to end transform feedback here if this buffer was bound.
183 * This should only be possible if STATE_STREAM_OUTPUT is dirty; if we
184 * do a draw call before destroying this buffer then the draw call will
185 * already rebind the GL target. */
186 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl
);
187 wined3d_context_gl_end_transform_feedback(context_gl
);
190 buffer_gl
->b
.bo_user
.valid
= false;
191 list_remove(&buffer_gl
->b
.bo_user
.entry
);
192 wined3d_context_gl_destroy_bo(context_gl
, bo_gl
);
194 buffer_gl
->b
.buffer_object
= NULL
;
197 /* Context activation is done by the caller. */
198 static BOOL
wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
199 struct wined3d_context_gl
*context_gl
)
201 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(buffer_gl
->b
.resource
.device
);
202 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
203 GLenum usage
= GL_STATIC_DRAW
;
204 GLbitfield gl_storage_flags
;
205 struct wined3d_bo_gl
*bo
;
206 bool coherent
= true;
210 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
211 buffer_gl
, debug_d3dusage(buffer_gl
->b
.resource
.usage
));
213 if (!(bo
= heap_alloc(sizeof(*bo
))))
216 size
= buffer_gl
->b
.resource
.size
;
217 binding
= wined3d_buffer_gl_binding_from_bind_flags(gl_info
, buffer_gl
->b
.resource
.bind_flags
);
218 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
220 usage
= GL_STREAM_DRAW_ARB
;
223 gl_storage_flags
= wined3d_resource_gl_storage_flags(&buffer_gl
->b
.resource
);
224 if (!wined3d_device_gl_create_bo(device_gl
, context_gl
, size
, binding
, usage
, coherent
, gl_storage_flags
, bo
))
226 ERR("Failed to create OpenGL buffer object.\n");
227 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_USE_BO
;
228 buffer_clear_dirty_areas(&buffer_gl
->b
);
233 list_add_head(&bo
->b
.users
, &buffer_gl
->b
.bo_user
.entry
);
234 buffer_gl
->b
.buffer_object
= &bo
->b
;
235 buffer_invalidate_bo_range(&buffer_gl
->b
, 0, 0);
240 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
241 const enum wined3d_buffer_conversion_type conversion_type
,
242 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
244 const struct wined3d_format
*format
= attrib
->format
;
249 /* Check for some valid situations which cause us pain. One is if the buffer is used for
250 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
251 * with different strides. In the 2nd case we might have to drop conversion entirely,
252 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
256 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
257 debug_d3dformat(format
->id
));
259 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
261 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
265 *stride_this_run
= attrib
->stride
;
266 if (buffer
->stride
!= *stride_this_run
)
268 /* We rely that this happens only on the first converted attribute that is found,
269 * if at all. See above check
271 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
272 buffer
->stride
= *stride_this_run
;
273 heap_free(buffer
->conversion_map
);
274 buffer
->conversion_map
= heap_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
279 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
280 for (i
= 0; i
< format
->byte_count
; ++i
)
282 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
283 if (buffer
->conversion_map
[idx
] != conversion_type
)
285 TRACE("Byte %lu in vertex changed:\n", idx
);
286 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
288 buffer
->conversion_map
[idx
] = conversion_type
;
295 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
296 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
298 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
299 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
301 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
302 enum wined3d_format_id format
;
305 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
306 * there, on nonexistent attribs the vbo is 0.
308 if (!(si
->use_map
& (1u << attrib_idx
))
309 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
312 format
= attrib
->format
->id
;
313 /* Look for newly appeared conversion */
314 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
316 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
318 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
320 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
322 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
326 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
328 else if (This
->conversion_map
)
330 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
336 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
337 const struct wined3d_state
*state
, DWORD fixup_flags
)
339 UINT stride_this_run
= 0;
342 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
343 * Once we have our declaration there is no need to look it up again. Index buffers also never need
344 * conversion, so once the (empty) conversion structure is created don't bother checking again
346 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
348 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
353 TRACE("No fixup required.\n");
354 if(This
->conversion_map
)
356 heap_free(This
->conversion_map
);
357 This
->conversion_map
= NULL
;
365 TRACE("Finding vertex buffer conversion information\n");
366 /* Certain declaration types need some fixups before we can pass them to
367 * opengl. This means D3DCOLOR attributes with fixed function vertex
368 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
369 * GL_ARB_half_float_vertex is not supported.
371 * Note for d3d8 and d3d9:
372 * The vertex buffer FVF doesn't help with finding them, we have to use
373 * the decoded vertex declaration and pick the things that concern the
374 * current buffer. A problem with this is that this can change between
375 * draws, so we have to validate the information and reprocess the buffer
376 * if it changes, and avoid false positives for performance reasons.
377 * WineD3D doesn't even know the vertex buffer any more, it is managed
378 * by the client libraries and passed to SetStreamSource and ProcessVertices
381 * We have to distinguish between vertex shaders and fixed function to
382 * pick the way we access the strided vertex information.
384 * This code sets up a per-byte array with the size of the detected
385 * stride of the arrays in the buffer. For each byte we have a field
386 * that marks the conversion needed on this byte. For example, the
387 * following declaration with fixed function vertex processing:
395 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
396 * [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]
398 * Where in this example map P means 4 component position conversion, 0
399 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
400 * conversion (red / blue swizzle).
402 * If we're doing conversion and the stride changes we have to reconvert
403 * the whole buffer. Note that we do not mind if the semantic changes,
404 * we only care for the conversion type. So if the NORMAL is replaced
405 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
406 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
407 * conversion types depend on the semantic as well, for example a FLOAT4
408 * texcoord needs no conversion while a FLOAT4 positiont needs one
411 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
412 fixup_flags
, &stride_this_run
) || ret
;
413 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
415 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
416 fixup_flags
, &stride_this_run
) || ret
;
417 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
418 fixup_flags
, &stride_this_run
) || ret
;
419 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
420 fixup_flags
, &stride_this_run
) || ret
;
421 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
422 fixup_flags
, &stride_this_run
) || ret
;
423 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
424 fixup_flags
, &stride_this_run
) || ret
;
425 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
426 fixup_flags
, &stride_this_run
) || ret
;
427 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
428 fixup_flags
, &stride_this_run
) || ret
;
429 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
430 fixup_flags
, &stride_this_run
) || ret
;
431 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
432 fixup_flags
, &stride_this_run
) || ret
;
433 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
434 fixup_flags
, &stride_this_run
) || ret
;
435 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
436 fixup_flags
, &stride_this_run
) || ret
;
437 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
438 fixup_flags
, &stride_this_run
) || ret
;
439 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
440 fixup_flags
, &stride_this_run
) || ret
;
442 if (!stride_this_run
&& This
->conversion_map
)
446 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
447 heap_free(This
->conversion_map
);
448 This
->conversion_map
= NULL
;
452 if (ret
) TRACE("Conversion information changed\n");
457 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
459 DWORD src_color
= *dst_color
;
461 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
462 * endianness. If we want this to work on big-endian machines as well we
463 * have to consider more things.
465 * 0xff000000: Alpha mask
466 * 0x00ff0000: Blue mask
467 * 0x0000ff00: Green mask
468 * 0x000000ff: Red mask
471 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
472 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
473 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
475 return sizeof(*dst_color
);
478 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
480 /* rhw conversion like in position_float4(). */
481 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
483 float w
= 1.0f
/ p
->w
;
493 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
495 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
497 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
502 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
504 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
505 struct wined3d_bo_address src
, dst
;
508 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
510 ERR("Failed to load system memory.\n");
513 buffer
->resource
.pin_sysmem
= 1;
515 /* Now for each vertex in the buffer that needs conversion. */
516 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
518 if (!(data
= heap_alloc(buffer
->resource
.size
)))
520 ERR("Out of memory.\n");
524 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
526 start
= buffer
->maps
[range_idx
].offset
;
527 end
= start
+ buffer
->maps
[range_idx
].size
;
529 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
530 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
532 for (j
= 0; j
< buffer
->stride
;)
534 switch (buffer
->conversion_map
[j
])
541 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
544 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
547 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
554 dst
.buffer_object
= buffer
->buffer_object
;
556 src
.buffer_object
= NULL
;
558 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
);
563 BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
564 struct wined3d_context
*context
, unsigned int location
)
566 return buffer
->buffer_ops
->buffer_prepare_location(buffer
, context
, location
);
569 static void wined3d_buffer_unload_location(struct wined3d_buffer
*buffer
,
570 struct wined3d_context
*context
, unsigned int location
)
572 buffer
->buffer_ops
->buffer_unload_location(buffer
, context
, location
);
575 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
576 struct wined3d_context
*context
, DWORD location
)
578 struct wined3d_bo_address src
, dst
;
579 struct wined3d_range range
;
581 TRACE("buffer %p, context %p, location %s.\n",
582 buffer
, context
, wined3d_debug_location(location
));
584 if (buffer
->locations
& location
)
586 TRACE("Location (%#x) is already up to date.\n", location
);
590 if (!buffer
->locations
)
592 ERR("Buffer %p does not have any up to date location.\n", buffer
);
593 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
594 return wined3d_buffer_load_location(buffer
, context
, location
);
597 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
599 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
602 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
604 TRACE("Buffer previously discarded, nothing to do.\n");
605 wined3d_buffer_validate_location(buffer
, location
);
606 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
612 case WINED3D_LOCATION_SYSMEM
:
613 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
615 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
619 dst
.buffer_object
= NULL
;
620 dst
.addr
= buffer
->resource
.heap_memory
;
621 src
.buffer_object
= buffer
->buffer_object
;
624 range
.size
= buffer
->resource
.size
;
625 wined3d_context_copy_bo_address(context
, &dst
, &src
, 1, &range
);
629 case WINED3D_LOCATION_BUFFER
:
630 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
632 /* FIXME: Clear the buffer on the GPU if possible. */
633 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
635 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
638 dst
.buffer_object
= buffer
->buffer_object
;
640 src
.buffer_object
= NULL
;
641 src
.addr
= buffer
->resource
.heap_memory
;
643 if (!buffer
->conversion_map
)
644 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
);
646 buffer_conversion_upload(buffer
, context
);
650 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
654 wined3d_buffer_validate_location(buffer
, location
);
655 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
656 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
657 wined3d_buffer_evict_sysmem(buffer
);
662 /* Context activation is done by the caller. */
663 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
665 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
666 buffer
->resource
.pin_sysmem
= 1;
667 return buffer
->resource
.heap_memory
;
670 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
671 struct wined3d_bo_address
*data
)
673 unsigned int locations
= buffer
->locations
;
675 TRACE("buffer %p, context %p, data %p, locations %s.\n",
676 buffer
, context
, data
, wined3d_debug_location(locations
));
678 if (locations
& (WINED3D_LOCATION_DISCARDED
| WINED3D_LOCATION_CLEARED
))
680 locations
= ((buffer
->flags
& WINED3D_BUFFER_USE_BO
) ? WINED3D_LOCATION_BUFFER
: WINED3D_LOCATION_SYSMEM
);
681 if (!wined3d_buffer_load_location(buffer
, context
, locations
))
683 data
->buffer_object
= 0;
688 if (locations
& WINED3D_LOCATION_BUFFER
)
690 data
->buffer_object
= buffer
->buffer_object
;
692 return WINED3D_LOCATION_BUFFER
;
694 if (locations
& WINED3D_LOCATION_SYSMEM
)
696 data
->buffer_object
= 0;
697 data
->addr
= buffer
->resource
.heap_memory
;
698 return WINED3D_LOCATION_SYSMEM
;
701 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
702 data
->buffer_object
= 0;
707 static void buffer_resource_unload(struct wined3d_resource
*resource
)
709 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
711 TRACE("buffer %p.\n", buffer
);
713 if (buffer
->buffer_object
)
715 struct wined3d_context
*context
;
717 context
= context_acquire(resource
->device
, NULL
, 0);
719 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
720 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
721 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
722 buffer_clear_dirty_areas(buffer
);
724 context_release(context
);
726 heap_free(buffer
->conversion_map
);
727 buffer
->conversion_map
= NULL
;
729 buffer
->conversion_stride
= 0;
730 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
733 resource_unload(resource
);
736 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
738 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
739 buffer_resource_unload(&buffer
->resource
);
742 static void wined3d_buffer_destroy_object(void *object
)
744 struct wined3d_buffer
*buffer
= object
;
745 struct wined3d_context
*context
;
747 TRACE("buffer %p.\n", buffer
);
749 if (buffer
->buffer_object
)
751 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
752 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
753 context_release(context
);
755 heap_free(buffer
->conversion_map
);
756 heap_free(buffer
->maps
);
759 void wined3d_buffer_cleanup(struct wined3d_buffer
*buffer
)
761 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
762 resource_cleanup(&buffer
->resource
);
765 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
767 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
769 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
773 wined3d_mutex_lock();
774 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
775 buffer
->resource
.device
->adapter
->adapter_ops
->adapter_destroy_buffer(buffer
);
776 wined3d_mutex_unlock();
782 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
784 TRACE("buffer %p.\n", buffer
);
786 return buffer
->resource
.parent
;
789 /* Context activation is done by the caller. */
790 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
791 const struct wined3d_state
*state
)
793 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
794 BOOL decl_changed
= FALSE
;
796 TRACE("buffer %p.\n", buffer
);
798 if (buffer
->resource
.map_count
&& buffer
->map_ptr
)
800 FIXME("Buffer is mapped through buffer object, not loading.\n");
803 else if (buffer
->resource
.map_count
)
805 WARN("Loading mapped buffer.\n");
808 /* TODO: Make converting independent from VBOs */
809 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
811 /* Not doing any conversion */
815 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
817 ERR("Failed to prepare buffer location.\n");
821 /* Reading the declaration makes only sense if we have valid state information
822 * (i.e., if this function is called during draws). */
825 DWORD fixup_flags
= 0;
829 if (!d3d_info
->vertex_bgra
&& !d3d_info
->ffp_generic_attributes
)
830 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
831 if (!d3d_info
->xyzrhw
)
832 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
835 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
836 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
839 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
841 ++buffer
->draw_count
;
842 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
843 buffer
->decl_change_count
= 0;
844 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
845 buffer
->full_conversion_count
= 0;
849 /* If applications change the declaration over and over, reconverting all the time is a huge
850 * performance hit. So count the declaration changes and release the VBO if there are too many
851 * of them (and thus stop converting)
855 ++buffer
->decl_change_count
;
856 buffer
->draw_count
= 0;
858 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
859 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
861 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
862 wined3d_buffer_drop_bo(buffer
);
866 /* The declaration changed, reload the whole buffer. */
867 WARN("Reloading buffer because of a vertex declaration change.\n");
868 buffer_invalidate_bo_range(buffer
, 0, 0);
872 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
873 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
874 * decl changes and reset the decl change count after a specific number of them
876 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
878 ++buffer
->full_conversion_count
;
879 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
881 FIXME("Too many full buffer conversions, stopping converting.\n");
882 wined3d_buffer_drop_bo(buffer
);
888 ++buffer
->draw_count
;
889 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
890 buffer
->decl_change_count
= 0;
891 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
892 buffer
->full_conversion_count
= 0;
896 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
897 ERR("Failed to load buffer location.\n");
900 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
902 TRACE("buffer %p.\n", buffer
);
904 return &buffer
->resource
;
907 static HRESULT
buffer_resource_sub_resource_get_desc(struct wined3d_resource
*resource
,
908 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
910 if (sub_resource_idx
)
912 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
916 desc
->format
= WINED3DFMT_R8_UNORM
;
917 desc
->multisample_type
= WINED3D_MULTISAMPLE_NONE
;
918 desc
->multisample_quality
= 0;
919 desc
->usage
= resource
->usage
;
920 desc
->bind_flags
= resource
->bind_flags
;
921 desc
->access
= resource
->access
;
922 desc
->width
= resource
->size
;
925 desc
->size
= resource
->size
;
929 static void buffer_resource_sub_resource_get_map_pitch(struct wined3d_resource
*resource
,
930 unsigned int sub_resource_idx
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
932 *row_pitch
= *slice_pitch
= resource
->size
;
935 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
936 void **map_ptr
, const struct wined3d_box
*box
, uint32_t flags
)
938 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
939 unsigned int offset
, size
, dirty_offset
, dirty_size
;
940 struct wined3d_device
*device
= resource
->device
;
941 struct wined3d_context
*context
;
942 struct wined3d_bo_address addr
;
946 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
947 resource
, sub_resource_idx
, map_ptr
, debug_box(box
), flags
);
949 dirty_offset
= offset
= box
->left
;
950 dirty_size
= size
= box
->right
- box
->left
;
952 count
= ++resource
->map_count
;
954 /* DISCARD invalidates the entire buffer, regardless of the specified
955 * offset and size. Some applications also depend on the entire buffer
956 * being uploaded in that case. Two such applications are Port Royale
957 * and Darkstar One. */
958 if (flags
& WINED3D_MAP_DISCARD
)
964 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
965 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
966 || buffer
->resource
.pin_sysmem
967 || !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
969 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
971 context
= context_acquire(device
, NULL
, 0);
972 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
973 context_release(context
);
976 if (flags
& WINED3D_MAP_WRITE
)
977 wined3d_buffer_invalidate_range(buffer
, ~WINED3D_LOCATION_SYSMEM
, dirty_offset
, dirty_size
);
981 context
= context_acquire(device
, NULL
, 0);
983 if (flags
& WINED3D_MAP_DISCARD
)
985 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
987 context_release(context
);
988 return E_OUTOFMEMORY
;
990 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
994 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
997 if (flags
& WINED3D_MAP_WRITE
)
999 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1000 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1003 if ((flags
& WINED3D_MAP_DISCARD
) && resource
->heap_memory
)
1004 wined3d_buffer_evict_sysmem(buffer
);
1008 addr
.buffer_object
= buffer
->buffer_object
;
1010 buffer
->map_ptr
= wined3d_context_map_bo_address(context
, &addr
, resource
->size
, flags
);
1012 /* We are accessing buffer->resource.client from the CS thread,
1013 * but it's safe because the client thread will wait for the
1014 * map to return, thus completely serializing this call with
1015 * other client code. */
1016 if (context
->d3d_info
->persistent_map
)
1017 buffer
->resource
.client
.addr
= addr
;
1019 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1021 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1023 wined3d_context_unmap_bo_address(context
, &addr
, 0, NULL
);
1024 buffer
->map_ptr
= NULL
;
1026 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1028 /* The extra copy is more expensive than not using VBOs
1029 * at all on the NVIDIA Linux driver, which is the
1030 * only driver that returns unaligned pointers. */
1031 TRACE("Dynamic buffer, dropping VBO.\n");
1032 wined3d_buffer_drop_bo(buffer
);
1036 TRACE("Falling back to doublebuffered operation.\n");
1037 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1038 buffer
->resource
.pin_sysmem
= 1;
1040 TRACE("New pointer is %p.\n", resource
->heap_memory
);
1044 context_release(context
);
1047 base
= buffer
->map_ptr
? buffer
->map_ptr
: resource
->heap_memory
;
1048 *map_ptr
= base
+ offset
;
1050 TRACE("Returning memory at %p (base %p, offset %u).\n", *map_ptr
, base
, offset
);
1055 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1057 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1058 unsigned int range_count
= buffer
->modified_areas
;
1059 struct wined3d_device
*device
= resource
->device
;
1060 struct wined3d_context
*context
;
1061 struct wined3d_bo_address addr
;
1063 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1065 if (sub_resource_idx
)
1067 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1068 return E_INVALIDARG
;
1071 if (!resource
->map_count
)
1073 WARN("Unmap called without a previous map call.\n");
1077 if (--resource
->map_count
)
1079 /* Delay loading the buffer until everything is unmapped. */
1080 TRACE("Ignoring unmap.\n");
1084 if (!buffer
->map_ptr
)
1087 context
= context_acquire(device
, NULL
, 0);
1089 addr
.buffer_object
= buffer
->buffer_object
;
1091 wined3d_context_unmap_bo_address(context
, &addr
, range_count
, buffer
->maps
);
1093 context_release(context
);
1095 buffer_clear_dirty_areas(buffer
);
1096 buffer
->map_ptr
= NULL
;
1101 static void wined3d_buffer_set_bo(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
, struct wined3d_bo
*bo
)
1103 struct wined3d_bo
*prev_bo
= buffer
->buffer_object
;
1105 TRACE("buffer %p, context %p, bo %p.\n", buffer
, context
, bo
);
1109 struct wined3d_bo_user
*bo_user
;
1111 LIST_FOR_EACH_ENTRY(bo_user
, &prev_bo
->users
, struct wined3d_bo_user
, entry
)
1112 bo_user
->valid
= false;
1113 assert(list_empty(&bo
->users
));
1114 list_move_head(&bo
->users
, &prev_bo
->users
);
1116 wined3d_context_destroy_bo(context
, prev_bo
);
1121 list_add_head(&bo
->users
, &buffer
->bo_user
.entry
);
1124 buffer
->buffer_object
= bo
;
1127 void wined3d_buffer_copy_bo_address(struct wined3d_buffer
*dst_buffer
, struct wined3d_context
*context
,
1128 unsigned int dst_offset
, const struct wined3d_const_bo_address
*src_addr
, unsigned int size
)
1130 struct wined3d_bo_address dst_addr
;
1131 struct wined3d_range range
;
1134 dst_location
= wined3d_buffer_get_memory(dst_buffer
, context
, &dst_addr
);
1135 dst_addr
.addr
+= dst_offset
;
1139 wined3d_context_copy_bo_address(context
, &dst_addr
, (const struct wined3d_bo_address
*)src_addr
, 1, &range
);
1140 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1143 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1144 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1146 struct wined3d_context
*context
;
1147 struct wined3d_bo_address src
;
1149 TRACE("dst_buffer %p, dst_offset %u, src_buffer %p, src_offset %u, size %u.\n",
1150 dst_buffer
, dst_offset
, src_buffer
, src_offset
, size
);
1152 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1154 wined3d_buffer_get_memory(src_buffer
, context
, &src
);
1155 src
.addr
+= src_offset
;
1157 wined3d_buffer_copy_bo_address(dst_buffer
, context
, dst_offset
, wined3d_const_bo_address(&src
), size
);
1159 context_release(context
);
1162 void wined3d_buffer_update_sub_resource(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1163 const struct upload_bo
*upload_bo
, unsigned int offset
, unsigned int size
)
1165 if (upload_bo
->flags
& UPLOAD_BO_RENAME_ON_UNMAP
)
1167 wined3d_buffer_set_bo(buffer
, context
, upload_bo
->addr
.buffer_object
);
1168 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1169 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1172 if (upload_bo
->addr
.buffer_object
&& upload_bo
->addr
.buffer_object
== buffer
->buffer_object
)
1174 struct wined3d_range range
;
1176 /* We need to flush changes, which is implicitly done by
1177 * wined3d_context_unmap_bo_address() even if we aren't actually going
1180 * We would also like to free up virtual address space used by this BO
1181 * if it's at a premium—note that this BO was allocated for an
1182 * accelerated map. Hence we unmap the BO instead of merely flushing it;
1183 * if we don't care about unmapping BOs then
1184 * wined3d_context_unmap_bo_address() will flush and return.
1186 range
.offset
= offset
;
1188 wined3d_context_unmap_bo_address(context
, (const struct wined3d_bo_address
*)&upload_bo
->addr
, 1, &range
);
1192 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &upload_bo
->addr
, size
);
1196 static void wined3d_buffer_init_data(struct wined3d_buffer
*buffer
,
1197 struct wined3d_device
*device
, const struct wined3d_sub_resource_data
*data
)
1199 struct wined3d_resource
*resource
= &buffer
->resource
;
1200 struct wined3d_box box
;
1202 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
1204 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1205 wined3d_device_context_emit_update_sub_resource(&device
->cs
->c
, resource
,
1206 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1210 memcpy(buffer
->resource
.heap_memory
, data
->data
, resource
->size
);
1211 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
1212 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_SYSMEM
);
1216 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1218 return wined3d_buffer_incref(buffer_from_resource(resource
));
1221 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1223 return wined3d_buffer_decref(buffer_from_resource(resource
));
1226 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1228 struct wined3d_context
*context
;
1230 context
= context_acquire(resource
->device
, NULL
, 0);
1231 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1232 context_release(context
);
1235 static const struct wined3d_resource_ops buffer_resource_ops
=
1237 buffer_resource_incref
,
1238 buffer_resource_decref
,
1239 buffer_resource_preload
,
1240 buffer_resource_unload
,
1241 buffer_resource_sub_resource_get_desc
,
1242 buffer_resource_sub_resource_get_map_pitch
,
1243 buffer_resource_sub_resource_map
,
1244 buffer_resource_sub_resource_unmap
,
1247 static HRESULT
wined3d_buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1248 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1249 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_buffer_ops
*buffer_ops
)
1251 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, WINED3DFMT_R8_UNORM
, desc
->bind_flags
);
1252 struct wined3d_resource
*resource
= &buffer
->resource
;
1253 unsigned int access
;
1256 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1257 "access %s, data %p, parent %p, parent_ops %p.\n",
1258 buffer
, device
, desc
->byte_width
, debug_d3dusage(desc
->usage
), wined3d_debug_bind_flags(desc
->bind_flags
),
1259 wined3d_debug_resource_access(desc
->access
), data
, parent
, parent_ops
);
1261 if (!desc
->byte_width
)
1263 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1264 return E_INVALIDARG
;
1267 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& desc
->byte_width
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1269 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc
->byte_width
);
1270 return E_INVALIDARG
;
1273 if (data
&& !data
->data
)
1275 WARN("Invalid sub-resource data specified.\n");
1276 return E_INVALIDARG
;
1279 access
= desc
->access
;
1280 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& wined3d_settings
.cb_access_map_w
)
1281 access
|= WINED3D_RESOURCE_ACCESS_MAP_W
;
1283 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1284 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->bind_flags
, access
,
1285 desc
->byte_width
, 1, 1, desc
->byte_width
, parent
, parent_ops
, &buffer_resource_ops
)))
1287 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1290 buffer
->buffer_ops
= buffer_ops
;
1291 buffer
->structure_byte_stride
= desc
->structure_byte_stride
;
1292 buffer
->locations
= WINED3D_LOCATION_CLEARED
;
1294 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1295 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
, buffer
->resource
.heap_memory
);
1297 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1298 || wined3d_resource_access_is_managed(access
))
1300 /* SWvp and managed buffers always return the same pointer in buffer
1301 * maps and retain data in DISCARD maps. Keep a system memory copy of
1302 * the buffer to provide the same behavior to the application. */
1303 TRACE("Pinning system memory.\n");
1304 buffer
->resource
.pin_sysmem
= 1;
1305 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1308 if (buffer
->locations
& WINED3D_LOCATION_SYSMEM
|| !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1310 if (!wined3d_resource_prepare_sysmem(&buffer
->resource
))
1311 return E_OUTOFMEMORY
;
1314 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1316 ERR("Out of memory.\n");
1317 buffer_resource_unload(resource
);
1318 resource_cleanup(resource
);
1319 wined3d_resource_wait_idle(resource
);
1320 return E_OUTOFMEMORY
;
1322 buffer
->maps_size
= 1;
1324 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
1325 buffer
->resource
.client
.addr
.buffer_object
= CLIENT_BO_DISCARDED
;
1328 wined3d_buffer_init_data(buffer
, device
, data
);
1333 static BOOL
wined3d_buffer_no3d_prepare_location(struct wined3d_buffer
*buffer
,
1334 struct wined3d_context
*context
, unsigned int location
)
1336 if (location
== WINED3D_LOCATION_SYSMEM
)
1337 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1339 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1344 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer
*buffer
,
1345 struct wined3d_context
*context
, unsigned int location
)
1347 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1350 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops
=
1352 wined3d_buffer_no3d_prepare_location
,
1353 wined3d_buffer_no3d_unload_location
,
1356 HRESULT
wined3d_buffer_no3d_init(struct wined3d_buffer
*buffer_no3d
, struct wined3d_device
*device
,
1357 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1358 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1360 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1361 buffer_no3d
, device
, desc
, data
, parent
, parent_ops
);
1363 return wined3d_buffer_init(buffer_no3d
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_no3d_ops
);
1366 static BOOL
wined3d_buffer_gl_prepare_location(struct wined3d_buffer
*buffer
,
1367 struct wined3d_context
*context
, unsigned int location
)
1369 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
1370 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
1374 case WINED3D_LOCATION_SYSMEM
:
1375 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1377 case WINED3D_LOCATION_BUFFER
:
1378 if (buffer
->buffer_object
)
1381 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1383 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
1386 return wined3d_buffer_gl_create_buffer_object(buffer_gl
, context_gl
);
1389 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1394 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer
*buffer
,
1395 struct wined3d_context
*context
, unsigned int location
)
1397 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1401 case WINED3D_LOCATION_BUFFER
:
1402 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer
), wined3d_context_gl(context
));
1406 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1411 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops
=
1413 wined3d_buffer_gl_prepare_location
,
1414 wined3d_buffer_gl_unload_location
,
1417 HRESULT
wined3d_buffer_gl_init(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_device
*device
,
1418 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1419 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1421 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1423 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1424 buffer_gl
, device
, desc
, data
, parent
, parent_ops
);
1426 /* Observations show that draw_primitive_immediate_mode() is faster on
1427 * dynamic vertex buffers than converting + draw_primitive_arrays().
1428 * (Half-Life 2 and others.) */
1429 if (!(desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
1430 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1431 else if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1432 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1433 else if (!(gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1434 && (desc
->usage
& WINED3DUSAGE_DYNAMIC
))
1435 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1437 buffer_gl
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1439 return wined3d_buffer_init(&buffer_gl
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_gl_ops
);
1442 VkBufferUsageFlags
vk_buffer_usage_from_bind_flags(uint32_t bind_flags
)
1444 VkBufferUsageFlags usage
;
1446 usage
= VK_BUFFER_USAGE_TRANSFER_SRC_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT
;
1447 if (bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
1448 usage
|= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
;
1449 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
1450 usage
|= VK_BUFFER_USAGE_INDEX_BUFFER_BIT
;
1451 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1452 usage
|= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
;
1453 if (bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
1454 usage
|= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
;
1455 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1456 usage
|= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT
;
1457 if (bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
)
1458 usage
|= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
;
1459 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
)
1460 usage
|= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
;
1461 if (bind_flags
& (WINED3D_BIND_RENDER_TARGET
| WINED3D_BIND_DEPTH_STENCIL
))
1462 FIXME("Ignoring some bind flags %#x.\n", bind_flags
);
1466 VkMemoryPropertyFlags
vk_memory_type_from_access_flags(uint32_t access
, uint32_t usage
)
1468 VkMemoryPropertyFlags memory_type
= 0;
1470 if (access
& WINED3D_RESOURCE_ACCESS_MAP_R
)
1471 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_CACHED_BIT
;
1472 else if (access
& WINED3D_RESOURCE_ACCESS_MAP_W
)
1473 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
;
1474 else if (!(usage
& WINED3DUSAGE_DYNAMIC
))
1475 memory_type
|= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
;
1479 static BOOL
wined3d_buffer_vk_create_buffer_object(struct wined3d_buffer_vk
*buffer_vk
,
1480 struct wined3d_context_vk
*context_vk
)
1482 struct wined3d_resource
*resource
= &buffer_vk
->b
.resource
;
1483 struct wined3d_bo_vk
*bo_vk
;
1485 if (!(bo_vk
= heap_alloc(sizeof(*bo_vk
))))
1488 if (!(wined3d_context_vk_create_bo(context_vk
, resource
->size
,
1489 vk_buffer_usage_from_bind_flags(resource
->bind_flags
),
1490 vk_memory_type_from_access_flags(resource
->access
, resource
->usage
), bo_vk
)))
1492 WARN("Failed to create Vulkan buffer.\n");
1497 list_init(&buffer_vk
->b
.bo_user
.entry
);
1498 list_add_head(&bo_vk
->b
.users
, &buffer_vk
->b
.bo_user
.entry
);
1499 buffer_vk
->b
.buffer_object
= &bo_vk
->b
;
1500 buffer_invalidate_bo_range(&buffer_vk
->b
, 0, 0);
1505 const VkDescriptorBufferInfo
*wined3d_buffer_vk_get_buffer_info(struct wined3d_buffer_vk
*buffer_vk
)
1507 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1509 if (buffer_vk
->b
.bo_user
.valid
)
1510 return &buffer_vk
->buffer_info
;
1512 buffer_vk
->buffer_info
.buffer
= bo
->vk_buffer
;
1513 buffer_vk
->buffer_info
.offset
= bo
->b
.buffer_offset
;
1514 buffer_vk
->buffer_info
.range
= buffer_vk
->b
.resource
.size
;
1515 buffer_vk
->b
.bo_user
.valid
= true;
1517 return &buffer_vk
->buffer_info
;
1520 static BOOL
wined3d_buffer_vk_prepare_location(struct wined3d_buffer
*buffer
,
1521 struct wined3d_context
*context
, unsigned int location
)
1525 case WINED3D_LOCATION_SYSMEM
:
1526 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1528 case WINED3D_LOCATION_BUFFER
:
1529 if (buffer
->buffer_object
)
1532 return wined3d_buffer_vk_create_buffer_object(wined3d_buffer_vk(buffer
), wined3d_context_vk(context
));
1535 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1540 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer
*buffer
,
1541 struct wined3d_context
*context
, unsigned int location
)
1543 struct wined3d_context_vk
*context_vk
= wined3d_context_vk(context
);
1544 struct wined3d_bo_vk
*bo_vk
= wined3d_bo_vk(buffer
->buffer_object
);
1546 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1550 case WINED3D_LOCATION_BUFFER
:
1551 buffer
->bo_user
.valid
= false;
1552 list_remove(&buffer
->bo_user
.entry
);
1553 wined3d_context_vk_destroy_bo(context_vk
, bo_vk
);
1555 buffer
->buffer_object
= NULL
;
1559 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1564 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops
=
1566 wined3d_buffer_vk_prepare_location
,
1567 wined3d_buffer_vk_unload_location
,
1570 HRESULT
wined3d_buffer_vk_init(struct wined3d_buffer_vk
*buffer_vk
, struct wined3d_device
*device
,
1571 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1572 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1574 const struct wined3d_vk_info
*vk_info
= &wined3d_adapter_vk(device
->adapter
)->vk_info
;
1576 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1577 buffer_vk
, device
, desc
, data
, parent
, parent_ops
);
1579 if ((desc
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1580 && !vk_info
->supported
[WINED3D_VK_EXT_TRANSFORM_FEEDBACK
])
1582 WARN("The Vulkan implementation does not support transform feedback.\n");
1583 return WINED3DERR_INVALIDCALL
;
1586 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
1587 buffer_vk
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1589 return wined3d_buffer_init(&buffer_vk
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_vk_ops
);
1592 void wined3d_buffer_vk_barrier(struct wined3d_buffer_vk
*buffer_vk
,
1593 struct wined3d_context_vk
*context_vk
, uint32_t bind_mask
)
1595 uint32_t src_bind_mask
= 0;
1597 TRACE("buffer_vk %p, context_vk %p, bind_mask %s.\n",
1598 buffer_vk
, context_vk
, wined3d_debug_bind_flags(bind_mask
));
1600 if (bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
)
1602 src_bind_mask
= buffer_vk
->bind_mask
& WINED3D_READ_ONLY_BIND_MASK
;
1604 src_bind_mask
= buffer_vk
->bind_mask
;
1606 buffer_vk
->bind_mask
= bind_mask
;
1608 else if ((buffer_vk
->bind_mask
& bind_mask
) != bind_mask
)
1610 src_bind_mask
= buffer_vk
->bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
;
1611 buffer_vk
->bind_mask
|= bind_mask
;
1616 const struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1617 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1618 VkBufferMemoryBarrier vk_barrier
;
1620 TRACE(" %s -> %s.\n",
1621 wined3d_debug_bind_flags(src_bind_mask
), wined3d_debug_bind_flags(bind_mask
));
1623 wined3d_context_vk_end_current_render_pass(context_vk
);
1625 vk_barrier
.sType
= VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER
;
1626 vk_barrier
.pNext
= NULL
;
1627 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(src_bind_mask
);
1628 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(bind_mask
);
1629 vk_barrier
.srcQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1630 vk_barrier
.dstQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1631 vk_barrier
.buffer
= bo
->vk_buffer
;
1632 vk_barrier
.offset
= bo
->b
.buffer_offset
;
1633 vk_barrier
.size
= buffer_vk
->b
.resource
.size
;
1634 VK_CALL(vkCmdPipelineBarrier(wined3d_context_vk_get_command_buffer(context_vk
),
1635 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask
),
1636 vk_pipeline_stage_mask_from_bind_flags(bind_mask
),
1637 0, 0, NULL
, 1, &vk_barrier
, 0, NULL
));
1641 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1642 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1643 struct wined3d_buffer
**buffer
)
1645 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1646 device
, desc
, data
, parent
, parent_ops
, buffer
);
1648 return device
->adapter
->adapter_ops
->adapter_create_buffer(device
, desc
, data
, parent
, parent_ops
, buffer
);