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
26 #include "wine/port.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
32 #define WINED3D_BUFFER_HASDESC 0x01 /* A vertex description has been found. */
33 #define WINED3D_BUFFER_USE_BO 0x02 /* Use a buffer object for this buffer. */
34 #define WINED3D_BUFFER_DOUBLEBUFFER 0x04 /* Keep both a buffer object and a system memory copy for this buffer. */
35 #define WINED3D_BUFFER_DISCARD 0x08 /* A DISCARD lock has occurred since the last preload. */
36 #define WINED3D_BUFFER_SYNC 0x10 /* There has been at least one synchronized map since the last preload. */
37 #define WINED3D_BUFFER_MAP 0x20 /* There has been at least one map since the last preload. */
38 #define WINED3D_BUFFER_APPLESYNC 0x40 /* Using sync as in GL_APPLE_flush_buffer_range. */
40 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
41 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
42 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
43 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
45 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
47 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
50 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
52 WARN("Invalid range specified, invalidating entire buffer.\n");
56 if (buffer
->modified_areas
>= buffer
->maps_size
)
58 struct wined3d_map_range
*new;
60 if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer
->maps
, 2 * buffer
->maps_size
* sizeof(*buffer
->maps
))))
62 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
67 buffer
->maps_size
*= 2;
70 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
71 buffer
->maps
[buffer
->modified_areas
].size
= size
;
72 ++buffer
->modified_areas
;
76 buffer
->modified_areas
= 1;
77 buffer
->maps
[0].offset
= 0;
78 buffer
->maps
[0].size
= buffer
->resource
.size
;
81 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
83 This
->modified_areas
= 0;
86 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
88 return !!buffer
->modified_areas
;
91 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
93 return buffer
->modified_areas
== 1
94 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
97 static void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, DWORD location
)
99 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
101 if (location
& WINED3D_LOCATION_BUFFER
)
102 buffer_clear_dirty_areas(buffer
);
104 buffer
->locations
|= location
;
106 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
109 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
110 unsigned int offset
, unsigned int size
)
112 TRACE("buffer %p, location %s, offset %u, size %u.\n",
113 buffer
, wined3d_debug_location(location
), offset
, size
);
115 if (location
& WINED3D_LOCATION_BUFFER
)
116 buffer_invalidate_bo_range(buffer
, offset
, size
);
118 buffer
->locations
&= ~location
;
120 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
122 if (!buffer
->locations
)
123 ERR("Buffer %p does not have any up to date location.\n", buffer
);
126 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, DWORD location
)
128 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
131 /* Context activation is done by the caller. */
132 static void buffer_bind(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
134 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
136 if (buffer
->buffer_type_hint
== GL_ELEMENT_ARRAY_BUFFER
)
137 context_invalidate_state(context
, STATE_INDEXBUFFER
);
139 GL_EXTCALL(glBindBuffer(buffer
->buffer_type_hint
, buffer
->buffer_object
));
142 /* Context activation is done by the caller. */
143 static void buffer_destroy_buffer_object(struct wined3d_buffer
*buffer
, const struct wined3d_context
*context
)
145 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
146 struct wined3d_resource
*resource
= &buffer
->resource
;
148 if (!buffer
->buffer_object
)
151 GL_EXTCALL(glDeleteBuffers(1, &buffer
->buffer_object
));
152 checkGLcall("glDeleteBuffers");
153 buffer
->buffer_object
= 0;
155 /* The stream source state handler might have read the memory of the
156 * vertex buffer already and got the memory in the vbo which is not
157 * valid any longer. Dirtify the stream source to force a reload. This
158 * happens only once per changed vertexbuffer and should occur rather
160 if (resource
->bind_count
)
162 if (buffer
->bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
163 device_invalidate_state(resource
->device
, STATE_STREAMSRC
);
164 if (buffer
->bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
165 device_invalidate_state(resource
->device
, STATE_INDEXBUFFER
);
166 if (buffer
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
168 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX
));
169 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL
));
170 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN
));
171 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY
));
172 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL
));
173 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE
));
179 wined3d_event_query_destroy(buffer
->query
);
180 buffer
->query
= NULL
;
182 buffer
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
185 /* Context activation is done by the caller. */
186 static BOOL
buffer_create_buffer_object(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
188 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
189 GLenum gl_usage
= GL_STATIC_DRAW
;
192 TRACE("Creating an OpenGL buffer object for wined3d_buffer %p with usage %s.\n",
193 buffer
, debug_d3dusage(buffer
->resource
.usage
));
195 /* Make sure that the gl error is cleared. Do not use checkGLcall
196 * here because checkGLcall just prints a fixme and continues. However,
197 * if an error during VBO creation occurs we can fall back to non-VBO operation
198 * with full functionality(but performance loss).
200 while (gl_info
->gl_ops
.gl
.p_glGetError() != GL_NO_ERROR
);
202 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
203 * The vertex declaration from the device determines how the data in the
204 * buffer is interpreted. This means that on each draw call the buffer has
205 * to be verified to check if the rhw and color values are in the correct
208 GL_EXTCALL(glGenBuffers(1, &buffer
->buffer_object
));
209 error
= gl_info
->gl_ops
.gl
.p_glGetError();
210 if (!buffer
->buffer_object
|| error
!= GL_NO_ERROR
)
212 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error
), error
);
216 buffer_bind(buffer
, context
);
217 error
= gl_info
->gl_ops
.gl
.p_glGetError();
218 if (error
!= GL_NO_ERROR
)
220 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error
), error
);
224 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
226 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
227 gl_usage
= GL_STREAM_DRAW_ARB
;
229 if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
231 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_FLUSHING_UNMAP_APPLE
, GL_FALSE
));
232 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_FALSE
));
233 checkGLcall("glBufferParameteriAPPLE");
234 buffer
->flags
|= WINED3D_BUFFER_APPLESYNC
;
236 /* No setup is needed here for GL_ARB_map_buffer_range. */
239 /* Reserve memory for the buffer. The amount of data won't change
240 * so we are safe with calling glBufferData once and
241 * calling glBufferSubData on updates. Upload the actual data in case
242 * we're not double buffering, so we can release the heap mem afterwards.
244 GL_EXTCALL(glBufferData(buffer
->buffer_type_hint
, buffer
->resource
.size
, buffer
->resource
.heap_memory
, gl_usage
));
245 error
= gl_info
->gl_ops
.gl
.p_glGetError();
246 if (error
!= GL_NO_ERROR
)
248 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error
), error
);
252 buffer
->buffer_object_usage
= gl_usage
;
254 if (buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
)
256 buffer_invalidate_bo_range(buffer
, 0, 0);
260 wined3d_resource_free_sysmem(&buffer
->resource
);
261 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
262 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
268 /* Clean up all BO init, but continue because we can work without a BO :-) */
269 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
270 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
271 buffer_destroy_buffer_object(buffer
, context
);
272 buffer_clear_dirty_areas(buffer
);
276 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
277 const enum wined3d_buffer_conversion_type conversion_type
,
278 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
280 const struct wined3d_format
*format
= attrib
->format
;
285 /* Check for some valid situations which cause us pain. One is if the buffer is used for
286 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
287 * with different strides. In the 2nd case we might have to drop conversion entirely,
288 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
292 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
293 debug_d3dformat(format
->id
));
295 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
297 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
301 *stride_this_run
= attrib
->stride
;
302 if (buffer
->stride
!= *stride_this_run
)
304 /* We rely that this happens only on the first converted attribute that is found,
305 * if at all. See above check
307 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
308 buffer
->stride
= *stride_this_run
;
309 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY
, buffer
->conversion_map
);
310 buffer
->conversion_map
= wined3d_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
315 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
316 for (i
= 0; i
< format
->attribute_size
; ++i
)
318 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
319 if (buffer
->conversion_map
[idx
] != conversion_type
)
321 TRACE("Byte %lu in vertex changed:\n", idx
);
322 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
324 buffer
->conversion_map
[idx
] = conversion_type
;
331 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
332 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
334 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
335 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
337 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
338 enum wined3d_format_id format
;
341 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
342 * there, on nonexistent attribs the vbo is 0.
344 if (!(si
->use_map
& (1u << attrib_idx
))
345 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
348 format
= attrib
->format
->id
;
349 /* Look for newly appeared conversion */
350 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
352 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
354 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
356 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
358 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
362 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
364 else if (This
->conversion_map
)
366 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
372 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
373 const struct wined3d_state
*state
, DWORD fixup_flags
)
375 UINT stride_this_run
= 0;
378 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
379 * Once we have our declaration there is no need to look it up again. Index buffers also never need
380 * conversion, so once the (empty) conversion structure is created don't bother checking again
382 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
384 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
389 TRACE("No fixup required.\n");
390 if(This
->conversion_map
)
392 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
393 This
->conversion_map
= NULL
;
401 TRACE("Finding vertex buffer conversion information\n");
402 /* Certain declaration types need some fixups before we can pass them to
403 * opengl. This means D3DCOLOR attributes with fixed function vertex
404 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
405 * GL_ARB_half_float_vertex is not supported.
407 * Note for d3d8 and d3d9:
408 * The vertex buffer FVF doesn't help with finding them, we have to use
409 * the decoded vertex declaration and pick the things that concern the
410 * current buffer. A problem with this is that this can change between
411 * draws, so we have to validate the information and reprocess the buffer
412 * if it changes, and avoid false positives for performance reasons.
413 * WineD3D doesn't even know the vertex buffer any more, it is managed
414 * by the client libraries and passed to SetStreamSource and ProcessVertices
417 * We have to distinguish between vertex shaders and fixed function to
418 * pick the way we access the strided vertex information.
420 * This code sets up a per-byte array with the size of the detected
421 * stride of the arrays in the buffer. For each byte we have a field
422 * that marks the conversion needed on this byte. For example, the
423 * following declaration with fixed function vertex processing:
431 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
432 * [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]
434 * Where in this example map P means 4 component position conversion, 0
435 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
436 * conversion (red / blue swizzle).
438 * If we're doing conversion and the stride changes we have to reconvert
439 * the whole buffer. Note that we do not mind if the semantic changes,
440 * we only care for the conversion type. So if the NORMAL is replaced
441 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
442 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
443 * conversion types depend on the semantic as well, for example a FLOAT4
444 * texcoord needs no conversion while a FLOAT4 positiont needs one
447 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
448 fixup_flags
, &stride_this_run
) || ret
;
449 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
451 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
452 fixup_flags
, &stride_this_run
) || ret
;
453 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
454 fixup_flags
, &stride_this_run
) || ret
;
455 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
456 fixup_flags
, &stride_this_run
) || ret
;
457 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
458 fixup_flags
, &stride_this_run
) || ret
;
459 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
460 fixup_flags
, &stride_this_run
) || ret
;
461 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
462 fixup_flags
, &stride_this_run
) || ret
;
463 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
464 fixup_flags
, &stride_this_run
) || ret
;
465 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
466 fixup_flags
, &stride_this_run
) || ret
;
467 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
468 fixup_flags
, &stride_this_run
) || ret
;
469 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
470 fixup_flags
, &stride_this_run
) || ret
;
471 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
472 fixup_flags
, &stride_this_run
) || ret
;
473 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
474 fixup_flags
, &stride_this_run
) || ret
;
475 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
476 fixup_flags
, &stride_this_run
) || ret
;
478 if (!stride_this_run
&& This
->conversion_map
)
481 if (!ret
) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
482 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
483 This
->conversion_map
= NULL
;
487 if (ret
) TRACE("Conversion information changed\n");
492 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
494 DWORD src_color
= *dst_color
;
496 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
497 * endianness. If we want this to work on big-endian machines as well we
498 * have to consider more things.
500 * 0xff000000: Alpha mask
501 * 0x00ff0000: Blue mask
502 * 0x0000ff00: Green mask
503 * 0x000000ff: Red mask
506 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
507 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
508 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
510 return sizeof(*dst_color
);
513 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
515 /* rhw conversion like in position_float4(). */
516 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
518 float w
= 1.0f
/ p
->w
;
528 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
530 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
532 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
537 static BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
538 struct wined3d_context
*context
, DWORD location
)
542 case WINED3D_LOCATION_SYSMEM
:
543 if (buffer
->resource
.heap_memory
)
546 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
548 ERR("Failed to allocate system memory.\n");
553 case WINED3D_LOCATION_BUFFER
:
554 if (buffer
->buffer_object
)
557 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
559 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
562 return buffer_create_buffer_object(buffer
, context
);
565 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
570 static BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
571 struct wined3d_context
*context
, DWORD location
)
573 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
575 TRACE("buffer %p, context %p, location %s.\n",
576 buffer
, context
, wined3d_debug_location(location
));
578 if (buffer
->locations
& location
)
580 TRACE("Location (%#x) is already up to date.\n", location
);
584 if (!buffer
->locations
)
586 ERR("Buffer %p does not have any up to date location.\n", buffer
);
587 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
588 return wined3d_buffer_load_location(buffer
, context
, location
);
591 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
593 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
596 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
598 TRACE("Buffer previously discarded, nothing to do.\n");
599 wined3d_buffer_validate_location(buffer
, location
);
600 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
606 case WINED3D_LOCATION_SYSMEM
:
607 buffer_bind(buffer
, context
);
608 GL_EXTCALL(glGetBufferSubData(buffer
->buffer_type_hint
, 0, buffer
->resource
.size
,
609 buffer
->resource
.heap_memory
));
610 checkGLcall("buffer download");
611 buffer
->flags
|= WINED3D_BUFFER_DOUBLEBUFFER
;
614 case WINED3D_LOCATION_BUFFER
:
615 FIXME("Not implemented yet.\n");
619 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
623 wined3d_buffer_validate_location(buffer
, location
);
627 /* Context activation is done by the caller. */
628 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
630 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
631 return buffer
->resource
.heap_memory
;
634 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
,
635 struct wined3d_bo_address
*data
, DWORD locations
)
637 TRACE("buffer %p, data %p, locations %s.\n",
638 buffer
, data
, wined3d_debug_location(locations
));
640 if (locations
& WINED3D_LOCATION_BUFFER
)
642 data
->buffer_object
= buffer
->buffer_object
;
644 return WINED3D_LOCATION_BUFFER
;
646 if (locations
& WINED3D_LOCATION_SYSMEM
)
648 data
->buffer_object
= 0;
649 data
->addr
= buffer
->resource
.heap_memory
;
650 return WINED3D_LOCATION_SYSMEM
;
653 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
654 data
->buffer_object
= 0;
659 static void buffer_unload(struct wined3d_resource
*resource
)
661 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
662 DWORD flags
= buffer
->flags
;
664 TRACE("buffer %p.\n", buffer
);
666 if (buffer
->buffer_object
)
668 struct wined3d_context
*context
;
670 context
= context_acquire(resource
->device
, NULL
);
672 /* Download the buffer, but don't permanently enable double buffering. */
673 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
674 if (!(flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
675 buffer
->flags
&= ~WINED3D_BUFFER_DOUBLEBUFFER
;
677 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
678 buffer_destroy_buffer_object(buffer
, context
);
679 buffer_clear_dirty_areas(buffer
);
681 context_release(context
);
683 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
684 buffer
->conversion_map
= NULL
;
686 buffer
->conversion_stride
= 0;
687 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
690 resource_unload(resource
);
693 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
695 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
696 buffer_unload(&buffer
->resource
);
699 static void wined3d_buffer_destroy_object(void *object
)
701 struct wined3d_buffer
*buffer
= object
;
702 struct wined3d_context
*context
;
704 if (buffer
->buffer_object
)
706 context
= context_acquire(buffer
->resource
.device
, NULL
);
707 buffer_destroy_buffer_object(buffer
, context
);
708 context_release(context
);
710 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
713 HeapFree(GetProcessHeap(), 0, buffer
->maps
);
714 HeapFree(GetProcessHeap(), 0, buffer
);
717 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
719 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
721 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
725 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
726 resource_cleanup(&buffer
->resource
);
727 wined3d_cs_emit_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
733 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
735 TRACE("buffer %p.\n", buffer
);
737 return buffer
->resource
.parent
;
740 /* The caller provides a context and binds the buffer */
741 static void buffer_sync_apple(struct wined3d_buffer
*This
, DWORD flags
, const struct wined3d_gl_info
*gl_info
)
743 enum wined3d_event_query_result ret
;
745 /* No fencing needs to be done if the app promises not to overwrite
747 if (flags
& WINED3D_MAP_NOOVERWRITE
)
750 if (flags
& WINED3D_MAP_DISCARD
)
752 GL_EXTCALL(glBufferData(This
->buffer_type_hint
, This
->resource
.size
, NULL
, This
->buffer_object_usage
));
753 checkGLcall("glBufferData");
759 TRACE("Creating event query for buffer %p\n", This
);
761 if (!wined3d_event_query_supported(gl_info
))
763 FIXME("Event queries not supported, dropping async buffer locks.\n");
767 This
->query
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->query
));
770 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
774 /* Since we don't know about old draws a glFinish is needed once */
775 gl_info
->gl_ops
.gl
.p_glFinish();
778 TRACE("Synchronizing buffer %p\n", This
);
779 ret
= wined3d_event_query_finish(This
->query
, This
->resource
.device
);
782 case WINED3D_EVENT_QUERY_NOT_STARTED
:
783 case WINED3D_EVENT_QUERY_OK
:
787 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
788 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
792 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret
);
799 wined3d_event_query_destroy(This
->query
);
803 gl_info
->gl_ops
.gl
.p_glFinish();
804 GL_EXTCALL(glBufferParameteriAPPLE(This
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
805 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
806 This
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
809 /* The caller provides a GL context */
810 static void buffer_direct_upload(struct wined3d_buffer
*This
, struct wined3d_context
*context
, DWORD flags
)
812 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
813 unsigned int start
, len
;
816 buffer_bind(This
, context
);
817 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
820 mapflags
= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
821 if (flags
& WINED3D_BUFFER_DISCARD
)
822 mapflags
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
823 else if (flags
& WINED3D_BUFFER_MAP
&& !(flags
& WINED3D_BUFFER_SYNC
))
824 mapflags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
825 map
= GL_EXTCALL(glMapBufferRange(This
->buffer_type_hint
, 0,
826 This
->resource
.size
, mapflags
));
827 checkGLcall("glMapBufferRange");
831 if (This
->flags
& WINED3D_BUFFER_APPLESYNC
)
834 if (flags
& WINED3D_BUFFER_DISCARD
)
835 syncflags
|= WINED3D_MAP_DISCARD
;
836 else if (flags
& WINED3D_BUFFER_MAP
&& !(flags
& WINED3D_BUFFER_SYNC
))
837 syncflags
|= WINED3D_MAP_NOOVERWRITE
;
838 buffer_sync_apple(This
, syncflags
, gl_info
);
840 map
= GL_EXTCALL(glMapBuffer(This
->buffer_type_hint
, GL_WRITE_ONLY
));
841 checkGLcall("glMapBuffer");
845 ERR("Failed to map OpenGL buffer.\n");
849 while (This
->modified_areas
)
851 This
->modified_areas
--;
852 start
= This
->maps
[This
->modified_areas
].offset
;
853 len
= This
->maps
[This
->modified_areas
].size
;
855 memcpy(map
+ start
, (BYTE
*)This
->resource
.heap_memory
+ start
, len
);
857 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
859 GL_EXTCALL(glFlushMappedBufferRange(This
->buffer_type_hint
, start
, len
));
860 checkGLcall("glFlushMappedBufferRange");
862 else if (This
->flags
& WINED3D_BUFFER_APPLESYNC
)
864 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This
->buffer_type_hint
, start
, len
));
865 checkGLcall("glFlushMappedBufferRangeAPPLE");
868 GL_EXTCALL(glUnmapBuffer(This
->buffer_type_hint
));
869 checkGLcall("glUnmapBuffer");
871 wined3d_buffer_validate_location(This
, WINED3D_LOCATION_BUFFER
);
874 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
876 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
877 unsigned int i
, j
, start
, end
, len
, vertex_count
;
880 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
882 /* Now for each vertex in the buffer that needs conversion. */
883 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
885 if (!(data
= HeapAlloc(GetProcessHeap(), 0, buffer
->resource
.size
)))
887 ERR("Out of memory.\n");
891 buffer_bind(buffer
, context
);
892 while (buffer
->modified_areas
)
894 buffer
->modified_areas
--;
895 start
= buffer
->maps
[buffer
->modified_areas
].offset
;
896 len
= buffer
->maps
[buffer
->modified_areas
].size
;
899 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
900 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
902 for (j
= 0; j
< buffer
->stride
;)
904 switch (buffer
->conversion_map
[j
])
911 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
914 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
917 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
923 GL_EXTCALL(glBufferSubData(buffer
->buffer_type_hint
, start
, len
, data
+ start
));
924 checkGLcall("glBufferSubData");
927 HeapFree(GetProcessHeap(), 0, data
);
929 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
932 void buffer_mark_used(struct wined3d_buffer
*buffer
)
934 buffer
->flags
&= ~(WINED3D_BUFFER_MAP
| WINED3D_BUFFER_SYNC
| WINED3D_BUFFER_DISCARD
);
937 /* Context activation is done by the caller. */
938 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
939 const struct wined3d_state
*state
)
941 DWORD flags
= buffer
->flags
& (WINED3D_BUFFER_MAP
| WINED3D_BUFFER_SYNC
| WINED3D_BUFFER_DISCARD
);
942 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
943 BOOL decl_changed
= FALSE
;
945 TRACE("buffer %p.\n", buffer
);
947 if (buffer
->resource
.map_count
)
949 WARN("Buffer is mapped, skipping preload.\n");
953 buffer_mark_used(buffer
);
955 /* TODO: Make converting independent from VBOs */
956 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
958 /* Not doing any conversion */
962 wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
964 /* Reading the declaration makes only sense if we have valid state information
965 * (i.e., if this function is called during draws). */
968 DWORD fixup_flags
= 0;
972 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !context
->d3d_info
->ffp_generic_attributes
)
973 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
974 if (!context
->d3d_info
->xyzrhw
)
975 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
978 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
979 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
982 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
984 ++buffer
->draw_count
;
985 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
986 buffer
->decl_change_count
= 0;
987 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
988 buffer
->full_conversion_count
= 0;
992 /* If applications change the declaration over and over, reconverting all the time is a huge
993 * performance hit. So count the declaration changes and release the VBO if there are too many
994 * of them (and thus stop converting)
998 ++buffer
->decl_change_count
;
999 buffer
->draw_count
= 0;
1001 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
1002 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
1004 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
1005 wined3d_buffer_drop_bo(buffer
);
1009 /* The declaration changed, reload the whole buffer. */
1010 WARN("Reloading buffer because of a vertex declaration change.\n");
1011 buffer_invalidate_bo_range(buffer
, 0, 0);
1013 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
1014 * cleared for unsynchronized updates.
1020 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
1021 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
1022 * decl changes and reset the decl change count after a specific number of them
1024 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
1026 ++buffer
->full_conversion_count
;
1027 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
1029 FIXME("Too many full buffer conversions, stopping converting.\n");
1030 wined3d_buffer_drop_bo(buffer
);
1036 ++buffer
->draw_count
;
1037 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
1038 buffer
->decl_change_count
= 0;
1039 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
1040 buffer
->full_conversion_count
= 0;
1044 if (!buffer
->conversion_map
)
1046 /* That means that there is nothing to fixup. Just upload from
1047 * buffer->resource.heap_memory directly into the BO. Do not
1048 * free the system memory copy because drawPrimitive may need it if
1049 * the stride is 0, for instancing emulation, vertex blending
1050 * emulation or shader emulation. */
1051 TRACE("No conversion needed.\n");
1053 /* Nothing to do because heap memory exists if the buffer is double buffer or has no BO at all. */
1054 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
1057 buffer_direct_upload(buffer
, context
, flags
);
1061 buffer_conversion_upload(buffer
, context
);
1065 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
1067 TRACE("buffer %p.\n", buffer
);
1069 return &buffer
->resource
;
1072 static HRESULT
wined3d_buffer_map(struct wined3d_buffer
*buffer
, UINT offset
, UINT size
, BYTE
**data
, DWORD flags
)
1074 struct wined3d_device
*device
= buffer
->resource
.device
;
1075 struct wined3d_context
*context
;
1079 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer
, offset
, size
, data
, flags
);
1081 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
1082 * fill rate test seems to depend on this. When we map a buffer with
1083 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
1084 * previous contents of the buffer. The r600g driver only does this when
1085 * the buffer is currently in use, while the proprietary NVIDIA driver
1086 * appears to do this unconditionally. */
1087 if (buffer
->flags
& WINED3D_BUFFER_DISCARD
)
1088 flags
&= ~WINED3D_MAP_DISCARD
;
1089 count
= ++buffer
->resource
.map_count
;
1091 if (buffer
->buffer_object
)
1093 unsigned int dirty_offset
= offset
, dirty_size
= size
;
1095 /* DISCARD invalidates the entire buffer, regardless of the specified
1096 * offset and size. Some applications also depend on the entire buffer
1097 * being uploaded in that case. Two such applications are Port Royale
1098 * and Darkstar One. */
1099 if (flags
& WINED3D_MAP_DISCARD
)
1105 if (buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
)
1107 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1109 context
= context_acquire(device
, NULL
);
1110 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1111 context_release(context
);
1114 if (!(flags
& WINED3D_MAP_READONLY
))
1115 wined3d_buffer_invalidate_range(buffer
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
1119 if (!(flags
& WINED3D_MAP_READONLY
))
1120 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1124 const struct wined3d_gl_info
*gl_info
;
1126 context
= context_acquire(device
, NULL
);
1127 gl_info
= context
->gl_info
;
1129 buffer_bind(buffer
, context
);
1131 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1133 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
1134 buffer
->map_ptr
= GL_EXTCALL(glMapBufferRange(buffer
->buffer_type_hint
,
1135 0, buffer
->resource
.size
, mapflags
));
1136 checkGLcall("glMapBufferRange");
1140 if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1141 buffer_sync_apple(buffer
, flags
, gl_info
);
1142 buffer
->map_ptr
= GL_EXTCALL(glMapBuffer(buffer
->buffer_type_hint
,
1144 checkGLcall("glMapBuffer");
1147 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1149 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1151 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1152 checkGLcall("glUnmapBuffer");
1153 buffer
->map_ptr
= NULL
;
1155 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1157 /* The extra copy is more expensive than not using VBOs at
1158 * all on the Nvidia Linux driver, which is the only driver
1159 * that returns unaligned pointers.
1161 TRACE("Dynamic buffer, dropping VBO.\n");
1162 wined3d_buffer_drop_bo(buffer
);
1166 TRACE("Falling back to doublebuffered operation.\n");
1167 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1169 TRACE("New pointer is %p.\n", buffer
->resource
.heap_memory
);
1170 buffer
->map_ptr
= NULL
;
1172 context_release(context
);
1176 buffer
->flags
|= WINED3D_BUFFER_MAP
;
1177 if (flags
& WINED3D_MAP_DISCARD
)
1178 buffer
->flags
|= WINED3D_BUFFER_DISCARD
;
1179 else if (!(flags
& WINED3D_MAP_NOOVERWRITE
))
1180 buffer
->flags
|= WINED3D_BUFFER_SYNC
;
1183 base
= buffer
->map_ptr
? buffer
->map_ptr
: buffer
->resource
.heap_memory
;
1184 *data
= base
+ offset
;
1186 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1187 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1192 static void wined3d_buffer_unmap(struct wined3d_buffer
*buffer
)
1196 TRACE("buffer %p.\n", buffer
);
1198 /* In the case that the number of Unmap calls > the
1199 * number of Map calls, d3d returns always D3D_OK.
1200 * This is also needed to prevent Map from returning garbage on
1201 * the next call (this will happen if the lock_count is < 0). */
1202 if (!buffer
->resource
.map_count
)
1204 WARN("Unmap called without a previous map call.\n");
1208 if (--buffer
->resource
.map_count
)
1210 /* Delay loading the buffer until everything is unlocked */
1211 TRACE("Ignoring unmap.\n");
1215 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
) && buffer
->buffer_object
)
1217 struct wined3d_device
*device
= buffer
->resource
.device
;
1218 const struct wined3d_gl_info
*gl_info
;
1219 struct wined3d_context
*context
;
1221 context
= context_acquire(device
, NULL
);
1222 gl_info
= context
->gl_info
;
1224 buffer_bind(buffer
, context
);
1226 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1228 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1230 GL_EXTCALL(glFlushMappedBufferRange(buffer
->buffer_type_hint
,
1231 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1232 checkGLcall("glFlushMappedBufferRange");
1235 else if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1237 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1239 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer
->buffer_type_hint
,
1240 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1241 checkGLcall("glFlushMappedBufferRangeAPPLE");
1245 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1246 if (wined3d_settings
.strict_draw_ordering
)
1247 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
1248 context_release(context
);
1250 buffer_clear_dirty_areas(buffer
);
1251 buffer
->map_ptr
= NULL
;
1253 else if (buffer
->flags
& WINED3D_BUFFER_HASDESC
)
1255 wined3d_resource_preload(&buffer
->resource
);
1259 HRESULT
wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1260 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1262 const struct wined3d_gl_info
*gl_info
;
1263 struct wined3d_bo_address dst
, src
;
1264 struct wined3d_context
*context
;
1265 struct wined3d_device
*device
;
1266 BYTE
*dst_ptr
, *src_ptr
;
1270 buffer_mark_used(dst_buffer
);
1271 buffer_mark_used(src_buffer
);
1273 device
= dst_buffer
->resource
.device
;
1275 context
= context_acquire(device
, NULL
);
1276 gl_info
= context
->gl_info
;
1278 dst_location
= wined3d_buffer_get_memory(dst_buffer
, &dst
, dst_buffer
->locations
);
1279 wined3d_buffer_get_memory(src_buffer
, &src
, src_buffer
->locations
);
1281 if (dst
.buffer_object
&& src
.buffer_object
)
1283 if (gl_info
->supported
[ARB_COPY_BUFFER
])
1285 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src
.buffer_object
));
1286 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst
.buffer_object
));
1287 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
1288 src_offset
, dst_offset
, size
));
1289 checkGLcall("direct buffer copy");
1293 if (FAILED(hr
= wined3d_buffer_map(dst_buffer
, dst_offset
, size
, &dst_ptr
, 0)))
1295 WARN("Failed to map dst_buffer, hr %#x.\n", hr
);
1296 context_release(context
);
1297 return WINED3DERR_INVALIDCALL
;
1299 if (FAILED(hr
= wined3d_buffer_map(src_buffer
, src_offset
, size
, &src_ptr
, WINED3D_MAP_READONLY
)))
1301 WARN("Failed to map src_buffer, hr %#x.\n", hr
);
1302 wined3d_buffer_unmap(dst_buffer
);
1303 context_release(context
);
1304 return WINED3DERR_INVALIDCALL
;
1307 memcpy(dst_ptr
, src_ptr
, size
);
1309 wined3d_buffer_unmap(src_buffer
);
1310 wined3d_buffer_unmap(dst_buffer
);
1313 else if (!dst
.buffer_object
&& src
.buffer_object
)
1315 buffer_bind(src_buffer
, context
);
1316 GL_EXTCALL(glGetBufferSubData(src_buffer
->buffer_type_hint
, src_offset
, size
, dst
.addr
+ dst_offset
));
1317 checkGLcall("buffer download");
1319 else if (dst
.buffer_object
&& !src
.buffer_object
)
1321 buffer_bind(dst_buffer
, context
);
1322 GL_EXTCALL(glBufferSubData(dst_buffer
->buffer_type_hint
, dst_offset
, size
, src
.addr
+ src_offset
));
1323 checkGLcall("buffer upload");
1327 memcpy(dst
.addr
+ dst_offset
, src
.addr
+ src_offset
, size
);
1330 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1332 context_release(context
);
1336 HRESULT
wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
,
1337 const struct wined3d_box
*box
, const void *data
)
1346 size
= box
->right
- box
->left
;
1351 size
= buffer
->resource
.size
;
1354 if (FAILED(hr
= wined3d_buffer_map(buffer
, offset
, size
, &ptr
, 0)))
1357 memcpy(ptr
, data
, size
);
1359 wined3d_buffer_unmap(buffer
);
1363 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1365 return wined3d_buffer_incref(buffer_from_resource(resource
));
1368 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1370 return wined3d_buffer_decref(buffer_from_resource(resource
));
1373 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1375 struct wined3d_context
*context
;
1377 context
= context_acquire(resource
->device
, NULL
);
1378 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1379 context_release(context
);
1382 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1383 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1385 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1388 if (sub_resource_idx
)
1390 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1391 return E_INVALIDARG
;
1397 size
= box
->right
- box
->left
;
1404 map_desc
->row_pitch
= map_desc
->slice_pitch
= buffer
->desc
.byte_width
;
1405 return wined3d_buffer_map(buffer
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1408 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1410 if (sub_resource_idx
)
1412 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1413 return E_INVALIDARG
;
1416 wined3d_buffer_unmap(buffer_from_resource(resource
));
1420 static const struct wined3d_resource_ops buffer_resource_ops
=
1422 buffer_resource_incref
,
1423 buffer_resource_decref
,
1424 buffer_resource_preload
,
1426 buffer_resource_sub_resource_map
,
1427 buffer_resource_sub_resource_unmap
,
1430 static GLenum
buffer_type_hint_from_bind_flags(unsigned int bind_flags
)
1432 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
1433 return GL_ELEMENT_ARRAY_BUFFER
;
1435 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1436 return GL_UNIFORM_BUFFER
;
1438 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
1439 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
1441 return GL_ARRAY_BUFFER
;
1444 static HRESULT
buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1445 UINT size
, DWORD usage
, enum wined3d_format_id format_id
, enum wined3d_pool pool
, unsigned int bind_flags
,
1446 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1448 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1449 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, usage
);
1450 BOOL dynamic_buffer_ok
;
1455 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL.\n");
1456 return WINED3DERR_INVALIDCALL
;
1459 if (data
&& !data
->data
)
1461 WARN("Invalid sub-resource data specified.\n");
1462 return E_INVALIDARG
;
1465 hr
= resource_init(&buffer
->resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1466 WINED3D_MULTISAMPLE_NONE
, 0, usage
, pool
, size
, 1, 1, size
, parent
, parent_ops
, &buffer_resource_ops
);
1469 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1472 buffer
->buffer_type_hint
= buffer_type_hint_from_bind_flags(bind_flags
);
1473 buffer
->bind_flags
= bind_flags
;
1474 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1476 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1477 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
,
1478 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
);
1480 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
|| pool
== WINED3D_POOL_MANAGED
)
1482 /* SWvp and managed buffers always return the same pointer in buffer
1483 * maps and retain data in DISCARD maps. Keep a system memory copy of
1484 * the buffer to provide the same behavior to the application. */
1485 TRACE("Using doublebuffer mode.\n");
1486 buffer
->flags
|= WINED3D_BUFFER_DOUBLEBUFFER
;
1489 /* Observations show that draw_primitive_immediate_mode() is faster on
1490 * dynamic vertex buffers than converting + draw_primitive_arrays().
1491 * (Half-Life 2 and others.) */
1492 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1494 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1496 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1498 else if (buffer
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
1500 TRACE("Not creating a BO because the buffer is in system memory.\n");
1502 else if (!dynamic_buffer_ok
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
1504 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1508 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1511 if (!(buffer
->maps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
->maps
))))
1513 ERR("Out of memory.\n");
1514 buffer_unload(&buffer
->resource
);
1515 resource_cleanup(&buffer
->resource
);
1516 wined3d_resource_wait_idle(&buffer
->resource
);
1517 return E_OUTOFMEMORY
;
1519 buffer
->maps_size
= 1;
1522 wined3d_device_update_sub_resource(device
, &buffer
->resource
,
1523 0, NULL
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1528 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1529 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1530 struct wined3d_buffer
**buffer
)
1532 struct wined3d_buffer
*object
;
1535 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1536 device
, desc
, data
, parent
, parent_ops
, buffer
);
1538 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1540 return E_OUTOFMEMORY
;
1542 FIXME("Ignoring access flags (pool).\n");
1544 hr
= buffer_init(object
, device
, desc
->byte_width
, desc
->usage
, WINED3DFMT_UNKNOWN
,
1545 WINED3D_POOL_MANAGED
, desc
->bind_flags
, data
, parent
, parent_ops
);
1548 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1549 HeapFree(GetProcessHeap(), 0, object
);
1552 object
->desc
= *desc
;
1554 TRACE("Created buffer %p.\n", object
);
1561 HRESULT CDECL
wined3d_buffer_create_vb(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1562 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1564 struct wined3d_buffer
*object
;
1567 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1568 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1570 if (pool
== WINED3D_POOL_SCRATCH
)
1572 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1573 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1574 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1576 return WINED3DERR_INVALIDCALL
;
1579 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1583 return WINED3DERR_OUTOFVIDEOMEMORY
;
1586 hr
= buffer_init(object
, device
, size
, usage
, WINED3DFMT_UNKNOWN
,
1587 pool
, WINED3D_BIND_VERTEX_BUFFER
, NULL
, parent
, parent_ops
);
1590 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1591 HeapFree(GetProcessHeap(), 0, object
);
1595 TRACE("Created buffer %p.\n", object
);
1601 HRESULT CDECL
wined3d_buffer_create_ib(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1602 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1604 struct wined3d_buffer
*object
;
1607 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1608 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1610 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1614 return WINED3DERR_OUTOFVIDEOMEMORY
;
1617 hr
= buffer_init(object
, device
, size
, usage
| WINED3DUSAGE_STATICDECL
,
1618 WINED3DFMT_UNKNOWN
, pool
, WINED3D_BIND_INDEX_BUFFER
, NULL
,
1619 parent
, parent_ops
);
1622 WARN("Failed to initialize buffer, hr %#x\n", hr
);
1623 HeapFree(GetProcessHeap(), 0, object
);
1627 TRACE("Created buffer %p.\n", object
);