buffer.c revision 9a0dee30f5bea4fb02c0e836cd9689822e4645d1
/*
* Copyright 2002-2005 Jason Edmeades
* Copyright 2002-2005 Raphael Junqueira
* Copyright 2004 Christian Costa
* Copyright 2005 Oliver Stieber
* Copyright 2007-2010 Stefan Dösinger for CodeWeavers
* Copyright 2009 Henri Verbeet for CodeWeavers
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
/*
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
* a choice of LGPL license versions is made available with the language indicating
* that LGPLv2 or any later version may be used, or where a choice of which version
* of the LGPL is applied is otherwise unspecified.
*/
#include "config.h"
#include "wined3d_private.h"
{
{
if (!new)
{
ERR("Out of memory\n");
return FALSE;
}
else
{
}
}
{
WARN("Invalid range dirtified, marking entire buffer dirty\n");
offset = 0;
}
{
}
This->modified_areas++;
return TRUE;
}
{
This->modified_areas = 0;
}
{
return This->modified_areas != 0;
}
{
unsigned int i;
for(i = 0; i < This->modified_areas; i++)
{
{
return TRUE;
}
}
return FALSE;
}
/* Context activation is done by the caller */
{
if(!This->buffer_object) return;
ENTER_GL();
checkGLcall("glDeleteBuffersARB");
LEAVE_GL();
This->buffer_object = 0;
{
}
}
/* Context activation is done by the caller. */
{
TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n",
ENTER_GL();
/* Make sure that the gl error is cleared. Do not use checkGLcall
* here because checkGLcall just prints a fixme and continues. However,
* if an error during VBO creation occurs we can fall back to non-vbo operation
* with full functionality(but performance loss)
*/
while (glGetError() != GL_NO_ERROR);
/* Basically the FVF parameter passed to CreateVertexBuffer is no good
* It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
* IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
* look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
* to check if the rhw and color values are in the correct format.
*/
error = glGetError();
{
LEAVE_GL();
goto fail;
}
{
}
error = glGetError();
if (error != GL_NO_ERROR)
{
LEAVE_GL();
goto fail;
}
/* Don't use static, because dx apps tend to update the buffer
* quite often even if they specify 0 usage.
*/
{
TRACE("Gl usage = GL_STREAM_DRAW_ARB\n");
{
GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE)");
GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE)");
}
/* No setup is needed here for GL_ARB_map_buffer_range */
}
else
{
TRACE("Gl usage = GL_DYNAMIC_DRAW_ARB\n");
}
/* Reserve memory for the buffer. The amount of data won't change
* so we are safe with calling glBufferData once and
* calling glBufferSubData on updates. Upload the actual data in case
* we're not double buffering, so we can release the heap mem afterwards
*/
GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, This->resource.allocatedMemory, gl_usage));
error = glGetError();
LEAVE_GL();
if (error != GL_NO_ERROR)
{
goto fail;
}
{
if(!buffer_add_dirty_area(This, 0, 0))
{
ERR("buffer_add_dirty_area failed, this is not expected\n");
goto fail;
}
}
else
{
}
return;
fail:
/* Clean up all vbo init, but continue because we can work without a vbo :-) */
ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
}
const enum wined3d_buffer_conversion_type conversion_type,
{
unsigned int i;
/* Check for some valid situations which cause us pain. One is if the buffer is used for
* constant attributes(stride = 0), the other one is if the buffer is used on two streams
* with different strides. In the 2nd case we might have to drop conversion entirely,
* it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
*/
{
FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
}
{
}
else
{
{
/* We rely that this happens only on the first converted attribute that is found,
* if at all. See above check
*/
TRACE("Reconverting because converted attributes occur, and the stride changed\n");
}
}
for (i = 0; i < attrib_size; ++i)
{
{
}
}
return ret;
}
static BOOL buffer_check_attribute(struct wined3d_buffer *This, const struct wined3d_stream_info *si,
{
/* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
* there, on nonexistent attribs the vbo is 0.
*/
return FALSE;
/* Look for newly appeared conversion */
{
*float16_used = TRUE;
}
{
}
{
}
else if (This->conversion_map)
{
}
return ret;
}
{
if (!stride)
{
TRACE("No shift\n");
return NULL;
}
for (i = 0; i < MAX_ATTRIBS; ++i)
{
if (!(strided->use_map & (1 << i)) || strided->elements[i].buffer_object != This->buffer_object) continue;
if (format == WINED3DFMT_R16G16_FLOAT)
{
shift = 4;
}
else if (format == WINED3DFMT_R16G16B16A16_FLOAT)
{
shift = 8;
/* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
* compatible
*/
for (j = 4; j < 8; ++j)
{
}
}
else
{
shift = 0;
}
if (shift)
{
{
}
}
}
{
TRACE("Dumping conversion shift:\n");
for (i = 0; i < stride; ++i)
{
}
TRACE("\n");
}
return ret;
}
{
UINT stride_this_run = 0;
unsigned int i;
/* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
* Once we have our declaration there is no need to look it up again. Index buffers also never need
* conversion, so once the (empty) conversion structure is created don't bother checking again
*/
{
}
TRACE("Finding vertex buffer conversion information\n");
/* Certain declaration types need some fixups before we can pass them to
* opengl. This means D3DCOLOR attributes with fixed function vertex
* processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
* GL_ARB_half_float_vertex is not supported.
*
* Note for d3d8 and d3d9:
* The vertex buffer FVF doesn't help with finding them, we have to use
* the decoded vertex declaration and pick the things that concern the
* current buffer. A problem with this is that this can change between
* draws, so we have to validate the information and reprocess the buffer
* if it changes, and avoid false positives for performance reasons.
* WineD3D doesn't even know the vertex buffer any more, it is managed
* by the client libraries and passed to SetStreamSource and ProcessVertices
* as needed.
*
* We have to distinguish between vertex shaders and fixed function to
* pick the way we access the strided vertex information.
*
* This code sets up a per-byte array with the size of the detected
* stride of the arrays in the buffer. For each byte we have a field
* that marks the conversion needed on this byte. For example, the
* following declaration with fixed function vertex processing:
*
* POSITIONT, FLOAT4
* NORMAL, FLOAT3
* DIFFUSE, FLOAT16_4
* SPECULAR, D3DCOLOR
*
* Will result in
* { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
* [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]
*
* Where in this example map P means 4 component position conversion, 0
* means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
* conversion (red / blue swizzle).
*
* If we're doing conversion and the stride changes we have to reconvert
* the whole buffer. Note that we do not mind if the semantic changes,
* we only care for the conversion type. So if the NORMAL is replaced
* with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
* with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
* conversion types depend on the semantic as well, for example a FLOAT4
* texcoord needs no conversion while a FLOAT4 positiont needs one
*/
{
TRACE("vshader\n");
/* If the current vertex declaration is marked for no half float conversion don't bother to
* analyse the strided streams in depth, just set them up for no conversion. Return decl changed
* if we used conversion before
*/
{
if (This->conversion_map)
{
TRACE("Now using shaders without conversion, but conversion used before\n");
This->conversion_stride = 0;
return TRUE;
}
else
{
return FALSE;
}
}
for (i = 0; i < MAX_ATTRIBS; ++i)
{
ret = buffer_check_attribute(This, si, i, FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
}
/* Recalculate the conversion shift map if the declaration has changed,
* and we're using float16 conversion or used it on the last run
*/
{
}
}
else
{
/* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
* FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
* the attributes that our current fixed function pipeline implementation cares for.
*/
}
{
/* Sanity test */
if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
}
return ret;
}
/* Context activation is done by the caller. */
{
{
{
}
/* Rescue the data before resizing the buffer object if we do not have our backup copy */
{
}
ENTER_GL();
checkGLcall("glBindBufferARB");
checkGLcall("glBufferDataARB");
LEAVE_GL();
}
}
{
/* Color conversion like in drawStridedSlow. watch out for little endianity
* If we want that stuff to work on big endian machines too we have to consider more things
*
* 0xff000000: Alpha mask
* 0x00ff0000: Blue mask
* 0x0000ff00: Green mask
* 0x000000ff: Red mask
*/
*dst_color = 0;
}
static inline void fixup_transformed_pos(float *p)
{
/* rhw conversion like in position_float4(). */
if (p[3] != 1.0f && p[3] != 0.0f)
{
float w = 1.0f / p[3];
p[0] *= w;
p[1] *= w;
p[2] *= w;
p[3] = w;
}
}
/* Context activation is done by the caller. */
{
if (!This->buffer_object)
{
{
if (This->buffer_object)
{
return NULL;
}
}
}
else
{
return NULL;
}
}
/* IUnknown methods */
{
{
return S_OK;
}
return E_NOINTERFACE;
}
{
return refcount;
}
/* Context activation is done by the caller. */
{
/* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT);
This->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
ENTER_GL();
GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.allocatedMemory));
LEAVE_GL();
}
{
if (This->buffer_object)
{
struct wined3d_context *context;
/* Download the buffer, but don't permanently enable double buffering */
{
}
This->conversion_stride = 0;
}
}
{
if (!refcount)
{
}
return refcount;
}
/* IWineD3DBase methods */
{
}
/* IWineD3DResource methods */
{
}
{
}
{
}
{
}
{
}
/* The caller provides a context and binds the buffer */
static void buffer_sync_apple(struct wined3d_buffer *This, DWORD flags, const struct wined3d_gl_info *gl_info)
{
/* No fencing needs to be done if the app promises not to overwrite
* existing data */
if(flags & WINED3DLOCK_NOOVERWRITE) return;
if(flags & WINED3DLOCK_DISCARD)
{
ENTER_GL();
GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, NULL, This->buffer_object_usage));
checkGLcall("glBufferDataARB\n");
LEAVE_GL();
return;
}
{
{
FIXME("Event queries not supported, dropping async buffer locks.\n");
goto drop_query;
}
{
ERR("Failed to allocate event query memory, dropping async buffer locks.\n");
goto drop_query;
}
/* Since we don't know about old draws a glFinish is needed once */
wglFinish();
return;
}
switch(ret)
{
case WINED3D_EVENT_QUERY_OK:
/* All done */
return;
WARN("Cannot synchronize buffer lock due to a thread conflict\n");
goto drop_query;
default:
goto drop_query;
}
{
}
wglFinish();
ENTER_GL();
GL_EXTCALL(glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE));
checkGLcall("glBufferParameteriAPPLE(This->buffer_type_hint, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_TRUE)");
LEAVE_GL();
}
/* The caller provides a GL context */
static void buffer_direct_upload(struct wined3d_buffer *This, const struct wined3d_gl_info *gl_info, DWORD flags)
{
ENTER_GL();
checkGLcall("glBindBufferARB");
{
if (flags & WINED3D_BUFFER_DISCARD)
{
}
else if (flags & WINED3D_BUFFER_NOSYNC)
{
}
checkGLcall("glMapBufferRange");
}
else
{
{
LEAVE_GL();
ENTER_GL();
}
checkGLcall("glMapBufferARB");
}
if (!map)
{
LEAVE_GL();
ERR("Failed to map opengl buffer\n");
return;
}
while(This->modified_areas)
{
This->modified_areas--;
{
checkGLcall("glFlushMappedBufferRange");
}
{
checkGLcall("glFlushMappedBufferRangeAPPLE");
}
}
checkGLcall("glUnmapBufferARB");
LEAVE_GL();
}
{
struct wined3d_context *context;
unsigned int i, j;
if (!This->buffer_object)
{
/* TODO: Make converting independent from VBOs */
{
}
else
{
/* Not doing any conversion */
goto end;
}
}
/* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
{
}
{
++This->draw_count;
return;
}
/* If applications change the declaration over and over, reconverting all the time is a huge
* performance hit. So count the declaration changes and release the VBO if there are too many
* of them (and thus stop converting)
*/
if (decl_changed)
{
This->draw_count = 0;
{
FIXME("Too many declaration changes or converting dynamic buffer, stopping converting\n");
/* The stream source state handler might have read the memory of the vertex buffer already
* and got the memory in the vbo which is not valid any longer. Dirtify the stream source
* to force a reload. This happens only once per changed vertexbuffer and should occur rather
* rarely
*/
goto end;
}
/* The declaration changed, reload the whole buffer */
WARN("Reloading buffer because of decl change\n");
if(!buffer_add_dirty_area(This, 0, 0))
{
ERR("buffer_add_dirty_area failed, this is not expected\n");
return;
}
/* Avoid unfenced updates, we might overwrite more areas of the buffer than the application
* cleared for unsynchronized updates
*/
flags = 0;
}
else
{
/* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
* changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
* decl changes and reset the decl change count after a specific number of them
*/
{
{
FIXME("Too many full buffer conversions, stopping converting\n");
goto end;
}
}
else
{
++This->draw_count;
}
}
{
}
if (!This->conversion_map)
{
/* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
* directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
* the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
*/
TRACE("No conversion needed\n");
/* Nothing to do because we locked directly into the vbo */
{
return;
}
return;
}
{
}
/* Now for each vertex in the buffer that needs conversion */
if (This->conversion_shift)
{
TRACE("Shifted conversion\n");
start = 0;
{
FIXME("Implement partial buffer load with shifted conversion\n");
}
{
{
switch(This->conversion_map[j])
{
case CONV_NONE:
break;
case CONV_FLOAT16_2:
{
j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
break;
}
default:
break;
}
}
}
ENTER_GL();
checkGLcall("glBindBufferARB");
GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, 0, vertices * This->conversion_stride, data));
checkGLcall("glBufferSubDataARB");
LEAVE_GL();
}
else
{
while(This->modified_areas)
{
This->modified_areas--;
{
{
switch(This->conversion_map[j])
{
case CONV_NONE:
/* Done already */
j += 3;
break;
case CONV_D3DCOLOR:
j += 3;
break;
case CONV_POSITIONT:
j += 15;
break;
case CONV_FLOAT16_2:
ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
default:
}
}
}
ENTER_GL();
checkGLcall("glBindBufferARB");
checkGLcall("glBufferSubDataARB");
LEAVE_GL();
}
}
end:
}
{
}
/* IWineD3DBuffer methods */
{
/* Not all flags make sense together, but Windows never returns an error. Catch the
* cases that could cause issues */
if(flags & WINED3DLOCK_READONLY)
{
if(flags & WINED3DLOCK_DISCARD)
{
WARN("WINED3DLOCK_READONLY combined with WINED3DLOCK_DISCARD, ignoring flags\n");
return 0;
}
if(flags & WINED3DLOCK_NOOVERWRITE)
{
WARN("WINED3DLOCK_READONLY combined with WINED3DLOCK_NOOVERWRITE, ignoring flags\n");
return 0;
}
}
else if((flags & (WINED3DLOCK_DISCARD | WINED3DLOCK_NOOVERWRITE)) == (WINED3DLOCK_DISCARD | WINED3DLOCK_NOOVERWRITE))
{
WARN("WINED3DLOCK_DISCARD and WINED3DLOCK_NOOVERWRITE used together, ignoring\n");
return 0;
}
else if (flags & (WINED3DLOCK_DISCARD | WINED3DLOCK_NOOVERWRITE) && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
{
WARN("DISCARD or NOOVERWRITE lock on non-dynamic buffer, ignoring\n");
return 0;
}
return flags;
}
{
GLbitfield ret = 0;
{
}
else
{
ret |= GL_MAP_READ_BIT;
}
return ret;
}
static HRESULT STDMETHODCALLTYPE buffer_Map(IWineD3DBuffer *iface, UINT offset, UINT size, BYTE **data, DWORD flags)
{
if (!(flags & WINED3DLOCK_READONLY))
{
}
if (This->buffer_object)
{
{
if(count == 1)
{
struct wined3d_context *context;
const struct wined3d_gl_info *gl_info;
{
}
ENTER_GL();
{
checkGLcall("glMapBufferRange");
}
else
{
{
LEAVE_GL();
ENTER_GL();
}
This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_READ_WRITE_ARB));
checkGLcall("glMapBufferARB");
}
LEAVE_GL();
{
WARN("Pointer %p is not %u byte aligned, falling back to double buffered operation\n",
ENTER_GL();
checkGLcall("glUnmapBufferARB");
LEAVE_GL();
}
}
}
else
{
if (dirty)
{
{
}
}
else if(flags & WINED3DLOCK_NOOVERWRITE)
{
}
if (flags & WINED3DLOCK_DISCARD)
{
}
}
}
TRACE("Returning memory at %p (base %p, offset %u)\n", *data, This->resource.allocatedMemory, offset);
/* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
return WINED3D_OK;
}
{
ULONG i;
/* In the case that the number of Unmap calls > the
* number of Map calls, d3d returns always D3D_OK.
* This is also needed to prevent Map from returning garbage on
* the next call (this will happen if the lock_count is < 0). */
if(This->lock_count == 0)
{
TRACE("Unmap called without a previous Map call!\n");
return WINED3D_OK;
}
{
/* Delay loading the buffer until everything is unlocked */
TRACE("Ignoring unlock\n");
return WINED3D_OK;
}
{
const struct wined3d_gl_info *gl_info;
struct wined3d_context *context;
{
}
ENTER_GL();
{
for(i = 0; i < This->modified_areas; i++)
{
checkGLcall("glFlushMappedBufferRange");
}
}
{
for(i = 0; i < This->modified_areas; i++)
{
checkGLcall("glFlushMappedBufferRangeAPPLE");
}
}
LEAVE_GL();
}
{
}
return WINED3D_OK;
}
{
return WINED3D_OK;
}
static const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
{
/* IUnknown methods */
/* IWineD3DBase methods */
/* IWineD3DResource methods */
/* IWineD3DBuffer methods */
};
{
const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, &device->adapter->gl_info);
if (!size)
{
WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
return WINED3DERR_INVALIDCALL;
}
#ifdef VBOXWDDM
#endif
);
{
return hr;
}
TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
/* Observations show that drawStridedSlow is faster on dynamic VBs than converting +
* drawStridedFast (half-life 2 and others).
*
* Basically converting the vertices in the buffer is quite expensive, and observations
* show that drawStridedSlow is faster than converting + uploading + drawStridedFast.
* Therefore do not create a VBO for WINED3DUSAGE_DYNAMIC buffers.
*/
{
TRACE("Not creating a vbo because GL_ARB_vertex_buffer is not supported\n");
}
{
TRACE("Not creating a vbo because the vertex buffer is in system memory\n");
}
{
TRACE("Not creating a vbo because the buffer has dynamic usage and no GL support\n");
}
else
{
}
if (data)
{
{
return hr;
}
{
return hr;
}
}
{
ERR("Out of memory\n");
return E_OUTOFMEMORY;
}
return WINED3D_OK;
}