Change VertexBufferInterface and VertexDataManager to use new VertexBuffer.

TRAC #22226 Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch Author: Geoff Lang git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1589 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 3f255b48
......@@ -70,7 +70,7 @@ void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
{
memcpy(mContents + offset, data, size);
if ((mStaticVertexBuffer && mStaticVertexBuffer->size() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0))
if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0))
{
invalidateStaticData();
}
......
......@@ -9,8 +9,6 @@
#include "libGLESv2/renderer/VertexBuffer.h"
#include "libGLESv2/renderer/Renderer9.h"
namespace rx
{
......@@ -35,189 +33,150 @@ unsigned int VertexBuffer::getSerial() const
return mSerial;
}
unsigned int VertexBufferInterface::mCurrentSerial = 1;
VertexBufferInterface::VertexBufferInterface(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : mRenderer(renderer), mVertexBuffer(NULL)
VertexBufferInterface::VertexBufferInterface(rx::Renderer *renderer, bool dynamic) : mRenderer(renderer)
{
if (size > 0)
{
// D3D9_REPLACE
HRESULT result = mRenderer->createVertexBuffer(size, usageFlags,&mVertexBuffer);
mSerial = issueSerial();
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", size);
}
}
mBufferSize = size;
mDynamic = dynamic;
mWritePosition = 0;
mRequiredSpace = 0;
}
mReservedSpace = 0;
VertexBufferInterface::~VertexBufferInterface()
{
if (mVertexBuffer)
{
mVertexBuffer->Release();
}
mVertexBuffer = renderer->createVertexBuffer();
}
void VertexBufferInterface::unmap()
VertexBufferInterface::~VertexBufferInterface()
{
if (mVertexBuffer)
{
mVertexBuffer->Unlock();
}
delete mVertexBuffer;
}
IDirect3DVertexBuffer9 *VertexBufferInterface::getBuffer() const
unsigned int VertexBufferInterface::getSerial() const
{
return mVertexBuffer;
return mVertexBuffer->getSerial();
}
unsigned int VertexBufferInterface::getSerial() const
unsigned int VertexBufferInterface::getBufferSize() const
{
return mSerial;
return mVertexBuffer->getBufferSize();
}
unsigned int VertexBufferInterface::issueSerial()
bool VertexBufferInterface::setBufferSize(unsigned int size)
{
return mCurrentSerial++;
if (mVertexBuffer->getBufferSize() == 0)
{
return mVertexBuffer->initialize(size, mDynamic);
}
else
{
return mVertexBuffer->setBufferSize(size);
}
}
void VertexBufferInterface::addRequiredSpace(UINT requiredSpace)
unsigned int VertexBufferInterface::getWritePosition() const
{
mRequiredSpace += requiredSpace;
return mWritePosition;
}
StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer9 *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
void VertexBufferInterface::setWritePosition(unsigned int writePosition)
{
mWritePosition = writePosition;
}
StreamingVertexBufferInterface::~StreamingVertexBufferInterface()
bool VertexBufferInterface::discard()
{
return mVertexBuffer->discard();
}
void *StreamingVertexBufferInterface::map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
int VertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances)
{
void *mapPtr = NULL;
if (mVertexBuffer)
if (!reserveSpace(mReservedSpace))
{
HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
if (FAILED(result))
{
ERR("Lock failed with error 0x%08x", result);
return NULL;
}
return -1;
}
mReservedSpace = 0;
*offset = mWritePosition;
mWritePosition += requiredSpace;
if (!mVertexBuffer->storeVertexAttributes(attrib, start, count, instances, mWritePosition))
{
return -1;
}
return mapPtr;
int oldWritePos = static_cast<int>(mWritePosition);
mWritePosition += mVertexBuffer->getSpaceRequired(attrib, count, instances);
return oldWritePos;
}
void StreamingVertexBufferInterface::reserveRequiredSpace()
int VertexBufferInterface::storeRawData(const void* data, unsigned int size)
{
if (mRequiredSpace > mBufferSize)
if (!reserveSpace(mReservedSpace))
{
if (mVertexBuffer)
{
mVertexBuffer->Release();
mVertexBuffer = NULL;
}
mBufferSize = std::max(mRequiredSpace, 3 * mBufferSize / 2); // 1.5 x mBufferSize is arbitrary and should be checked to see we don't have too many reallocations.
// D3D9_REPLACE
HRESULT result = mRenderer->createVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, &mVertexBuffer);
mSerial = issueSerial();
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
}
mWritePosition = 0;
return -1;
}
else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
{
if (mVertexBuffer)
{
void *dummy;
mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
mVertexBuffer->Unlock();
}
mReservedSpace = 0;
mWritePosition = 0;
if (!mVertexBuffer->storeRawData(data, size, mWritePosition))
{
return -1;
}
mRequiredSpace = 0;
int oldWritePos = static_cast<int>(mWritePosition);
mWritePosition += size;
return oldWritePos;
}
StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer9 *renderer) : VertexBufferInterface(renderer, 0, D3DUSAGE_WRITEONLY)
void VertexBufferInterface::reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances)
{
mReservedSpace += mVertexBuffer->getSpaceRequired(attribute, count, instances);
}
StaticVertexBufferInterface::~StaticVertexBufferInterface()
void VertexBufferInterface::reserveRawDataSpace(unsigned int size)
{
mReservedSpace += size;
}
void *StaticVertexBufferInterface::map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
VertexBuffer* VertexBufferInterface::getVertexBuffer() const
{
void *mapPtr = NULL;
if (mVertexBuffer)
{
HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
if (FAILED(result))
{
ERR("Lock failed with error 0x%08x", result);
return NULL;
}
return mVertexBuffer;
}
int attributeOffset = attribute.mOffset % attribute.stride();
VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
mCache.push_back(element);
*streamOffset = mWritePosition;
mWritePosition += requiredSpace;
}
StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true)
{
setBufferSize(initialSize);
}
return mapPtr;
StreamingVertexBufferInterface::~StreamingVertexBufferInterface()
{
}
void StaticVertexBufferInterface::reserveRequiredSpace()
bool StreamingVertexBufferInterface::reserveSpace(unsigned int size)
{
if (!mVertexBuffer && mBufferSize == 0)
bool result = true;
unsigned int curBufferSize = getBufferSize();
if (size > curBufferSize)
{
// D3D9_REPLACE
HRESULT result = mRenderer->createVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, &mVertexBuffer);
mSerial = issueSerial();
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
}
mBufferSize = mRequiredSpace;
result = setBufferSize(std::max(size, 3 * curBufferSize / 2));
setWritePosition(0);
}
else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
else if (getWritePosition() + size > curBufferSize)
{
// Already allocated
if (!discard())
{
return false;
}
setWritePosition(0);
}
else UNREACHABLE(); // Static vertex buffers can't be resized
mRequiredSpace = 0;
return result;
}
StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer *renderer) : VertexBufferInterface(renderer, false)
{
}
std::size_t StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute)
StaticVertexBufferInterface::~StaticVertexBufferInterface()
{
}
int StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute)
{
for (unsigned int element = 0; element < mCache.size(); element++)
{
......@@ -236,4 +195,32 @@ std::size_t StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribu
return -1;
}
bool StaticVertexBufferInterface::reserveSpace(unsigned int size)
{
unsigned int curSize = getBufferSize();
if (curSize == 0)
{
setBufferSize(size);
return true;
}
else if (curSize >= size)
{
return true;
}
else
{
UNREACHABLE(); // Static vertex buffers can't be resized
return false;
}
}
int StaticVertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances)
{
int attributeOffset = attrib.mOffset % attrib.stride();
VertexElement element = { attrib.mType, attrib.mSize, attrib.stride(), attrib.mNormalized, attributeOffset, getWritePosition() };
mCache.push_back(element);
return VertexBufferInterface::storeVertexAttributes(attrib, start, count, instances);
}
}
......@@ -17,6 +17,7 @@
#include <GLES2/gl2.h>
#include "libGLESv2/Context.h"
#include "libGLESv2/renderer/Renderer.h"
namespace rx
{
......@@ -55,55 +56,66 @@ class VertexBuffer
class VertexBufferInterface
{
public:
VertexBufferInterface(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags);
VertexBufferInterface(rx::Renderer *renderer, bool dynamic);
virtual ~VertexBufferInterface();
void unmap();
virtual void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset) = 0;
void reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances);
void reserveRawDataSpace(unsigned int size);
std::size_t size() const { return mBufferSize; }
virtual void reserveRequiredSpace() = 0;
void addRequiredSpace(UINT requiredSpace);
unsigned int getBufferSize() const;
IDirect3DVertexBuffer9 *getBuffer() const;
unsigned int getSerial() const;
virtual int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances);
virtual int storeRawData(const void* data, unsigned int size);
VertexBuffer* getVertexBuffer() const;
protected:
rx::Renderer9 *const mRenderer; // D3D9_REPLACE
IDirect3DVertexBuffer9 *mVertexBuffer;
virtual bool reserveSpace(unsigned int size) = 0;
unsigned int mSerial;
static unsigned int issueSerial();
static unsigned int mCurrentSerial;
unsigned int getWritePosition() const;
void setWritePosition(unsigned int writePosition);
std::size_t mBufferSize;
std::size_t mWritePosition;
std::size_t mRequiredSpace;
bool discard();
bool setBufferSize(unsigned int size);
private:
DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface);
rx::Renderer *const mRenderer;
VertexBuffer* mVertexBuffer;
unsigned int mWritePosition;
unsigned int mReservedSpace;
bool mDynamic;
};
class StreamingVertexBufferInterface : public VertexBufferInterface
{
public:
StreamingVertexBufferInterface(rx::Renderer9 *renderer, std::size_t initialSize);
StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize);
~StreamingVertexBufferInterface();
void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset);
void reserveRequiredSpace();
protected:
bool reserveSpace(unsigned int size);
};
class StaticVertexBufferInterface : public VertexBufferInterface
{
public:
explicit StaticVertexBufferInterface(rx::Renderer9 *renderer);
explicit StaticVertexBufferInterface(rx::Renderer *renderer);
~StaticVertexBufferInterface();
void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset);
void reserveRequiredSpace();
int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances);
std::size_t lookupAttribute(const gl::VertexAttribute &attribute); // Returns the offset into the vertex buffer, or -1 if not found
// Returns the offset into the vertex buffer, or -1 if not found
int lookupAttribute(const gl::VertexAttribute &attribute);
protected:
bool reserveSpace(unsigned int size);
private:
struct VertexElement
......@@ -114,7 +126,7 @@ class StaticVertexBufferInterface : public VertexBufferInterface
bool normalized;
int attributeOffset;
std::size_t streamOffset;
unsigned int streamOffset;
};
std::vector<VertexElement> mCache;
......
......@@ -26,11 +26,11 @@ struct TranslatedAttribute
{
bool active;
D3DDECLTYPE type;
const gl::VertexAttribute *attribute;
UINT offset;
UINT stride; // 0 means not to advance the read pointer at all
IDirect3DVertexBuffer9 *vertexBuffer;
VertexBuffer *vertexBuffer;
unsigned int serial;
unsigned int divisor;
};
......@@ -38,7 +38,7 @@ struct TranslatedAttribute
class VertexDataManager
{
public:
VertexDataManager(rx::Renderer9 *renderer);
VertexDataManager(rx::Renderer *renderer);
virtual ~VertexDataManager();
GLenum prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *outAttribs, GLsizei instances);
......@@ -46,44 +46,13 @@ class VertexDataManager
private:
DISALLOW_COPY_AND_ASSIGN(VertexDataManager);
std::size_t spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const;
std::size_t writeAttributeData(VertexBufferInterface *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances);
rx::Renderer9 *const mRenderer; // D3D9_REPLACE
rx::Renderer *const mRenderer;
StreamingVertexBufferInterface *mStreamingBuffer;
float mCurrentValue[gl::MAX_VERTEX_ATTRIBS][4];
StreamingVertexBufferInterface *mCurrentValueBuffer[gl::MAX_VERTEX_ATTRIBS];
std::size_t mCurrentValueOffsets[gl::MAX_VERTEX_ATTRIBS];
// Attribute format conversion
struct FormatConverter
{
bool identity;
std::size_t outputElementSize;
void (*convertArray)(const void *in, std::size_t stride, std::size_t n, void *out);
D3DDECLTYPE d3dDeclType;
};
enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 };
FormatConverter mAttributeTypes[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size - 1]
struct TranslationDescription
{
DWORD capsFlag;
FormatConverter preferredConversion;
FormatConverter fallbackConversion;
};
// This table is used to generate mAttributeTypes.
static const TranslationDescription mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size - 1]
void checkVertexCaps(DWORD declTypes);
unsigned int typeIndex(GLenum type) const;
const FormatConverter &formatConverter(const gl::VertexAttribute &attribute) const;
};
}
......
......@@ -7,6 +7,7 @@
// VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations.
#include "libGLESv2/ProgramBinary.h"
#include "libGLESv2/renderer/VertexBuffer9.h"
#include "libGLESv2/renderer/VertexDataManager.h"
#include "libGLESv2/renderer/VertexDeclarationCache.h"
......@@ -122,11 +123,13 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl
}
}
VertexBuffer9 *vertexBuffer = VertexBuffer9::makeVertexBuffer9(attributes[i].vertexBuffer);
if (mAppliedVBs[stream].serial != attributes[i].serial ||
mAppliedVBs[stream].stride != attributes[i].stride ||
mAppliedVBs[stream].offset != attributes[i].offset)
{
device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
device->SetStreamSource(stream, vertexBuffer->getBuffer(), attributes[i].offset, attributes[i].stride);
mAppliedVBs[stream].serial = attributes[i].serial;
mAppliedVBs[stream].stride = attributes[i].stride;
mAppliedVBs[stream].offset = attributes[i].offset;
......@@ -134,7 +137,7 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl
element->Stream = stream;
element->Offset = 0;
element->Type = attributes[i].type;
element->Type = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDeclType(*attributes[i].attribute) : D3DDECLTYPE_FLOAT4;
element->Method = D3DDECLMETHOD_DEFAULT;
element->Usage = D3DDECLUSAGE_TEXCOORD;
element->UsageIndex = programBinary->getSemanticIndex(i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment