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_PIN_SYSMEM 0x04 /* Keep 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_APPLESYNC 0x10 /* Using sync as in GL_APPLE_flush_buffer_range. */
38 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
39 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
40 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
41 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
43 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
45 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
48 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
50 WARN("Invalid range specified, invalidating entire buffer.\n");
54 if (buffer
->modified_areas
>= buffer
->maps_size
)
56 struct wined3d_map_range
*new;
58 if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer
->maps
, 2 * buffer
->maps_size
* sizeof(*buffer
->maps
))))
60 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
65 buffer
->maps_size
*= 2;
68 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
69 buffer
->maps
[buffer
->modified_areas
].size
= size
;
70 ++buffer
->modified_areas
;
74 buffer
->modified_areas
= 1;
75 buffer
->maps
[0].offset
= 0;
76 buffer
->maps
[0].size
= buffer
->resource
.size
;
79 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
81 This
->modified_areas
= 0;
84 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
86 return !!buffer
->modified_areas
;
89 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
91 return buffer
->modified_areas
== 1
92 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
95 static void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, DWORD location
)
97 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
99 if (location
& WINED3D_LOCATION_BUFFER
)
100 buffer_clear_dirty_areas(buffer
);
102 buffer
->locations
|= location
;
104 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
107 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
108 unsigned int offset
, unsigned int size
)
110 TRACE("buffer %p, location %s, offset %u, size %u.\n",
111 buffer
, wined3d_debug_location(location
), offset
, size
);
113 if (location
& WINED3D_LOCATION_BUFFER
)
114 buffer_invalidate_bo_range(buffer
, offset
, size
);
116 buffer
->locations
&= ~location
;
118 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
120 if (!buffer
->locations
)
121 ERR("Buffer %p does not have any up to date location.\n", buffer
);
124 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, DWORD location
)
126 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
129 /* Context activation is done by the caller. */
130 static void buffer_bind(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
132 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
134 if (buffer
->buffer_type_hint
== GL_ELEMENT_ARRAY_BUFFER
)
135 context_invalidate_state(context
, STATE_INDEXBUFFER
);
137 GL_EXTCALL(glBindBuffer(buffer
->buffer_type_hint
, buffer
->buffer_object
));
140 /* Context activation is done by the caller. */
141 static void buffer_destroy_buffer_object(struct wined3d_buffer
*buffer
, const struct wined3d_context
*context
)
143 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
144 struct wined3d_resource
*resource
= &buffer
->resource
;
146 if (!buffer
->buffer_object
)
149 GL_EXTCALL(glDeleteBuffers(1, &buffer
->buffer_object
));
150 checkGLcall("glDeleteBuffers");
151 buffer
->buffer_object
= 0;
153 /* The stream source state handler might have read the memory of the
154 * vertex buffer already and got the memory in the vbo which is not
155 * valid any longer. Dirtify the stream source to force a reload. This
156 * happens only once per changed vertexbuffer and should occur rather
158 if (resource
->bind_count
)
160 if (buffer
->bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
161 device_invalidate_state(resource
->device
, STATE_STREAMSRC
);
162 if (buffer
->bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
163 device_invalidate_state(resource
->device
, STATE_INDEXBUFFER
);
164 if (buffer
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
166 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX
));
167 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL
));
168 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN
));
169 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY
));
170 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL
));
171 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE
));
177 wined3d_event_query_destroy(buffer
->query
);
178 buffer
->query
= NULL
;
180 buffer
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
183 /* Context activation is done by the caller. */
184 static BOOL
buffer_create_buffer_object(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
186 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
187 GLenum gl_usage
= GL_STATIC_DRAW
;
190 TRACE("Creating an OpenGL buffer object for wined3d_buffer %p with usage %s.\n",
191 buffer
, debug_d3dusage(buffer
->resource
.usage
));
193 /* Make sure that the gl error is cleared. Do not use checkGLcall
194 * here because checkGLcall just prints a fixme and continues. However,
195 * if an error during VBO creation occurs we can fall back to non-VBO operation
196 * with full functionality(but performance loss).
198 while (gl_info
->gl_ops
.gl
.p_glGetError() != GL_NO_ERROR
);
200 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
201 * The vertex declaration from the device determines how the data in the
202 * buffer is interpreted. This means that on each draw call the buffer has
203 * to be verified to check if the rhw and color values are in the correct
206 GL_EXTCALL(glGenBuffers(1, &buffer
->buffer_object
));
207 error
= gl_info
->gl_ops
.gl
.p_glGetError();
208 if (!buffer
->buffer_object
|| error
!= GL_NO_ERROR
)
210 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error
), error
);
214 buffer_bind(buffer
, context
);
215 error
= gl_info
->gl_ops
.gl
.p_glGetError();
216 if (error
!= GL_NO_ERROR
)
218 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error
), error
);
222 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
224 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
225 gl_usage
= GL_STREAM_DRAW_ARB
;
227 if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
229 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_FLUSHING_UNMAP_APPLE
, GL_FALSE
));
230 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_FALSE
));
231 checkGLcall("glBufferParameteriAPPLE");
232 buffer
->flags
|= WINED3D_BUFFER_APPLESYNC
;
234 /* No setup is needed here for GL_ARB_map_buffer_range. */
237 /* Reserve memory for the buffer. The amount of data won't change
238 * so we are safe with calling glBufferData once and
239 * calling glBufferSubData on updates. Upload the actual data in case
240 * we're not double buffering, so we can release the heap mem afterwards.
242 GL_EXTCALL(glBufferData(buffer
->buffer_type_hint
, buffer
->resource
.size
, buffer
->resource
.heap_memory
, gl_usage
));
243 error
= gl_info
->gl_ops
.gl
.p_glGetError();
244 if (error
!= GL_NO_ERROR
)
246 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error
), error
);
250 buffer
->buffer_object_usage
= gl_usage
;
252 if (buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
254 buffer_invalidate_bo_range(buffer
, 0, 0);
258 wined3d_resource_free_sysmem(&buffer
->resource
);
259 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
260 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
266 /* Clean up all BO init, but continue because we can work without a BO :-) */
267 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
268 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
269 buffer_destroy_buffer_object(buffer
, context
);
270 buffer_clear_dirty_areas(buffer
);
274 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
275 const enum wined3d_buffer_conversion_type conversion_type
,
276 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
278 const struct wined3d_format
*format
= attrib
->format
;
283 /* Check for some valid situations which cause us pain. One is if the buffer is used for
284 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
285 * with different strides. In the 2nd case we might have to drop conversion entirely,
286 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
290 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
291 debug_d3dformat(format
->id
));
293 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
295 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
299 *stride_this_run
= attrib
->stride
;
300 if (buffer
->stride
!= *stride_this_run
)
302 /* We rely that this happens only on the first converted attribute that is found,
303 * if at all. See above check
305 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
306 buffer
->stride
= *stride_this_run
;
307 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY
, buffer
->conversion_map
);
308 buffer
->conversion_map
= wined3d_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
313 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
314 for (i
= 0; i
< format
->attribute_size
; ++i
)
316 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
317 if (buffer
->conversion_map
[idx
] != conversion_type
)
319 TRACE("Byte %lu in vertex changed:\n", idx
);
320 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
322 buffer
->conversion_map
[idx
] = conversion_type
;
329 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
330 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
332 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
333 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
335 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
336 enum wined3d_format_id format
;
339 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
340 * there, on nonexistent attribs the vbo is 0.
342 if (!(si
->use_map
& (1u << attrib_idx
))
343 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
346 format
= attrib
->format
->id
;
347 /* Look for newly appeared conversion */
348 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
350 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
352 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
354 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
356 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
360 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
362 else if (This
->conversion_map
)
364 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
370 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
371 const struct wined3d_state
*state
, DWORD fixup_flags
)
373 UINT stride_this_run
= 0;
376 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
377 * Once we have our declaration there is no need to look it up again. Index buffers also never need
378 * conversion, so once the (empty) conversion structure is created don't bother checking again
380 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
382 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
387 TRACE("No fixup required.\n");
388 if(This
->conversion_map
)
390 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
391 This
->conversion_map
= NULL
;
399 TRACE("Finding vertex buffer conversion information\n");
400 /* Certain declaration types need some fixups before we can pass them to
401 * opengl. This means D3DCOLOR attributes with fixed function vertex
402 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
403 * GL_ARB_half_float_vertex is not supported.
405 * Note for d3d8 and d3d9:
406 * The vertex buffer FVF doesn't help with finding them, we have to use
407 * the decoded vertex declaration and pick the things that concern the
408 * current buffer. A problem with this is that this can change between
409 * draws, so we have to validate the information and reprocess the buffer
410 * if it changes, and avoid false positives for performance reasons.
411 * WineD3D doesn't even know the vertex buffer any more, it is managed
412 * by the client libraries and passed to SetStreamSource and ProcessVertices
415 * We have to distinguish between vertex shaders and fixed function to
416 * pick the way we access the strided vertex information.
418 * This code sets up a per-byte array with the size of the detected
419 * stride of the arrays in the buffer. For each byte we have a field
420 * that marks the conversion needed on this byte. For example, the
421 * following declaration with fixed function vertex processing:
429 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
430 * [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]
432 * Where in this example map P means 4 component position conversion, 0
433 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
434 * conversion (red / blue swizzle).
436 * If we're doing conversion and the stride changes we have to reconvert
437 * the whole buffer. Note that we do not mind if the semantic changes,
438 * we only care for the conversion type. So if the NORMAL is replaced
439 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
440 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
441 * conversion types depend on the semantic as well, for example a FLOAT4
442 * texcoord needs no conversion while a FLOAT4 positiont needs one
445 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
446 fixup_flags
, &stride_this_run
) || ret
;
447 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
449 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
450 fixup_flags
, &stride_this_run
) || ret
;
451 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
452 fixup_flags
, &stride_this_run
) || ret
;
453 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
454 fixup_flags
, &stride_this_run
) || ret
;
455 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
456 fixup_flags
, &stride_this_run
) || ret
;
457 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
458 fixup_flags
, &stride_this_run
) || ret
;
459 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
460 fixup_flags
, &stride_this_run
) || ret
;
461 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
462 fixup_flags
, &stride_this_run
) || ret
;
463 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
464 fixup_flags
, &stride_this_run
) || ret
;
465 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
466 fixup_flags
, &stride_this_run
) || ret
;
467 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
468 fixup_flags
, &stride_this_run
) || ret
;
469 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
470 fixup_flags
, &stride_this_run
) || ret
;
471 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
472 fixup_flags
, &stride_this_run
) || ret
;
473 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
474 fixup_flags
, &stride_this_run
) || ret
;
476 if (!stride_this_run
&& This
->conversion_map
)
479 if (!ret
) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
480 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
481 This
->conversion_map
= NULL
;
485 if (ret
) TRACE("Conversion information changed\n");
490 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
492 DWORD src_color
= *dst_color
;
494 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
495 * endianness. If we want this to work on big-endian machines as well we
496 * have to consider more things.
498 * 0xff000000: Alpha mask
499 * 0x00ff0000: Blue mask
500 * 0x0000ff00: Green mask
501 * 0x000000ff: Red mask
504 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
505 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
506 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
508 return sizeof(*dst_color
);
511 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
513 /* rhw conversion like in position_float4(). */
514 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
516 float w
= 1.0f
/ p
->w
;
526 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
528 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
530 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
535 static BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
536 struct wined3d_context
*context
, DWORD location
)
540 case WINED3D_LOCATION_SYSMEM
:
541 if (buffer
->resource
.heap_memory
)
544 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
546 ERR("Failed to allocate system memory.\n");
551 case WINED3D_LOCATION_BUFFER
:
552 if (buffer
->buffer_object
)
555 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
557 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
560 return buffer_create_buffer_object(buffer
, context
);
563 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
568 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
569 struct wined3d_context
*context
, DWORD location
)
571 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
573 TRACE("buffer %p, context %p, location %s.\n",
574 buffer
, context
, wined3d_debug_location(location
));
576 if (buffer
->locations
& location
)
578 TRACE("Location (%#x) is already up to date.\n", location
);
582 if (!buffer
->locations
)
584 ERR("Buffer %p does not have any up to date location.\n", buffer
);
585 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
586 return wined3d_buffer_load_location(buffer
, context
, location
);
589 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
591 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
594 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
596 TRACE("Buffer previously discarded, nothing to do.\n");
597 wined3d_buffer_validate_location(buffer
, location
);
598 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
604 case WINED3D_LOCATION_SYSMEM
:
605 buffer_bind(buffer
, context
);
606 GL_EXTCALL(glGetBufferSubData(buffer
->buffer_type_hint
, 0, buffer
->resource
.size
,
607 buffer
->resource
.heap_memory
));
608 checkGLcall("buffer download");
609 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
612 case WINED3D_LOCATION_BUFFER
:
613 FIXME("Not implemented yet.\n");
617 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
621 wined3d_buffer_validate_location(buffer
, location
);
625 /* Context activation is done by the caller. */
626 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
628 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
629 return buffer
->resource
.heap_memory
;
632 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
,
633 struct wined3d_bo_address
*data
, DWORD locations
)
635 TRACE("buffer %p, data %p, locations %s.\n",
636 buffer
, data
, wined3d_debug_location(locations
));
638 if (locations
& WINED3D_LOCATION_BUFFER
)
640 data
->buffer_object
= buffer
->buffer_object
;
642 return WINED3D_LOCATION_BUFFER
;
644 if (locations
& WINED3D_LOCATION_SYSMEM
)
646 data
->buffer_object
= 0;
647 data
->addr
= buffer
->resource
.heap_memory
;
648 return WINED3D_LOCATION_SYSMEM
;
651 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
652 data
->buffer_object
= 0;
657 static void buffer_unload(struct wined3d_resource
*resource
)
659 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
660 DWORD flags
= buffer
->flags
;
662 TRACE("buffer %p.\n", buffer
);
664 if (buffer
->buffer_object
)
666 struct wined3d_context
*context
;
668 context
= context_acquire(resource
->device
, NULL
);
670 /* Download the buffer, but don't permanently enable double buffering. */
671 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
672 if (!(flags
& WINED3D_BUFFER_PIN_SYSMEM
))
673 buffer
->flags
&= ~WINED3D_BUFFER_PIN_SYSMEM
;
675 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
676 buffer_destroy_buffer_object(buffer
, context
);
677 buffer_clear_dirty_areas(buffer
);
679 context_release(context
);
681 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
682 buffer
->conversion_map
= NULL
;
684 buffer
->conversion_stride
= 0;
685 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
688 resource_unload(resource
);
691 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
693 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
694 buffer_unload(&buffer
->resource
);
697 static void wined3d_buffer_destroy_object(void *object
)
699 struct wined3d_buffer
*buffer
= object
;
700 struct wined3d_context
*context
;
702 if (buffer
->buffer_object
)
704 context
= context_acquire(buffer
->resource
.device
, NULL
);
705 buffer_destroy_buffer_object(buffer
, context
);
706 context_release(context
);
708 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
711 HeapFree(GetProcessHeap(), 0, buffer
->maps
);
712 HeapFree(GetProcessHeap(), 0, buffer
);
715 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
717 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
719 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
723 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
724 resource_cleanup(&buffer
->resource
);
725 wined3d_cs_emit_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
731 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
733 TRACE("buffer %p.\n", buffer
);
735 return buffer
->resource
.parent
;
738 /* The caller provides a context and binds the buffer */
739 static void buffer_sync_apple(struct wined3d_buffer
*This
, DWORD flags
, const struct wined3d_gl_info
*gl_info
)
741 enum wined3d_event_query_result ret
;
743 /* No fencing needs to be done if the app promises not to overwrite
745 if (flags
& WINED3D_MAP_NOOVERWRITE
)
748 if (flags
& WINED3D_MAP_DISCARD
)
750 GL_EXTCALL(glBufferData(This
->buffer_type_hint
, This
->resource
.size
, NULL
, This
->buffer_object_usage
));
751 checkGLcall("glBufferData");
757 TRACE("Creating event query for buffer %p\n", This
);
759 if (!wined3d_event_query_supported(gl_info
))
761 FIXME("Event queries not supported, dropping async buffer locks.\n");
765 This
->query
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->query
));
768 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
772 /* Since we don't know about old draws a glFinish is needed once */
773 gl_info
->gl_ops
.gl
.p_glFinish();
776 TRACE("Synchronizing buffer %p\n", This
);
777 ret
= wined3d_event_query_finish(This
->query
, This
->resource
.device
);
780 case WINED3D_EVENT_QUERY_NOT_STARTED
:
781 case WINED3D_EVENT_QUERY_OK
:
785 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
786 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
790 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret
);
797 wined3d_event_query_destroy(This
->query
);
801 gl_info
->gl_ops
.gl
.p_glFinish();
802 GL_EXTCALL(glBufferParameteriAPPLE(This
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
803 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
804 This
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
807 /* Context activation is done by the caller. */
808 static void wined3d_buffer_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
809 const void *data
, unsigned int range_count
, const struct wined3d_map_range
*ranges
)
811 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
812 const struct wined3d_map_range
*range
;
814 buffer_bind(buffer
, context
);
816 while (range_count
--)
818 range
= &ranges
[range_count
];
819 GL_EXTCALL(glBufferSubData(buffer
->buffer_type_hint
,
820 range
->offset
, range
->size
, (BYTE
*)data
+ range
->offset
));
822 checkGLcall("glBufferSubData");
825 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
827 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
830 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
832 /* Now for each vertex in the buffer that needs conversion. */
833 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
835 if (!(data
= HeapAlloc(GetProcessHeap(), 0, buffer
->resource
.size
)))
837 ERR("Out of memory.\n");
841 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
843 start
= buffer
->maps
[range_idx
].offset
;
844 end
= start
+ buffer
->maps
[range_idx
].size
;
846 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
847 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
849 for (j
= 0; j
< buffer
->stride
;)
851 switch (buffer
->conversion_map
[j
])
858 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
861 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
864 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
871 wined3d_buffer_upload_ranges(buffer
, context
, data
, buffer
->modified_areas
, buffer
->maps
);
873 HeapFree(GetProcessHeap(), 0, data
);
876 void buffer_mark_used(struct wined3d_buffer
*buffer
)
878 buffer
->flags
&= ~WINED3D_BUFFER_DISCARD
;
881 /* Context activation is done by the caller. */
882 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
883 const struct wined3d_state
*state
)
885 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
886 BOOL decl_changed
= FALSE
;
888 TRACE("buffer %p.\n", buffer
);
890 if (buffer
->resource
.map_count
)
892 WARN("Buffer is mapped, skipping preload.\n");
896 buffer_mark_used(buffer
);
898 /* TODO: Make converting independent from VBOs */
899 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
901 /* Not doing any conversion */
905 wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
907 /* Reading the declaration makes only sense if we have valid state information
908 * (i.e., if this function is called during draws). */
911 DWORD fixup_flags
= 0;
915 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !context
->d3d_info
->ffp_generic_attributes
)
916 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
917 if (!context
->d3d_info
->xyzrhw
)
918 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
921 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
922 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
925 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
927 ++buffer
->draw_count
;
928 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
929 buffer
->decl_change_count
= 0;
930 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
931 buffer
->full_conversion_count
= 0;
935 /* If applications change the declaration over and over, reconverting all the time is a huge
936 * performance hit. So count the declaration changes and release the VBO if there are too many
937 * of them (and thus stop converting)
941 ++buffer
->decl_change_count
;
942 buffer
->draw_count
= 0;
944 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
945 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
947 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
948 wined3d_buffer_drop_bo(buffer
);
952 /* The declaration changed, reload the whole buffer. */
953 WARN("Reloading buffer because of a vertex declaration change.\n");
954 buffer_invalidate_bo_range(buffer
, 0, 0);
958 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
959 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
960 * decl changes and reset the decl change count after a specific number of them
962 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
964 ++buffer
->full_conversion_count
;
965 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
967 FIXME("Too many full buffer conversions, stopping converting.\n");
968 wined3d_buffer_drop_bo(buffer
);
974 ++buffer
->draw_count
;
975 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
976 buffer
->decl_change_count
= 0;
977 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
978 buffer
->full_conversion_count
= 0;
982 if (!buffer
->conversion_map
)
984 /* That means that there is nothing to fixup. Just upload from
985 * buffer->resource.heap_memory directly into the BO. Do not
986 * free the system memory copy because drawPrimitive may need it if
987 * the stride is 0, for instancing emulation, vertex blending
988 * emulation or shader emulation. */
989 TRACE("No conversion needed.\n");
991 /* Nothing to do because heap memory exists if the buffer is double buffer or has no BO at all. */
992 if (!(buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
))
995 wined3d_buffer_upload_ranges(buffer
, context
, buffer
->resource
.heap_memory
,
996 buffer
->modified_areas
, buffer
->maps
);
1000 buffer_conversion_upload(buffer
, context
);
1002 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1005 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
1007 TRACE("buffer %p.\n", buffer
);
1009 return &buffer
->resource
;
1012 static HRESULT
wined3d_buffer_map(struct wined3d_buffer
*buffer
, UINT offset
, UINT size
, BYTE
**data
, DWORD flags
)
1014 struct wined3d_device
*device
= buffer
->resource
.device
;
1015 struct wined3d_context
*context
;
1019 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer
, offset
, size
, data
, flags
);
1021 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
1022 * fill rate test seems to depend on this. When we map a buffer with
1023 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
1024 * previous contents of the buffer. The r600g driver only does this when
1025 * the buffer is currently in use, while the proprietary NVIDIA driver
1026 * appears to do this unconditionally. */
1027 if (buffer
->flags
& WINED3D_BUFFER_DISCARD
)
1028 flags
&= ~WINED3D_MAP_DISCARD
;
1029 count
= ++buffer
->resource
.map_count
;
1031 if (buffer
->buffer_object
)
1033 unsigned int dirty_offset
= offset
, dirty_size
= size
;
1035 /* DISCARD invalidates the entire buffer, regardless of the specified
1036 * offset and size. Some applications also depend on the entire buffer
1037 * being uploaded in that case. Two such applications are Port Royale
1038 * and Darkstar One. */
1039 if (flags
& WINED3D_MAP_DISCARD
)
1045 if (buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
1047 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1049 context
= context_acquire(device
, NULL
);
1050 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1051 context_release(context
);
1054 if (!(flags
& WINED3D_MAP_READONLY
))
1055 wined3d_buffer_invalidate_range(buffer
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
1059 if (!(flags
& WINED3D_MAP_READONLY
))
1060 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1064 const struct wined3d_gl_info
*gl_info
;
1066 context
= context_acquire(device
, NULL
);
1067 gl_info
= context
->gl_info
;
1069 buffer_bind(buffer
, context
);
1071 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1073 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
1074 buffer
->map_ptr
= GL_EXTCALL(glMapBufferRange(buffer
->buffer_type_hint
,
1075 0, buffer
->resource
.size
, mapflags
));
1076 checkGLcall("glMapBufferRange");
1080 if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1081 buffer_sync_apple(buffer
, flags
, gl_info
);
1082 buffer
->map_ptr
= GL_EXTCALL(glMapBuffer(buffer
->buffer_type_hint
,
1084 checkGLcall("glMapBuffer");
1087 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1089 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1091 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1092 checkGLcall("glUnmapBuffer");
1093 buffer
->map_ptr
= NULL
;
1095 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1097 /* The extra copy is more expensive than not using VBOs at
1098 * all on the Nvidia Linux driver, which is the only driver
1099 * that returns unaligned pointers.
1101 TRACE("Dynamic buffer, dropping VBO.\n");
1102 wined3d_buffer_drop_bo(buffer
);
1106 TRACE("Falling back to doublebuffered operation.\n");
1107 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1109 TRACE("New pointer is %p.\n", buffer
->resource
.heap_memory
);
1110 buffer
->map_ptr
= NULL
;
1112 context_release(context
);
1116 if (flags
& WINED3D_MAP_DISCARD
)
1117 buffer
->flags
|= WINED3D_BUFFER_DISCARD
;
1120 base
= buffer
->map_ptr
? buffer
->map_ptr
: buffer
->resource
.heap_memory
;
1121 *data
= base
+ offset
;
1123 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1124 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1129 static void wined3d_buffer_unmap(struct wined3d_buffer
*buffer
)
1133 TRACE("buffer %p.\n", buffer
);
1135 /* In the case that the number of Unmap calls > the
1136 * number of Map calls, d3d returns always D3D_OK.
1137 * This is also needed to prevent Map from returning garbage on
1138 * the next call (this will happen if the lock_count is < 0). */
1139 if (!buffer
->resource
.map_count
)
1141 WARN("Unmap called without a previous map call.\n");
1145 if (--buffer
->resource
.map_count
)
1147 /* Delay loading the buffer until everything is unlocked */
1148 TRACE("Ignoring unmap.\n");
1152 if (!(buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
) && buffer
->buffer_object
)
1154 struct wined3d_device
*device
= buffer
->resource
.device
;
1155 const struct wined3d_gl_info
*gl_info
;
1156 struct wined3d_context
*context
;
1158 context
= context_acquire(device
, NULL
);
1159 gl_info
= context
->gl_info
;
1161 buffer_bind(buffer
, context
);
1163 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1165 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1167 GL_EXTCALL(glFlushMappedBufferRange(buffer
->buffer_type_hint
,
1168 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1169 checkGLcall("glFlushMappedBufferRange");
1172 else if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1174 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1176 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer
->buffer_type_hint
,
1177 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1178 checkGLcall("glFlushMappedBufferRangeAPPLE");
1182 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1183 if (wined3d_settings
.strict_draw_ordering
)
1184 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
1185 context_release(context
);
1187 buffer_clear_dirty_areas(buffer
);
1188 buffer
->map_ptr
= NULL
;
1190 else if (buffer
->flags
& WINED3D_BUFFER_HASDESC
)
1192 wined3d_resource_preload(&buffer
->resource
);
1196 HRESULT
wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1197 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1199 const struct wined3d_gl_info
*gl_info
;
1200 struct wined3d_bo_address dst
, src
;
1201 struct wined3d_context
*context
;
1202 struct wined3d_device
*device
;
1203 BYTE
*dst_ptr
, *src_ptr
;
1207 buffer_mark_used(dst_buffer
);
1208 buffer_mark_used(src_buffer
);
1210 device
= dst_buffer
->resource
.device
;
1212 context
= context_acquire(device
, NULL
);
1213 gl_info
= context
->gl_info
;
1215 dst_location
= wined3d_buffer_get_memory(dst_buffer
, &dst
, dst_buffer
->locations
);
1216 wined3d_buffer_get_memory(src_buffer
, &src
, src_buffer
->locations
);
1218 if (dst
.buffer_object
&& src
.buffer_object
)
1220 if (gl_info
->supported
[ARB_COPY_BUFFER
])
1222 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src
.buffer_object
));
1223 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst
.buffer_object
));
1224 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
1225 src_offset
, dst_offset
, size
));
1226 checkGLcall("direct buffer copy");
1230 if (FAILED(hr
= wined3d_buffer_map(dst_buffer
, dst_offset
, size
, &dst_ptr
, 0)))
1232 WARN("Failed to map dst_buffer, hr %#x.\n", hr
);
1233 context_release(context
);
1234 return WINED3DERR_INVALIDCALL
;
1236 if (FAILED(hr
= wined3d_buffer_map(src_buffer
, src_offset
, size
, &src_ptr
, WINED3D_MAP_READONLY
)))
1238 WARN("Failed to map src_buffer, hr %#x.\n", hr
);
1239 wined3d_buffer_unmap(dst_buffer
);
1240 context_release(context
);
1241 return WINED3DERR_INVALIDCALL
;
1244 memcpy(dst_ptr
, src_ptr
, size
);
1246 wined3d_buffer_unmap(src_buffer
);
1247 wined3d_buffer_unmap(dst_buffer
);
1250 else if (!dst
.buffer_object
&& src
.buffer_object
)
1252 buffer_bind(src_buffer
, context
);
1253 GL_EXTCALL(glGetBufferSubData(src_buffer
->buffer_type_hint
, src_offset
, size
, dst
.addr
+ dst_offset
));
1254 checkGLcall("buffer download");
1256 else if (dst
.buffer_object
&& !src
.buffer_object
)
1258 buffer_bind(dst_buffer
, context
);
1259 GL_EXTCALL(glBufferSubData(dst_buffer
->buffer_type_hint
, dst_offset
, size
, src
.addr
+ src_offset
));
1260 checkGLcall("buffer upload");
1264 memcpy(dst
.addr
+ dst_offset
, src
.addr
+ src_offset
, size
);
1267 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1269 context_release(context
);
1273 HRESULT
wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
,
1274 const struct wined3d_box
*box
, const void *data
)
1283 size
= box
->right
- box
->left
;
1288 size
= buffer
->resource
.size
;
1291 if (FAILED(hr
= wined3d_buffer_map(buffer
, offset
, size
, &ptr
, 0)))
1294 memcpy(ptr
, data
, size
);
1296 wined3d_buffer_unmap(buffer
);
1300 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1302 return wined3d_buffer_incref(buffer_from_resource(resource
));
1305 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1307 return wined3d_buffer_decref(buffer_from_resource(resource
));
1310 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1312 struct wined3d_context
*context
;
1314 context
= context_acquire(resource
->device
, NULL
);
1315 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1316 context_release(context
);
1319 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1320 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1322 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1325 if (sub_resource_idx
)
1327 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1328 return E_INVALIDARG
;
1334 size
= box
->right
- box
->left
;
1341 map_desc
->row_pitch
= map_desc
->slice_pitch
= buffer
->desc
.byte_width
;
1342 return wined3d_buffer_map(buffer
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1345 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1347 if (sub_resource_idx
)
1349 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1350 return E_INVALIDARG
;
1353 wined3d_buffer_unmap(buffer_from_resource(resource
));
1357 static const struct wined3d_resource_ops buffer_resource_ops
=
1359 buffer_resource_incref
,
1360 buffer_resource_decref
,
1361 buffer_resource_preload
,
1363 buffer_resource_sub_resource_map
,
1364 buffer_resource_sub_resource_unmap
,
1367 static GLenum
buffer_type_hint_from_bind_flags(unsigned int bind_flags
)
1369 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
1370 return GL_ELEMENT_ARRAY_BUFFER
;
1372 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1373 return GL_UNIFORM_BUFFER
;
1375 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
1376 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
1378 return GL_ARRAY_BUFFER
;
1381 static HRESULT
buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1382 UINT size
, DWORD usage
, enum wined3d_format_id format_id
, enum wined3d_pool pool
, unsigned int bind_flags
,
1383 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1385 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1386 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, usage
);
1387 BOOL dynamic_buffer_ok
;
1392 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL.\n");
1393 return WINED3DERR_INVALIDCALL
;
1396 if (data
&& !data
->data
)
1398 WARN("Invalid sub-resource data specified.\n");
1399 return E_INVALIDARG
;
1402 hr
= resource_init(&buffer
->resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1403 WINED3D_MULTISAMPLE_NONE
, 0, usage
, pool
, size
, 1, 1, size
, parent
, parent_ops
, &buffer_resource_ops
);
1406 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1409 buffer
->buffer_type_hint
= buffer_type_hint_from_bind_flags(bind_flags
);
1410 buffer
->bind_flags
= bind_flags
;
1411 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1413 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1414 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
,
1415 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
);
1417 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
|| pool
== WINED3D_POOL_MANAGED
)
1419 /* SWvp and managed buffers always return the same pointer in buffer
1420 * maps and retain data in DISCARD maps. Keep a system memory copy of
1421 * the buffer to provide the same behavior to the application. */
1422 TRACE("Using doublebuffer mode.\n");
1423 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1426 /* Observations show that draw_primitive_immediate_mode() is faster on
1427 * dynamic vertex buffers than converting + draw_primitive_arrays().
1428 * (Half-Life 2 and others.) */
1429 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1431 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1433 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1435 else if (buffer
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
1437 TRACE("Not creating a BO because the buffer is in system memory.\n");
1439 else if (!dynamic_buffer_ok
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
1441 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1445 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1448 if (!(buffer
->maps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
->maps
))))
1450 ERR("Out of memory.\n");
1451 buffer_unload(&buffer
->resource
);
1452 resource_cleanup(&buffer
->resource
);
1453 wined3d_resource_wait_idle(&buffer
->resource
);
1454 return E_OUTOFMEMORY
;
1456 buffer
->maps_size
= 1;
1459 wined3d_device_update_sub_resource(device
, &buffer
->resource
,
1460 0, NULL
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1465 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1466 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1467 struct wined3d_buffer
**buffer
)
1469 struct wined3d_buffer
*object
;
1472 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1473 device
, desc
, data
, parent
, parent_ops
, buffer
);
1475 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1477 return E_OUTOFMEMORY
;
1479 FIXME("Ignoring access flags (pool).\n");
1481 hr
= buffer_init(object
, device
, desc
->byte_width
, desc
->usage
, WINED3DFMT_UNKNOWN
,
1482 WINED3D_POOL_MANAGED
, desc
->bind_flags
, data
, parent
, parent_ops
);
1485 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1486 HeapFree(GetProcessHeap(), 0, object
);
1489 object
->desc
= *desc
;
1491 TRACE("Created buffer %p.\n", object
);
1498 HRESULT CDECL
wined3d_buffer_create_vb(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1499 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1501 struct wined3d_buffer
*object
;
1504 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1505 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1507 if (pool
== WINED3D_POOL_SCRATCH
)
1509 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1510 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1511 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1513 return WINED3DERR_INVALIDCALL
;
1516 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1520 return WINED3DERR_OUTOFVIDEOMEMORY
;
1523 hr
= buffer_init(object
, device
, size
, usage
, WINED3DFMT_UNKNOWN
,
1524 pool
, WINED3D_BIND_VERTEX_BUFFER
, NULL
, parent
, parent_ops
);
1527 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1528 HeapFree(GetProcessHeap(), 0, object
);
1532 TRACE("Created buffer %p.\n", object
);
1538 HRESULT CDECL
wined3d_buffer_create_ib(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1539 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1541 struct wined3d_buffer
*object
;
1544 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1545 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1547 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1551 return WINED3DERR_OUTOFVIDEOMEMORY
;
1554 hr
= buffer_init(object
, device
, size
, usage
| WINED3DUSAGE_STATICDECL
,
1555 WINED3DFMT_UNKNOWN
, pool
, WINED3D_BIND_INDEX_BUFFER
, NULL
,
1556 parent
, parent_ops
);
1559 WARN("Failed to initialize buffer, hr %#x\n", hr
);
1560 HeapFree(GetProcessHeap(), 0, object
);
1564 TRACE("Created buffer %p.\n", object
);