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 heap_free(buffer
->conversion_map
);
306 buffer
->conversion_map
= heap_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 heap_free(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
)
478 ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
479 heap_free(This
->conversion_map
);
480 This
->conversion_map
= NULL
;
484 if (ret
) TRACE("Conversion information changed\n");
489 static inline unsigned int fixup_d3dcolor(DWORD
*dst_color
)
491 DWORD src_color
= *dst_color
;
493 /* Color conversion like in draw_primitive_immediate_mode(). Watch out for
494 * endianness. If we want this to work on big-endian machines as well we
495 * have to consider more things.
497 * 0xff000000: Alpha mask
498 * 0x00ff0000: Blue mask
499 * 0x0000ff00: Green mask
500 * 0x000000ff: Red mask
503 *dst_color
|= (src_color
& 0xff00ff00u
); /* Alpha Green */
504 *dst_color
|= (src_color
& 0x00ff0000u
) >> 16; /* Red */
505 *dst_color
|= (src_color
& 0x000000ffu
) << 16; /* Blue */
507 return sizeof(*dst_color
);
510 static inline unsigned int fixup_transformed_pos(struct wined3d_vec4
*p
)
512 /* rhw conversion like in position_float4(). */
513 if (p
->w
!= 1.0f
&& p
->w
!= 0.0f
)
515 float w
= 1.0f
/ p
->w
;
525 ULONG CDECL
wined3d_buffer_incref(struct wined3d_buffer
*buffer
)
527 ULONG refcount
= InterlockedIncrement(&buffer
->resource
.ref
);
529 TRACE("%p increasing refcount to %u.\n", buffer
, refcount
);
534 /* Context activation is done by the caller. */
535 static void wined3d_buffer_upload_ranges(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
536 const void *data
, unsigned int data_offset
, unsigned int range_count
, const struct wined3d_map_range
*ranges
)
538 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
539 const struct wined3d_map_range
*range
;
541 buffer_bind(buffer
, context
);
543 while (range_count
--)
545 range
= &ranges
[range_count
];
546 GL_EXTCALL(glBufferSubData(buffer
->buffer_type_hint
,
547 range
->offset
, range
->size
, (BYTE
*)data
+ range
->offset
- data_offset
));
549 checkGLcall("glBufferSubData");
552 static void buffer_conversion_upload(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
554 unsigned int i
, j
, range_idx
, start
, end
, vertex_count
;
557 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
559 ERR("Failed to load system memory.\n");
562 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
564 /* Now for each vertex in the buffer that needs conversion. */
565 vertex_count
= buffer
->resource
.size
/ buffer
->stride
;
567 if (!(data
= heap_alloc(buffer
->resource
.size
)))
569 ERR("Out of memory.\n");
573 for (range_idx
= 0; range_idx
< buffer
->modified_areas
; ++range_idx
)
575 start
= buffer
->maps
[range_idx
].offset
;
576 end
= start
+ buffer
->maps
[range_idx
].size
;
578 memcpy(data
+ start
, (BYTE
*)buffer
->resource
.heap_memory
+ start
, end
- start
);
579 for (i
= start
/ buffer
->stride
; i
< min((end
/ buffer
->stride
) + 1, vertex_count
); ++i
)
581 for (j
= 0; j
< buffer
->stride
;)
583 switch (buffer
->conversion_map
[j
])
590 j
+= fixup_d3dcolor((DWORD
*) (data
+ i
* buffer
->stride
+ j
));
593 j
+= fixup_transformed_pos((struct wined3d_vec4
*) (data
+ i
* buffer
->stride
+ j
));
596 FIXME("Unimplemented conversion %d in shifted conversion.\n", buffer
->conversion_map
[j
]);
603 wined3d_buffer_upload_ranges(buffer
, context
, data
, 0, buffer
->modified_areas
, buffer
->maps
);
608 static BOOL
wined3d_buffer_prepare_location(struct wined3d_buffer
*buffer
,
609 struct wined3d_context
*context
, DWORD location
)
613 case WINED3D_LOCATION_SYSMEM
:
614 if (buffer
->resource
.heap_memory
)
617 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
621 case WINED3D_LOCATION_BUFFER
:
622 if (buffer
->buffer_object
)
625 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
627 WARN("Trying to create BO for buffer %p with no WINED3D_BUFFER_USE_BO.\n", buffer
);
630 return buffer_create_buffer_object(buffer
, context
);
633 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
638 BOOL
wined3d_buffer_load_location(struct wined3d_buffer
*buffer
,
639 struct wined3d_context
*context
, DWORD location
)
641 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
643 TRACE("buffer %p, context %p, location %s.\n",
644 buffer
, context
, wined3d_debug_location(location
));
646 if (buffer
->locations
& location
)
648 TRACE("Location (%#x) is already up to date.\n", location
);
652 if (!buffer
->locations
)
654 ERR("Buffer %p does not have any up to date location.\n", buffer
);
655 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
656 return wined3d_buffer_load_location(buffer
, context
, location
);
659 TRACE("Current buffer location %s.\n", wined3d_debug_location(buffer
->locations
));
661 if (!wined3d_buffer_prepare_location(buffer
, context
, location
))
664 if (buffer
->locations
& WINED3D_LOCATION_DISCARDED
)
666 TRACE("Buffer previously discarded, nothing to do.\n");
667 wined3d_buffer_validate_location(buffer
, location
);
668 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_DISCARDED
);
674 case WINED3D_LOCATION_SYSMEM
:
675 buffer_bind(buffer
, context
);
676 GL_EXTCALL(glGetBufferSubData(buffer
->buffer_type_hint
, 0, buffer
->resource
.size
,
677 buffer
->resource
.heap_memory
));
678 checkGLcall("buffer download");
681 case WINED3D_LOCATION_BUFFER
:
682 if (!buffer
->conversion_map
)
683 wined3d_buffer_upload_ranges(buffer
, context
, buffer
->resource
.heap_memory
,
684 0, buffer
->modified_areas
, buffer
->maps
);
686 buffer_conversion_upload(buffer
, context
);
690 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
694 wined3d_buffer_validate_location(buffer
, location
);
695 if (buffer
->resource
.heap_memory
&& location
== WINED3D_LOCATION_BUFFER
696 && !(buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
))
697 wined3d_buffer_evict_sysmem(buffer
);
702 /* Context activation is done by the caller. */
703 BYTE
*wined3d_buffer_load_sysmem(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
)
705 if (wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
))
706 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
707 return buffer
->resource
.heap_memory
;
710 DWORD
wined3d_buffer_get_memory(struct wined3d_buffer
*buffer
,
711 struct wined3d_bo_address
*data
, DWORD locations
)
713 TRACE("buffer %p, data %p, locations %s.\n",
714 buffer
, data
, wined3d_debug_location(locations
));
716 if (locations
& WINED3D_LOCATION_BUFFER
)
718 data
->buffer_object
= buffer
->buffer_object
;
720 return WINED3D_LOCATION_BUFFER
;
722 if (locations
& WINED3D_LOCATION_SYSMEM
)
724 data
->buffer_object
= 0;
725 data
->addr
= buffer
->resource
.heap_memory
;
726 return WINED3D_LOCATION_SYSMEM
;
729 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
730 data
->buffer_object
= 0;
735 static void buffer_unload(struct wined3d_resource
*resource
)
737 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
739 TRACE("buffer %p.\n", buffer
);
741 if (buffer
->buffer_object
)
743 struct wined3d_context
*context
;
745 context
= context_acquire(resource
->device
, NULL
, 0);
747 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
748 wined3d_buffer_invalidate_location(buffer
, WINED3D_LOCATION_BUFFER
);
749 buffer_destroy_buffer_object(buffer
, context
);
750 buffer_clear_dirty_areas(buffer
);
752 context_release(context
);
754 heap_free(buffer
->conversion_map
);
755 buffer
->conversion_map
= NULL
;
757 buffer
->conversion_stride
= 0;
758 buffer
->flags
&= ~WINED3D_BUFFER_HASDESC
;
761 resource_unload(resource
);
764 static void wined3d_buffer_drop_bo(struct wined3d_buffer
*buffer
)
766 buffer
->flags
&= ~WINED3D_BUFFER_USE_BO
;
767 buffer_unload(&buffer
->resource
);
770 static void wined3d_buffer_destroy_object(void *object
)
772 struct wined3d_buffer
*buffer
= object
;
773 struct wined3d_context
*context
;
775 if (buffer
->buffer_object
)
777 context
= context_acquire(buffer
->resource
.device
, NULL
, 0);
778 buffer_destroy_buffer_object(buffer
, context
);
779 context_release(context
);
781 heap_free(buffer
->conversion_map
);
784 heap_free(buffer
->maps
);
788 ULONG CDECL
wined3d_buffer_decref(struct wined3d_buffer
*buffer
)
790 ULONG refcount
= InterlockedDecrement(&buffer
->resource
.ref
);
792 TRACE("%p decreasing refcount to %u.\n", buffer
, refcount
);
796 buffer
->resource
.parent_ops
->wined3d_object_destroyed(buffer
->resource
.parent
);
797 resource_cleanup(&buffer
->resource
);
798 wined3d_cs_destroy_object(buffer
->resource
.device
->cs
, wined3d_buffer_destroy_object
, buffer
);
804 void * CDECL
wined3d_buffer_get_parent(const struct wined3d_buffer
*buffer
)
806 TRACE("buffer %p.\n", buffer
);
808 return buffer
->resource
.parent
;
811 /* The caller provides a context and binds the buffer */
812 static void buffer_sync_apple(struct wined3d_buffer
*buffer
, DWORD flags
, const struct wined3d_gl_info
*gl_info
)
814 enum wined3d_fence_result ret
;
817 /* No fencing needs to be done if the app promises not to overwrite
819 if (flags
& WINED3D_MAP_NOOVERWRITE
)
822 if (flags
& WINED3D_MAP_DISCARD
)
824 GL_EXTCALL(glBufferData(buffer
->buffer_type_hint
, buffer
->resource
.size
, NULL
, buffer
->buffer_object_usage
));
825 checkGLcall("glBufferData");
831 TRACE("Creating fence for buffer %p.\n", buffer
);
833 if (FAILED(hr
= wined3d_fence_create(buffer
->resource
.device
, &buffer
->fence
)))
835 if (hr
== WINED3DERR_NOTAVAILABLE
)
836 FIXME("Fences not supported, dropping async buffer locks.\n");
838 ERR("Failed to create fence, hr %#x.\n", hr
);
842 /* Since we don't know about old draws a glFinish is needed once */
843 gl_info
->gl_ops
.gl
.p_glFinish();
847 TRACE("Synchronizing buffer %p.\n", buffer
);
848 ret
= wined3d_fence_wait(buffer
->fence
, buffer
->resource
.device
);
851 case WINED3D_FENCE_NOT_STARTED
:
852 case WINED3D_FENCE_OK
:
856 case WINED3D_FENCE_WRONG_THREAD
:
857 WARN("Cannot synchronize buffer lock due to a thread conflict.\n");
861 ERR("wined3d_fence_wait() returned %u, dropping async buffer locks.\n", ret
);
868 wined3d_fence_destroy(buffer
->fence
);
869 buffer
->fence
= NULL
;
872 gl_info
->gl_ops
.gl
.p_glFinish();
873 GL_EXTCALL(glBufferParameteriAPPLE(buffer
->buffer_type_hint
, GL_BUFFER_SERIALIZED_MODIFY_APPLE
, GL_TRUE
));
874 checkGLcall("glBufferParameteriAPPLE(buffer->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
875 buffer
->flags
&= ~WINED3D_BUFFER_APPLESYNC
;
878 static void buffer_mark_used(struct wined3d_buffer
*buffer
)
880 buffer
->flags
&= ~WINED3D_BUFFER_DISCARD
;
883 /* Context activation is done by the caller. */
884 void wined3d_buffer_load(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
885 const struct wined3d_state
*state
)
887 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
888 BOOL decl_changed
= FALSE
;
890 TRACE("buffer %p.\n", buffer
);
892 if (buffer
->resource
.map_count
)
894 WARN("Buffer is mapped, skipping preload.\n");
898 buffer_mark_used(buffer
);
900 /* TODO: Make converting independent from VBOs */
901 if (!(buffer
->flags
& WINED3D_BUFFER_USE_BO
))
903 /* Not doing any conversion */
907 if (!wined3d_buffer_prepare_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
909 ERR("Failed to prepare buffer location.\n");
913 /* Reading the declaration makes only sense if we have valid state information
914 * (i.e., if this function is called during draws). */
917 DWORD fixup_flags
= 0;
921 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !context
->d3d_info
->ffp_generic_attributes
)
922 fixup_flags
|= WINED3D_BUFFER_FIXUP_D3DCOLOR
;
923 if (!context
->d3d_info
->xyzrhw
)
924 fixup_flags
|= WINED3D_BUFFER_FIXUP_XYZRHW
;
927 decl_changed
= buffer_find_decl(buffer
, &context
->stream_info
, state
, fixup_flags
);
928 buffer
->flags
|= WINED3D_BUFFER_HASDESC
;
931 if (!decl_changed
&& !(buffer
->flags
& WINED3D_BUFFER_HASDESC
&& buffer_is_dirty(buffer
)))
933 ++buffer
->draw_count
;
934 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
935 buffer
->decl_change_count
= 0;
936 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
937 buffer
->full_conversion_count
= 0;
941 /* If applications change the declaration over and over, reconverting all the time is a huge
942 * performance hit. So count the declaration changes and release the VBO if there are too many
943 * of them (and thus stop converting)
947 ++buffer
->decl_change_count
;
948 buffer
->draw_count
= 0;
950 if (buffer
->decl_change_count
> VB_MAXDECLCHANGES
951 || (buffer
->conversion_map
&& (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)))
953 FIXME("Too many declaration changes or converting dynamic buffer, stopping converting.\n");
954 wined3d_buffer_drop_bo(buffer
);
958 /* The declaration changed, reload the whole buffer. */
959 WARN("Reloading buffer because of a vertex declaration change.\n");
960 buffer_invalidate_bo_range(buffer
, 0, 0);
964 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
965 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
966 * decl changes and reset the decl change count after a specific number of them
968 if (buffer
->conversion_map
&& buffer_is_fully_dirty(buffer
))
970 ++buffer
->full_conversion_count
;
971 if (buffer
->full_conversion_count
> VB_MAXFULLCONVERSIONS
)
973 FIXME("Too many full buffer conversions, stopping converting.\n");
974 wined3d_buffer_drop_bo(buffer
);
980 ++buffer
->draw_count
;
981 if (buffer
->draw_count
> VB_RESETDECLCHANGE
)
982 buffer
->decl_change_count
= 0;
983 if (buffer
->draw_count
> VB_RESETFULLCONVS
)
984 buffer
->full_conversion_count
= 0;
988 if (!wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
))
989 ERR("Failed to load buffer location.\n");
992 struct wined3d_resource
* CDECL
wined3d_buffer_get_resource(struct wined3d_buffer
*buffer
)
994 TRACE("buffer %p.\n", buffer
);
996 return &buffer
->resource
;
999 static HRESULT
wined3d_buffer_map(struct wined3d_buffer
*buffer
, UINT offset
, UINT size
, BYTE
**data
, DWORD flags
)
1001 struct wined3d_device
*device
= buffer
->resource
.device
;
1002 struct wined3d_context
*context
;
1006 TRACE("buffer %p, offset %u, size %u, data %p, flags %#x.\n", buffer
, offset
, size
, data
, flags
);
1008 count
= ++buffer
->resource
.map_count
;
1010 if (buffer
->buffer_object
)
1012 unsigned int dirty_offset
= offset
, dirty_size
= size
;
1014 /* DISCARD invalidates the entire buffer, regardless of the specified
1015 * offset and size. Some applications also depend on the entire buffer
1016 * being uploaded in that case. Two such applications are Port Royale
1017 * and Darkstar One. */
1018 if (flags
& WINED3D_MAP_DISCARD
)
1024 if (((flags
& WINED3D_MAP_WRITE
) && !(flags
& (WINED3D_MAP_NOOVERWRITE
| WINED3D_MAP_DISCARD
)))
1025 || (!(flags
& WINED3D_MAP_WRITE
) && (buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1026 || buffer
->flags
& WINED3D_BUFFER_PIN_SYSMEM
)
1028 if (!(buffer
->locations
& WINED3D_LOCATION_SYSMEM
))
1030 context
= context_acquire(device
, NULL
, 0);
1031 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1032 context_release(context
);
1035 if (flags
& WINED3D_MAP_WRITE
)
1036 wined3d_buffer_invalidate_range(buffer
, WINED3D_LOCATION_BUFFER
, dirty_offset
, dirty_size
);
1040 const struct wined3d_gl_info
*gl_info
;
1042 context
= context_acquire(device
, NULL
, 0);
1043 gl_info
= context
->gl_info
;
1045 if (flags
& WINED3D_MAP_DISCARD
)
1046 wined3d_buffer_validate_location(buffer
, WINED3D_LOCATION_BUFFER
);
1048 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1050 if (flags
& WINED3D_MAP_WRITE
)
1051 buffer_invalidate_bo_range(buffer
, dirty_offset
, dirty_size
);
1053 if ((flags
& WINED3D_MAP_DISCARD
) && buffer
->resource
.heap_memory
)
1054 wined3d_buffer_evict_sysmem(buffer
);
1058 buffer_bind(buffer
, context
);
1060 /* Filter redundant WINED3D_MAP_DISCARD maps. The 3DMark2001
1061 * multitexture fill rate test seems to depend on this. When
1062 * we map a buffer with GL_MAP_INVALIDATE_BUFFER_BIT, the
1063 * driver is free to discard the previous contents of the
1064 * buffer. The r600g driver only does this when the buffer is
1065 * currently in use, while the proprietary NVIDIA driver
1066 * appears to do this unconditionally. */
1067 if (buffer
->flags
& WINED3D_BUFFER_DISCARD
)
1068 flags
&= ~WINED3D_MAP_DISCARD
;
1070 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1072 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
1073 buffer
->map_ptr
= GL_EXTCALL(glMapBufferRange(buffer
->buffer_type_hint
,
1074 0, buffer
->resource
.size
, mapflags
));
1075 checkGLcall("glMapBufferRange");
1079 if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1080 buffer_sync_apple(buffer
, flags
, gl_info
);
1081 buffer
->map_ptr
= GL_EXTCALL(glMapBuffer(buffer
->buffer_type_hint
,
1083 checkGLcall("glMapBuffer");
1086 if (((DWORD_PTR
)buffer
->map_ptr
) & (RESOURCE_ALIGNMENT
- 1))
1088 WARN("Pointer %p is not %u byte aligned.\n", buffer
->map_ptr
, RESOURCE_ALIGNMENT
);
1090 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1091 checkGLcall("glUnmapBuffer");
1092 buffer
->map_ptr
= NULL
;
1094 if (buffer
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
1096 /* The extra copy is more expensive than not using VBOs at
1097 * all on the Nvidia Linux driver, which is the only driver
1098 * that returns unaligned pointers.
1100 TRACE("Dynamic buffer, dropping VBO.\n");
1101 wined3d_buffer_drop_bo(buffer
);
1105 TRACE("Falling back to doublebuffered operation.\n");
1106 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_SYSMEM
);
1107 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1109 TRACE("New pointer is %p.\n", buffer
->resource
.heap_memory
);
1113 context_release(context
);
1116 if (flags
& WINED3D_MAP_DISCARD
)
1117 buffer
->flags
|= WINED3D_BUFFER_DISCARD
;
1120 base
= buffer
->map_ptr
? buffer
->map_ptr
: buffer
->resource
.heap_memory
;
1121 *data
= base
+ offset
;
1123 TRACE("Returning memory at %p (base %p, offset %u).\n", *data
, base
, offset
);
1124 /* TODO: check Flags compatibility with buffer->currentDesc.Usage (see MSDN) */
1129 static void wined3d_buffer_unmap(struct wined3d_buffer
*buffer
)
1133 TRACE("buffer %p.\n", buffer
);
1135 /* In the case that the number of Unmap calls > the
1136 * number of Map calls, d3d returns always D3D_OK.
1137 * This is also needed to prevent Map from returning garbage on
1138 * the next call (this will happen if the lock_count is < 0). */
1139 if (!buffer
->resource
.map_count
)
1141 WARN("Unmap called without a previous map call.\n");
1145 if (--buffer
->resource
.map_count
)
1147 /* Delay loading the buffer until everything is unlocked */
1148 TRACE("Ignoring unmap.\n");
1152 if (buffer
->map_ptr
)
1154 struct wined3d_device
*device
= buffer
->resource
.device
;
1155 const struct wined3d_gl_info
*gl_info
;
1156 struct wined3d_context
*context
;
1158 context
= context_acquire(device
, NULL
, 0);
1159 gl_info
= context
->gl_info
;
1161 buffer_bind(buffer
, context
);
1163 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1165 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1167 GL_EXTCALL(glFlushMappedBufferRange(buffer
->buffer_type_hint
,
1168 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1169 checkGLcall("glFlushMappedBufferRange");
1172 else if (buffer
->flags
& WINED3D_BUFFER_APPLESYNC
)
1174 for (i
= 0; i
< buffer
->modified_areas
; ++i
)
1176 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(buffer
->buffer_type_hint
,
1177 buffer
->maps
[i
].offset
, buffer
->maps
[i
].size
));
1178 checkGLcall("glFlushMappedBufferRangeAPPLE");
1182 GL_EXTCALL(glUnmapBuffer(buffer
->buffer_type_hint
));
1183 context_release(context
);
1185 buffer_clear_dirty_areas(buffer
);
1186 buffer
->map_ptr
= NULL
;
1190 void wined3d_buffer_copy(struct wined3d_buffer
*dst_buffer
, unsigned int dst_offset
,
1191 struct wined3d_buffer
*src_buffer
, unsigned int src_offset
, unsigned int size
)
1193 struct wined3d_bo_address dst
, src
;
1194 struct wined3d_context
*context
;
1197 buffer_mark_used(dst_buffer
);
1198 buffer_mark_used(src_buffer
);
1200 dst_location
= wined3d_buffer_get_memory(dst_buffer
, &dst
, dst_buffer
->locations
);
1201 dst
.addr
+= dst_offset
;
1203 wined3d_buffer_get_memory(src_buffer
, &src
, src_buffer
->locations
);
1204 src
.addr
+= src_offset
;
1206 context
= context_acquire(dst_buffer
->resource
.device
, NULL
, 0);
1207 context_copy_bo_address(context
, &dst
, dst_buffer
->buffer_type_hint
,
1208 &src
, src_buffer
->buffer_type_hint
, size
);
1209 context_release(context
);
1211 wined3d_buffer_invalidate_range(dst_buffer
, ~dst_location
, dst_offset
, size
);
1214 void wined3d_buffer_upload_data(struct wined3d_buffer
*buffer
, struct wined3d_context
*context
,
1215 const struct wined3d_box
*box
, const void *data
)
1217 struct wined3d_map_range range
;
1221 range
.offset
= box
->left
;
1222 range
.size
= box
->right
- box
->left
;
1227 range
.size
= buffer
->resource
.size
;
1230 wined3d_buffer_upload_ranges(buffer
, context
, data
, range
.offset
, 1, &range
);
1233 static ULONG
buffer_resource_incref(struct wined3d_resource
*resource
)
1235 return wined3d_buffer_incref(buffer_from_resource(resource
));
1238 static ULONG
buffer_resource_decref(struct wined3d_resource
*resource
)
1240 return wined3d_buffer_decref(buffer_from_resource(resource
));
1243 static void buffer_resource_preload(struct wined3d_resource
*resource
)
1245 struct wined3d_context
*context
;
1247 context
= context_acquire(resource
->device
, NULL
, 0);
1248 wined3d_buffer_load(buffer_from_resource(resource
), context
, NULL
);
1249 context_release(context
);
1252 static HRESULT
buffer_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1253 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1255 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1258 if (sub_resource_idx
)
1260 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1261 return E_INVALIDARG
;
1267 size
= box
->right
- box
->left
;
1274 map_desc
->row_pitch
= map_desc
->slice_pitch
= buffer
->desc
.byte_width
;
1275 return wined3d_buffer_map(buffer
, offset
, size
, (BYTE
**)&map_desc
->data
, flags
);
1278 static HRESULT
buffer_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1280 if (sub_resource_idx
)
1282 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
1283 return E_INVALIDARG
;
1286 wined3d_buffer_unmap(buffer_from_resource(resource
));
1290 static const struct wined3d_resource_ops buffer_resource_ops
=
1292 buffer_resource_incref
,
1293 buffer_resource_decref
,
1294 buffer_resource_preload
,
1296 buffer_resource_sub_resource_map
,
1297 buffer_resource_sub_resource_unmap
,
1300 static GLenum
buffer_type_hint_from_bind_flags(const struct wined3d_gl_info
*gl_info
,
1301 unsigned int bind_flags
)
1303 if (bind_flags
== WINED3D_BIND_INDEX_BUFFER
)
1304 return GL_ELEMENT_ARRAY_BUFFER
;
1306 if (bind_flags
& (WINED3D_BIND_SHADER_RESOURCE
| WINED3D_BIND_UNORDERED_ACCESS
)
1307 && gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1308 return GL_TEXTURE_BUFFER
;
1310 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
1311 return GL_UNIFORM_BUFFER
;
1313 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
1314 return GL_TRANSFORM_FEEDBACK_BUFFER
;
1316 if (bind_flags
& ~(WINED3D_BIND_VERTEX_BUFFER
| WINED3D_BIND_INDEX_BUFFER
))
1317 FIXME("Unhandled bind flags %#x.\n", bind_flags
);
1319 return GL_ARRAY_BUFFER
;
1322 static HRESULT
buffer_init(struct wined3d_buffer
*buffer
, struct wined3d_device
*device
,
1323 UINT size
, DWORD usage
, enum wined3d_format_id format_id
, unsigned int access
, unsigned int bind_flags
,
1324 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1326 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1327 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, usage
);
1328 struct wined3d_resource
*resource
= &buffer
->resource
;
1329 struct wined3d_box box
;
1330 BOOL dynamic_buffer_ok
;
1335 WARN("Size 0 requested, returning E_INVALIDARG.\n");
1336 return E_INVALIDARG
;
1339 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
&& size
& (WINED3D_CONSTANT_BUFFER_ALIGNMENT
- 1))
1341 WARN("Size %#x is not suitably aligned for constant buffers.\n", size
);
1342 return E_INVALIDARG
;
1345 if (data
&& !data
->data
)
1347 WARN("Invalid sub-resource data specified.\n");
1348 return E_INVALIDARG
;
1351 if (FAILED(hr
= resource_init(resource
, device
, WINED3D_RTYPE_BUFFER
, format
, WINED3D_MULTISAMPLE_NONE
,
1352 0, usage
, access
, size
, 1, 1, size
, parent
, parent_ops
, &buffer_resource_ops
)))
1354 WARN("Failed to initialize resource, hr %#x.\n", hr
);
1357 buffer
->buffer_type_hint
= buffer_type_hint_from_bind_flags(gl_info
, bind_flags
);
1358 buffer
->bind_flags
= bind_flags
;
1359 buffer
->locations
= data
? WINED3D_LOCATION_DISCARDED
: WINED3D_LOCATION_SYSMEM
;
1361 if (!wined3d_resource_allocate_sysmem(&buffer
->resource
))
1362 return E_OUTOFMEMORY
;
1364 TRACE("buffer %p, size %#x, usage %#x, format %s, memory @ %p.\n",
1365 buffer
, buffer
->resource
.size
, buffer
->resource
.usage
,
1366 debug_d3dformat(buffer
->resource
.format
->id
), buffer
->resource
.heap_memory
);
1368 if (device
->create_parms
.flags
& WINED3DCREATE_SOFTWARE_VERTEXPROCESSING
1369 || wined3d_resource_access_is_managed(access
))
1371 /* SWvp and managed buffers always return the same pointer in buffer
1372 * maps and retain data in DISCARD maps. Keep a system memory copy of
1373 * the buffer to provide the same behavior to the application. */
1374 TRACE("Using doublebuffer mode.\n");
1375 buffer
->flags
|= WINED3D_BUFFER_PIN_SYSMEM
;
1378 /* Observations show that draw_primitive_immediate_mode() is faster on
1379 * dynamic vertex buffers than converting + draw_primitive_arrays().
1380 * (Half-Life 2 and others.) */
1381 dynamic_buffer_ok
= gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
] || gl_info
->supported
[ARB_MAP_BUFFER_RANGE
];
1383 if (!gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
1385 TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
1387 else if (!(access
& WINED3D_RESOURCE_ACCESS_GPU
))
1389 TRACE("Not creating a BO because the buffer is not GPU accessible.\n");
1391 else if (!dynamic_buffer_ok
&& (resource
->usage
& WINED3DUSAGE_DYNAMIC
))
1393 TRACE("Not creating a BO because the buffer has dynamic usage and no GL support.\n");
1397 buffer
->flags
|= WINED3D_BUFFER_USE_BO
;
1400 if (!(buffer
->maps
= heap_alloc(sizeof(*buffer
->maps
))))
1402 ERR("Out of memory.\n");
1403 buffer_unload(resource
);
1404 resource_cleanup(resource
);
1405 wined3d_resource_wait_idle(resource
);
1406 return E_OUTOFMEMORY
;
1408 buffer
->maps_size
= 1;
1412 wined3d_box_set(&box
, 0, 0, resource
->size
, 1, 0, 1);
1413 wined3d_cs_emit_update_sub_resource(device
->cs
, resource
,
1414 0, &box
, data
->data
, data
->row_pitch
, data
->slice_pitch
);
1420 HRESULT CDECL
wined3d_buffer_create(struct wined3d_device
*device
, const struct wined3d_buffer_desc
*desc
,
1421 const struct wined3d_sub_resource_data
*data
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1422 struct wined3d_buffer
**buffer
)
1424 struct wined3d_buffer
*object
;
1427 TRACE("device %p, desc %p, data %p, parent %p, parent_ops %p, buffer %p.\n",
1428 device
, desc
, data
, parent
, parent_ops
, buffer
);
1430 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1431 return E_OUTOFMEMORY
;
1433 if (FAILED(hr
= buffer_init(object
, device
, desc
->byte_width
, desc
->usage
, WINED3DFMT_UNKNOWN
,
1434 desc
->access
, desc
->bind_flags
, data
, parent
, parent_ops
)))
1436 WARN("Failed to initialize buffer, hr %#x.\n", hr
);
1440 object
->desc
= *desc
;
1442 TRACE("Created buffer %p.\n", object
);