2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2007-2011, 2013-2014 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
26 #include "wined3d_vk.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
30 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
31 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
33 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
34 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
35 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
36 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
38 #define SB_MIN_SIZE (512 * 1024) /* Minimum size of an allocated streaming buffer. */
40 struct wined3d_buffer_ops
42 BOOL (*buffer_prepare_location
)(struct wined3d_buffer
*buffer
,
43 struct wined3d_context
*context
, unsigned int location
);
44 void (*buffer_unload_location
)(struct wined3d_buffer
*buffer
,
45 struct wined3d_context
*context
, unsigned int location
);
48 static void wined3d_buffer_evict_sysmem(struct wined3d_buffer
*buffer
)
50 if (buffer
->resource
.pin_sysmem
)
52 TRACE("Not evicting system memory for buffer %p.\n", buffer
);
56 TRACE("Evicting system memory for buffer %p.\n", buffer
);
57 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
58 wined3d_resource_free_sysmem(&buffer
->resource
);
61 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
63 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
66 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
68 WARN("Invalid range specified, invalidating entire buffer.\n");
72 if (!wined3d_array_reserve((void **)&buffer
->maps
, &buffer
->maps_size
,
73 buffer
->modified_areas
+ 1, sizeof(*buffer
->maps
)))
75 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
79 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
80 buffer
->maps
[buffer
->modified_areas
].size
= size
;
81 ++buffer
->modified_areas
;
85 buffer
->modified_areas
= 1;
86 buffer
->maps
[0].offset
= 0;
87 buffer
->maps
[0].size
= buffer
->resource
.size
;
90 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
92 This
->modified_areas
= 0;
95 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
97 return !!buffer
->modified_areas
;
100 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
102 return buffer
->modified_areas
== 1
103 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
106 void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, uint32_t location
)
108 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
110 if (location
& WINED3D_LOCATION_BUFFER
)
111 buffer_clear_dirty_areas(buffer
);
113 buffer
->locations
|= location
;
115 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
118 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
119 unsigned int offset
, unsigned int size
)
121 TRACE("buffer %p, location %s, offset %u, size %u.\n",
122 buffer
, wined3d_debug_location(location
), offset
, size
);
124 if (location
& WINED3D_LOCATION_BUFFER
)
125 buffer_invalidate_bo_range(buffer
, offset
, size
);
127 buffer
->locations
&= ~location
;
129 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
131 if (!buffer
->locations
)
132 ERR("Buffer %p does not have any up to date location.\n", buffer
);
135 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, uint32_t location
)
137 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
140 GLenum
wined3d_buffer_gl_binding_from_bind_flags(const struct wined3d_gl_info
*gl_info
, uint32_t bind_flags
)
143 return GL_PIXEL_UNPACK_BUFFER
;
145 /* We must always return GL_ELEMENT_ARRAY_BUFFER here;
146 * wined3d_device_gl_create_bo() checks the GL binding to see whether we
147 * can suballocate, and we cannot suballocate if this BO might be used for
148 * an index buffer. */
149 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
150 return GL_ELEMENT_ARRAY_BUFFER
;
152 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
153 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
154 return GL_TEXTURE_BUFFER
;
156 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
157 return GL_UNIFORM_BUFFER
;
159 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
160 return GL_TRANSFORM_FEEDBACK_BUFFER
;
162 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
163 && gl_info
->supported
[ARB_DRAW_INDIRECT
])
164 return GL_DRAW_INDIRECT_BUFFER
;
166 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
167 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
169 return GL_ARRAY_BUFFER
;
172 /* Context activation is done by the caller. */
173 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
174 struct wined3d_context_gl
*context_gl
)
176 struct wined3d_resource
*resource
= &buffer_gl
->b
.resource
;
177 struct wined3d_bo_gl
*bo_gl
;
179 if (!buffer_gl
->b
.buffer_object
)
181 bo_gl
= wined3d_bo_gl(buffer_gl
->b
.buffer_object
);
183 if (context_gl
->c
.transform_feedback_active
&& (resource
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
184 && wined3d_context_is_graphics_state_dirty(&context_gl
->c
, STATE_STREAM_OUTPUT
))
186 /* It's illegal to (un)bind GL_TRANSFORM_FEEDBACK_BUFFER while transform
187 * feedback is active. Deleting a buffer implicitly unbinds it, so we
188 * need to end transform feedback here if this buffer was bound.
190 * This should only be possible if STATE_STREAM_OUTPUT is dirty; if we
191 * do a draw call before destroying this buffer then the draw call will
192 * already rebind the GL target. */
193 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl
);
194 wined3d_context_gl_end_transform_feedback(context_gl
);
197 if (buffer_gl
->b
.bo_user
.valid
)
199 buffer_gl
->b
.bo_user
.valid
= false;
200 list_remove(&buffer_gl
->b
.bo_user
.entry
);
202 if (!--bo_gl
->b
.refcount
)
204 wined3d_context_gl_destroy_bo(context_gl
, bo_gl
);
207 buffer_gl
->b
.buffer_object
= NULL
;
210 /* Context activation is done by the caller. */
211 static BOOL
wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
212 struct wined3d_context_gl
*context_gl
)
214 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(buffer_gl
->b
.resource
.device
);
215 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
216 GLenum usage
= GL_STATIC_DRAW
;
217 GLbitfield gl_storage_flags
;
218 struct wined3d_bo_gl
*bo
;
219 bool coherent
= true;
223 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
224 buffer_gl
, debug_d3dusage(buffer_gl
->b
.resource
.usage
));
226 if (!(bo
= heap_alloc(sizeof(*bo
))))
229 size
= buffer_gl
->b
.resource
.size
;
230 binding
= wined3d_buffer_gl_binding_from_bind_flags(gl_info
, buffer_gl
->b
.resource
.bind_flags
);
231 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
233 usage
= GL_STREAM_DRAW_ARB
;
236 gl_storage_flags
= wined3d_resource_gl_storage_flags(&buffer_gl
->b
.resource
);
237 if (!wined3d_device_gl_create_bo(device_gl
, context_gl
, size
, binding
, usage
, coherent
, gl_storage_flags
, bo
))
239 ERR("Failed to create OpenGL buffer object.\n");
240 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_USE_BO
;
241 buffer_clear_dirty_areas(&buffer_gl
->b
);
246 buffer_gl
->b
.buffer_object
= &bo
->b
;
247 buffer_invalidate_bo_range(&buffer_gl
->b
, 0, 0);
252 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
253 const enum wined3d_buffer_conversion_type conversion_type
,
254 const struct wined3d_stream_info_element
*attrib
, UINT
*stride_this_run
)
256 const struct wined3d_format
*format
= attrib
->format
;
261 /* Check for some valid situations which cause us pain. One is if the buffer is used for
262 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
263 * with different strides. In the 2nd case we might have to drop conversion entirely,
264 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
268 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
269 debug_d3dformat(format
->id
));
271 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
273 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
277 *stride_this_run
= attrib
->stride
;
278 if (buffer
->stride
!= *stride_this_run
)
280 /* We rely that this happens only on the first converted attribute that is found,
281 * if at all. See above check
283 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
284 buffer
->stride
= *stride_this_run
;
285 heap_free(buffer
->conversion_map
);
286 buffer
->conversion_map
= heap_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
291 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
292 for (i
= 0; i
< format
->byte_count
; ++i
)
294 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
295 if (buffer
->conversion_map
[idx
] != conversion_type
)
297 TRACE("Byte %Iu in vertex changed:\n", idx
);
298 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
300 buffer
->conversion_map
[idx
] = conversion_type
;
307 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
308 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
310 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
311 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, UINT
*stride_this_run
)
313 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
314 enum wined3d_format_id format
;
317 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
318 * there, on nonexistent attribs the vbo is 0.
320 if (!(si
->use_map
& (1u << attrib_idx
))
321 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
324 format
= attrib
->format
->id
;
325 /* Look for newly appeared conversion */
326 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
328 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
330 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
332 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
334 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
338 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
340 else if (This
->conversion_map
)
342 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
348 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
349 const struct wined3d_state
*state
, DWORD fixup_flags
)
351 UINT stride_this_run
= 0;
354 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
355 * Once we have our declaration there is no need to look it up again. Index buffers also never need
356 * conversion, so once the (empty) conversion structure is created don't bother checking again
358 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
360 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
365 TRACE("No fixup required.\n");
366 if(This
->conversion_map
)
368 heap_free(This
->conversion_map
);
369 This
->conversion_map
= NULL
;
377 TRACE("Finding vertex buffer conversion information\n");
378 /* Certain declaration types need some fixups before we can pass them to
379 * opengl. This means D3DCOLOR attributes with fixed function vertex
380 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
381 * GL_ARB_half_float_vertex is not supported.
383 * Note for d3d8 and d3d9:
384 * The vertex buffer FVF doesn't help with finding them, we have to use
385 * the decoded vertex declaration and pick the things that concern the
386 * current buffer. A problem with this is that this can change between
387 * draws, so we have to validate the information and reprocess the buffer
388 * if it changes, and avoid false positives for performance reasons.
389 * WineD3D doesn't even know the vertex buffer any more, it is managed
390 * by the client libraries and passed to SetStreamSource and ProcessVertices
393 * We have to distinguish between vertex shaders and fixed function to
394 * pick the way we access the strided vertex information.
396 * This code sets up a per-byte array with the size of the detected
397 * stride of the arrays in the buffer. For each byte we have a field
398 * that marks the conversion needed on this byte. For example, the
399 * following declaration with fixed function vertex processing:
407 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
408 * [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]
410 * Where in this example map P means 4 component position conversion, 0
411 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
412 * conversion (red / blue swizzle).
414 * If we're doing conversion and the stride changes we have to reconvert
415 * the whole buffer. Note that we do not mind if the semantic changes,
416 * we only care for the conversion type. So if the NORMAL is replaced
417 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
418 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
419 * conversion types depend on the semantic as well, for example a FLOAT4
420 * texcoord needs no conversion while a FLOAT4 positiont needs one
423 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
424 fixup_flags
, &stride_this_run
) || ret
;
425 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
427 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
428 fixup_flags
, &stride_this_run
) || ret
;
429 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
430 fixup_flags
, &stride_this_run
) || ret
;
431 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
432 fixup_flags
, &stride_this_run
) || ret
;
433 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
434 fixup_flags
, &stride_this_run
) || ret
;
435 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
436 fixup_flags
, &stride_this_run
) || ret
;
437 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
438 fixup_flags
, &stride_this_run
) || ret
;
439 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
440 fixup_flags
, &stride_this_run
) || ret
;
441 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
442 fixup_flags
, &stride_this_run
) || ret
;
443 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
444 fixup_flags
, &stride_this_run
) || ret
;
445 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
446 fixup_flags
, &stride_this_run
) || ret
;
447 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
448 fixup_flags
, &stride_this_run
) || ret
;
449 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
450 fixup_flags
, &stride_this_run
) || ret
;
451 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
452 fixup_flags
, &stride_this_run
) || ret
;
454 if (!stride_this_run
&& This
->conversion_map
)
458 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
459 heap_free(This
->conversion_map
);
460 This
->conversion_map
= NULL
;
464 if (ret
) TRACE("Conversion information changed\n");
469 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
471 DWORD src_color
= *dst_color
;
473 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
474 * endianness. If we want this to work on big-endian machines as well we
475 * have to consider more things.
477 * 0xff000000: Alpha mask
478 * 0x00ff0000: Blue mask
479 * 0x0000ff00: Green mask
480 * 0x000000ff: Red mask
483 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
484 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
485 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
487 return sizeof(*dst_color
);
490 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
492 /* rhw conversion like in position_float4(). */
493 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
495 float w
= 1.0f
/ p
->w
;
505 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
507 unsigned int refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
509 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
514 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
516 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
517 struct wined3d_bo_address src
, dst
;
520 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
522 ERR("Failed to load system memory.\n");
525 buffer
->resource
.pin_sysmem
= 1;
527 /* Now for each vertex in the buffer that needs conversion. */
528 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
530 if (!(data
= heap_alloc(buffer
->resource
.size
)))
532 ERR("Out of memory.\n");
536 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
538 start
= buffer
->maps
[range_idx
].offset
;
539 end
= start
+ buffer
->maps
[range_idx
].size
;
541 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
542 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
544 for (j
= 0; j
< buffer
->stride
;)
546 switch (buffer
->conversion_map
[j
])
553 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
556 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
559 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
566 dst
.buffer_object
= buffer
->buffer_object
;
568 src
.buffer_object
= NULL
;
570 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
, WINED3D_MAP_WRITE
);
575 BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
576 struct wined3d_context
*context
, unsigned int location
)
578 return buffer
->buffer_ops
->buffer_prepare_location(buffer
, context
, location
);
581 static void wined3d_buffer_unload_location(struct wined3d_buffer
*buffer
,
582 struct wined3d_context
*context
, unsigned int location
)
584 buffer
->buffer_ops
->buffer_unload_location(buffer
, context
, location
);
587 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
588 struct wined3d_context
*context
, uint32_t location
)
590 struct wined3d_bo_address src
, dst
;
591 struct wined3d_range range
;
593 TRACE("buffer %p, context %p, location %s.\n",
594 buffer
, context
, wined3d_debug_location(location
));
596 if (buffer
->locations
& location
)
598 TRACE("Location (%#x) is already up to date.\n", location
);
602 if (!buffer
->locations
)
604 ERR("Buffer %p does not have any up to date location.\n", buffer
);
605 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
606 return wined3d_buffer_load_location(buffer
, context
, location
);
609 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
611 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
614 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
616 TRACE("Buffer previously discarded, nothing to do.\n");
617 wined3d_buffer_validate_location(buffer
, location
);
618 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
624 case WINED3D_LOCATION_SYSMEM
:
625 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
627 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
631 dst
.buffer_object
= NULL
;
632 dst
.addr
= buffer
->resource
.heap_memory
;
633 src
.buffer_object
= buffer
->buffer_object
;
636 range
.size
= buffer
->resource
.size
;
637 wined3d_context_copy_bo_address(context
, &dst
, &src
, 1, &range
, WINED3D_MAP_WRITE
);
641 case WINED3D_LOCATION_BUFFER
:
642 if (buffer
->locations
& WINED3D_LOCATION_CLEARED
)
644 /* FIXME: Clear the buffer on the GPU if possible. */
645 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
647 memset(buffer
->resource
.heap_memory
, 0, buffer
->resource
.size
);
650 dst
.buffer_object
= buffer
->buffer_object
;
652 src
.buffer_object
= NULL
;
653 src
.addr
= buffer
->resource
.heap_memory
;
655 if (!buffer
->conversion_map
)
657 uint32_t map_flags
= WINED3D_MAP_WRITE
;
659 if (buffer
->modified_areas
== 1 && !buffer
->maps
[0].offset
660 && buffer
->maps
[0].size
== buffer
->resource
.size
)
661 map_flags
|= WINED3D_MAP_DISCARD
;
663 wined3d_context_copy_bo_address(context
, &dst
, &src
, buffer
->modified_areas
, buffer
->maps
, map_flags
);
667 buffer_conversion_upload(buffer
, context
);
672 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
676 wined3d_buffer_validate_location(buffer
, location
);
677 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
678 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
679 wined3d_buffer_evict_sysmem(buffer
);
684 /* Context activation is done by the caller. */
685 void *wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
687 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
688 buffer
->resource
.pin_sysmem
= 1;
689 return buffer
->resource
.heap_memory
;
692 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
693 struct wined3d_bo_address
*data
)
695 unsigned int locations
= buffer
->locations
;
697 TRACE("buffer %p, context %p, data %p, locations %s.\n",
698 buffer
, context
, data
, wined3d_debug_location(locations
));
700 if (locations
& (WINED3D_LOCATION_DISCARDED
| WINED3D_LOCATION_CLEARED
))
702 locations
= ((buffer
->flags
& WINED3D_BUFFER_USE_BO
) ? WINED3D_LOCATION_BUFFER
: WINED3D_LOCATION_SYSMEM
);
703 if (!wined3d_buffer_load_location(buffer
, context
, locations
))
705 data
->buffer_object
= 0;
710 if (locations
& WINED3D_LOCATION_BUFFER
)
712 data
->buffer_object
= buffer
->buffer_object
;
714 return WINED3D_LOCATION_BUFFER
;
716 if (locations
& WINED3D_LOCATION_SYSMEM
)
718 data
->buffer_object
= 0;
719 data
->addr
= buffer
->resource
.heap_memory
;
720 return WINED3D_LOCATION_SYSMEM
;
723 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
724 data
->buffer_object
= 0;
729 static void buffer_resource_unload(struct wined3d_resource
*resource
)
731 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
733 TRACE("buffer %p.\n", buffer
);
735 if (buffer
->buffer_object
)
737 struct wined3d_context
*context
;
739 context
= context_acquire(resource
->device
, NULL
, 0);
741 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
742 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
743 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
744 buffer_clear_dirty_areas(buffer
);
746 context_release(context
);
748 heap_free(buffer
->conversion_map
);
749 buffer
->conversion_map
= NULL
;
751 buffer
->conversion_stride
= 0;
752 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
755 resource_unload(resource
);
758 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
760 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
761 buffer_resource_unload(&buffer
->resource
);
764 static void wined3d_buffer_destroy_object(void *object
)
766 struct wined3d_buffer
*buffer
= object
;
767 struct wined3d_context
*context
;
769 TRACE("buffer %p.\n", buffer
);
771 if (buffer
->buffer_object
)
773 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
774 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
775 context_release(context
);
777 heap_free(buffer
->conversion_map
);
778 heap_free(buffer
->maps
);
781 void wined3d_buffer_cleanup(struct wined3d_buffer
*buffer
)
783 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
784 resource_cleanup(&buffer
->resource
);
787 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
789 unsigned int refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
791 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
795 wined3d_mutex_lock();
796 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
797 buffer
->resource
.device
->adapter
->adapter_ops
->adapter_destroy_buffer(buffer
);
798 wined3d_mutex_unlock();
804 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
806 TRACE("buffer %p.\n", buffer
);
808 return buffer
->resource
.parent
;
811 /* Context activation is done by the caller. */
812 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
813 const struct wined3d_state
*state
)
815 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
816 BOOL decl_changed
= FALSE
;
818 TRACE("buffer %p.\n", buffer
);
820 if (buffer
->resource
.map_count
&& buffer
->map_ptr
)
822 FIXME("Buffer is mapped through buffer object, not loading.\n");
825 else if (buffer
->resource
.map_count
)
827 WARN("Loading mapped buffer.\n");
830 /* TODO: Make converting independent from VBOs */
831 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
833 /* Not doing any conversion */
837 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
839 ERR("Failed to prepare buffer location.\n");
843 /* Reading the declaration makes only sense if we have valid state information
844 * (i.e., if this function is called during draws). */
847 DWORD fixup_flags
= 0;
851 if (!d3d_info
->vertex_bgra
&& !d3d_info
->ffp_generic_attributes
)
852 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
853 if (!d3d_info
->xyzrhw
)
854 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
857 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
858 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
861 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
863 ++buffer
->draw_count
;
864 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
865 buffer
->decl_change_count
= 0;
866 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
867 buffer
->full_conversion_count
= 0;
871 /* If applications change the declaration over and over, reconverting all the time is a huge
872 * performance hit. So count the declaration changes and release the VBO if there are too many
873 * of them (and thus stop converting)
877 ++buffer
->decl_change_count
;
878 buffer
->draw_count
= 0;
880 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
881 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
883 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
884 wined3d_buffer_drop_bo(buffer
);
888 /* The declaration changed, reload the whole buffer. */
889 WARN("Reloading buffer because of a vertex declaration change.\n");
890 buffer_invalidate_bo_range(buffer
, 0, 0);
894 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
895 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
896 * decl changes and reset the decl change count after a specific number of them
898 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
900 ++buffer
->full_conversion_count
;
901 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
903 FIXME("Too many full buffer conversions, stopping converting.\n");
904 wined3d_buffer_drop_bo(buffer
);
910 ++buffer
->draw_count
;
911 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
912 buffer
->decl_change_count
= 0;
913 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
914 buffer
->full_conversion_count
= 0;
918 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
919 ERR("Failed to load buffer location.\n");
922 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
924 TRACE("buffer %p.\n", buffer
);
926 return &buffer
->resource
;
929 static HRESULT
buffer_resource_sub_resource_get_desc(struct wined3d_resource
*resource
,
930 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
932 if (sub_resource_idx
)
934 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
938 desc
->format
= WINED3DFMT_R8_UNORM
;
939 desc
->multisample_type
= WINED3D_MULTISAMPLE_NONE
;
940 desc
->multisample_quality
= 0;
941 desc
->usage
= resource
->usage
;
942 desc
->bind_flags
= resource
->bind_flags
;
943 desc
->access
= resource
->access
;
944 desc
->width
= resource
->size
;
947 desc
->size
= resource
->size
;
951 static void buffer_resource_sub_resource_get_map_pitch(struct wined3d_resource
*resource
,
952 unsigned int sub_resource_idx
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
954 *row_pitch
= *slice_pitch
= resource
->size
;
957 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
958 void **map_ptr
, const struct wined3d_box
*box
, uint32_t flags
)
960 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
961 unsigned int offset
, size
, dirty_offset
, dirty_size
;
962 struct wined3d_device
*device
= resource
->device
;
963 struct wined3d_context
*context
;
964 struct wined3d_bo_address addr
;
968 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
969 resource
, sub_resource_idx
, map_ptr
, debug_box(box
), flags
);
971 dirty_offset
= offset
= box
->left
;
972 dirty_size
= size
= box
->right
- box
->left
;
974 count
= ++resource
->map_count
;
976 /* DISCARD invalidates the entire buffer, regardless of the specified
977 * offset and size. Some applications also depend on the entire buffer
978 * being uploaded in that case. Two such applications are Port Royale
979 * and Darkstar One. */
980 if (flags
& WINED3D_MAP_DISCARD
)
986 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
987 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
988 || buffer
->resource
.pin_sysmem
989 || !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
991 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
993 context
= context_acquire(device
, NULL
, 0);
994 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
995 context_release(context
);
998 if (flags
& WINED3D_MAP_WRITE
)
999 wined3d_buffer_invalidate_range(buffer
, ~WINED3D_LOCATION_SYSMEM
, dirty_offset
, dirty_size
);
1003 context
= context_acquire(device
, NULL
, 0);
1005 if (flags
& WINED3D_MAP_DISCARD
)
1007 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
1009 context_release(context
);
1010 return E_OUTOFMEMORY
;
1012 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1016 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1019 if (flags
& WINED3D_MAP_WRITE
)
1021 wined3d_buffer_acquire_bo_for_write(buffer
, context
);
1022 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1023 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1026 if ((flags
& WINED3D_MAP_DISCARD
) && resource
->heap_memory
)
1027 wined3d_buffer_evict_sysmem(buffer
);
1031 addr
.buffer_object
= buffer
->buffer_object
;
1033 buffer
->map_ptr
= wined3d_context_map_bo_address(context
, &addr
, resource
->size
, flags
);
1035 /* We are accessing buffer->resource.client from the CS thread,
1036 * but it's safe because the client thread will wait for the
1037 * map to return, thus completely serializing this call with
1038 * other client code. */
1039 if (context
->d3d_info
->persistent_map
)
1040 buffer
->resource
.client
.addr
= addr
;
1042 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1044 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1046 wined3d_context_unmap_bo_address(context
, &addr
, 0, NULL
);
1047 buffer
->map_ptr
= NULL
;
1049 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1051 /* The extra copy is more expensive than not using VBOs
1052 * at all on the NVIDIA Linux driver, which is the
1053 * only driver that returns unaligned pointers. */
1054 TRACE("Dynamic buffer, dropping VBO.\n");
1055 wined3d_buffer_drop_bo(buffer
);
1059 TRACE("Falling back to doublebuffered operation.\n");
1060 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1061 buffer
->resource
.pin_sysmem
= 1;
1063 TRACE("New pointer is %p.\n", resource
->heap_memory
);
1067 context_release(context
);
1070 base
= buffer
->map_ptr
? buffer
->map_ptr
: resource
->heap_memory
;
1071 *map_ptr
= base
+ offset
;
1073 TRACE("Returning memory at %p (base %p, offset %u).\n", *map_ptr
, base
, offset
);
1078 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1080 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1081 unsigned int range_count
= buffer
->modified_areas
;
1082 struct wined3d_device
*device
= resource
->device
;
1083 struct wined3d_context
*context
;
1084 struct wined3d_bo_address addr
;
1086 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1088 if (sub_resource_idx
)
1090 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1091 return E_INVALIDARG
;
1094 if (!resource
->map_count
)
1096 WARN("Unmap called without a previous map call.\n");
1100 if (--resource
->map_count
)
1102 /* Delay loading the buffer until everything is unmapped. */
1103 TRACE("Ignoring unmap.\n");
1107 if (!buffer
->map_ptr
)
1110 context
= context_acquire(device
, NULL
, 0);
1112 addr
.buffer_object
= buffer
->buffer_object
;
1114 wined3d_context_unmap_bo_address(context
, &addr
, range_count
, buffer
->maps
);
1116 context_release(context
);
1118 buffer_clear_dirty_areas(buffer
);
1119 buffer
->map_ptr
= NULL
;
1124 static void wined3d_buffer_set_bo(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
, struct wined3d_bo
*bo
)
1126 struct wined3d_bo
*prev_bo
= buffer
->buffer_object
;
1128 TRACE("buffer %p, context %p, bo %p.\n", buffer
, context
, bo
);
1132 struct wined3d_bo_user
*bo_user
;
1134 /* The previous BO might have users in other buffers which were valid,
1135 * and should in theory remain valid. The problem is that it's not easy
1136 * to tell which users belong to this buffer and which don't. We could
1137 * add a field, but for now it's easier and probably fine to just
1138 * invalidate every user. */
1139 LIST_FOR_EACH_ENTRY(bo_user
, &prev_bo
->users
, struct wined3d_bo_user
, entry
)
1140 bo_user
->valid
= false;
1141 list_init(&prev_bo
->users
);
1143 if (!--prev_bo
->refcount
)
1145 wined3d_context_destroy_bo(context
, prev_bo
);
1150 buffer
->buffer_object
= bo
;
1153 void wined3d_buffer_acquire_bo_for_write(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
1155 const struct wined3d_range range
= {.size
= buffer
->resource
.size
};
1156 struct wined3d_bo_address dst
, src
;
1157 struct wined3d_bo
*bo
;
1159 if (!(bo
= buffer
->buffer_object
))
1162 /* If we are the only owner of this BO, there is nothing to do. */
1163 if (bo
->refcount
== 1)
1166 TRACE("Performing copy-on-write for BO %p.\n", bo
);
1168 /* Grab a reference to the current BO. It's okay if this overflows, because
1169 * the following unload will release it. */
1172 /* Unload and re-prepare to get a new buffer. This is a bit cheap and not
1173 * perfectly idiomatic—we should really just factor out an adapter-agnostic
1174 * function to create a BO and then use wined3d_buffer_set_bo()—but it'll
1175 * do nonetheless. */
1176 wined3d_buffer_unload_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1177 wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1179 /* And finally, perform the actual copy. */
1180 assert(buffer
->buffer_object
!= bo
);
1181 dst
.buffer_object
= buffer
->buffer_object
;
1183 src
.buffer_object
= bo
;
1185 wined3d_context_copy_bo_address(context
, &dst
, &src
, 1, &range
, WINED3D_MAP_WRITE
| WINED3D_MAP_DISCARD
);
1188 void wined3d_buffer_copy_bo_address(struct wined3d_buffer
*dst_buffer
, struct wined3d_context
*context
,
1189 unsigned int dst_offset
, const struct wined3d_const_bo_address
*src_addr
, unsigned int size
)
1191 uint32_t map_flags
= WINED3D_MAP_WRITE
;
1192 struct wined3d_bo_address dst_addr
;
1193 struct wined3d_range range
;
1196 if (!dst_offset
&& size
== dst_buffer
->resource
.size
)
1197 map_flags
|= WINED3D_MAP_DISCARD
;
1199 if (map_flags
& WINED3D_MAP_DISCARD
)
1200 wined3d_buffer_acquire_bo_for_write(dst_buffer
, context
);
1202 dst_location
= wined3d_buffer_get_memory(dst_buffer
, context
, &dst_addr
);
1203 dst_addr
.addr
+= dst_offset
;
1207 wined3d_context_copy_bo_address(context
, &dst_addr
, (const struct wined3d_bo_address
*)src_addr
, 1, &range
, map_flags
);
1208 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1211 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1212 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1214 struct wined3d_context
*context
;
1215 struct wined3d_bo_address src
;
1217 TRACE("dst_buffer %p, dst_offset %u, src_buffer %p, src_offset %u, size %u.\n",
1218 dst_buffer
, dst_offset
, src_buffer
, src_offset
, size
);
1220 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1222 wined3d_buffer_get_memory(src_buffer
, context
, &src
);
1223 src
.addr
+= src_offset
;
1225 wined3d_buffer_copy_bo_address(dst_buffer
, context
, dst_offset
, wined3d_const_bo_address(&src
), size
);
1227 context_release(context
);
1230 void wined3d_buffer_update_sub_resource(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1231 const struct upload_bo
*upload_bo
, unsigned int offset
, unsigned int size
)
1233 struct wined3d_bo
*bo
= upload_bo
->addr
.buffer_object
;
1234 uint32_t flags
= upload_bo
->flags
;
1236 /* Try to take this buffer for COW. Don't take it if we've saturated the
1238 if (!offset
&& size
== buffer
->resource
.size
1239 && bo
&& bo
->refcount
< UINT8_MAX
&& !(upload_bo
->flags
& UPLOAD_BO_RENAME_ON_UNMAP
))
1241 flags
|= UPLOAD_BO_RENAME_ON_UNMAP
;
1245 if (flags
& UPLOAD_BO_RENAME_ON_UNMAP
)
1247 /* Don't increment the refcount. UPLOAD_BO_RENAME_ON_UNMAP transfers an
1248 * existing reference.
1250 * FIXME: We could degenerate RENAME to a copy + free and rely on the
1251 * COW logic to detect this case.
1253 wined3d_buffer_set_bo(buffer
, context
, upload_bo
->addr
.buffer_object
);
1254 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1255 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
1258 if (upload_bo
->addr
.buffer_object
&& upload_bo
->addr
.buffer_object
== buffer
->buffer_object
)
1260 struct wined3d_range range
;
1262 /* We need to flush changes, which is implicitly done by
1263 * wined3d_context_unmap_bo_address() even if we aren't actually going
1266 * We would also like to free up virtual address space used by this BO
1267 * if it's at a premium—note that this BO was allocated for an
1268 * accelerated map. Hence we unmap the BO instead of merely flushing it;
1269 * if we don't care about unmapping BOs then
1270 * wined3d_context_unmap_bo_address() will flush and return.
1272 range
.offset
= offset
;
1274 if (upload_bo
->addr
.buffer_object
->map_ptr
)
1275 wined3d_context_unmap_bo_address(context
, (const struct wined3d_bo_address
*)&upload_bo
->addr
, 1, &range
);
1279 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &upload_bo
->addr
, size
);
1283 static void wined3d_buffer_init_data(struct wined3d_buffer
*buffer
,
1284 struct wined3d_device
*device
, const struct wined3d_sub_resource_data
*data
)
1286 struct wined3d_resource
*resource
= &buffer
->resource
;
1287 struct wined3d_box box
;
1289 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
1291 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1292 wined3d_device_context_emit_update_sub_resource(&device
->cs
->c
, resource
,
1293 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1297 memcpy(buffer
->resource
.heap_memory
, data
->data
, resource
->size
);
1298 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
1299 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_SYSMEM
);
1303 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1305 return wined3d_buffer_incref(buffer_from_resource(resource
));
1308 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1310 return wined3d_buffer_decref(buffer_from_resource(resource
));
1313 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1315 struct wined3d_context
*context
;
1317 context
= context_acquire(resource
->device
, NULL
, 0);
1318 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1319 context_release(context
);
1322 static const struct wined3d_resource_ops buffer_resource_ops
=
1324 buffer_resource_incref
,
1325 buffer_resource_decref
,
1326 buffer_resource_preload
,
1327 buffer_resource_unload
,
1328 buffer_resource_sub_resource_get_desc
,
1329 buffer_resource_sub_resource_get_map_pitch
,
1330 buffer_resource_sub_resource_map
,
1331 buffer_resource_sub_resource_unmap
,
1334 static HRESULT
wined3d_buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1335 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1336 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_buffer_ops
*buffer_ops
)
1338 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, WINED3DFMT_R8_UNORM
, desc
->bind_flags
);
1339 struct wined3d_resource
*resource
= &buffer
->resource
;
1340 unsigned int access
;
1343 TRACE("buffer %p, device %p, desc byte_width %u, usage %s, bind_flags %s, "
1344 "access %s, data %p, parent %p, parent_ops %p.\n",
1345 buffer
, device
, desc
->byte_width
, debug_d3dusage(desc
->usage
), wined3d_debug_bind_flags(desc
->bind_flags
),
1346 wined3d_debug_resource_access(desc
->access
), data
, parent
, parent_ops
);
1348 if (!desc
->byte_width
)
1350 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1351 return E_INVALIDARG
;
1354 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& desc
->byte_width
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1356 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc
->byte_width
);
1357 return E_INVALIDARG
;
1360 if (data
&& !data
->data
)
1362 WARN("Invalid sub-resource data specified.\n");
1363 return E_INVALIDARG
;
1366 access
= desc
->access
;
1367 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& wined3d_settings
.cb_access_map_w
)
1368 access
|= WINED3D_RESOURCE_ACCESS_MAP_W
;
1370 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1371 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->bind_flags
, access
,
1372 desc
->byte_width
, 1, 1, desc
->byte_width
, parent
, parent_ops
, &buffer_resource_ops
)))
1374 WARN("Failed to initialize resource, hr %#lx.\n", hr
);
1377 buffer
->buffer_ops
= buffer_ops
;
1378 buffer
->structure_byte_stride
= desc
->structure_byte_stride
;
1379 buffer
->locations
= WINED3D_LOCATION_CLEARED
;
1381 TRACE("buffer %p, size %#x, usage %#x, memory @ %p.\n",
1382 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
, buffer
->resource
.heap_memory
);
1384 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1385 || (desc
->usage
& WINED3DUSAGE_MANAGED
))
1387 /* SWvp and managed buffers always return the same pointer in buffer
1388 * maps and retain data in DISCARD maps. Keep a system memory copy of
1389 * the buffer to provide the same behavior to the application. */
1390 TRACE("Pinning system memory.\n");
1391 buffer
->resource
.pin_sysmem
= 1;
1392 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1395 if (buffer
->locations
& WINED3D_LOCATION_SYSMEM
|| !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1397 if (!wined3d_resource_prepare_sysmem(&buffer
->resource
))
1398 return E_OUTOFMEMORY
;
1401 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1403 ERR("Out of memory.\n");
1404 buffer_resource_unload(resource
);
1405 resource_cleanup(resource
);
1406 wined3d_resource_wait_idle(resource
);
1407 return E_OUTOFMEMORY
;
1409 buffer
->maps_size
= 1;
1411 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
1412 buffer
->resource
.client
.addr
.buffer_object
= CLIENT_BO_DISCARDED
;
1415 wined3d_buffer_init_data(buffer
, device
, data
);
1420 static BOOL
wined3d_buffer_no3d_prepare_location(struct wined3d_buffer
*buffer
,
1421 struct wined3d_context
*context
, unsigned int location
)
1423 if (location
== WINED3D_LOCATION_SYSMEM
)
1424 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1426 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1431 static void wined3d_buffer_no3d_unload_location(struct wined3d_buffer
*buffer
,
1432 struct wined3d_context
*context
, unsigned int location
)
1434 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1437 static const struct wined3d_buffer_ops wined3d_buffer_no3d_ops
=
1439 wined3d_buffer_no3d_prepare_location
,
1440 wined3d_buffer_no3d_unload_location
,
1443 HRESULT
wined3d_buffer_no3d_init(struct wined3d_buffer
*buffer_no3d
, struct wined3d_device
*device
,
1444 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1445 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1447 TRACE("buffer_no3d %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1448 buffer_no3d
, device
, desc
, data
, parent
, parent_ops
);
1450 return wined3d_buffer_init(buffer_no3d
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_no3d_ops
);
1453 static BOOL
wined3d_buffer_gl_prepare_location(struct wined3d_buffer
*buffer
,
1454 struct wined3d_context
*context
, unsigned int location
)
1456 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
1457 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
1461 case WINED3D_LOCATION_SYSMEM
:
1462 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1464 case WINED3D_LOCATION_BUFFER
:
1465 if (buffer
->buffer_object
)
1468 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1470 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
1473 return wined3d_buffer_gl_create_buffer_object(buffer_gl
, context_gl
);
1476 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1481 static void wined3d_buffer_gl_unload_location(struct wined3d_buffer
*buffer
,
1482 struct wined3d_context
*context
, unsigned int location
)
1484 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1488 case WINED3D_LOCATION_BUFFER
:
1489 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer
), wined3d_context_gl(context
));
1493 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1498 static const struct wined3d_buffer_ops wined3d_buffer_gl_ops
=
1500 wined3d_buffer_gl_prepare_location
,
1501 wined3d_buffer_gl_unload_location
,
1504 HRESULT
wined3d_buffer_gl_init(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_device
*device
,
1505 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1506 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1508 const struct wined3d_gl_info
*gl_info
= &wined3d_adapter_gl(device
->adapter
)->gl_info
;
1510 TRACE("buffer_gl %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1511 buffer_gl
, device
, desc
, data
, parent
, parent_ops
);
1513 /* Observations show that draw_primitive_immediate_mode() is faster on
1514 * dynamic vertex buffers than converting + draw_primitive_arrays().
1515 * (Half-Life 2 and others.) */
1516 if (!(desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
1517 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1518 else if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1519 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1520 else if (!(gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1521 && (desc
->usage
& WINED3DUSAGE_DYNAMIC
))
1522 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1524 buffer_gl
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1526 return wined3d_buffer_init(&buffer_gl
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_gl_ops
);
1529 VkBufferUsageFlags
vk_buffer_usage_from_bind_flags(uint32_t bind_flags
)
1531 VkBufferUsageFlags usage
;
1533 usage
= VK_BUFFER_USAGE_TRANSFER_SRC_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT
;
1534 if (bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
1535 usage
|= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
;
1536 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
1537 usage
|= VK_BUFFER_USAGE_INDEX_BUFFER_BIT
;
1538 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1539 usage
|= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
;
1540 if (bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
1541 usage
|= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
;
1542 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1543 usage
|= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT
;
1544 if (bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
)
1545 usage
|= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
;
1546 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
)
1547 usage
|= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
;
1548 if (bind_flags
& (WINED3D_BIND_RENDER_TARGET
| WINED3D_BIND_DEPTH_STENCIL
))
1549 FIXME("Ignoring some bind flags %#x.\n", bind_flags
);
1553 VkMemoryPropertyFlags
vk_memory_type_from_access_flags(uint32_t access
, uint32_t usage
)
1555 VkMemoryPropertyFlags memory_type
= 0;
1557 if (access
& WINED3D_RESOURCE_ACCESS_MAP_R
)
1558 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_CACHED_BIT
;
1559 else if (access
& WINED3D_RESOURCE_ACCESS_MAP_W
)
1560 memory_type
|= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
;
1561 else if (!(usage
& WINED3DUSAGE_DYNAMIC
))
1562 memory_type
|= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
;
1566 static BOOL
wined3d_buffer_vk_create_buffer_object(struct wined3d_buffer_vk
*buffer_vk
,
1567 struct wined3d_context_vk
*context_vk
)
1569 struct wined3d_resource
*resource
= &buffer_vk
->b
.resource
;
1570 struct wined3d_bo_vk
*bo_vk
;
1572 if (!(bo_vk
= heap_alloc(sizeof(*bo_vk
))))
1575 if (!(wined3d_context_vk_create_bo(context_vk
, resource
->size
,
1576 vk_buffer_usage_from_bind_flags(resource
->bind_flags
),
1577 vk_memory_type_from_access_flags(resource
->access
, resource
->usage
), bo_vk
)))
1579 WARN("Failed to create Vulkan buffer.\n");
1584 buffer_vk
->b
.buffer_object
= &bo_vk
->b
;
1585 buffer_invalidate_bo_range(&buffer_vk
->b
, 0, 0);
1590 const VkDescriptorBufferInfo
*wined3d_buffer_vk_get_buffer_info(struct wined3d_buffer_vk
*buffer_vk
)
1592 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1594 if (buffer_vk
->b
.bo_user
.valid
)
1595 return &buffer_vk
->buffer_info
;
1597 buffer_vk
->buffer_info
.buffer
= bo
->vk_buffer
;
1598 buffer_vk
->buffer_info
.offset
= bo
->b
.buffer_offset
;
1599 buffer_vk
->buffer_info
.range
= buffer_vk
->b
.resource
.size
;
1600 wined3d_buffer_validate_user(&buffer_vk
->b
);
1602 return &buffer_vk
->buffer_info
;
1605 static BOOL
wined3d_buffer_vk_prepare_location(struct wined3d_buffer
*buffer
,
1606 struct wined3d_context
*context
, unsigned int location
)
1610 case WINED3D_LOCATION_SYSMEM
:
1611 return wined3d_resource_prepare_sysmem(&buffer
->resource
);
1613 case WINED3D_LOCATION_BUFFER
:
1614 if (buffer
->buffer_object
)
1617 return wined3d_buffer_vk_create_buffer_object(wined3d_buffer_vk(buffer
), wined3d_context_vk(context
));
1620 FIXME("Unhandled location %s.\n", wined3d_debug_location(location
));
1625 static void wined3d_buffer_vk_unload_location(struct wined3d_buffer
*buffer
,
1626 struct wined3d_context
*context
, unsigned int location
)
1628 struct wined3d_context_vk
*context_vk
= wined3d_context_vk(context
);
1629 struct wined3d_bo_vk
*bo_vk
= wined3d_bo_vk(buffer
->buffer_object
);
1631 TRACE("buffer %p, context %p, location %s.\n", buffer
, context
, wined3d_debug_location(location
));
1635 case WINED3D_LOCATION_BUFFER
:
1636 if (buffer
->bo_user
.valid
)
1638 buffer
->bo_user
.valid
= false;
1639 list_remove(&buffer
->bo_user
.entry
);
1641 if (!--bo_vk
->b
.refcount
)
1643 wined3d_context_vk_destroy_bo(context_vk
, bo_vk
);
1646 buffer
->buffer_object
= NULL
;
1650 ERR("Unhandled location %s.\n", wined3d_debug_location(location
));
1655 static const struct wined3d_buffer_ops wined3d_buffer_vk_ops
=
1657 wined3d_buffer_vk_prepare_location
,
1658 wined3d_buffer_vk_unload_location
,
1661 HRESULT
wined3d_buffer_vk_init(struct wined3d_buffer_vk
*buffer_vk
, struct wined3d_device
*device
,
1662 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1663 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1665 const struct wined3d_vk_info
*vk_info
= &wined3d_adapter_vk(device
->adapter
)->vk_info
;
1667 TRACE("buffer_vk %p, device %p, desc %p, data %p, parent %p, parent_ops %p.\n",
1668 buffer_vk
, device
, desc
, data
, parent
, parent_ops
);
1670 if ((desc
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1671 && !vk_info
->supported
[WINED3D_VK_EXT_TRANSFORM_FEEDBACK
])
1673 WARN("The Vulkan implementation does not support transform feedback.\n");
1674 return WINED3DERR_INVALIDCALL
;
1677 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
1678 buffer_vk
->b
.flags
|= WINED3D_BUFFER_USE_BO
;
1680 return wined3d_buffer_init(&buffer_vk
->b
, device
, desc
, data
, parent
, parent_ops
, &wined3d_buffer_vk_ops
);
1683 void wined3d_buffer_vk_barrier(struct wined3d_buffer_vk
*buffer_vk
,
1684 struct wined3d_context_vk
*context_vk
, uint32_t bind_mask
)
1686 uint32_t src_bind_mask
= 0;
1688 TRACE("buffer_vk %p, context_vk %p, bind_mask %s.\n",
1689 buffer_vk
, context_vk
, wined3d_debug_bind_flags(bind_mask
));
1691 if (bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
)
1693 src_bind_mask
= buffer_vk
->bind_mask
& WINED3D_READ_ONLY_BIND_MASK
;
1695 src_bind_mask
= buffer_vk
->bind_mask
;
1697 buffer_vk
->bind_mask
= bind_mask
;
1699 else if ((buffer_vk
->bind_mask
& bind_mask
) != bind_mask
)
1701 src_bind_mask
= buffer_vk
->bind_mask
& ~WINED3D_READ_ONLY_BIND_MASK
;
1702 buffer_vk
->bind_mask
|= bind_mask
;
1707 const struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1708 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1709 VkBufferMemoryBarrier vk_barrier
;
1711 TRACE(" %s -> %s.\n",
1712 wined3d_debug_bind_flags(src_bind_mask
), wined3d_debug_bind_flags(bind_mask
));
1714 wined3d_context_vk_end_current_render_pass(context_vk
);
1716 vk_barrier
.sType
= VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER
;
1717 vk_barrier
.pNext
= NULL
;
1718 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(src_bind_mask
);
1719 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(bind_mask
);
1720 vk_barrier
.srcQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1721 vk_barrier
.dstQueueFamilyIndex
= VK_QUEUE_FAMILY_IGNORED
;
1722 vk_barrier
.buffer
= bo
->vk_buffer
;
1723 vk_barrier
.offset
= bo
->b
.buffer_offset
;
1724 vk_barrier
.size
= buffer_vk
->b
.resource
.size
;
1725 VK_CALL(vkCmdPipelineBarrier(wined3d_context_vk_get_command_buffer(context_vk
),
1726 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask
),
1727 vk_pipeline_stage_mask_from_bind_flags(bind_mask
),
1728 0, 0, NULL
, 1, &vk_barrier
, 0, NULL
));
1732 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1733 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1734 struct wined3d_buffer
**buffer
)
1736 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1737 device
, desc
, data
, parent
, parent_ops
, buffer
);
1739 return device
->adapter
->adapter_ops
->adapter_create_buffer(device
, desc
, data
, parent
, parent_ops
, buffer
);
1742 static HRESULT
wined3d_streaming_buffer_prepare(struct wined3d_device
*device
,
1743 struct wined3d_streaming_buffer
*buffer
, unsigned int min_size
)
1745 struct wined3d_buffer
*wined3d_buffer
;
1746 struct wined3d_buffer_desc desc
;
1747 unsigned int old_size
= 0;
1753 old_size
= buffer
->buffer
->resource
.size
;
1754 if (old_size
>= min_size
)
1758 size
= max(SB_MIN_SIZE
, max(old_size
* 2, min_size
));
1759 TRACE("Growing buffer to %u bytes.\n", size
);
1761 desc
.byte_width
= size
;
1762 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
1763 desc
.bind_flags
= buffer
->bind_flags
;
1764 desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1765 desc
.misc_flags
= 0;
1766 desc
.structure_byte_stride
= 0;
1768 if (SUCCEEDED(hr
= wined3d_buffer_create(device
, &desc
, NULL
, NULL
, &wined3d_null_parent_ops
, &wined3d_buffer
)))
1771 wined3d_buffer_decref(buffer
->buffer
);
1772 buffer
->buffer
= wined3d_buffer
;
1778 HRESULT CDECL
wined3d_streaming_buffer_map(struct wined3d_device
*device
,
1779 struct wined3d_streaming_buffer
*buffer
, unsigned int size
, unsigned int stride
,
1780 unsigned int *ret_pos
, void **ret_data
)
1782 unsigned int map_flags
= WINED3D_MAP_WRITE
;
1783 struct wined3d_resource
*resource
;
1784 struct wined3d_map_desc map_desc
;
1785 unsigned int pos
, align
;
1786 struct wined3d_box box
;
1789 TRACE("device %p, buffer %p, size %u, stride %u, ret_pos %p, ret_data %p.\n",
1790 device
, buffer
, size
, stride
, ret_pos
, ret_data
);
1792 if (FAILED(hr
= wined3d_streaming_buffer_prepare(device
, buffer
, size
)))
1794 resource
= &buffer
->buffer
->resource
;
1797 if ((align
= pos
% stride
))
1798 align
= stride
- align
;
1799 if (pos
+ size
+ align
> resource
->size
)
1802 map_flags
|= WINED3D_MAP_DISCARD
;
1807 map_flags
|= WINED3D_MAP_NOOVERWRITE
;
1810 wined3d_box_set(&box
, pos
, 0, pos
+ size
, 1, 0, 1);
1811 if (SUCCEEDED(hr
= wined3d_resource_map(resource
, 0, &map_desc
, &box
, map_flags
)))
1814 *ret_data
= map_desc
.data
;
1815 buffer
->pos
= pos
+ size
;
1820 void CDECL
wined3d_streaming_buffer_unmap(struct wined3d_streaming_buffer
*buffer
)
1822 wined3d_resource_unmap(&buffer
->buffer
->resource
, 0);
1825 HRESULT CDECL
wined3d_streaming_buffer_upload(struct wined3d_device
*device
, struct wined3d_streaming_buffer
*buffer
,
1826 const void *data
, unsigned int size
, unsigned int stride
, unsigned int *ret_pos
)
1831 if (SUCCEEDED(hr
= wined3d_streaming_buffer_map(device
, buffer
, size
, stride
, ret_pos
, &dst_data
)))
1833 memcpy(dst_data
, data
, size
);
1834 wined3d_streaming_buffer_unmap(buffer
);