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 wined3d_buffer_evict_sysmem(struct wined3d_buffer
*buffer
)
45 if (buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
47 TRACE("Not evicting system memory for buffer %p.\n", buffer
);
51 TRACE("Evicting system memory for buffer %p.\n", buffer
);
52 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
53 wined3d_resource_free_sysmem(&buffer
->resource
);
56 static void buffer_invalidate_bo_range(struct wined3d_buffer
*buffer
, unsigned int offset
, unsigned int size
)
58 if (!offset
&& (!size
|| size
== buffer
->resource
.size
))
61 if (offset
> buffer
->resource
.size
|| size
> buffer
->resource
.size
- offset
)
63 WARN("Invalid range specified, invalidating entire buffer.\n");
67 if (!wined3d_array_reserve((void **)&buffer
->maps
, &buffer
->maps_size
,
68 buffer
->modified_areas
+ 1, sizeof(*buffer
->maps
)))
70 ERR("Failed to allocate maps array, invalidating entire buffer.\n");
74 buffer
->maps
[buffer
->modified_areas
].offset
= offset
;
75 buffer
->maps
[buffer
->modified_areas
].size
= size
;
76 ++buffer
->modified_areas
;
80 buffer
->modified_areas
= 1;
81 buffer
->maps
[0].offset
= 0;
82 buffer
->maps
[0].size
= buffer
->resource
.size
;
85 static inline void buffer_clear_dirty_areas(struct wined3d_buffer
*This
)
87 This
->modified_areas
= 0;
90 static BOOL
buffer_is_dirty(const struct wined3d_buffer
*buffer
)
92 return !!buffer
->modified_areas
;
95 static BOOL
buffer_is_fully_dirty(const struct wined3d_buffer
*buffer
)
97 return buffer
->modified_areas
== 1
98 && !buffer
->maps
->offset
&& buffer
->maps
->size
== buffer
->resource
.size
;
101 static void wined3d_buffer_validate_location(struct wined3d_buffer
*buffer
, DWORD location
)
103 TRACE("buffer %p, location %s.\n", buffer
, wined3d_debug_location(location
));
105 if (location
& WINED3D_LOCATION_BUFFER
)
106 buffer_clear_dirty_areas(buffer
);
108 buffer
->locations
|= location
;
110 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
113 static void wined3d_buffer_invalidate_range(struct wined3d_buffer
*buffer
, DWORD location
,
114 unsigned int offset
, unsigned int size
)
116 TRACE("buffer %p, location %s, offset %u, size %u.\n",
117 buffer
, wined3d_debug_location(location
), offset
, size
);
119 if (location
& WINED3D_LOCATION_BUFFER
)
120 buffer_invalidate_bo_range(buffer
, offset
, size
);
122 buffer
->locations
&= ~location
;
124 TRACE("New locations flags are %s.\n", wined3d_debug_location(buffer
->locations
));
126 if (!buffer
->locations
)
127 ERR("Buffer %p does not have any up to date location.\n", buffer
);
130 void wined3d_buffer_invalidate_location(struct wined3d_buffer
*buffer
, DWORD location
)
132 wined3d_buffer_invalidate_range(buffer
, location
, 0, 0);
135 /* Context activation is done by the caller. */
136 static void wined3d_buffer_gl_bind(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_context_gl
*context_gl
)
138 wined3d_context_gl_bind_bo(context_gl
, buffer_gl
->buffer_type_hint
, buffer_gl
->buffer_object
);
141 /* Context activation is done by the caller. */
142 static void wined3d_buffer_gl_destroy_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
143 struct wined3d_context_gl
*context_gl
)
145 const struct wined3d_gl_info
*gl_info
= context_gl
->c
.gl_info
;
146 struct wined3d_resource
*resource
= &buffer_gl
->b
.resource
;
148 if (!buffer_gl
->buffer_object
)
151 /* The stream source state handler might have read the memory of the
152 * vertex buffer already and got the memory in the vbo which is not
153 * valid any longer. Dirtify the stream source to force a reload. This
154 * happens only once per changed vertexbuffer and should occur rather
156 if (resource
->bind_count
)
158 if (resource
->bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
159 device_invalidate_state(resource
->device
, STATE_STREAMSRC
);
160 if (resource
->bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
161 device_invalidate_state(resource
->device
, STATE_INDEXBUFFER
);
162 if (resource
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
164 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX
));
165 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL
));
166 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN
));
167 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY
));
168 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL
));
169 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE
));
171 if (resource
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
173 device_invalidate_state(resource
->device
, STATE_STREAM_OUTPUT
);
174 if (context_gl
->c
.transform_feedback_active
)
176 /* We have to make sure that transform feedback is not active
177 * when deleting a potentially bound transform feedback buffer.
178 * This may happen when the device is being destroyed. */
179 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer_gl
);
180 wined3d_context_gl_end_transform_feedback(context_gl
);
185 GL_EXTCALL(glDeleteBuffers(1, &buffer_gl
->buffer_object
));
186 checkGLcall("glDeleteBuffers");
187 buffer_gl
->buffer_object
= 0;
189 if (buffer_gl
->b
.fence
)
191 wined3d_fence_destroy(buffer_gl
->b
.fence
);
192 buffer_gl
->b
.fence
= NULL
;
194 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_APPLESYNC
;
197 /* Context activation is done by the caller. */
198 static BOOL
wined3d_buffer_gl_create_buffer_object(struct wined3d_buffer_gl
*buffer_gl
,
199 struct wined3d_context_gl
*context_gl
)
201 const struct wined3d_gl_info
*gl_info
= context_gl
->c
.gl_info
;
202 GLenum gl_usage
= GL_STATIC_DRAW
;
205 TRACE("Creating an OpenGL buffer object for wined3d buffer %p with usage %s.\n",
206 buffer_gl
, debug_d3dusage(buffer_gl
->b
.resource
.usage
));
208 /* Make sure that the gl error is cleared. Do not use checkGLcall
209 * here because checkGLcall just prints a fixme and continues. However,
210 * if an error during VBO creation occurs we can fall back to non-VBO operation
211 * with full functionality(but performance loss).
213 while (gl_info
->gl_ops
.gl
.p_glGetError() != GL_NO_ERROR
);
215 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
216 * The vertex declaration from the device determines how the data in the
217 * buffer is interpreted. This means that on each draw call the buffer has
218 * to be verified to check if the rhw and color values are in the correct
221 GL_EXTCALL(glGenBuffers(1, &buffer_gl
->buffer_object
));
222 error
= gl_info
->gl_ops
.gl
.p_glGetError();
223 if (!buffer_gl
->buffer_object
|| error
!= GL_NO_ERROR
)
225 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error
), error
);
229 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
230 error
= gl_info
->gl_ops
.gl
.p_glGetError();
231 if (error
!= GL_NO_ERROR
)
233 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error
), error
);
237 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
239 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
240 gl_usage
= GL_STREAM_DRAW_ARB
;
242 if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
244 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl
->buffer_type_hint
,
245 GL_BUFFER_FLUSHING_UNMAP_APPLE
, GL_FALSE
));
246 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl
->buffer_type_hint
,
247 GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_FALSE
));
248 checkGLcall("glBufferParameteriAPPLE");
249 buffer_gl
->b
.flags
|= WINED3D_BUFFER_APPLESYNC
;
251 /* No setup is needed here for GL_ARB_map_buffer_range. */
254 GL_EXTCALL(glBufferData(buffer_gl
->buffer_type_hint
, buffer_gl
->b
.resource
.size
, NULL
, gl_usage
));
255 error
= gl_info
->gl_ops
.gl
.p_glGetError();
256 if (error
!= GL_NO_ERROR
)
258 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error
), error
);
262 buffer_gl
->buffer_object_usage
= gl_usage
;
263 buffer_invalidate_bo_range(&buffer_gl
->b
, 0, 0);
268 /* Clean up all BO init, but continue because we can work without a BO :-) */
269 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
270 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_USE_BO
;
271 wined3d_buffer_gl_destroy_buffer_object(buffer_gl
, context_gl
);
272 buffer_clear_dirty_areas(&buffer_gl
->b
);
276 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
277 const enum wined3d_buffer_conversion_type conversion_type
,
278 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
280 const struct wined3d_format
*format
= attrib
->format
;
285 /* Check for some valid situations which cause us pain. One is if the buffer is used for
286 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
287 * with different strides. In the 2nd case we might have to drop conversion entirely,
288 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
292 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
293 debug_d3dformat(format
->id
));
295 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
297 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
301 *stride_this_run
= attrib
->stride
;
302 if (buffer
->stride
!= *stride_this_run
)
304 /* We rely that this happens only on the first converted attribute that is found,
305 * if at all. See above check
307 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
308 buffer
->stride
= *stride_this_run
;
309 heap_free(buffer
->conversion_map
);
310 buffer
->conversion_map
= heap_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
315 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
316 for (i
= 0; i
< format
->byte_count
; ++i
)
318 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
319 if (buffer
->conversion_map
[idx
] != conversion_type
)
321 TRACE("Byte %lu in vertex changed:\n", idx
);
322 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
324 buffer
->conversion_map
[idx
] = conversion_type
;
331 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
332 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
334 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
335 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
337 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
338 enum wined3d_format_id format
;
341 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
342 * there, on nonexistent attribs the vbo is 0.
344 if (!(si
->use_map
& (1u << attrib_idx
))
345 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
348 format
= attrib
->format
->id
;
349 /* Look for newly appeared conversion */
350 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
352 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
354 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
356 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
358 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
362 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
364 else if (This
->conversion_map
)
366 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
372 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
373 const struct wined3d_state
*state
, DWORD fixup_flags
)
375 UINT stride_this_run
= 0;
378 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
379 * Once we have our declaration there is no need to look it up again. Index buffers also never need
380 * conversion, so once the (empty) conversion structure is created don't bother checking again
382 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
384 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
389 TRACE("No fixup required.\n");
390 if(This
->conversion_map
)
392 heap_free(This
->conversion_map
);
393 This
->conversion_map
= NULL
;
401 TRACE("Finding vertex buffer conversion information\n");
402 /* Certain declaration types need some fixups before we can pass them to
403 * opengl. This means D3DCOLOR attributes with fixed function vertex
404 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
405 * GL_ARB_half_float_vertex is not supported.
407 * Note for d3d8 and d3d9:
408 * The vertex buffer FVF doesn't help with finding them, we have to use
409 * the decoded vertex declaration and pick the things that concern the
410 * current buffer. A problem with this is that this can change between
411 * draws, so we have to validate the information and reprocess the buffer
412 * if it changes, and avoid false positives for performance reasons.
413 * WineD3D doesn't even know the vertex buffer any more, it is managed
414 * by the client libraries and passed to SetStreamSource and ProcessVertices
417 * We have to distinguish between vertex shaders and fixed function to
418 * pick the way we access the strided vertex information.
420 * This code sets up a per-byte array with the size of the detected
421 * stride of the arrays in the buffer. For each byte we have a field
422 * that marks the conversion needed on this byte. For example, the
423 * following declaration with fixed function vertex processing:
431 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
432 * [P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][0][0][0][0][0][0][0][0][0][0][0][0][F][F][F][F][F][F][F][F][C][C][C][C]
434 * Where in this example map P means 4 component position conversion, 0
435 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
436 * conversion (red / blue swizzle).
438 * If we're doing conversion and the stride changes we have to reconvert
439 * the whole buffer. Note that we do not mind if the semantic changes,
440 * we only care for the conversion type. So if the NORMAL is replaced
441 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
442 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
443 * conversion types depend on the semantic as well, for example a FLOAT4
444 * texcoord needs no conversion while a FLOAT4 positiont needs one
447 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
448 fixup_flags
, &stride_this_run
) || ret
;
449 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
451 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
452 fixup_flags
, &stride_this_run
) || ret
;
453 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
454 fixup_flags
, &stride_this_run
) || ret
;
455 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
456 fixup_flags
, &stride_this_run
) || ret
;
457 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
458 fixup_flags
, &stride_this_run
) || ret
;
459 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
460 fixup_flags
, &stride_this_run
) || ret
;
461 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
462 fixup_flags
, &stride_this_run
) || ret
;
463 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
464 fixup_flags
, &stride_this_run
) || ret
;
465 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
466 fixup_flags
, &stride_this_run
) || ret
;
467 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
468 fixup_flags
, &stride_this_run
) || ret
;
469 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
470 fixup_flags
, &stride_this_run
) || ret
;
471 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
472 fixup_flags
, &stride_this_run
) || ret
;
473 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
474 fixup_flags
, &stride_this_run
) || ret
;
475 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
476 fixup_flags
, &stride_this_run
) || ret
;
478 if (!stride_this_run
&& This
->conversion_map
)
482 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
483 heap_free(This
->conversion_map
);
484 This
->conversion_map
= NULL
;
488 if (ret
) TRACE("Conversion information changed\n");
493 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
495 DWORD src_color
= *dst_color
;
497 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
498 * endianness. If we want this to work on big-endian machines as well we
499 * have to consider more things.
501 * 0xff000000: Alpha mask
502 * 0x00ff0000: Blue mask
503 * 0x0000ff00: Green mask
504 * 0x000000ff: Red mask
507 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
508 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
509 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
511 return sizeof(*dst_color
);
514 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
516 /* rhw conversion like in position_float4(). */
517 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
519 float w
= 1.0f
/ p
->w
;
529 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
531 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
533 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
538 /* Context activation is done by the caller. */
539 static void wined3d_buffer_gl_upload_ranges(struct wined3d_buffer_gl
*buffer_gl
, struct wined3d_context
*context
,
540 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_map_range
*ranges
)
542 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
543 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
544 const struct wined3d_map_range
*range
;
546 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
548 while (range_count
--)
550 range
= &ranges
[range_count
];
551 GL_EXTCALL(glBufferSubData(buffer_gl
->buffer_type_hint
,
552 range
->offset
, range
->size
, (BYTE
*)data
+ range
->offset
- data_offset
));
554 checkGLcall("glBufferSubData");
557 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
559 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
562 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
564 ERR("Failed to load system memory.\n");
567 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
569 /* Now for each vertex in the buffer that needs conversion. */
570 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
572 if (!(data
= heap_alloc(buffer
->resource
.size
)))
574 ERR("Out of memory.\n");
578 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
580 start
= buffer
->maps
[range_idx
].offset
;
581 end
= start
+ buffer
->maps
[range_idx
].size
;
583 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
584 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
586 for (j
= 0; j
< buffer
->stride
;)
588 switch (buffer
->conversion_map
[j
])
595 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
598 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
601 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
608 wined3d_buffer_gl_upload_ranges(wined3d_buffer_gl(buffer
),
609 context
, data
, 0, buffer
->modified_areas
, buffer
->maps
);
614 static BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
615 struct wined3d_context
*context
, DWORD location
)
617 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
618 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
622 case WINED3D_LOCATION_SYSMEM
:
623 if (buffer
->resource
.heap_memory
)
626 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
630 case WINED3D_LOCATION_BUFFER
:
631 if (buffer_gl
->buffer_object
)
634 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
636 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
639 return wined3d_buffer_gl_create_buffer_object(buffer_gl
, context_gl
);
642 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
647 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
648 struct wined3d_context
*context
, DWORD location
)
650 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
651 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer
);
652 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
654 TRACE("buffer %p, context %p, location %s.\n",
655 buffer
, context
, wined3d_debug_location(location
));
657 if (buffer
->locations
& location
)
659 TRACE("Location (%#x) is already up to date.\n", location
);
663 if (!buffer
->locations
)
665 ERR("Buffer %p does not have any up to date location.\n", buffer
);
666 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
667 return wined3d_buffer_load_location(buffer
, context
, location
);
670 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
672 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
675 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
677 TRACE("Buffer previously discarded, nothing to do.\n");
678 wined3d_buffer_validate_location(buffer
, location
);
679 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
685 case WINED3D_LOCATION_SYSMEM
:
686 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
687 GL_EXTCALL(glGetBufferSubData(buffer_gl
->buffer_type_hint
, 0, buffer
->resource
.size
,
688 buffer
->resource
.heap_memory
));
689 checkGLcall("buffer download");
692 case WINED3D_LOCATION_BUFFER
:
693 if (!buffer
->conversion_map
)
694 wined3d_buffer_gl_upload_ranges(buffer_gl
, context
, buffer
->resource
.heap_memory
,
695 0, buffer
->modified_areas
, buffer
->maps
);
697 buffer_conversion_upload(buffer
, context
);
701 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
705 wined3d_buffer_validate_location(buffer
, location
);
706 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
707 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
708 wined3d_buffer_evict_sysmem(buffer
);
713 /* Context activation is done by the caller. */
714 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
716 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
717 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
718 return buffer
->resource
.heap_memory
;
721 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
,
722 struct wined3d_bo_address
*data
, DWORD locations
)
724 TRACE("buffer %p, data %p, locations %s.\n",
725 buffer
, data
, wined3d_debug_location(locations
));
727 if (locations
& WINED3D_LOCATION_BUFFER
)
729 data
->buffer_object
= wined3d_buffer_gl(buffer
)->buffer_object
;
731 return WINED3D_LOCATION_BUFFER
;
733 if (locations
& WINED3D_LOCATION_SYSMEM
)
735 data
->buffer_object
= 0;
736 data
->addr
= buffer
->resource
.heap_memory
;
737 return WINED3D_LOCATION_SYSMEM
;
740 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
741 data
->buffer_object
= 0;
746 static void buffer_unload(struct wined3d_resource
*resource
)
748 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
750 TRACE("buffer %p.\n", buffer
);
752 if (wined3d_buffer_gl(buffer
)->buffer_object
)
754 struct wined3d_context
*context
;
756 context
= context_acquire(resource
->device
, NULL
, 0);
758 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
759 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
760 wined3d_buffer_gl_destroy_buffer_object(wined3d_buffer_gl(buffer
), wined3d_context_gl(context
));
761 buffer_clear_dirty_areas(buffer
);
763 context_release(context
);
765 heap_free(buffer
->conversion_map
);
766 buffer
->conversion_map
= NULL
;
768 buffer
->conversion_stride
= 0;
769 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
772 resource_unload(resource
);
775 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
777 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
778 buffer_unload(&buffer
->resource
);
781 static void wined3d_buffer_gl_destroy_object(void *object
)
783 struct wined3d_buffer_gl
*buffer_gl
= object
;
784 struct wined3d_context
*context
;
786 if (buffer_gl
->buffer_object
)
788 context
= context_acquire(buffer_gl
->b
.resource
.device
, NULL
, 0);
789 wined3d_buffer_gl_destroy_buffer_object(buffer_gl
, wined3d_context_gl(context
));
790 context_release(context
);
792 heap_free(buffer_gl
->b
.conversion_map
);
795 heap_free(buffer_gl
->b
.maps
);
796 heap_free(buffer_gl
);
799 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
801 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
803 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
807 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
808 resource_cleanup(&buffer
->resource
);
809 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
,
810 wined3d_buffer_gl_destroy_object
, wined3d_buffer_gl(buffer
));
816 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
818 TRACE("buffer %p.\n", buffer
);
820 return buffer
->resource
.parent
;
823 /* The caller provides a context and binds the buffer */
824 static void wined3d_buffer_gl_sync_apple(struct wined3d_buffer_gl
*buffer_gl
,
825 DWORD flags
, const struct wined3d_gl_info
*gl_info
)
827 enum wined3d_fence_result ret
;
830 /* No fencing needs to be done if the app promises not to overwrite
832 if (flags
& WINED3D_MAP_NOOVERWRITE
)
835 if (flags
& WINED3D_MAP_DISCARD
)
837 GL_EXTCALL(glBufferData(buffer_gl
->buffer_type_hint
, buffer_gl
->b
.resource
.size
,
838 NULL
, buffer_gl
->buffer_object_usage
));
839 checkGLcall("glBufferData");
843 if (!buffer_gl
->b
.fence
)
845 TRACE("Creating fence for buffer %p.\n", buffer_gl
);
847 if (FAILED(hr
= wined3d_fence_create(buffer_gl
->b
.resource
.device
, &buffer_gl
->b
.fence
)))
849 if (hr
== WINED3DERR_NOTAVAILABLE
)
850 FIXME("Fences not supported, dropping async buffer locks.\n");
852 ERR("Failed to create fence, hr %#x.\n", hr
);
856 /* Since we don't know about old draws a glFinish is needed once */
857 gl_info
->gl_ops
.gl
.p_glFinish();
861 TRACE("Synchronizing buffer %p.\n", buffer_gl
);
862 ret
= wined3d_fence_wait(buffer_gl
->b
.fence
, buffer_gl
->b
.resource
.device
);
865 case WINED3D_FENCE_NOT_STARTED
:
866 case WINED3D_FENCE_OK
:
870 case WINED3D_FENCE_WRONG_THREAD
:
871 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
875 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret
);
880 if (buffer_gl
->b
.fence
)
882 wined3d_fence_destroy(buffer_gl
->b
.fence
);
883 buffer_gl
->b
.fence
= NULL
;
886 gl_info
->gl_ops
.gl
.p_glFinish();
887 GL_EXTCALL(glBufferParameteriAPPLE(buffer_gl
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
888 checkGLcall("glBufferParameteriAPPLE(buffer_gl->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
889 buffer_gl
->b
.flags
&= ~WINED3D_BUFFER_APPLESYNC
;
892 static void buffer_mark_used(struct wined3d_buffer
*buffer
)
894 buffer
->flags
&= ~WINED3D_BUFFER_DISCARD
;
897 /* Context activation is done by the caller. */
898 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
899 const struct wined3d_state
*state
)
901 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
902 BOOL decl_changed
= FALSE
;
904 TRACE("buffer %p.\n", buffer
);
906 if (buffer
->resource
.map_count
)
908 WARN("Buffer is mapped, skipping preload.\n");
912 buffer_mark_used(buffer
);
914 /* TODO: Make converting independent from VBOs */
915 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
917 /* Not doing any conversion */
921 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
923 ERR("Failed to prepare buffer location.\n");
927 /* Reading the declaration makes only sense if we have valid state information
928 * (i.e., if this function is called during draws). */
931 DWORD fixup_flags
= 0;
935 if (!d3d_info
->vertex_bgra
&& !d3d_info
->ffp_generic_attributes
)
936 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
937 if (!d3d_info
->xyzrhw
)
938 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
941 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
942 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
945 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
947 ++buffer
->draw_count
;
948 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
949 buffer
->decl_change_count
= 0;
950 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
951 buffer
->full_conversion_count
= 0;
955 /* If applications change the declaration over and over, reconverting all the time is a huge
956 * performance hit. So count the declaration changes and release the VBO if there are too many
957 * of them (and thus stop converting)
961 ++buffer
->decl_change_count
;
962 buffer
->draw_count
= 0;
964 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
965 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
967 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
968 wined3d_buffer_drop_bo(buffer
);
972 /* The declaration changed, reload the whole buffer. */
973 WARN("Reloading buffer because of a vertex declaration change.\n");
974 buffer_invalidate_bo_range(buffer
, 0, 0);
978 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
979 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
980 * decl changes and reset the decl change count after a specific number of them
982 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
984 ++buffer
->full_conversion_count
;
985 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
987 FIXME("Too many full buffer conversions, stopping converting.\n");
988 wined3d_buffer_drop_bo(buffer
);
994 ++buffer
->draw_count
;
995 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
996 buffer
->decl_change_count
= 0;
997 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
998 buffer
->full_conversion_count
= 0;
1002 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
1003 ERR("Failed to load buffer location.\n");
1006 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
1008 TRACE("buffer %p.\n", buffer
);
1010 return &buffer
->resource
;
1013 static HRESULT
wined3d_buffer_gl_map(struct wined3d_buffer_gl
*buffer_gl
,
1014 unsigned int offset
, unsigned int size
, BYTE
**data
, DWORD flags
)
1016 struct wined3d_device
*device
= buffer_gl
->b
.resource
.device
;
1017 struct wined3d_context_gl
*context_gl
;
1018 struct wined3d_context
*context
;
1022 TRACE("buffer_gl %p, offset %u, size %u, data %p, flags %#x.\n", buffer_gl
, offset
, size
, data
, flags
);
1024 count
= ++buffer_gl
->b
.resource
.map_count
;
1026 if (buffer_gl
->buffer_object
)
1028 unsigned int dirty_offset
= offset
, dirty_size
= size
;
1030 /* DISCARD invalidates the entire buffer, regardless of the specified
1031 * offset and size. Some applications also depend on the entire buffer
1032 * being uploaded in that case. Two such applications are Port Royale
1033 * and Darkstar One. */
1034 if (flags
& WINED3D_MAP_DISCARD
)
1040 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
1041 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer_gl
->b
.locations
& WINED3D_LOCATION_SYSMEM
))
1042 || buffer_gl
->b
.flags
& WINED3D_BUFFER_PIN_SYSMEM
)
1044 if (!(buffer_gl
->b
.locations
& WINED3D_LOCATION_SYSMEM
))
1046 context
= context_acquire(device
, NULL
, 0);
1047 wined3d_buffer_load_location(&buffer_gl
->b
, context
, WINED3D_LOCATION_SYSMEM
);
1048 context_release(context
);
1051 if (flags
& WINED3D_MAP_WRITE
)
1052 wined3d_buffer_invalidate_range(&buffer_gl
->b
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
1056 const struct wined3d_gl_info
*gl_info
;
1058 context
= context_acquire(device
, NULL
, 0);
1059 context_gl
= wined3d_context_gl(context
);
1060 gl_info
= context
->gl_info
;
1062 if (flags
& WINED3D_MAP_DISCARD
)
1063 wined3d_buffer_validate_location(&buffer_gl
->b
, WINED3D_LOCATION_BUFFER
);
1065 wined3d_buffer_load_location(&buffer_gl
->b
, context
, WINED3D_LOCATION_BUFFER
);
1067 if (flags
& WINED3D_MAP_WRITE
)
1069 wined3d_buffer_invalidate_location(&buffer_gl
->b
, WINED3D_LOCATION_SYSMEM
);
1070 buffer_invalidate_bo_range(&buffer_gl
->b
, dirty_offset
, dirty_size
);
1073 if ((flags
& WINED3D_MAP_DISCARD
) && buffer_gl
->b
.resource
.heap_memory
)
1074 wined3d_buffer_evict_sysmem(&buffer_gl
->b
);
1078 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
1080 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1081 * multitexture fill rate test seems to depend on this. When
1082 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1083 * driver is free to discard the previous contents of the
1084 * buffer. The r600g driver only does this when the buffer is
1085 * currently in use, while the proprietary NVIDIA driver
1086 * appears to do this unconditionally. */
1087 if (buffer_gl
->b
.flags
& WINED3D_BUFFER_DISCARD
)
1088 flags
&= ~WINED3D_MAP_DISCARD
;
1090 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1092 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
1093 buffer_gl
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(buffer_gl
->buffer_type_hint
,
1094 0, buffer_gl
->b
.resource
.size
, mapflags
));
1095 checkGLcall("glMapBufferRange");
1099 if (buffer_gl
->b
.flags
& WINED3D_BUFFER_APPLESYNC
)
1100 wined3d_buffer_gl_sync_apple(buffer_gl
, flags
, gl_info
);
1101 buffer_gl
->b
.map_ptr
= GL_EXTCALL(glMapBuffer(buffer_gl
->buffer_type_hint
,
1103 checkGLcall("glMapBuffer");
1106 if (((DWORD_PTR
)buffer_gl
->b
.map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1108 WARN("Pointer %p is not %u byte aligned.\n", buffer_gl
->b
.map_ptr
, RESOURCE_ALIGNMENT
);
1110 GL_EXTCALL(glUnmapBuffer(buffer_gl
->buffer_type_hint
));
1111 checkGLcall("glUnmapBuffer");
1112 buffer_gl
->b
.map_ptr
= NULL
;
1114 if (buffer_gl
->b
.resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1116 /* The extra copy is more expensive than not using VBOs at
1117 * all on the Nvidia Linux driver, which is the only driver
1118 * that returns unaligned pointers.
1120 TRACE("Dynamic buffer, dropping VBO.\n");
1121 wined3d_buffer_drop_bo(&buffer_gl
->b
);
1125 TRACE("Falling back to doublebuffered operation.\n");
1126 wined3d_buffer_load_location(&buffer_gl
->b
, context
, WINED3D_LOCATION_SYSMEM
);
1127 buffer_gl
->b
.flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1129 TRACE("New pointer is %p.\n", buffer_gl
->b
.resource
.heap_memory
);
1133 context_release(context
);
1136 if (flags
& WINED3D_MAP_DISCARD
)
1137 buffer_gl
->b
.flags
|= WINED3D_BUFFER_DISCARD
;
1140 base
= buffer_gl
->b
.map_ptr
? buffer_gl
->b
.map_ptr
: buffer_gl
->b
.resource
.heap_memory
;
1141 *data
= base
+ offset
;
1143 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1144 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1149 static void wined3d_buffer_gl_unmap(struct wined3d_buffer_gl
*buffer_gl
)
1153 TRACE("buffer_gl %p.\n", buffer_gl
);
1155 /* In the case that the number of Unmap calls > the
1156 * number of Map calls, d3d returns always D3D_OK.
1157 * This is also needed to prevent Map from returning garbage on
1158 * the next call (this will happen if the lock_count is < 0). */
1159 if (!buffer_gl
->b
.resource
.map_count
)
1161 WARN("Unmap called without a previous map call.\n");
1165 if (--buffer_gl
->b
.resource
.map_count
)
1167 /* Delay loading the buffer until everything is unlocked */
1168 TRACE("Ignoring unmap.\n");
1172 if (buffer_gl
->b
.map_ptr
)
1174 struct wined3d_device
*device
= buffer_gl
->b
.resource
.device
;
1175 const struct wined3d_gl_info
*gl_info
;
1176 struct wined3d_context_gl
*context_gl
;
1177 struct wined3d_context
*context
;
1179 context
= context_acquire(device
, NULL
, 0);
1180 context_gl
= wined3d_context_gl(context
);
1181 gl_info
= context
->gl_info
;
1183 wined3d_buffer_gl_bind(buffer_gl
, context_gl
);
1185 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1187 for (i
= 0; i
< buffer_gl
->b
.modified_areas
; ++i
)
1189 GL_EXTCALL(glFlushMappedBufferRange(buffer_gl
->buffer_type_hint
,
1190 buffer_gl
->b
.maps
[i
].offset
, buffer_gl
->b
.maps
[i
].size
));
1191 checkGLcall("glFlushMappedBufferRange");
1194 else if (buffer_gl
->b
.flags
& WINED3D_BUFFER_APPLESYNC
)
1196 for (i
= 0; i
< buffer_gl
->b
.modified_areas
; ++i
)
1198 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer_gl
->buffer_type_hint
,
1199 buffer_gl
->b
.maps
[i
].offset
, buffer_gl
->b
.maps
[i
].size
));
1200 checkGLcall("glFlushMappedBufferRangeAPPLE");
1204 GL_EXTCALL(glUnmapBuffer(buffer_gl
->buffer_type_hint
));
1205 context_release(context
);
1207 buffer_clear_dirty_areas(&buffer_gl
->b
);
1208 buffer_gl
->b
.map_ptr
= NULL
;
1212 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1213 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1215 struct wined3d_bo_address dst
, src
;
1216 struct wined3d_context
*context
;
1219 buffer_mark_used(dst_buffer
);
1220 buffer_mark_used(src_buffer
);
1222 dst_location
= wined3d_buffer_get_memory(dst_buffer
, &dst
, dst_buffer
->locations
);
1223 dst
.addr
+= dst_offset
;
1225 wined3d_buffer_get_memory(src_buffer
, &src
, src_buffer
->locations
);
1226 src
.addr
+= src_offset
;
1228 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1229 wined3d_context_gl_copy_bo_address(wined3d_context_gl(context
),
1230 &dst
, wined3d_buffer_gl(dst_buffer
)->buffer_type_hint
,
1231 &src
, wined3d_buffer_gl(src_buffer
)->buffer_type_hint
, size
);
1232 context_release(context
);
1234 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1237 void wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1238 const struct wined3d_box
*box
, const void *data
)
1240 struct wined3d_map_range range
;
1244 range
.offset
= box
->left
;
1245 range
.size
= box
->right
- box
->left
;
1250 range
.size
= buffer
->resource
.size
;
1253 wined3d_buffer_gl_upload_ranges(wined3d_buffer_gl(buffer
), context
, data
, range
.offset
, 1, &range
);
1256 static void wined3d_buffer_init_data(struct wined3d_buffer
*buffer
,
1257 struct wined3d_device
*device
, const struct wined3d_sub_resource_data
*data
)
1259 struct wined3d_resource
*resource
= &buffer
->resource
;
1260 struct wined3d_bo_address bo
;
1261 struct wined3d_box box
;
1263 if (buffer
->flags
& WINED3D_BUFFER_USE_BO
)
1265 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1266 wined3d_cs_emit_update_sub_resource(device
->cs
, resource
,
1267 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1271 wined3d_buffer_get_memory(buffer
, &bo
, WINED3D_LOCATION_SYSMEM
);
1272 memcpy(bo
.addr
, data
->data
, resource
->size
);
1273 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_SYSMEM
);
1274 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_SYSMEM
);
1278 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1280 return wined3d_buffer_incref(buffer_from_resource(resource
));
1283 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1285 return wined3d_buffer_decref(buffer_from_resource(resource
));
1288 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1290 struct wined3d_context
*context
;
1292 context
= context_acquire(resource
->device
, NULL
, 0);
1293 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1294 context_release(context
);
1297 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1298 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1300 struct wined3d_buffer_gl
*buffer_gl
= wined3d_buffer_gl(buffer_from_resource(resource
));
1303 if (sub_resource_idx
)
1305 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1306 return E_INVALIDARG
;
1312 size
= box
->right
- box
->left
;
1319 map_desc
->row_pitch
= map_desc
->slice_pitch
= resource
->size
;
1320 return wined3d_buffer_gl_map(buffer_gl
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1323 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1325 if (sub_resource_idx
)
1327 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1328 return E_INVALIDARG
;
1331 wined3d_buffer_gl_unmap(wined3d_buffer_gl(buffer_from_resource(resource
)));
1335 static const struct wined3d_resource_ops buffer_resource_ops
=
1337 buffer_resource_incref
,
1338 buffer_resource_decref
,
1339 buffer_resource_preload
,
1341 buffer_resource_sub_resource_map
,
1342 buffer_resource_sub_resource_unmap
,
1345 static GLenum
buffer_type_hint_from_bind_flags(const struct wined3d_gl_info
*gl_info
,
1346 unsigned int bind_flags
)
1348 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
1349 return GL_ELEMENT_ARRAY_BUFFER
;
1351 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
1352 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1353 return GL_TEXTURE_BUFFER
;
1355 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1356 return GL_UNIFORM_BUFFER
;
1358 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1359 return GL_TRANSFORM_FEEDBACK_BUFFER
;
1361 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
1362 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
1364 return GL_ARRAY_BUFFER
;
1367 static HRESULT
wined3d_buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1368 const struct wined3d_buffer_desc
*desc
, const struct wined3d_sub_resource_data
*data
,
1369 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1371 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, WINED3DFMT_UNKNOWN
, desc
->bind_flags
);
1372 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1373 struct wined3d_resource
*resource
= &buffer
->resource
;
1374 BOOL dynamic_buffer_ok
;
1377 if (!desc
->byte_width
)
1379 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1380 return E_INVALIDARG
;
1383 if (desc
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& desc
->byte_width
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1385 WARN("Size %#x is not suitably aligned for constant buffers.\n", desc
->byte_width
);
1386 return E_INVALIDARG
;
1389 if (data
&& !data
->data
)
1391 WARN("Invalid sub-resource data specified.\n");
1392 return E_INVALIDARG
;
1395 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1396 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->bind_flags
, desc
->access
,
1397 desc
->byte_width
, 1, 1, desc
->byte_width
, parent
, parent_ops
, &buffer_resource_ops
)))
1399 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1402 buffer
->structure_byte_stride
= desc
->structure_byte_stride
;
1403 buffer
->locations
= data
? WINED3D_LOCATION_DISCARDED
: WINED3D_LOCATION_SYSMEM
;
1405 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1406 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
,
1407 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
);
1409 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1410 || wined3d_resource_access_is_managed(desc
->access
))
1412 /* SWvp and managed buffers always return the same pointer in buffer
1413 * maps and retain data in DISCARD maps. Keep a system memory copy of
1414 * the buffer to provide the same behavior to the application. */
1415 TRACE("Pinning system memory.\n");
1416 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1417 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1420 /* Observations show that draw_primitive_immediate_mode() is faster on
1421 * dynamic vertex buffers than converting + draw_primitive_arrays().
1422 * (Half-Life 2 and others.) */
1423 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1425 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1427 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1429 else if (!(desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
1431 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1433 else if (!dynamic_buffer_ok
&& (resource
->usage
& WINED3DUSAGE_DYNAMIC
))
1435 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1439 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1442 if (buffer
->locations
& WINED3D_LOCATION_SYSMEM
|| !(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
1444 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
1445 return E_OUTOFMEMORY
;
1448 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1450 ERR("Out of memory.\n");
1451 buffer_unload(resource
);
1452 resource_cleanup(resource
);
1453 wined3d_resource_wait_idle(resource
);
1454 return E_OUTOFMEMORY
;
1456 buffer
->maps_size
= 1;
1459 wined3d_buffer_init_data(buffer
, device
, data
);
1464 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1465 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1466 struct wined3d_buffer
**buffer
)
1468 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1469 struct wined3d_buffer_gl
*object
;
1472 TRACE("device %p, desc byte_width %u, usage %s, bind_flags %s, access %s, data %p, parent %p, "
1473 "parent_ops %p, buffer %p.\n",
1474 device
, desc
->byte_width
, debug_d3dusage(desc
->usage
),
1475 wined3d_debug_bind_flags(desc
->bind_flags
), wined3d_debug_resource_access(desc
->access
),
1476 data
, parent
, parent_ops
, buffer
);
1478 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1479 return E_OUTOFMEMORY
;
1481 object
->buffer_type_hint
= buffer_type_hint_from_bind_flags(gl_info
, desc
->bind_flags
);
1483 if (FAILED(hr
= wined3d_buffer_init(&object
->b
, device
, desc
, data
, parent
, parent_ops
)))
1485 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1490 TRACE("Created buffer %p.\n", object
);
1492 *buffer
= &object
->b
;