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_APPLESYNC 0x20 /* Using sync as in GL_APPLE_flush_buffer_range. */
39 #define VB_MAXDECLCHANGES 100 /* After that number of decl changes we stop converting */
40 #define VB_RESETDECLCHANGE 1000 /* Reset the decl changecount after that number of draws */
41 #define VB_MAXFULLCONVERSIONS 5 /* Number of full conversions before we stop converting */
42 #define VB_RESETFULLCONVS 20 /* Reset full conversion counts after that number of draws */
44 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
46 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
49 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
51 WARN("Invalid range specified, invalidating entire buffer.\n");
55 if (buffer
->modified_areas
>= buffer
->maps_size
)
57 struct wined3d_map_range
*new;
59 if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer
->maps
, 2 * buffer
->maps_size
* sizeof(*buffer
->maps
))))
61 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
66 buffer
->maps_size
*= 2;
69 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
70 buffer
->maps
[buffer
->modified_areas
].size
= size
;
71 ++buffer
->modified_areas
;
75 buffer
->modified_areas
= 1;
76 buffer
->maps
[0].offset
= 0;
77 buffer
->maps
[0].size
= buffer
->resource
.size
;
80 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
82 This
->modified_areas
= 0;
85 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
87 return !!buffer
->modified_areas
;
90 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
92 return buffer
->modified_areas
== 1
93 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
96 /* Context activation is done by the caller. */
97 static void buffer_bind(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
99 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
101 if (buffer
->buffer_type_hint
== GL_ELEMENT_ARRAY_BUFFER
)
102 context_invalidate_state(context
, STATE_INDEXBUFFER
);
104 GL_EXTCALL(glBindBuffer(buffer
->buffer_type_hint
, buffer
->buffer_object
));
107 /* Context activation is done by the caller */
108 static void delete_gl_buffer(struct wined3d_buffer
*This
, const struct wined3d_gl_info
*gl_info
)
110 if(!This
->buffer_object
) return;
112 GL_EXTCALL(glDeleteBuffers(1, &This
->buffer_object
));
113 checkGLcall("glDeleteBuffers");
114 This
->buffer_object
= 0;
118 wined3d_event_query_destroy(This
->query
);
121 This
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
124 /* Context activation is done by the caller. */
125 static void buffer_create_buffer_object(struct wined3d_buffer
*This
, struct wined3d_context
*context
)
127 GLenum gl_usage
= GL_STATIC_DRAW_ARB
;
129 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
131 TRACE("Creating an OpenGL vertex buffer object for wined3d_buffer %p with usage %s.\n",
132 This
, debug_d3dusage(This
->resource
.usage
));
134 /* Make sure that the gl error is cleared. Do not use checkGLcall
135 * here because checkGLcall just prints a fixme and continues. However,
136 * if an error during VBO creation occurs we can fall back to non-vbo operation
137 * with full functionality(but performance loss)
139 while (gl_info
->gl_ops
.gl
.p_glGetError() != GL_NO_ERROR
);
141 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
142 * The vertex declaration from the device determines how the data in the
143 * buffer is interpreted. This means that on each draw call the buffer has
144 * to be verified to check if the rhw and color values are in the correct
147 GL_EXTCALL(glGenBuffers(1, &This
->buffer_object
));
148 error
= gl_info
->gl_ops
.gl
.p_glGetError();
149 if (!This
->buffer_object
|| error
!= GL_NO_ERROR
)
151 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error
), error
);
155 buffer_bind(This
, context
);
156 error
= gl_info
->gl_ops
.gl
.p_glGetError();
157 if (error
!= GL_NO_ERROR
)
159 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error
), error
);
163 if (This
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
165 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
166 gl_usage
= GL_STREAM_DRAW_ARB
;
168 if(gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
170 GL_EXTCALL(glBufferParameteriAPPLE(This
->buffer_type_hint
, GL_BUFFER_FLUSHING_UNMAP_APPLE
, GL_FALSE
));
171 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
172 GL_EXTCALL(glBufferParameteriAPPLE(This
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_FALSE
));
173 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
174 This
->flags
|= WINED3D_BUFFER_APPLESYNC
;
176 /* No setup is needed here for GL_ARB_map_buffer_range */
179 /* Reserve memory for the buffer. The amount of data won't change
180 * so we are safe with calling glBufferData once and
181 * calling glBufferSubData on updates. Upload the actual data in case
182 * we're not double buffering, so we can release the heap mem afterwards
184 GL_EXTCALL(glBufferData(This
->buffer_type_hint
, This
->resource
.size
, This
->resource
.heap_memory
, gl_usage
));
185 error
= gl_info
->gl_ops
.gl
.p_glGetError();
186 if (error
!= GL_NO_ERROR
)
188 ERR("glBufferData failed with error %s (%#x)\n", debug_glerror(error
), error
);
192 This
->buffer_object_usage
= gl_usage
;
194 if (This
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
)
195 buffer_invalidate_bo_range(This
, 0, 0);
197 wined3d_resource_free_sysmem(&This
->resource
);
202 /* Clean up all VBO init, but continue because we can work without a VBO :-) */
203 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
204 This
->flags
&= ~WINED3D_BUFFER_USE_BO
;
205 delete_gl_buffer(This
, gl_info
);
206 buffer_clear_dirty_areas(This
);
209 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
210 const enum wined3d_buffer_conversion_type conversion_type
,
211 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
213 const struct wined3d_format
*format
= attrib
->format
;
218 /* Check for some valid situations which cause us pain. One is if the buffer is used for
219 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
220 * with different strides. In the 2nd case we might have to drop conversion entirely,
221 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
225 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
226 debug_d3dformat(format
->id
));
228 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
230 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
234 *stride_this_run
= attrib
->stride
;
235 if (buffer
->stride
!= *stride_this_run
)
237 /* We rely that this happens only on the first converted attribute that is found,
238 * if at all. See above check
240 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
241 buffer
->stride
= *stride_this_run
;
242 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY
, buffer
->conversion_map
);
243 buffer
->conversion_map
= wined3d_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
248 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
249 for (i
= 0; i
< format
->attribute_size
; ++i
)
251 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
252 if (buffer
->conversion_map
[idx
] != conversion_type
)
254 TRACE("Byte %lu in vertex changed:\n", idx
);
255 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
257 buffer
->conversion_map
[idx
] = conversion_type
;
264 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
265 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
267 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
268 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
270 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
271 enum wined3d_format_id format
;
274 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
275 * there, on nonexistent attribs the vbo is 0.
277 if (!(si
->use_map
& (1u << attrib_idx
))
278 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
281 format
= attrib
->format
->id
;
282 /* Look for newly appeared conversion */
283 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
285 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
287 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
289 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
291 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
295 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
297 else if (This
->conversion_map
)
299 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
305 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
306 const struct wined3d_state
*state
, DWORD fixup_flags
)
308 UINT stride_this_run
= 0;
311 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
312 * Once we have our declaration there is no need to look it up again. Index buffers also never need
313 * conversion, so once the (empty) conversion structure is created don't bother checking again
315 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
317 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
322 TRACE("No fixup required.\n");
323 if(This
->conversion_map
)
325 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
326 This
->conversion_map
= NULL
;
334 TRACE("Finding vertex buffer conversion information\n");
335 /* Certain declaration types need some fixups before we can pass them to
336 * opengl. This means D3DCOLOR attributes with fixed function vertex
337 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
338 * GL_ARB_half_float_vertex is not supported.
340 * Note for d3d8 and d3d9:
341 * The vertex buffer FVF doesn't help with finding them, we have to use
342 * the decoded vertex declaration and pick the things that concern the
343 * current buffer. A problem with this is that this can change between
344 * draws, so we have to validate the information and reprocess the buffer
345 * if it changes, and avoid false positives for performance reasons.
346 * WineD3D doesn't even know the vertex buffer any more, it is managed
347 * by the client libraries and passed to SetStreamSource and ProcessVertices
350 * We have to distinguish between vertex shaders and fixed function to
351 * pick the way we access the strided vertex information.
353 * This code sets up a per-byte array with the size of the detected
354 * stride of the arrays in the buffer. For each byte we have a field
355 * that marks the conversion needed on this byte. For example, the
356 * following declaration with fixed function vertex processing:
364 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
365 * [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]
367 * Where in this example map P means 4 component position conversion, 0
368 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
369 * conversion (red / blue swizzle).
371 * If we're doing conversion and the stride changes we have to reconvert
372 * the whole buffer. Note that we do not mind if the semantic changes,
373 * we only care for the conversion type. So if the NORMAL is replaced
374 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
375 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
376 * conversion types depend on the semantic as well, for example a FLOAT4
377 * texcoord needs no conversion while a FLOAT4 positiont needs one
380 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
381 fixup_flags
, &stride_this_run
) || ret
;
382 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
384 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
385 fixup_flags
, &stride_this_run
) || ret
;
386 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
387 fixup_flags
, &stride_this_run
) || ret
;
388 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
389 fixup_flags
, &stride_this_run
) || ret
;
390 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
391 fixup_flags
, &stride_this_run
) || ret
;
392 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
393 fixup_flags
, &stride_this_run
) || ret
;
394 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
395 fixup_flags
, &stride_this_run
) || ret
;
396 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
397 fixup_flags
, &stride_this_run
) || ret
;
398 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
399 fixup_flags
, &stride_this_run
) || ret
;
400 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
401 fixup_flags
, &stride_this_run
) || ret
;
402 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
403 fixup_flags
, &stride_this_run
) || ret
;
404 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
405 fixup_flags
, &stride_this_run
) || ret
;
406 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
407 fixup_flags
, &stride_this_run
) || ret
;
408 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
409 fixup_flags
, &stride_this_run
) || ret
;
411 if (!stride_this_run
&& This
->conversion_map
)
414 if (!ret
) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
415 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
416 This
->conversion_map
= NULL
;
420 if (ret
) TRACE("Conversion information changed\n");
425 static inline void fixup_d3dcolor(DWORD
*dst_color
)
427 DWORD src_color
= *dst_color
;
429 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
430 * endianness. If we want this to work on big-endian machines as well we
431 * have to consider more things.
433 * 0xff000000: Alpha mask
434 * 0x00ff0000: Blue mask
435 * 0x0000ff00: Green mask
436 * 0x000000ff: Red mask
439 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
440 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
441 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
444 static inline void fixup_transformed_pos(float *p
)
446 /* rhw conversion like in position_float4(). */
447 if (p
[3] != 1.0f
&& p
[3] != 0.0f
)
449 float w
= 1.0f
/ p
[3];
457 /* Context activation is done by the caller. */
458 void buffer_get_memory(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
459 struct wined3d_bo_address
*data
)
461 data
->buffer_object
= buffer
->buffer_object
;
462 if (!buffer
->buffer_object
)
464 if ((buffer
->flags
& WINED3D_BUFFER_USE_BO
) && !buffer
->resource
.map_count
)
466 buffer_create_buffer_object(buffer
, context
);
467 if (buffer
->buffer_object
)
469 data
->buffer_object
= buffer
->buffer_object
;
474 data
->addr
= buffer
->resource
.heap_memory
;
482 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
484 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
486 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
491 /* Context activation is done by the caller. */
492 BYTE
*buffer_get_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
494 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
496 /* Heap_memory exists if the buffer is double buffered or has no buffer object at all. */
497 if (buffer
->resource
.heap_memory
)
498 return buffer
->resource
.heap_memory
;
500 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
501 ERR("Failed to allocate system memory.\n");
503 buffer_bind(buffer
, context
);
504 GL_EXTCALL(glGetBufferSubData(buffer
->buffer_type_hint
, 0, buffer
->resource
.size
, buffer
->resource
.heap_memory
));
505 checkGLcall("buffer download");
506 buffer
->flags
|= WINED3D_BUFFER_DOUBLEBUFFER
;
508 return buffer
->resource
.heap_memory
;
511 static void buffer_unload(struct wined3d_resource
*resource
)
513 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
515 TRACE("buffer %p.\n", buffer
);
517 if (buffer
->buffer_object
)
519 struct wined3d_device
*device
= resource
->device
;
520 struct wined3d_context
*context
;
522 context
= context_acquire(device
, NULL
);
524 /* Download the buffer, but don't permanently enable double buffering */
525 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
527 buffer_get_sysmem(buffer
, context
);
528 buffer
->flags
&= ~WINED3D_BUFFER_DOUBLEBUFFER
;
531 delete_gl_buffer(buffer
, context
->gl_info
);
532 buffer_clear_dirty_areas(buffer
);
534 context_release(context
);
536 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
537 buffer
->conversion_map
= NULL
;
539 buffer
->conversion_stride
= 0;
540 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
542 /* The stream source state handler might have read the memory of the
543 * vertex buffer already and got the memory in the vbo which is not
544 * valid any longer. Dirtify the stream source to force a reload. This
545 * happens only once per changed vertexbuffer and should occur rather
547 if (resource
->bind_count
)
548 device_invalidate_state(device
, STATE_STREAMSRC
);
551 resource_unload(resource
);
554 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
556 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
557 buffer_unload(&buffer
->resource
);
560 static void wined3d_buffer_destroy_object(void *object
)
562 struct wined3d_buffer
*buffer
= object
;
563 struct wined3d_context
*context
;
565 if (buffer
->buffer_object
)
567 context
= context_acquire(buffer
->resource
.device
, NULL
);
568 delete_gl_buffer(buffer
, context
->gl_info
);
569 context_release(context
);
571 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
574 HeapFree(GetProcessHeap(), 0, buffer
->maps
);
575 HeapFree(GetProcessHeap(), 0, buffer
);
578 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
580 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
582 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
586 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
587 resource_cleanup(&buffer
->resource
);
588 wined3d_cs_emit_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
594 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
596 TRACE("buffer %p.\n", buffer
);
598 return buffer
->resource
.parent
;
601 /* The caller provides a context and binds the buffer */
602 static void buffer_sync_apple(struct wined3d_buffer
*This
, DWORD flags
, const struct wined3d_gl_info
*gl_info
)
604 enum wined3d_event_query_result ret
;
606 /* No fencing needs to be done if the app promises not to overwrite
608 if (flags
& WINED3D_MAP_NOOVERWRITE
)
611 if (flags
& WINED3D_MAP_DISCARD
)
613 GL_EXTCALL(glBufferData(This
->buffer_type_hint
, This
->resource
.size
, NULL
, This
->buffer_object_usage
));
614 checkGLcall("glBufferData");
620 TRACE("Creating event query for buffer %p\n", This
);
622 if (!wined3d_event_query_supported(gl_info
))
624 FIXME("Event queries not supported, dropping async buffer locks.\n");
628 This
->query
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->query
));
631 ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
635 /* Since we don't know about old draws a glFinish is needed once */
636 gl_info
->gl_ops
.gl
.p_glFinish();
639 TRACE("Synchronizing buffer %p\n", This
);
640 ret
= wined3d_event_query_finish(This
->query
, This
->resource
.device
);
643 case WINED3D_EVENT_QUERY_NOT_STARTED
:
644 case WINED3D_EVENT_QUERY_OK
:
648 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
649 WARN("Cannot synchronize buffer lock due to a thread conflict\n");
653 ERR("wined3d_event_query_finish returned %u, dropping async buffer locks\n", ret
);
660 wined3d_event_query_destroy(This
->query
);
664 gl_info
->gl_ops
.gl
.p_glFinish();
665 GL_EXTCALL(glBufferParameteriAPPLE(This
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
666 checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
667 This
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
670 /* The caller provides a GL context */
671 static void buffer_direct_upload(struct wined3d_buffer
*This
, const struct wined3d_gl_info
*gl_info
, DWORD flags
)
676 /* This potentially invalidates the element array buffer binding, but the
677 * caller always takes care of this. */
678 GL_EXTCALL(glBindBuffer(This
->buffer_type_hint
, This
->buffer_object
));
679 checkGLcall("glBindBuffer");
680 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
683 mapflags
= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
684 if (flags
& WINED3D_BUFFER_DISCARD
)
685 mapflags
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
686 else if (!(flags
& WINED3D_BUFFER_SYNC
))
687 mapflags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
688 map
= GL_EXTCALL(glMapBufferRange(This
->buffer_type_hint
, 0,
689 This
->resource
.size
, mapflags
));
690 checkGLcall("glMapBufferRange");
694 if (This
->flags
& WINED3D_BUFFER_APPLESYNC
)
697 if (flags
& WINED3D_BUFFER_DISCARD
)
698 syncflags
|= WINED3D_MAP_DISCARD
;
699 else if (!(flags
& WINED3D_BUFFER_SYNC
))
700 syncflags
|= WINED3D_MAP_NOOVERWRITE
;
701 buffer_sync_apple(This
, syncflags
, gl_info
);
703 map
= GL_EXTCALL(glMapBuffer(This
->buffer_type_hint
, GL_WRITE_ONLY
));
704 checkGLcall("glMapBuffer");
708 ERR("Failed to map opengl buffer\n");
712 while (This
->modified_areas
)
714 This
->modified_areas
--;
715 start
= This
->maps
[This
->modified_areas
].offset
;
716 len
= This
->maps
[This
->modified_areas
].size
;
718 memcpy(map
+ start
, (BYTE
*)This
->resource
.heap_memory
+ start
, len
);
720 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
722 GL_EXTCALL(glFlushMappedBufferRange(This
->buffer_type_hint
, start
, len
));
723 checkGLcall("glFlushMappedBufferRange");
725 else if (This
->flags
& WINED3D_BUFFER_APPLESYNC
)
727 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(This
->buffer_type_hint
, start
, len
));
728 checkGLcall("glFlushMappedBufferRangeAPPLE");
731 GL_EXTCALL(glUnmapBuffer(This
->buffer_type_hint
));
732 checkGLcall("glUnmapBuffer");
735 void buffer_mark_used(struct wined3d_buffer
*buffer
)
737 buffer
->flags
&= ~(WINED3D_BUFFER_SYNC
| WINED3D_BUFFER_DISCARD
);
740 /* Context activation is done by the caller. */
741 void buffer_internal_preload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
742 const struct wined3d_state
*state
)
744 DWORD flags
= buffer
->flags
& (WINED3D_BUFFER_SYNC
| WINED3D_BUFFER_DISCARD
);
745 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
746 struct wined3d_device
*device
= buffer
->resource
.device
;
747 UINT start
, end
, len
, vertices
;
748 BOOL decl_changed
= FALSE
;
752 TRACE("buffer %p.\n", buffer
);
754 if (buffer
->resource
.map_count
)
756 WARN("Buffer is mapped, skipping preload.\n");
760 buffer_mark_used(buffer
);
762 if (!buffer
->buffer_object
)
764 /* TODO: Make converting independent from VBOs */
765 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
767 buffer_create_buffer_object(buffer
, context
);
771 /* Not doing any conversion */
776 /* Reading the declaration makes only sense if we have valid state information
777 * (i.e., if this function is called during draws). */
780 DWORD fixup_flags
= 0;
784 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !context
->d3d_info
->ffp_generic_attributes
)
785 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
786 if (!context
->d3d_info
->xyzrhw
)
787 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
790 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
791 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
794 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
796 ++buffer
->draw_count
;
797 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
798 buffer
->decl_change_count
= 0;
799 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
800 buffer
->full_conversion_count
= 0;
804 /* If applications change the declaration over and over, reconverting all the time is a huge
805 * performance hit. So count the declaration changes and release the VBO if there are too many
806 * of them (and thus stop converting)
810 ++buffer
->decl_change_count
;
811 buffer
->draw_count
= 0;
813 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
814 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
816 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
817 wined3d_buffer_drop_bo(buffer
);
821 /* The declaration changed, reload the whole buffer. */
822 WARN("Reloading buffer because of a vertex declaration change.\n");
823 buffer_invalidate_bo_range(buffer
, 0, 0);
825 /* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
826 * cleared for unsynchronized updates
832 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
833 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
834 * decl changes and reset the decl change count after a specific number of them
836 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
838 ++buffer
->full_conversion_count
;
839 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
841 FIXME("Too many full buffer conversions, stopping converting.\n");
842 wined3d_buffer_drop_bo(buffer
);
848 ++buffer
->draw_count
;
849 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
850 buffer
->decl_change_count
= 0;
851 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
852 buffer
->full_conversion_count
= 0;
856 if (buffer
->buffer_type_hint
== GL_ELEMENT_ARRAY_BUFFER_ARB
)
857 device_invalidate_state(device
, STATE_INDEXBUFFER
);
859 if (!buffer
->conversion_map
)
861 /* That means that there is nothing to fixup. Just upload from
862 * buffer->resource.heap_memory directly into the vbo. Do not
863 * free the system memory copy because drawPrimitive may need it if
864 * the stride is 0, for instancing emulation, vertex blending
865 * emulation or shader emulation. */
866 TRACE("No conversion needed.\n");
868 /* Nothing to do because we locked directly into the vbo */
869 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
874 buffer_direct_upload(buffer
, gl_info
, flags
);
879 if(!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
881 buffer_get_sysmem(buffer
, context
);
884 /* Now for each vertex in the buffer that needs conversion */
885 vertices
= buffer
->resource
.size
/ buffer
->stride
;
887 data
= HeapAlloc(GetProcessHeap(), 0, buffer
->resource
.size
);
889 while(buffer
->modified_areas
)
891 buffer
->modified_areas
--;
892 start
= buffer
->maps
[buffer
->modified_areas
].offset
;
893 len
= buffer
->maps
[buffer
->modified_areas
].size
;
896 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
897 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertices
); ++i
)
899 for (j
= 0; j
< buffer
->stride
; ++j
)
901 switch (buffer
->conversion_map
[j
])
908 fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
913 fixup_transformed_pos((float *) (data
+ i
* buffer
->stride
+ j
));
917 FIXME("Unimplemented conversion %d in shifted conversion\n", buffer
->conversion_map
[j
]);
922 GL_EXTCALL(glBindBuffer(buffer
->buffer_type_hint
, buffer
->buffer_object
));
923 checkGLcall("glBindBuffer");
924 GL_EXTCALL(glBufferSubData(buffer
->buffer_type_hint
, start
, len
, data
+ start
));
925 checkGLcall("glBufferSubData");
928 HeapFree(GetProcessHeap(), 0, data
);
931 void CDECL
wined3d_buffer_preload(struct wined3d_buffer
*buffer
)
933 struct wined3d_context
*context
;
934 context
= context_acquire(buffer
->resource
.device
, NULL
);
935 buffer_internal_preload(buffer
, context
, NULL
);
936 context_release(context
);
939 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
941 TRACE("buffer %p.\n", buffer
);
943 return &buffer
->resource
;
946 HRESULT CDECL
wined3d_buffer_map(struct wined3d_buffer
*buffer
, UINT offset
, UINT size
, BYTE
**data
, DWORD flags
)
951 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer
, offset
, size
, data
, flags
);
953 flags
= wined3d_resource_sanitize_map_flags(&buffer
->resource
, flags
);
954 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001 multitexture
955 * fill rate test seems to depend on this. When we map a buffer with
956 * GL_MAP_INVALIDATE_BUFFER_BIT, the driver is free to discard the
957 * previous contents of the buffer. The r600g driver only does this when
958 * the buffer is currently in use, while the proprietary NVIDIA driver
959 * appears to do this unconditionally. */
960 if (buffer
->flags
& WINED3D_BUFFER_DISCARD
)
961 flags
&= ~WINED3D_MAP_DISCARD
;
962 count
= ++buffer
->resource
.map_count
;
964 if (buffer
->buffer_object
)
966 /* DISCARD invalidates the entire buffer, regardless of the specified
967 * offset and size. Some applications also depend on the entire buffer
968 * being uploaded in that case. Two such applications are Port Royale
969 * and Darkstar One. */
970 if (flags
& WINED3D_MAP_DISCARD
)
971 buffer_invalidate_bo_range(buffer
, 0, 0);
972 else if (!(flags
& WINED3D_MAP_READONLY
))
973 buffer_invalidate_bo_range(buffer
, offset
, size
);
975 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
))
979 struct wined3d_device
*device
= buffer
->resource
.device
;
980 struct wined3d_context
*context
;
981 const struct wined3d_gl_info
*gl_info
;
983 context
= context_acquire(device
, NULL
);
984 gl_info
= context
->gl_info
;
986 buffer_bind(buffer
, context
);
988 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
990 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
991 buffer
->map_ptr
= GL_EXTCALL(glMapBufferRange(buffer
->buffer_type_hint
,
992 0, buffer
->resource
.size
, mapflags
));
993 checkGLcall("glMapBufferRange");
997 if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
998 buffer_sync_apple(buffer
, flags
, gl_info
);
999 buffer
->map_ptr
= GL_EXTCALL(glMapBuffer(buffer
->buffer_type_hint
,
1001 checkGLcall("glMapBuffer");
1004 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1006 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1008 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1009 checkGLcall("glUnmapBuffer");
1010 buffer
->map_ptr
= NULL
;
1012 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1014 /* The extra copy is more expensive than not using VBOs at
1015 * all on the Nvidia Linux driver, which is the only driver
1016 * that returns unaligned pointers.
1018 TRACE("Dynamic buffer, dropping VBO.\n");
1019 wined3d_buffer_drop_bo(buffer
);
1023 TRACE("Falling back to doublebuffered operation.\n");
1024 buffer_get_sysmem(buffer
, context
);
1026 TRACE("New pointer is %p.\n", buffer
->resource
.heap_memory
);
1027 buffer
->map_ptr
= NULL
;
1029 context_release(context
);
1033 if (flags
& WINED3D_MAP_DISCARD
)
1034 buffer
->flags
|= WINED3D_BUFFER_DISCARD
;
1035 else if (!(flags
& WINED3D_MAP_NOOVERWRITE
))
1036 buffer
->flags
|= WINED3D_BUFFER_SYNC
;
1039 base
= buffer
->map_ptr
? buffer
->map_ptr
: buffer
->resource
.heap_memory
;
1040 *data
= base
+ offset
;
1042 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1043 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1048 void CDECL
wined3d_buffer_unmap(struct wined3d_buffer
*buffer
)
1052 TRACE("buffer %p.\n", buffer
);
1054 /* In the case that the number of Unmap calls > the
1055 * number of Map calls, d3d returns always D3D_OK.
1056 * This is also needed to prevent Map from returning garbage on
1057 * the next call (this will happen if the lock_count is < 0). */
1058 if (!buffer
->resource
.map_count
)
1060 WARN("Unmap called without a previous map call.\n");
1064 if (--buffer
->resource
.map_count
)
1066 /* Delay loading the buffer until everything is unlocked */
1067 TRACE("Ignoring unmap.\n");
1071 if (!(buffer
->flags
& WINED3D_BUFFER_DOUBLEBUFFER
) && buffer
->buffer_object
)
1073 struct wined3d_device
*device
= buffer
->resource
.device
;
1074 const struct wined3d_gl_info
*gl_info
;
1075 struct wined3d_context
*context
;
1077 context
= context_acquire(device
, NULL
);
1078 gl_info
= context
->gl_info
;
1080 buffer_bind(buffer
, context
);
1082 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1084 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1086 GL_EXTCALL(glFlushMappedBufferRange(buffer
->buffer_type_hint
,
1087 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1088 checkGLcall("glFlushMappedBufferRange");
1091 else if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1093 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1095 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer
->buffer_type_hint
,
1096 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1097 checkGLcall("glFlushMappedBufferRangeAPPLE");
1101 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1102 if (wined3d_settings
.strict_draw_ordering
)
1103 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
1104 context_release(context
);
1106 buffer_clear_dirty_areas(buffer
);
1107 buffer
->map_ptr
= NULL
;
1109 else if (buffer
->flags
& WINED3D_BUFFER_HASDESC
)
1111 wined3d_buffer_preload(buffer
);
1115 HRESULT
wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1116 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1118 BYTE
*dst_buffer_mem
, *src_buffer_mem
, *dst_ptr
, *src_ptr
;
1119 struct wined3d_bo_address dst_bo_address
, src_bo_address
;
1120 const struct wined3d_gl_info
*gl_info
;
1121 struct wined3d_context
*context
;
1122 struct wined3d_device
*device
;
1125 device
= dst_buffer
->resource
.device
;
1127 context
= context_acquire(device
, NULL
);
1128 gl_info
= context
->gl_info
;
1130 buffer_get_memory(dst_buffer
, context
, &dst_bo_address
);
1131 buffer_get_memory(src_buffer
, context
, &src_bo_address
);
1133 dst_buffer_mem
= dst_buffer
->resource
.heap_memory
;
1134 src_buffer_mem
= src_buffer
->resource
.heap_memory
;
1136 if (!dst_buffer_mem
&& !src_buffer_mem
)
1138 if (gl_info
->supported
[ARB_COPY_BUFFER
])
1140 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src_bo_address
.buffer_object
));
1141 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst_bo_address
.buffer_object
));
1142 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
, src_offset
, dst_offset
, size
));
1143 checkGLcall("direct buffer copy");
1147 if (FAILED(hr
= wined3d_buffer_map(dst_buffer
, dst_offset
, size
, &dst_ptr
, 0)))
1149 WARN("Failed to map dst_buffer, hr %#x.\n", hr
);
1150 context_release(context
);
1151 return WINED3DERR_INVALIDCALL
;
1153 if (FAILED(hr
= wined3d_buffer_map(src_buffer
, src_offset
, size
, &src_ptr
, WINED3D_MAP_READONLY
)))
1155 WARN("Failed to map src_buffer, hr %#x.\n", hr
);
1156 wined3d_buffer_unmap(dst_buffer
);
1157 context_release(context
);
1158 return WINED3DERR_INVALIDCALL
;
1161 memcpy(dst_ptr
, src_ptr
, size
);
1163 wined3d_buffer_unmap(src_buffer
);
1164 wined3d_buffer_unmap(dst_buffer
);
1167 else if (dst_buffer_mem
&& !src_buffer_mem
)
1169 buffer_bind(src_buffer
, context
);
1170 GL_EXTCALL(glGetBufferSubData(src_buffer
->buffer_type_hint
, src_offset
, size
, dst_buffer_mem
+ dst_offset
));
1171 checkGLcall("buffer download");
1173 else if (!dst_buffer_mem
&& src_buffer_mem
)
1175 buffer_bind(dst_buffer
, context
);
1176 GL_EXTCALL(glBufferSubData(dst_buffer
->buffer_type_hint
, dst_offset
, size
, src_buffer_mem
+ src_offset
));
1177 checkGLcall("buffer upload");
1181 memcpy(dst_buffer_mem
+ dst_offset
, src_buffer_mem
+ src_offset
, size
);
1185 buffer_invalidate_bo_range(dst_buffer
, dst_offset
, size
);
1187 context_release(context
);
1191 HRESULT
wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
,
1192 const struct wined3d_box
*box
, const void *data
)
1201 size
= box
->right
- box
->left
;
1206 size
= buffer
->resource
.size
;
1209 if (FAILED(hr
= wined3d_buffer_map(buffer
, offset
, size
, &ptr
, 0)))
1212 memcpy(ptr
, data
, size
);
1214 wined3d_buffer_unmap(buffer
);
1218 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1220 return wined3d_buffer_incref(buffer_from_resource(resource
));
1223 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1225 return wined3d_buffer_decref(buffer_from_resource(resource
));
1228 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1229 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1231 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1234 if (sub_resource_idx
)
1236 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1237 return E_INVALIDARG
;
1243 size
= box
->right
- box
->left
;
1250 map_desc
->row_pitch
= map_desc
->slice_pitch
= buffer
->desc
.byte_width
;
1251 return wined3d_buffer_map(buffer
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1254 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1256 if (sub_resource_idx
)
1258 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1259 return E_INVALIDARG
;
1262 wined3d_buffer_unmap(buffer_from_resource(resource
));
1266 static const struct wined3d_resource_ops buffer_resource_ops
=
1268 buffer_resource_incref
,
1269 buffer_resource_decref
,
1271 buffer_resource_sub_resource_map
,
1272 buffer_resource_sub_resource_unmap
,
1275 static HRESULT
buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1276 UINT size
, DWORD usage
, enum wined3d_format_id format_id
, enum wined3d_pool pool
, GLenum bind_hint
,
1277 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1279 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1280 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
1282 BOOL dynamic_buffer_ok
;
1286 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL.\n");
1287 return WINED3DERR_INVALIDCALL
;
1290 if (data
&& !data
->data
)
1292 WARN("Invalid sub-resource data specified.\n");
1293 return E_INVALIDARG
;
1296 hr
= resource_init(&buffer
->resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1297 WINED3D_MULTISAMPLE_NONE
, 0, usage
, pool
, size
, 1, 1, size
, parent
, parent_ops
, &buffer_resource_ops
);
1300 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1303 buffer
->buffer_type_hint
= bind_hint
;
1305 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer
->resource
.size
, buffer
->resource
.usage
,
1306 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
, buffer
);
1308 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
|| pool
== WINED3D_POOL_MANAGED
)
1310 /* SWvp always returns the same pointer in buffer maps and retains data in DISCARD maps.
1311 * Keep a system memory copy of the buffer to provide the same behavior to the application.
1312 * Still use a VBO to support OpenGL 3 core contexts. */
1313 TRACE("Using doublebuffer mode because of software vertex processing.\n");
1314 buffer
->flags
|= WINED3D_BUFFER_DOUBLEBUFFER
;
1317 /* Observations show that draw_primitive_immediate_mode() is faster on
1318 * dynamic vertex buffers than converting + draw_primitive_arrays().
1319 * (Half-Life 2 and others.) */
1320 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1322 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1324 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1326 else if (buffer
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
1328 TRACE("Not creating a BO because the buffer is in system memory.\n");
1330 else if (!dynamic_buffer_ok
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
1332 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1336 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1339 if (!(buffer
->maps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
->maps
))))
1341 ERR("Out of memory.\n");
1342 buffer_unload(&buffer
->resource
);
1343 resource_cleanup(&buffer
->resource
);
1344 wined3d_resource_wait_idle(&buffer
->resource
);
1345 return E_OUTOFMEMORY
;
1347 buffer
->maps_size
= 1;
1349 if (data
&& FAILED(hr
= wined3d_buffer_upload_data(buffer
, NULL
, data
->data
)))
1351 ERR("Failed to upload data, hr %#x.\n", hr
);
1352 buffer_unload(&buffer
->resource
);
1353 resource_cleanup(&buffer
->resource
);
1354 wined3d_resource_wait_idle(&buffer
->resource
);
1355 HeapFree(GetProcessHeap(), 0, buffer
->maps
);
1362 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1363 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1364 struct wined3d_buffer
**buffer
)
1366 struct wined3d_buffer
*object
;
1369 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1370 device
, desc
, data
, parent
, parent_ops
, buffer
);
1372 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1374 return E_OUTOFMEMORY
;
1376 FIXME("Ignoring access flags (pool).\n");
1378 hr
= buffer_init(object
, device
, desc
->byte_width
, desc
->usage
, WINED3DFMT_UNKNOWN
,
1379 WINED3D_POOL_MANAGED
, GL_ARRAY_BUFFER_ARB
, data
, parent
, parent_ops
);
1382 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1383 HeapFree(GetProcessHeap(), 0, object
);
1386 object
->desc
= *desc
;
1388 TRACE("Created buffer %p.\n", object
);
1395 HRESULT CDECL
wined3d_buffer_create_vb(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1396 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1398 struct wined3d_buffer
*object
;
1401 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1402 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1404 if (pool
== WINED3D_POOL_SCRATCH
)
1406 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1407 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1408 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1410 return WINED3DERR_INVALIDCALL
;
1413 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1417 return WINED3DERR_OUTOFVIDEOMEMORY
;
1420 hr
= buffer_init(object
, device
, size
, usage
, WINED3DFMT_VERTEXDATA
,
1421 pool
, GL_ARRAY_BUFFER_ARB
, NULL
, parent
, parent_ops
);
1424 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1425 HeapFree(GetProcessHeap(), 0, object
);
1429 TRACE("Created buffer %p.\n", object
);
1435 HRESULT CDECL
wined3d_buffer_create_ib(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1436 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1438 struct wined3d_buffer
*object
;
1441 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1442 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1444 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1448 return WINED3DERR_OUTOFVIDEOMEMORY
;
1451 hr
= buffer_init(object
, device
, size
, usage
| WINED3DUSAGE_STATICDECL
,
1452 WINED3DFMT_UNKNOWN
, pool
, GL_ELEMENT_ARRAY_BUFFER_ARB
, NULL
,
1453 parent
, parent_ops
);
1456 WARN("Failed to initialize buffer, hr %#x\n", hr
);
1457 HeapFree(GetProcessHeap(), 0, object
);
1461 TRACE("Created buffer %p.\n", object
);