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;
......
...@@ -31,13 +31,13 @@ namespace ...@@ -31,13 +31,13 @@ namespace
namespace rx namespace rx
{ {
int elementsInBuffer(const gl::VertexAttribute &attribute, int size) static int elementsInBuffer(const gl::VertexAttribute &attribute, int size)
{ {
int stride = attribute.stride(); int stride = attribute.stride();
return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride; return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
} }
VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer) VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer)
{ {
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{ {
...@@ -49,9 +49,6 @@ VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer) ...@@ -49,9 +49,6 @@ VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer)
mCurrentValueOffsets[i] = 0; mCurrentValueOffsets[i] = 0;
} }
// D3D9_REPLACE
checkVertexCaps(renderer->getCapsDeclTypes());
mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
if (!mStreamingBuffer) if (!mStreamingBuffer)
...@@ -70,60 +67,6 @@ VertexDataManager::~VertexDataManager() ...@@ -70,60 +67,6 @@ VertexDataManager::~VertexDataManager()
} }
} }
std::size_t VertexDataManager::writeAttributeData(VertexBufferInterface *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances)
{
gl::Buffer *buffer = attribute.mBoundBuffer.get();
int inputStride = attribute.stride();
int elementSize = attribute.typeSize();
const FormatConverter &converter = formatConverter(attribute);
std::size_t streamOffset = 0;
void *output = NULL;
if (vertexBuffer)
{
output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
}
if (output == NULL)
{
ERR("Failed to map vertex buffer.");
return -1;
}
const char *input = NULL;
if (buffer)
{
int offset = attribute.mOffset;
input = static_cast<const char*>(buffer->data()) + offset;
}
else
{
input = static_cast<const char*>(attribute.mPointer);
}
if (instances == 0 || attribute.mDivisor == 0)
{
input += inputStride * start;
}
if (converter.identity && inputStride == elementSize)
{
memcpy(output, input, count * inputStride);
}
else
{
converter.convertArray(input, inputStride, count, output);
}
vertexBuffer->unmap();
return streamOffset;
}
GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances) GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
{ {
if (!mStreamingBuffer) if (!mStreamingBuffer)
...@@ -136,7 +79,7 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -136,7 +79,7 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1); translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
} }
// Determine the required storage size per used buffer, and invalidate static buffers that don't contain matching attributes // Invalidate static buffers that don't contain matching attributes
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{ {
if (translated[i].active && attribs[i].mArrayEnabled) if (translated[i].active && attribs[i].mArrayEnabled)
...@@ -146,53 +89,20 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -146,53 +89,20 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
if (staticBuffer) if (staticBuffer)
{ {
if (staticBuffer->size() == 0) if (staticBuffer->getBufferSize() == 0)
{ {
int totalCount = elementsInBuffer(attribs[i], buffer->size()); int totalCount = elementsInBuffer(attribs[i], buffer->size());
staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0)); staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0);
} }
else if (staticBuffer->lookupAttribute(attribs[i]) == -1) else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
{ {
// This static buffer doesn't have matching attributes, so fall back to using the streaming buffer mStreamingBuffer->reserveVertexSpace(attribs[i], count, instances);
// Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
for (int previous = 0; previous < i; previous++)
{
if (translated[previous].active && attribs[previous].mArrayEnabled)
{
gl::Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
StaticVertexBufferInterface *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
if (staticBuffer == previousStaticBuffer)
{
mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
}
}
}
mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
buffer->invalidateStaticData(); buffer->invalidateStaticData();
} }
} }
else else
{ {
mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances)); mStreamingBuffer->reserveVertexSpace(attribs[i], count, instances);
}
}
}
// Reserve the required space per used buffer
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{
if (translated[i].active && attribs[i].mArrayEnabled)
{
gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
VertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
if (vertexBuffer)
{
vertexBuffer->reserveRequiredSpace();
} }
} }
} }
...@@ -213,16 +123,16 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -213,16 +123,16 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
return GL_INVALID_OPERATION; return GL_INVALID_OPERATION;
} }
const FormatConverter &converter = formatConverter(attribs[i]);
StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
std::size_t streamOffset = -1; std::size_t streamOffset = -1;
unsigned int outputElementSize = 0;
if (staticBuffer) if (staticBuffer)
{ {
streamOffset = staticBuffer->lookupAttribute(attribs[i]); streamOffset = staticBuffer->lookupAttribute(attribs[i]);
outputElementSize = staticBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0);
if (streamOffset == -1) if (streamOffset == -1)
{ {
...@@ -230,22 +140,23 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -230,22 +140,23 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
int totalCount = elementsInBuffer(attribs[i], buffer->size()); int totalCount = elementsInBuffer(attribs[i], buffer->size());
int startIndex = attribs[i].mOffset / attribs[i].stride(); int startIndex = attribs[i].mOffset / attribs[i].stride();
streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0); streamOffset = staticBuffer->storeVertexAttributes(attribs[i], -startIndex, totalCount, 0);
} }
if (streamOffset != -1) if (streamOffset != -1)
{ {
streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize; streamOffset += (attribs[i].mOffset / attribs[i].stride()) * outputElementSize;
if (instances == 0 || attribs[i].mDivisor == 0) if (instances == 0 || attribs[i].mDivisor == 0)
{ {
streamOffset += start * converter.outputElementSize; streamOffset += start * outputElementSize;
} }
} }
} }
else else
{ {
streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances); outputElementSize = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0);
streamOffset = mStreamingBuffer->storeVertexAttributes(attribs[i], start, count, instances);
} }
if (streamOffset == -1) if (streamOffset == -1)
...@@ -253,12 +164,12 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -253,12 +164,12 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
return GL_OUT_OF_MEMORY; return GL_OUT_OF_MEMORY;
} }
translated[i].vertexBuffer = vertexBuffer->getBuffer(); translated[i].vertexBuffer = vertexBuffer->getVertexBuffer();
translated[i].serial = vertexBuffer->getSerial(); translated[i].serial = vertexBuffer->getSerial();
translated[i].divisor = attribs[i].mDivisor; translated[i].divisor = attribs[i].mDivisor;
translated[i].type = converter.d3dDeclType; translated[i].attribute = &attribs[i];
translated[i].stride = converter.outputElementSize; translated[i].stride = outputElementSize;
translated[i].offset = streamOffset; translated[i].offset = streamOffset;
} }
else else
...@@ -275,30 +186,22 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -275,30 +186,22 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
mCurrentValue[i][2] != attribs[i].mCurrentValue[2] || mCurrentValue[i][2] != attribs[i].mCurrentValue[2] ||
mCurrentValue[i][3] != attribs[i].mCurrentValue[3]) mCurrentValue[i][3] != attribs[i].mCurrentValue[3])
{ {
const int requiredSpace = 4 * sizeof(float); unsigned int requiredSpace = sizeof(float) * 4;
buffer->addRequiredSpace(requiredSpace); buffer->reserveRawDataSpace(requiredSpace);
buffer->reserveRequiredSpace(); int streamOffset = buffer->storeRawData(attribs[i].mCurrentValue, requiredSpace);
float *data = static_cast<float*>(buffer->map(gl::VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i])); if (streamOffset == -1)
if (data)
{ {
data[0] = attribs[i].mCurrentValue[0]; return GL_OUT_OF_MEMORY;
data[1] = attribs[i].mCurrentValue[1];
data[2] = attribs[i].mCurrentValue[2];
data[3] = attribs[i].mCurrentValue[3];
buffer->unmap();
mCurrentValue[i][0] = attribs[i].mCurrentValue[0];
mCurrentValue[i][1] = attribs[i].mCurrentValue[1];
mCurrentValue[i][2] = attribs[i].mCurrentValue[2];
mCurrentValue[i][3] = attribs[i].mCurrentValue[3];
} }
mCurrentValueOffsets[i] = streamOffset;
} }
translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer(); translated[i].vertexBuffer = mCurrentValueBuffer[i]->getVertexBuffer();
translated[i].serial = mCurrentValueBuffer[i]->getSerial(); translated[i].serial = mCurrentValueBuffer[i]->getSerial();
translated[i].divisor = 0; translated[i].divisor = 0;
translated[i].type = D3DDECLTYPE_FLOAT4; translated[i].attribute = &attribs[i];
translated[i].stride = 0; translated[i].stride = 0;
translated[i].offset = mCurrentValueOffsets[i]; translated[i].offset = mCurrentValueOffsets[i];
} }
...@@ -321,268 +224,4 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], ...@@ -321,268 +224,4 @@ GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[],
return GL_NO_ERROR; return GL_NO_ERROR;
} }
std::size_t VertexDataManager::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const
{
size_t elementSize = formatConverter(attrib).outputElementSize;
if (instances == 0 || attrib.mDivisor == 0)
{
return elementSize * count;
}
else
{
return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
}
}
// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
//
// BYTE SHORT (Cast)
// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
// SHORT SHORT (Identity)
// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
// UNSIGNED_SHORT FLOAT (Cast)
// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
// FIXED (not in WebGL) FLOAT (FixedToFloat)
// FLOAT FLOAT (Identity)
// GLToCType maps from GL type (as GLenum) to the C typedef.
template <GLenum GLType> struct GLToCType { };
template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
enum D3DVertexType
{
D3DVT_FLOAT,
D3DVT_SHORT,
D3DVT_SHORT_NORM,
D3DVT_UBYTE,
D3DVT_UBYTE_NORM,
D3DVT_USHORT_NORM
};
// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
template <unsigned int D3DType> struct D3DToCType { };
template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
template <unsigned int type, int size>
struct WidenRule
{
};
template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { };
template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { };
template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { };
template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { };
template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { };
template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { };
// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
template <unsigned int d3dtype, int size>
struct VertexTypeFlags
{
};
template <unsigned int _capflag, unsigned int _declflag>
struct VertexTypeFlagsHelper
{
enum { capflag = _capflag };
enum { declflag = _declflag };
};
template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
template <GLenum GLtype, bool normalized>
struct VertexTypeMapping
{
};
template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
struct VertexTypeMappingBase
{
enum { preferred = Preferred };
enum { fallback = Fallback };
};
template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
// The conversion rules themselves are defined in vertexconversion.h.
// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
template <GLenum fromType, bool normalized, unsigned int toType>
struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
{
};
// All conversions from normalized types to float use the Normalize operator.
template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { };
// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
// whether it is normalized or not.
template <class T, bool normalized>
struct DefaultVertexValuesStage2
{
};
template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { };
template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { };
// Work out the default value rule for a D3D type (expressed as the C type) and
template <class T, bool normalized>
struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
{
};
template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { };
// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
// The fallback conversion produces an output that all D3D9 devices must support.
template <class T> struct UsePreferred { enum { type = T::preferred }; };
template <class T> struct UseFallback { enum { type = T::fallback }; };
// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
struct Converter
: VertexDataConverter<typename GLToCType<fromType>::type,
WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
ConversionRule<fromType,
normalized,
PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
{
private:
enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
public:
enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
};
// Initialise a TranslationInfo
#define TRANSLATION(type, norm, size, preferred) \
{ \
Converter<type, norm, size, preferred>::identity, \
Converter<type, norm, size, preferred>::finalSize, \
Converter<type, norm, size, preferred>::convertArray, \
static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
}
#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
{ \
Converter<type, norm, size, UsePreferred>::capflag, \
TRANSLATION(type, norm, size, UsePreferred), \
TRANSLATION(type, norm, size, UseFallback) \
}
#define TRANSLATIONS_FOR_TYPE(type) \
{ \
{ TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
{ TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \
}
#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
{ \
{ TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
{ TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
}
const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
{
TRANSLATIONS_FOR_TYPE(GL_BYTE),
TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
TRANSLATIONS_FOR_TYPE(GL_SHORT),
TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
};
void VertexDataManager::checkVertexCaps(DWORD declTypes)
{
for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
{
for (unsigned int j = 0; j < 2; j++)
{
for (unsigned int k = 0; k < 4; k++)
{
if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
{
mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
}
else
{
mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
}
}
}
}
}
// This is used to index mAttributeTypes and mPossibleTranslations.
unsigned int VertexDataManager::typeIndex(GLenum type) const
{
switch (type)
{
case GL_BYTE: return 0;
case GL_UNSIGNED_BYTE: return 1;
case GL_SHORT: return 2;
case GL_UNSIGNED_SHORT: return 3;
case GL_FIXED: return 4;
case GL_FLOAT: return 5;
default: UNREACHABLE(); return 5;
}
}
const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const gl::VertexAttribute &attribute) const
{
return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
}
} }
...@@ -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