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 buffer_bind(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
138 context_bind_bo(context
, buffer
->buffer_type_hint
, buffer
->buffer_object
);
141 /* Context activation is done by the caller. */
142 static void buffer_destroy_buffer_object(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
144 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
145 struct wined3d_resource
*resource
= &buffer
->resource
;
147 if (!buffer
->buffer_object
)
150 /* The stream source state handler might have read the memory of the
151 * vertex buffer already and got the memory in the vbo which is not
152 * valid any longer. Dirtify the stream source to force a reload. This
153 * happens only once per changed vertexbuffer and should occur rather
155 if (resource
->bind_count
)
157 if (buffer
->bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
158 device_invalidate_state(resource
->device
, STATE_STREAMSRC
);
159 if (buffer
->bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
160 device_invalidate_state(resource
->device
, STATE_INDEXBUFFER
);
161 if (buffer
->bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
163 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX
));
164 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL
));
165 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN
));
166 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY
));
167 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL
));
168 device_invalidate_state(resource
->device
, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE
));
170 if (buffer
->bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
172 device_invalidate_state(resource
->device
, STATE_STREAM_OUTPUT
);
173 if (context
->transform_feedback_active
)
175 /* We have to make sure that transform feedback is not active
176 * when deleting a potentially bound transform feedback buffer.
177 * This may happen when the device is being destroyed. */
178 WARN("Deleting buffer object for buffer %p, disabling transform feedback.\n", buffer
);
179 context_end_transform_feedback(context
);
184 GL_EXTCALL(glDeleteBuffers(1, &buffer
->buffer_object
));
185 checkGLcall("glDeleteBuffers");
186 buffer
->buffer_object
= 0;
190 wined3d_fence_destroy(buffer
->fence
);
191 buffer
->fence
= NULL
;
193 buffer
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
196 /* Context activation is done by the caller. */
197 static BOOL
buffer_create_buffer_object(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
199 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
200 GLenum gl_usage
= GL_STATIC_DRAW
;
203 TRACE("Creating an OpenGL buffer object for wined3d_buffer %p with usage %s.\n",
204 buffer
, debug_d3dusage(buffer
->resource
.usage
));
206 /* Make sure that the gl error is cleared. Do not use checkGLcall
207 * here because checkGLcall just prints a fixme and continues. However,
208 * if an error during VBO creation occurs we can fall back to non-VBO operation
209 * with full functionality(but performance loss).
211 while (gl_info
->gl_ops
.gl
.p_glGetError() != GL_NO_ERROR
);
213 /* Basically the FVF parameter passed to CreateVertexBuffer is no good.
214 * The vertex declaration from the device determines how the data in the
215 * buffer is interpreted. This means that on each draw call the buffer has
216 * to be verified to check if the rhw and color values are in the correct
219 GL_EXTCALL(glGenBuffers(1, &buffer
->buffer_object
));
220 error
= gl_info
->gl_ops
.gl
.p_glGetError();
221 if (!buffer
->buffer_object
|| error
!= GL_NO_ERROR
)
223 ERR("Failed to create a BO with error %s (%#x).\n", debug_glerror(error
), error
);
227 buffer_bind(buffer
, context
);
228 error
= gl_info
->gl_ops
.gl
.p_glGetError();
229 if (error
!= GL_NO_ERROR
)
231 ERR("Failed to bind the BO with error %s (%#x).\n", debug_glerror(error
), error
);
235 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
237 TRACE("Buffer has WINED3DUSAGE_DYNAMIC set.\n");
238 gl_usage
= GL_STREAM_DRAW_ARB
;
240 if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
242 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_FLUSHING_UNMAP_APPLE
, GL_FALSE
));
243 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_FALSE
));
244 checkGLcall("glBufferParameteriAPPLE");
245 buffer
->flags
|= WINED3D_BUFFER_APPLESYNC
;
247 /* No setup is needed here for GL_ARB_map_buffer_range. */
250 GL_EXTCALL(glBufferData(buffer
->buffer_type_hint
, buffer
->resource
.size
, NULL
, gl_usage
));
251 error
= gl_info
->gl_ops
.gl
.p_glGetError();
252 if (error
!= GL_NO_ERROR
)
254 ERR("glBufferData failed with error %s (%#x).\n", debug_glerror(error
), error
);
258 buffer
->buffer_object_usage
= gl_usage
;
259 buffer_invalidate_bo_range(buffer
, 0, 0);
264 /* Clean up all BO init, but continue because we can work without a BO :-) */
265 ERR("Failed to create a buffer object. Continuing, but performance issues may occur.\n");
266 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
267 buffer_destroy_buffer_object(buffer
, context
);
268 buffer_clear_dirty_areas(buffer
);
272 static BOOL
buffer_process_converted_attribute(struct wined3d_buffer
*buffer
,
273 const enum wined3d_buffer_conversion_type conversion_type
,
274 const struct wined3d_stream_info_element
*attrib
, DWORD
*stride_this_run
)
276 const struct wined3d_format
*format
= attrib
->format
;
281 /* Check for some valid situations which cause us pain. One is if the buffer is used for
282 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
283 * with different strides. In the 2nd case we might have to drop conversion entirely,
284 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
288 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else.\n",
289 debug_d3dformat(format
->id
));
291 else if (attrib
->stride
!= *stride_this_run
&& *stride_this_run
)
293 FIXME("Got two concurrent strides, %d and %d.\n", attrib
->stride
, *stride_this_run
);
297 *stride_this_run
= attrib
->stride
;
298 if (buffer
->stride
!= *stride_this_run
)
300 /* We rely that this happens only on the first converted attribute that is found,
301 * if at all. See above check
303 TRACE("Reconverting because converted attributes occur, and the stride changed.\n");
304 buffer
->stride
= *stride_this_run
;
305 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY
, buffer
->conversion_map
);
306 buffer
->conversion_map
= wined3d_calloc(buffer
->stride
, sizeof(*buffer
->conversion_map
));
311 data
= ((DWORD_PTR
)attrib
->data
.addr
) % buffer
->stride
;
312 for (i
= 0; i
< format
->attribute_size
; ++i
)
314 DWORD_PTR idx
= (data
+ i
) % buffer
->stride
;
315 if (buffer
->conversion_map
[idx
] != conversion_type
)
317 TRACE("Byte %lu in vertex changed:\n", idx
);
318 TRACE(" It was type %#x, is %#x now.\n", buffer
->conversion_map
[idx
], conversion_type
);
320 buffer
->conversion_map
[idx
] = conversion_type
;
327 #define WINED3D_BUFFER_FIXUP_D3DCOLOR 0x01
328 #define WINED3D_BUFFER_FIXUP_XYZRHW 0x02
330 static BOOL
buffer_check_attribute(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
331 const struct wined3d_state
*state
, UINT attrib_idx
, DWORD fixup_flags
, DWORD
*stride_this_run
)
333 const struct wined3d_stream_info_element
*attrib
= &si
->elements
[attrib_idx
];
334 enum wined3d_format_id format
;
337 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
338 * there, on nonexistent attribs the vbo is 0.
340 if (!(si
->use_map
& (1u << attrib_idx
))
341 || state
->streams
[attrib
->stream_idx
].buffer
!= This
)
344 format
= attrib
->format
->id
;
345 /* Look for newly appeared conversion */
346 if (fixup_flags
& WINED3D_BUFFER_FIXUP_D3DCOLOR
&& format
== WINED3DFMT_B8G8R8A8_UNORM
)
348 ret
= buffer_process_converted_attribute(This
, CONV_D3DCOLOR
, attrib
, stride_this_run
);
350 else if (fixup_flags
& WINED3D_BUFFER_FIXUP_XYZRHW
&& si
->position_transformed
)
352 if (format
!= WINED3DFMT_R32G32B32A32_FLOAT
)
354 FIXME("Unexpected format %s for transformed position.\n", debug_d3dformat(format
));
358 ret
= buffer_process_converted_attribute(This
, CONV_POSITIONT
, attrib
, stride_this_run
);
360 else if (This
->conversion_map
)
362 ret
= buffer_process_converted_attribute(This
, CONV_NONE
, attrib
, stride_this_run
);
368 static BOOL
buffer_find_decl(struct wined3d_buffer
*This
, const struct wined3d_stream_info
*si
,
369 const struct wined3d_state
*state
, DWORD fixup_flags
)
371 UINT stride_this_run
= 0;
374 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
375 * Once we have our declaration there is no need to look it up again. Index buffers also never need
376 * conversion, so once the (empty) conversion structure is created don't bother checking again
378 if (This
->flags
& WINED3D_BUFFER_HASDESC
)
380 if(This
->resource
.usage
& WINED3DUSAGE_STATICDECL
) return FALSE
;
385 TRACE("No fixup required.\n");
386 if(This
->conversion_map
)
388 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
389 This
->conversion_map
= NULL
;
397 TRACE("Finding vertex buffer conversion information\n");
398 /* Certain declaration types need some fixups before we can pass them to
399 * opengl. This means D3DCOLOR attributes with fixed function vertex
400 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
401 * GL_ARB_half_float_vertex is not supported.
403 * Note for d3d8 and d3d9:
404 * The vertex buffer FVF doesn't help with finding them, we have to use
405 * the decoded vertex declaration and pick the things that concern the
406 * current buffer. A problem with this is that this can change between
407 * draws, so we have to validate the information and reprocess the buffer
408 * if it changes, and avoid false positives for performance reasons.
409 * WineD3D doesn't even know the vertex buffer any more, it is managed
410 * by the client libraries and passed to SetStreamSource and ProcessVertices
413 * We have to distinguish between vertex shaders and fixed function to
414 * pick the way we access the strided vertex information.
416 * This code sets up a per-byte array with the size of the detected
417 * stride of the arrays in the buffer. For each byte we have a field
418 * that marks the conversion needed on this byte. For example, the
419 * following declaration with fixed function vertex processing:
427 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
428 * [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]
430 * Where in this example map P means 4 component position conversion, 0
431 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
432 * conversion (red / blue swizzle).
434 * If we're doing conversion and the stride changes we have to reconvert
435 * the whole buffer. Note that we do not mind if the semantic changes,
436 * we only care for the conversion type. So if the NORMAL is replaced
437 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
438 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
439 * conversion types depend on the semantic as well, for example a FLOAT4
440 * texcoord needs no conversion while a FLOAT4 positiont needs one
443 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_POSITION
,
444 fixup_flags
, &stride_this_run
) || ret
;
445 fixup_flags
&= ~WINED3D_BUFFER_FIXUP_XYZRHW
;
447 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDWEIGHT
,
448 fixup_flags
, &stride_this_run
) || ret
;
449 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_BLENDINDICES
,
450 fixup_flags
, &stride_this_run
) || ret
;
451 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_NORMAL
,
452 fixup_flags
, &stride_this_run
) || ret
;
453 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_DIFFUSE
,
454 fixup_flags
, &stride_this_run
) || ret
;
455 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_SPECULAR
,
456 fixup_flags
, &stride_this_run
) || ret
;
457 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD0
,
458 fixup_flags
, &stride_this_run
) || ret
;
459 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD1
,
460 fixup_flags
, &stride_this_run
) || ret
;
461 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD2
,
462 fixup_flags
, &stride_this_run
) || ret
;
463 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD3
,
464 fixup_flags
, &stride_this_run
) || ret
;
465 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD4
,
466 fixup_flags
, &stride_this_run
) || ret
;
467 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD5
,
468 fixup_flags
, &stride_this_run
) || ret
;
469 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD6
,
470 fixup_flags
, &stride_this_run
) || ret
;
471 ret
= buffer_check_attribute(This
, si
, state
, WINED3D_FFP_TEXCOORD7
,
472 fixup_flags
, &stride_this_run
) || ret
;
474 if (!stride_this_run
&& This
->conversion_map
)
477 if (!ret
) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
478 HeapFree(GetProcessHeap(), 0, This
->conversion_map
);
479 This
->conversion_map
= NULL
;
483 if (ret
) TRACE("Conversion information changed\n");
488 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
490 DWORD src_color
= *dst_color
;
492 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
493 * endianness. If we want this to work on big-endian machines as well we
494 * have to consider more things.
496 * 0xff000000: Alpha mask
497 * 0x00ff0000: Blue mask
498 * 0x0000ff00: Green mask
499 * 0x000000ff: Red mask
502 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
503 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
504 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
506 return sizeof(*dst_color
);
509 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
511 /* rhw conversion like in position_float4(). */
512 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
514 float w
= 1.0f
/ p
->w
;
524 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
526 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
528 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
533 /* Context activation is done by the caller. */
534 static void wined3d_buffer_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
535 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_map_range
*ranges
)
537 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
538 const struct wined3d_map_range
*range
;
540 buffer_bind(buffer
, context
);
542 while (range_count
--)
544 range
= &ranges
[range_count
];
545 GL_EXTCALL(glBufferSubData(buffer
->buffer_type_hint
,
546 range
->offset
, range
->size
, (BYTE
*)data
+ range
->offset
- data_offset
));
548 checkGLcall("glBufferSubData");
551 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
553 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
556 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
558 ERR("Failed to load system memory.\n");
561 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
563 /* Now for each vertex in the buffer that needs conversion. */
564 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
566 if (!(data
= HeapAlloc(GetProcessHeap(), 0, buffer
->resource
.size
)))
568 ERR("Out of memory.\n");
572 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
574 start
= buffer
->maps
[range_idx
].offset
;
575 end
= start
+ buffer
->maps
[range_idx
].size
;
577 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
578 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
580 for (j
= 0; j
< buffer
->stride
;)
582 switch (buffer
->conversion_map
[j
])
589 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
592 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
595 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
602 wined3d_buffer_upload_ranges(buffer
, context
, data
, 0, buffer
->modified_areas
, buffer
->maps
);
604 HeapFree(GetProcessHeap(), 0, data
);
607 static BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
608 struct wined3d_context
*context
, DWORD location
)
612 case WINED3D_LOCATION_SYSMEM
:
613 if (buffer
->resource
.heap_memory
)
616 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
618 ERR("Failed to allocate system memory.\n");
623 case WINED3D_LOCATION_BUFFER
:
624 if (buffer
->buffer_object
)
627 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
629 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
632 return buffer_create_buffer_object(buffer
, context
);
635 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
640 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
641 struct wined3d_context
*context
, DWORD location
)
643 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
645 TRACE("buffer %p, context %p, location %s.\n",
646 buffer
, context
, wined3d_debug_location(location
));
648 if (buffer
->locations
& location
)
650 TRACE("Location (%#x) is already up to date.\n", location
);
654 if (!buffer
->locations
)
656 ERR("Buffer %p does not have any up to date location.\n", buffer
);
657 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
658 return wined3d_buffer_load_location(buffer
, context
, location
);
661 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
663 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
666 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
668 TRACE("Buffer previously discarded, nothing to do.\n");
669 wined3d_buffer_validate_location(buffer
, location
);
670 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
676 case WINED3D_LOCATION_SYSMEM
:
677 buffer_bind(buffer
, context
);
678 GL_EXTCALL(glGetBufferSubData(buffer
->buffer_type_hint
, 0, buffer
->resource
.size
,
679 buffer
->resource
.heap_memory
));
680 checkGLcall("buffer download");
683 case WINED3D_LOCATION_BUFFER
:
684 if (!buffer
->conversion_map
)
685 wined3d_buffer_upload_ranges(buffer
, context
, buffer
->resource
.heap_memory
,
686 0, buffer
->modified_areas
, buffer
->maps
);
688 buffer_conversion_upload(buffer
, context
);
692 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
696 wined3d_buffer_validate_location(buffer
, location
);
697 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
698 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
699 wined3d_buffer_evict_sysmem(buffer
);
704 /* Context activation is done by the caller. */
705 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
707 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
708 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
709 return buffer
->resource
.heap_memory
;
712 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
,
713 struct wined3d_bo_address
*data
, DWORD locations
)
715 TRACE("buffer %p, data %p, locations %s.\n",
716 buffer
, data
, wined3d_debug_location(locations
));
718 if (locations
& WINED3D_LOCATION_BUFFER
)
720 data
->buffer_object
= buffer
->buffer_object
;
722 return WINED3D_LOCATION_BUFFER
;
724 if (locations
& WINED3D_LOCATION_SYSMEM
)
726 data
->buffer_object
= 0;
727 data
->addr
= buffer
->resource
.heap_memory
;
728 return WINED3D_LOCATION_SYSMEM
;
731 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
732 data
->buffer_object
= 0;
737 static void buffer_unload(struct wined3d_resource
*resource
)
739 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
741 TRACE("buffer %p.\n", buffer
);
743 if (buffer
->buffer_object
)
745 struct wined3d_context
*context
;
747 context
= context_acquire(resource
->device
, NULL
, 0);
749 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
750 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
751 buffer_destroy_buffer_object(buffer
, context
);
752 buffer_clear_dirty_areas(buffer
);
754 context_release(context
);
756 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
757 buffer
->conversion_map
= NULL
;
759 buffer
->conversion_stride
= 0;
760 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
763 resource_unload(resource
);
766 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
768 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
769 buffer_unload(&buffer
->resource
);
772 static void wined3d_buffer_destroy_object(void *object
)
774 struct wined3d_buffer
*buffer
= object
;
775 struct wined3d_context
*context
;
777 if (buffer
->buffer_object
)
779 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
780 buffer_destroy_buffer_object(buffer
, context
);
781 context_release(context
);
783 HeapFree(GetProcessHeap(), 0, buffer
->conversion_map
);
786 HeapFree(GetProcessHeap(), 0, buffer
->maps
);
787 HeapFree(GetProcessHeap(), 0, buffer
);
790 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
792 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
794 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
798 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
799 resource_cleanup(&buffer
->resource
);
800 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
806 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
808 TRACE("buffer %p.\n", buffer
);
810 return buffer
->resource
.parent
;
813 /* The caller provides a context and binds the buffer */
814 static void buffer_sync_apple(struct wined3d_buffer
*buffer
, DWORD flags
, const struct wined3d_gl_info
*gl_info
)
816 enum wined3d_fence_result ret
;
819 /* No fencing needs to be done if the app promises not to overwrite
821 if (flags
& WINED3D_MAP_NOOVERWRITE
)
824 if (flags
& WINED3D_MAP_DISCARD
)
826 GL_EXTCALL(glBufferData(buffer
->buffer_type_hint
, buffer
->resource
.size
, NULL
, buffer
->buffer_object_usage
));
827 checkGLcall("glBufferData");
833 TRACE("Creating fence for buffer %p.\n", buffer
);
835 if (FAILED(hr
= wined3d_fence_create(buffer
->resource
.device
, &buffer
->fence
)))
837 if (hr
== WINED3DERR_NOTAVAILABLE
)
838 FIXME("Fences not supported, dropping async buffer locks.\n");
840 ERR("Failed to create fence, hr %#x.\n", hr
);
844 /* Since we don't know about old draws a glFinish is needed once */
845 gl_info
->gl_ops
.gl
.p_glFinish();
849 TRACE("Synchronizing buffer %p.\n", buffer
);
850 ret
= wined3d_fence_wait(buffer
->fence
, buffer
->resource
.device
);
853 case WINED3D_FENCE_NOT_STARTED
:
854 case WINED3D_FENCE_OK
:
858 case WINED3D_FENCE_WRONG_THREAD
:
859 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
863 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret
);
870 wined3d_fence_destroy(buffer
->fence
);
871 buffer
->fence
= NULL
;
874 gl_info
->gl_ops
.gl
.p_glFinish();
875 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
876 checkGLcall("glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
877 buffer
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
880 static void buffer_mark_used(struct wined3d_buffer
*buffer
)
882 buffer
->flags
&= ~WINED3D_BUFFER_DISCARD
;
885 /* Context activation is done by the caller. */
886 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
887 const struct wined3d_state
*state
)
889 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
890 BOOL decl_changed
= FALSE
;
892 TRACE("buffer %p.\n", buffer
);
894 if (buffer
->resource
.map_count
)
896 WARN("Buffer is mapped, skipping preload.\n");
900 buffer_mark_used(buffer
);
902 /* TODO: Make converting independent from VBOs */
903 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
905 /* Not doing any conversion */
909 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
911 ERR("Failed to prepare buffer location.\n");
915 /* Reading the declaration makes only sense if we have valid state information
916 * (i.e., if this function is called during draws). */
919 DWORD fixup_flags
= 0;
923 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !context
->d3d_info
->ffp_generic_attributes
)
924 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
925 if (!context
->d3d_info
->xyzrhw
)
926 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
929 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
930 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
933 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
935 ++buffer
->draw_count
;
936 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
937 buffer
->decl_change_count
= 0;
938 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
939 buffer
->full_conversion_count
= 0;
943 /* If applications change the declaration over and over, reconverting all the time is a huge
944 * performance hit. So count the declaration changes and release the VBO if there are too many
945 * of them (and thus stop converting)
949 ++buffer
->decl_change_count
;
950 buffer
->draw_count
= 0;
952 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
953 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
955 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
956 wined3d_buffer_drop_bo(buffer
);
960 /* The declaration changed, reload the whole buffer. */
961 WARN("Reloading buffer because of a vertex declaration change.\n");
962 buffer_invalidate_bo_range(buffer
, 0, 0);
966 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
967 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
968 * decl changes and reset the decl change count after a specific number of them
970 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
972 ++buffer
->full_conversion_count
;
973 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
975 FIXME("Too many full buffer conversions, stopping converting.\n");
976 wined3d_buffer_drop_bo(buffer
);
982 ++buffer
->draw_count
;
983 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
984 buffer
->decl_change_count
= 0;
985 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
986 buffer
->full_conversion_count
= 0;
990 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
991 ERR("Failed to load buffer location.\n");
994 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
996 TRACE("buffer %p.\n", buffer
);
998 return &buffer
->resource
;
1001 static HRESULT
wined3d_buffer_map(struct wined3d_buffer
*buffer
, UINT offset
, UINT size
, BYTE
**data
, DWORD flags
)
1003 struct wined3d_device
*device
= buffer
->resource
.device
;
1004 struct wined3d_context
*context
;
1008 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer
, offset
, size
, data
, flags
);
1010 count
= ++buffer
->resource
.map_count
;
1012 if (buffer
->buffer_object
)
1014 unsigned int dirty_offset
= offset
, dirty_size
= size
;
1016 /* DISCARD invalidates the entire buffer, regardless of the specified
1017 * offset and size. Some applications also depend on the entire buffer
1018 * being uploaded in that case. Two such applications are Port Royale
1019 * and Darkstar One. */
1020 if (flags
& WINED3D_MAP_DISCARD
)
1026 if (!(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
| WINED3D_MAP_READONLY
))
1027 || ((flags
& WINED3D_MAP_READONLY
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1028 || buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
1030 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1032 context
= context_acquire(device
, NULL
, 0);
1033 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1034 context_release(context
);
1037 if (!(flags
& WINED3D_MAP_READONLY
))
1038 wined3d_buffer_invalidate_range(buffer
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
1042 const struct wined3d_gl_info
*gl_info
;
1044 context
= context_acquire(device
, NULL
, 0);
1045 gl_info
= context
->gl_info
;
1047 if (flags
& WINED3D_MAP_DISCARD
)
1048 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1050 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1052 if (!(flags
& WINED3D_MAP_READONLY
))
1053 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1055 if ((flags
& WINED3D_MAP_DISCARD
) && buffer
->resource
.heap_memory
)
1056 wined3d_buffer_evict_sysmem(buffer
);
1060 buffer_bind(buffer
, context
);
1062 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1063 * multitexture fill rate test seems to depend on this. When
1064 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1065 * driver is free to discard the previous contents of the
1066 * buffer. The r600g driver only does this when the buffer is
1067 * currently in use, while the proprietary NVIDIA driver
1068 * appears to do this unconditionally. */
1069 if (buffer
->flags
& WINED3D_BUFFER_DISCARD
)
1070 flags
&= ~WINED3D_MAP_DISCARD
;
1072 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1074 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
1075 buffer
->map_ptr
= GL_EXTCALL(glMapBufferRange(buffer
->buffer_type_hint
,
1076 0, buffer
->resource
.size
, mapflags
));
1077 checkGLcall("glMapBufferRange");
1081 if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1082 buffer_sync_apple(buffer
, flags
, gl_info
);
1083 buffer
->map_ptr
= GL_EXTCALL(glMapBuffer(buffer
->buffer_type_hint
,
1085 checkGLcall("glMapBuffer");
1088 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1090 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1092 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1093 checkGLcall("glUnmapBuffer");
1094 buffer
->map_ptr
= NULL
;
1096 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1098 /* The extra copy is more expensive than not using VBOs at
1099 * all on the Nvidia Linux driver, which is the only driver
1100 * that returns unaligned pointers.
1102 TRACE("Dynamic buffer, dropping VBO.\n");
1103 wined3d_buffer_drop_bo(buffer
);
1107 TRACE("Falling back to doublebuffered operation.\n");
1108 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1109 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1111 TRACE("New pointer is %p.\n", buffer
->resource
.heap_memory
);
1115 context_release(context
);
1118 if (flags
& WINED3D_MAP_DISCARD
)
1119 buffer
->flags
|= WINED3D_BUFFER_DISCARD
;
1122 base
= buffer
->map_ptr
? buffer
->map_ptr
: buffer
->resource
.heap_memory
;
1123 *data
= base
+ offset
;
1125 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1126 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1131 static void wined3d_buffer_unmap(struct wined3d_buffer
*buffer
)
1135 TRACE("buffer %p.\n", buffer
);
1137 /* In the case that the number of Unmap calls > the
1138 * number of Map calls, d3d returns always D3D_OK.
1139 * This is also needed to prevent Map from returning garbage on
1140 * the next call (this will happen if the lock_count is < 0). */
1141 if (!buffer
->resource
.map_count
)
1143 WARN("Unmap called without a previous map call.\n");
1147 if (--buffer
->resource
.map_count
)
1149 /* Delay loading the buffer until everything is unlocked */
1150 TRACE("Ignoring unmap.\n");
1154 if (buffer
->map_ptr
)
1156 struct wined3d_device
*device
= buffer
->resource
.device
;
1157 const struct wined3d_gl_info
*gl_info
;
1158 struct wined3d_context
*context
;
1160 context
= context_acquire(device
, NULL
, 0);
1161 gl_info
= context
->gl_info
;
1163 buffer_bind(buffer
, context
);
1165 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1167 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1169 GL_EXTCALL(glFlushMappedBufferRange(buffer
->buffer_type_hint
,
1170 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1171 checkGLcall("glFlushMappedBufferRange");
1174 else if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1176 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1178 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer
->buffer_type_hint
,
1179 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1180 checkGLcall("glFlushMappedBufferRangeAPPLE");
1184 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1185 if (wined3d_settings
.strict_draw_ordering
)
1186 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
1187 context_release(context
);
1189 buffer_clear_dirty_areas(buffer
);
1190 buffer
->map_ptr
= NULL
;
1194 HRESULT
wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1195 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1197 const struct wined3d_gl_info
*gl_info
;
1198 struct wined3d_bo_address dst
, src
;
1199 struct wined3d_context
*context
;
1200 struct wined3d_device
*device
;
1201 BYTE
*dst_ptr
, *src_ptr
;
1204 buffer_mark_used(dst_buffer
);
1205 buffer_mark_used(src_buffer
);
1207 device
= dst_buffer
->resource
.device
;
1209 context
= context_acquire(device
, NULL
, 0);
1210 gl_info
= context
->gl_info
;
1212 dst_location
= wined3d_buffer_get_memory(dst_buffer
, &dst
, dst_buffer
->locations
);
1213 dst
.addr
+= dst_offset
;
1215 wined3d_buffer_get_memory(src_buffer
, &src
, src_buffer
->locations
);
1216 src
.addr
+= src_offset
;
1218 if (dst
.buffer_object
&& src
.buffer_object
)
1220 if (gl_info
->supported
[ARB_COPY_BUFFER
])
1222 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src
.buffer_object
));
1223 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst
.buffer_object
));
1224 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
1225 src_offset
, dst_offset
, size
));
1226 checkGLcall("direct buffer copy");
1230 src_ptr
= context_map_bo_address(context
, &src
, size
, src_buffer
->buffer_type_hint
, WINED3D_MAP_READONLY
);
1231 dst_ptr
= context_map_bo_address(context
, &dst
, size
, dst_buffer
->buffer_type_hint
, 0);
1233 memcpy(dst_ptr
, src_ptr
, size
);
1235 context_unmap_bo_address(context
, &dst
, dst_buffer
->buffer_type_hint
);
1236 context_unmap_bo_address(context
, &src
, src_buffer
->buffer_type_hint
);
1239 else if (!dst
.buffer_object
&& src
.buffer_object
)
1241 buffer_bind(src_buffer
, context
);
1242 GL_EXTCALL(glGetBufferSubData(src_buffer
->buffer_type_hint
, src_offset
, size
, dst
.addr
));
1243 checkGLcall("buffer download");
1245 else if (dst
.buffer_object
&& !src
.buffer_object
)
1247 buffer_bind(dst_buffer
, context
);
1248 GL_EXTCALL(glBufferSubData(dst_buffer
->buffer_type_hint
, dst_offset
, size
, src
.addr
));
1249 checkGLcall("buffer upload");
1253 memcpy(dst
.addr
, src
.addr
, size
);
1256 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1258 context_release(context
);
1262 void wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1263 const struct wined3d_box
*box
, const void *data
)
1265 struct wined3d_map_range range
;
1269 range
.offset
= box
->left
;
1270 range
.size
= box
->right
- box
->left
;
1275 range
.size
= buffer
->resource
.size
;
1278 wined3d_buffer_upload_ranges(buffer
, context
, data
, range
.offset
, 1, &range
);
1281 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1283 return wined3d_buffer_incref(buffer_from_resource(resource
));
1286 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1288 return wined3d_buffer_decref(buffer_from_resource(resource
));
1291 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1293 struct wined3d_context
*context
;
1295 context
= context_acquire(resource
->device
, NULL
, 0);
1296 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1297 context_release(context
);
1300 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1301 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1303 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1306 if (sub_resource_idx
)
1308 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1309 return E_INVALIDARG
;
1315 size
= box
->right
- box
->left
;
1322 map_desc
->row_pitch
= map_desc
->slice_pitch
= buffer
->desc
.byte_width
;
1323 return wined3d_buffer_map(buffer
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1326 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1328 if (sub_resource_idx
)
1330 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1331 return E_INVALIDARG
;
1334 wined3d_buffer_unmap(buffer_from_resource(resource
));
1338 static const struct wined3d_resource_ops buffer_resource_ops
=
1340 buffer_resource_incref
,
1341 buffer_resource_decref
,
1342 buffer_resource_preload
,
1344 buffer_resource_sub_resource_map
,
1345 buffer_resource_sub_resource_unmap
,
1348 static GLenum
buffer_type_hint_from_bind_flags(const struct wined3d_gl_info
*gl_info
,
1349 unsigned int bind_flags
)
1351 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
1352 return GL_ELEMENT_ARRAY_BUFFER
;
1354 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
1355 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1356 return GL_TEXTURE_BUFFER
;
1358 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1359 return GL_UNIFORM_BUFFER
;
1361 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1362 return GL_TRANSFORM_FEEDBACK_BUFFER
;
1364 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
1365 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
1367 return GL_ARRAY_BUFFER
;
1370 static HRESULT
buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1371 UINT size
, DWORD usage
, enum wined3d_format_id format_id
, enum wined3d_pool pool
, unsigned int bind_flags
,
1372 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1374 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1375 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, usage
);
1376 BOOL dynamic_buffer_ok
;
1381 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1382 return E_INVALIDARG
;
1385 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& size
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1387 WARN("Size %#x is not suitably aligned for constant buffers.\n", size
);
1388 return E_INVALIDARG
;
1391 if (data
&& !data
->data
)
1393 WARN("Invalid sub-resource data specified.\n");
1394 return E_INVALIDARG
;
1397 hr
= resource_init(&buffer
->resource
, device
, WINED3D_RTYPE_BUFFER
, format
,
1398 WINED3D_MULTISAMPLE_NONE
, 0, usage
, pool
, size
, 1, 1, size
, parent
, parent_ops
, &buffer_resource_ops
);
1401 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1404 buffer
->buffer_type_hint
= buffer_type_hint_from_bind_flags(gl_info
, bind_flags
);
1405 buffer
->bind_flags
= bind_flags
;
1406 buffer
->locations
= WINED3D_LOCATION_SYSMEM
;
1408 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1409 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
,
1410 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
);
1412 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
|| pool
== WINED3D_POOL_MANAGED
)
1414 /* SWvp and managed buffers always return the same pointer in buffer
1415 * maps and retain data in DISCARD maps. Keep a system memory copy of
1416 * the buffer to provide the same behavior to the application. */
1417 TRACE("Using doublebuffer mode.\n");
1418 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1421 /* Observations show that draw_primitive_immediate_mode() is faster on
1422 * dynamic vertex buffers than converting + draw_primitive_arrays().
1423 * (Half-Life 2 and others.) */
1424 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1426 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1428 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1430 else if (buffer
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
1432 TRACE("Not creating a BO because the buffer is in system memory.\n");
1434 else if (!dynamic_buffer_ok
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
1436 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1440 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1443 if (!(buffer
->maps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
->maps
))))
1445 ERR("Out of memory.\n");
1446 buffer_unload(&buffer
->resource
);
1447 resource_cleanup(&buffer
->resource
);
1448 wined3d_resource_wait_idle(&buffer
->resource
);
1449 return E_OUTOFMEMORY
;
1451 buffer
->maps_size
= 1;
1454 wined3d_device_update_sub_resource(device
, &buffer
->resource
,
1455 0, NULL
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1460 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1461 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1462 struct wined3d_buffer
**buffer
)
1464 struct wined3d_buffer
*object
;
1467 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1468 device
, desc
, data
, parent
, parent_ops
, buffer
);
1470 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1471 return E_OUTOFMEMORY
;
1473 FIXME("Ignoring access flags (pool).\n");
1475 if (FAILED(hr
= buffer_init(object
, device
, desc
->byte_width
, desc
->usage
, WINED3DFMT_UNKNOWN
,
1476 WINED3D_POOL_MANAGED
, desc
->bind_flags
, data
, parent
, parent_ops
)))
1478 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1479 HeapFree(GetProcessHeap(), 0, object
);
1482 object
->desc
= *desc
;
1484 TRACE("Created buffer %p.\n", object
);
1491 HRESULT CDECL
wined3d_buffer_create_vb(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1492 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1494 struct wined3d_buffer
*object
;
1497 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1498 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1500 if (pool
== WINED3D_POOL_SCRATCH
)
1502 /* The d3d9 tests shows that this is not allowed. It doesn't make much
1503 * sense anyway, SCRATCH buffers wouldn't be usable anywhere. */
1504 WARN("Vertex buffer in WINED3D_POOL_SCRATCH requested, returning WINED3DERR_INVALIDCALL.\n");
1506 return WINED3DERR_INVALIDCALL
;
1509 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1513 return WINED3DERR_OUTOFVIDEOMEMORY
;
1516 hr
= buffer_init(object
, device
, size
, usage
, WINED3DFMT_UNKNOWN
,
1517 pool
, WINED3D_BIND_VERTEX_BUFFER
, NULL
, parent
, parent_ops
);
1520 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1521 HeapFree(GetProcessHeap(), 0, object
);
1525 TRACE("Created buffer %p.\n", object
);
1531 HRESULT CDECL
wined3d_buffer_create_ib(struct wined3d_device
*device
, UINT size
, DWORD usage
, enum wined3d_pool pool
,
1532 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_buffer
**buffer
)
1534 struct wined3d_buffer
*object
;
1537 TRACE("device %p, size %u, usage %#x, pool %#x, parent %p, parent_ops %p, buffer %p.\n",
1538 device
, size
, usage
, pool
, parent
, parent_ops
, buffer
);
1540 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1544 return WINED3DERR_OUTOFVIDEOMEMORY
;
1547 hr
= buffer_init(object
, device
, size
, usage
| WINED3DUSAGE_STATICDECL
,
1548 WINED3DFMT_UNKNOWN
, pool
, WINED3D_BIND_INDEX_BUFFER
, NULL
,
1549 parent
, parent_ops
);
1552 WARN("Failed to initialize buffer, hr %#x\n", hr
);
1553 HeapFree(GetProcessHeap(), 0, object
);
1557 TRACE("Created buffer %p.\n", object
);