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) ...@@ -70,7 +70,7 @@ void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
{ {
memcpy(mContents + offset, data, size); memcpy(mContents + offset, data, size);
if ((mStaticVertexBuffer && mStaticVertexBuffer->size() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0)) if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0))
{ {
invalidateStaticData(); invalidateStaticData();
} }
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "libGLESv2/renderer/VertexBuffer.h" #include "libGLESv2/renderer/VertexBuffer.h"
#include "libGLESv2/renderer/Renderer9.h"
namespace rx namespace rx
{ {
...@@ -35,189 +33,150 @@ unsigned int VertexBuffer::getSerial() const ...@@ -35,189 +33,150 @@ unsigned int VertexBuffer::getSerial() const
return mSerial; return mSerial;
} }
VertexBufferInterface::VertexBufferInterface(rx::Renderer *renderer, bool dynamic) : mRenderer(renderer)
unsigned int VertexBufferInterface::mCurrentSerial = 1;
VertexBufferInterface::VertexBufferInterface(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : mRenderer(renderer), mVertexBuffer(NULL)
{ {
if (size > 0) mDynamic = dynamic;
{
// 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;
mWritePosition = 0; mWritePosition = 0;
mRequiredSpace = 0; mReservedSpace = 0;
}
VertexBufferInterface::~VertexBufferInterface() mVertexBuffer = renderer->createVertexBuffer();
{
if (mVertexBuffer)
{
mVertexBuffer->Release();
}
} }
void VertexBufferInterface::unmap() VertexBufferInterface::~VertexBufferInterface()
{ {
if (mVertexBuffer) delete mVertexBuffer;
{
mVertexBuffer->Unlock();
}
} }
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 (!reserveSpace(mReservedSpace))
if (mVertexBuffer)
{ {
HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE); return -1;
}
if (FAILED(result)) mReservedSpace = 0;
{
ERR("Lock failed with error 0x%08x", result);
return NULL;
}
*offset = mWritePosition; if (!mVertexBuffer->storeVertexAttributes(attrib, start, count, instances, mWritePosition))
mWritePosition += requiredSpace; {
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) return -1;
{
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;
} }
else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle mReservedSpace = 0;
{
if (mVertexBuffer)
{
void *dummy;
mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
mVertexBuffer->Unlock();
}
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; return mVertexBuffer;
}
if (mVertexBuffer)
{
HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
if (FAILED(result))
{
ERR("Lock failed with error 0x%08x", result);
return NULL;
}
int attributeOffset = attribute.mOffset % attribute.stride();
VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
mCache.push_back(element);
*streamOffset = mWritePosition; StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true)
mWritePosition += requiredSpace; {
} 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 result = setBufferSize(std::max(size, 3 * curBufferSize / 2));
HRESULT result = mRenderer->createVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, &mVertexBuffer); setWritePosition(0);
mSerial = issueSerial();
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
}
mBufferSize = mRequiredSpace;
} }
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++) for (unsigned int element = 0; element < mCache.size(); element++)
{ {
...@@ -236,4 +195,32 @@ std::size_t StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribu ...@@ -236,4 +195,32 @@ std::size_t StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribu
return -1; 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 @@ ...@@ -17,6 +17,7 @@
#include <GLES2/gl2.h> #include <GLES2/gl2.h>
#include "libGLESv2/Context.h" #include "libGLESv2/Context.h"
#include "libGLESv2/renderer/Renderer.h"
namespace rx namespace rx
{ {
...@@ -55,55 +56,66 @@ class VertexBuffer ...@@ -55,55 +56,66 @@ class VertexBuffer
class VertexBufferInterface class VertexBufferInterface
{ {
public: public:
VertexBufferInterface(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags); VertexBufferInterface(rx::Renderer *renderer, bool dynamic);
virtual ~VertexBufferInterface(); virtual ~VertexBufferInterface();
void unmap(); void reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances);
virtual void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset) = 0; void reserveRawDataSpace(unsigned int size);
std::size_t size() const { return mBufferSize; } unsigned int getBufferSize() const;
virtual void reserveRequiredSpace() = 0;
void addRequiredSpace(UINT requiredSpace);
IDirect3DVertexBuffer9 *getBuffer() const;
unsigned int getSerial() 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: protected:
rx::Renderer9 *const mRenderer; // D3D9_REPLACE virtual bool reserveSpace(unsigned int size) = 0;
IDirect3DVertexBuffer9 *mVertexBuffer;
unsigned int mSerial; unsigned int getWritePosition() const;
static unsigned int issueSerial(); void setWritePosition(unsigned int writePosition);
static unsigned int mCurrentSerial;
std::size_t mBufferSize; bool discard();
std::size_t mWritePosition;
std::size_t mRequiredSpace; bool setBufferSize(unsigned int size);
private: private:
DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface); DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface);
rx::Renderer *const mRenderer;
VertexBuffer* mVertexBuffer;
unsigned int mWritePosition;
unsigned int mReservedSpace;
bool mDynamic;
}; };
class StreamingVertexBufferInterface : public VertexBufferInterface class StreamingVertexBufferInterface : public VertexBufferInterface
{ {
public: public:
StreamingVertexBufferInterface(rx::Renderer9 *renderer, std::size_t initialSize); StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize);
~StreamingVertexBufferInterface(); ~StreamingVertexBufferInterface();
void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset); protected:
void reserveRequiredSpace(); bool reserveSpace(unsigned int size);
}; };
class StaticVertexBufferInterface : public VertexBufferInterface class StaticVertexBufferInterface : public VertexBufferInterface
{ {
public: public:
explicit StaticVertexBufferInterface(rx::Renderer9 *renderer); explicit StaticVertexBufferInterface(rx::Renderer *renderer);
~StaticVertexBufferInterface(); ~StaticVertexBufferInterface();
void *map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset); int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances);
void reserveRequiredSpace();
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: private:
struct VertexElement struct VertexElement
...@@ -114,7 +126,7 @@ class StaticVertexBufferInterface : public VertexBufferInterface ...@@ -114,7 +126,7 @@ class StaticVertexBufferInterface : public VertexBufferInterface
bool normalized; bool normalized;
int attributeOffset; int attributeOffset;
std::size_t streamOffset; unsigned int streamOffset;
}; };
std::vector<VertexElement> mCache; std::vector<VertexElement> mCache;
......
...@@ -26,11 +26,11 @@ struct TranslatedAttribute ...@@ -26,11 +26,11 @@ struct TranslatedAttribute
{ {
bool active; bool active;
D3DDECLTYPE type; const gl::VertexAttribute *attribute;
UINT offset; UINT offset;
UINT stride; // 0 means not to advance the read pointer at all UINT stride; // 0 means not to advance the read pointer at all
IDirect3DVertexBuffer9 *vertexBuffer; VertexBuffer *vertexBuffer;
unsigned int serial; unsigned int serial;
unsigned int divisor; unsigned int divisor;
}; };
...@@ -38,7 +38,7 @@ struct TranslatedAttribute ...@@ -38,7 +38,7 @@ struct TranslatedAttribute
class VertexDataManager class VertexDataManager
{ {
public: public:
VertexDataManager(rx::Renderer9 *renderer); VertexDataManager(rx::Renderer *renderer);
virtual ~VertexDataManager(); virtual ~VertexDataManager();
GLenum prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *outAttribs, GLsizei instances); GLenum prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *outAttribs, GLsizei instances);
...@@ -46,44 +46,13 @@ class VertexDataManager ...@@ -46,44 +46,13 @@ class VertexDataManager
private: private:
DISALLOW_COPY_AND_ASSIGN(VertexDataManager); DISALLOW_COPY_AND_ASSIGN(VertexDataManager);
std::size_t spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const; rx::Renderer *const mRenderer;
std::size_t writeAttributeData(VertexBufferInterface *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances);
rx::Renderer9 *const mRenderer; // D3D9_REPLACE
StreamingVertexBufferInterface *mStreamingBuffer; StreamingVertexBufferInterface *mStreamingBuffer;
float mCurrentValue[gl::MAX_VERTEX_ATTRIBS][4]; float mCurrentValue[gl::MAX_VERTEX_ATTRIBS][4];
StreamingVertexBufferInterface *mCurrentValueBuffer[gl::MAX_VERTEX_ATTRIBS]; StreamingVertexBufferInterface *mCurrentValueBuffer[gl::MAX_VERTEX_ATTRIBS];
std::size_t mCurrentValueOffsets[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 @@ ...@@ -7,6 +7,7 @@
// VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations. // VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations.
#include "libGLESv2/ProgramBinary.h" #include "libGLESv2/ProgramBinary.h"
#include "libGLESv2/renderer/VertexBuffer9.h"
#include "libGLESv2/renderer/VertexDataManager.h" #include "libGLESv2/renderer/VertexDataManager.h"
#include "libGLESv2/renderer/VertexDeclarationCache.h" #include "libGLESv2/renderer/VertexDeclarationCache.h"
...@@ -122,11 +123,13 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl ...@@ -122,11 +123,13 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl
} }
} }
VertexBuffer9 *vertexBuffer = VertexBuffer9::makeVertexBuffer9(attributes[i].vertexBuffer);
if (mAppliedVBs[stream].serial != attributes[i].serial || if (mAppliedVBs[stream].serial != attributes[i].serial ||
mAppliedVBs[stream].stride != attributes[i].stride || mAppliedVBs[stream].stride != attributes[i].stride ||
mAppliedVBs[stream].offset != attributes[i].offset) 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].serial = attributes[i].serial;
mAppliedVBs[stream].stride = attributes[i].stride; mAppliedVBs[stream].stride = attributes[i].stride;
mAppliedVBs[stream].offset = attributes[i].offset; mAppliedVBs[stream].offset = attributes[i].offset;
...@@ -134,7 +137,7 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl ...@@ -134,7 +137,7 @@ GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, Transl
element->Stream = stream; element->Stream = stream;
element->Offset = 0; 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->Method = D3DDECLMETHOD_DEFAULT;
element->Usage = D3DDECLUSAGE_TEXCOORD; element->Usage = D3DDECLUSAGE_TEXCOORD;
element->UsageIndex = programBinary->getSemanticIndex(i); 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