Integrated new IndexBuffer into IndexDataManager and Renderer9.

TRAC #22237 Author: Geoff Lang Signed-off-by: Shannon Woods Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1607 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 0b6d7741
......@@ -70,7 +70,7 @@ void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
{
memcpy(mContents + offset, data, size);
if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0))
if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->getBufferSize() != 0))
{
invalidateStaticData();
}
......
......@@ -9,8 +9,6 @@
#include "libGLESv2/renderer/IndexBuffer.h"
#include "libGLESv2/renderer/Renderer9.h"
namespace rx
{
......@@ -36,172 +34,143 @@ void IndexBuffer::updateSerial()
}
unsigned int IndexBufferInterface::mCurrentSerial = 1;
IndexBufferInterface::IndexBufferInterface(rx::Renderer9 *renderer, UINT size, D3DFORMAT format) : mRenderer(renderer), mBufferSize(size), mIndexBuffer(NULL)
IndexBufferInterface::IndexBufferInterface(Renderer *renderer, bool dynamic) : mRenderer(renderer)
{
if (size > 0)
{
// D3D9_REPLACE
HRESULT result = mRenderer->createIndexBuffer(size, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, format, &mIndexBuffer);
mSerial = issueSerial();
mIndexBuffer = renderer->createIndexBuffer();
if (FAILED(result))
{
ERR("Out of memory allocating an index buffer of size %lu.", size);
}
}
mDynamic = dynamic;
mWritePosition = 0;
}
IndexBufferInterface::~IndexBufferInterface()
{
if (mIndexBuffer)
{
mIndexBuffer->Release();
delete mIndexBuffer;
}
}
IDirect3DIndexBuffer9 *IndexBufferInterface::getBuffer() const
GLenum IndexBufferInterface::getIndexType() const
{
return mIndexBuffer;
return mIndexBuffer->getIndexType();
}
unsigned int IndexBufferInterface::getSerial() const
unsigned int IndexBufferInterface::getBufferSize() const
{
return mSerial;
return mIndexBuffer->getBufferSize();
}
unsigned int IndexBufferInterface::issueSerial()
unsigned int IndexBufferInterface::getSerial() const
{
return mCurrentSerial++;
return mIndexBuffer->getSerial();
}
void IndexBufferInterface::unmap()
int IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory)
{
if (mIndexBuffer)
if (!mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory))
{
mIndexBuffer->Unlock();
*outMappedMemory = NULL;
return -1;
}
int oldWritePos = static_cast<int>(mWritePosition);
mWritePosition += size;
return oldWritePos;
}
StreamingIndexBufferInterface::StreamingIndexBufferInterface(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format) : IndexBufferInterface(renderer, initialSize, format)
bool IndexBufferInterface::unmapBuffer()
{
mWritePosition = 0;
return mIndexBuffer->unmapBuffer();
}
StreamingIndexBufferInterface::~StreamingIndexBufferInterface()
IndexBuffer * IndexBufferInterface::getIndexBuffer() const
{
return mIndexBuffer;
}
void *StreamingIndexBufferInterface::map(UINT requiredSpace, UINT *offset)
unsigned int IndexBufferInterface::getWritePosition() const
{
void *mapPtr = NULL;
if (mIndexBuffer)
{
HRESULT result = mIndexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
if (FAILED(result))
{
ERR(" Lock failed with error 0x%08x", result);
return NULL;
}
return mWritePosition;
}
*offset = mWritePosition;
mWritePosition += requiredSpace;
}
void IndexBufferInterface::setWritePosition(unsigned int writePosition)
{
mWritePosition = writePosition;
}
return mapPtr;
bool IndexBufferInterface::discard()
{
return mIndexBuffer->discard();
}
void StreamingIndexBufferInterface::reserveSpace(UINT requiredSpace, GLenum type)
bool IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType)
{
if (requiredSpace > mBufferSize)
if (mIndexBuffer->getBufferSize() == 0)
{
if (mIndexBuffer)
{
mIndexBuffer->Release();
mIndexBuffer = NULL;
}
mBufferSize = std::max(requiredSpace, 2 * mBufferSize);
// D3D9_REPLACE
HRESULT result = mRenderer->createIndexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, &mIndexBuffer);
mSerial = issueSerial();
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
}
mWritePosition = 0;
return mIndexBuffer->initialize(bufferSize, indexType, mDynamic);
}
else if (mWritePosition + requiredSpace > mBufferSize) // Recycle
else
{
void *dummy;
mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
mIndexBuffer->Unlock();
mWritePosition = 0;
return mIndexBuffer->setSize(bufferSize, indexType);
}
}
StaticIndexBufferInterface::StaticIndexBufferInterface(rx::Renderer9 *renderer) : IndexBufferInterface(renderer, 0, D3DFMT_UNKNOWN)
StreamingIndexBufferInterface::StreamingIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, true)
{
mCacheType = GL_NONE;
}
StaticIndexBufferInterface::~StaticIndexBufferInterface()
StreamingIndexBufferInterface::~StreamingIndexBufferInterface()
{
}
void *StaticIndexBufferInterface::map(UINT requiredSpace, UINT *offset)
bool StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
{
void *mapPtr = NULL;
if (mIndexBuffer)
bool result = true;
unsigned int curBufferSize = getBufferSize();
if (size > curBufferSize)
{
HRESULT result = mIndexBuffer->Lock(0, requiredSpace, &mapPtr, 0);
if (FAILED(result))
result = setBufferSize(std::max(size, 2 * curBufferSize), indexType);
setWritePosition(0);
}
else if (getWritePosition() + size > curBufferSize)
{
if (!discard())
{
ERR(" Lock failed with error 0x%08x", result);
return NULL;
return false;
}
*offset = 0;
setWritePosition(0);
}
return mapPtr;
return result;
}
void StaticIndexBufferInterface::reserveSpace(UINT requiredSpace, GLenum type)
StaticIndexBufferInterface::StaticIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, false)
{
if (!mIndexBuffer && mBufferSize == 0)
{
// D3D9_REPLACE
HRESULT result = mRenderer->createIndexBuffer(requiredSpace, D3DUSAGE_WRITEONLY, type == GL_UNSIGNED_INT ? D3DFMT_INDEX32 : D3DFMT_INDEX16, &mIndexBuffer);
mSerial = issueSerial();
}
if (FAILED(result))
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
}
StaticIndexBufferInterface::~StaticIndexBufferInterface()
{
}
mBufferSize = requiredSpace;
mCacheType = type;
bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
{
unsigned int curSize = getBufferSize();
if (curSize == 0)
{
return setBufferSize(size, indexType);
}
else if (mIndexBuffer && mBufferSize >= requiredSpace && mCacheType == type)
else if (curSize >= size && indexType == getIndexType())
{
// Already allocated
return true;
}
else
{
ERR("Static index buffers can't be resized");
UNREACHABLE();
return false;
}
else UNREACHABLE(); // Static index buffers can't be resized
}
bool StaticIndexBufferInterface::lookupType(GLenum type)
{
return mCacheType == type;
}
UINT StaticIndexBufferInterface::lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex)
......
......@@ -10,9 +10,6 @@
#ifndef LIBGLESV2_RENDERER_INDEXBUFFER_H_
#define LIBGLESV2_RENDERER_INDEXBUFFER_H_
#include <vector>
#include <cstddef>
#define GL_APICALL
#include <GLES2/gl2.h>
......@@ -22,8 +19,6 @@
namespace rx
{
class Renderer9;
class IndexBuffer
{
public:
......@@ -56,60 +51,61 @@ class IndexBuffer
class IndexBufferInterface
{
public:
IndexBufferInterface(rx::Renderer9 *renderer, UINT size, D3DFORMAT format);
IndexBufferInterface(Renderer *renderer, bool dynamic);
virtual ~IndexBufferInterface();
UINT size() const { return mBufferSize; }
virtual void *map(UINT requiredSpace, UINT *offset) = 0;
void unmap();
virtual void reserveSpace(UINT requiredSpace, GLenum type) = 0;
virtual bool reserveBufferSpace(unsigned int size, GLenum indexType) = 0;
GLenum getIndexType() const;
unsigned int getBufferSize() const;
IDirect3DIndexBuffer9 *getBuffer() const;
unsigned int getSerial() const;
int mapBuffer(unsigned int size, void** outMappedMemory);
bool unmapBuffer();
IndexBuffer *getIndexBuffer() const;
protected:
rx::Renderer9 *const mRenderer; // D3D9_REPLACE
unsigned int getWritePosition() const;
void setWritePosition(unsigned int writePosition);
IDirect3DIndexBuffer9 *mIndexBuffer;
UINT mBufferSize;
bool discard();
unsigned int mSerial;
static unsigned int issueSerial();
static unsigned int mCurrentSerial;
bool setBufferSize(unsigned int bufferSize, GLenum indexType);
private:
DISALLOW_COPY_AND_ASSIGN(IndexBufferInterface);
rx::Renderer *const mRenderer;
IndexBuffer* mIndexBuffer;
unsigned int mWritePosition;
bool mDynamic;
};
class StreamingIndexBufferInterface : public IndexBufferInterface
{
public:
StreamingIndexBufferInterface(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format);
StreamingIndexBufferInterface(Renderer *renderer);
~StreamingIndexBufferInterface();
virtual void *map(UINT requiredSpace, UINT *offset);
virtual void reserveSpace(UINT requiredSpace, GLenum type);
private:
UINT mWritePosition;
virtual bool reserveBufferSpace(unsigned int size, GLenum indexType);
};
class StaticIndexBufferInterface : public IndexBufferInterface
{
public:
explicit StaticIndexBufferInterface(rx::Renderer9 *renderer);
explicit StaticIndexBufferInterface(Renderer *renderer);
~StaticIndexBufferInterface();
virtual void *map(UINT requiredSpace, UINT *offset);
virtual void reserveSpace(UINT requiredSpace, GLenum type);
virtual bool reserveBufferSpace(unsigned int size, GLenum indexType);
bool lookupType(GLenum type);
UINT lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex); // Returns the offset into the index buffer, or -1 if not found
void addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset);
private:
GLenum mCacheType;
struct IndexRange
{
intptr_t offset;
......
......@@ -32,24 +32,24 @@ struct TranslatedIndexData
UINT minIndex;
UINT maxIndex;
UINT startIndex;
IndexBuffer *indexBuffer;
unsigned int serial;
};
class IndexDataManager
{
public:
IndexDataManager(rx::Renderer9 *renderer);
explicit IndexDataManager(Renderer *renderer);
virtual ~IndexDataManager();
GLenum prepareIndexData(GLenum type, GLsizei count, gl::Buffer *arrayElementBuffer, const GLvoid *indices, TranslatedIndexData *translated, IDirect3DIndexBuffer9 **indexBuffer, unsigned int *serial);
GLenum prepareIndexData(GLenum type, GLsizei count, gl::Buffer *arrayElementBuffer, const GLvoid *indices, TranslatedIndexData *translated);
StaticIndexBufferInterface *getCountingIndices(GLsizei count);
private:
DISALLOW_COPY_AND_ASSIGN(IndexDataManager);
std::size_t typeSize(GLenum type) const;
std::size_t indexSize(D3DFORMAT format) const;
rx::Renderer9 *const mRenderer; // D3D9_REPLACE
Renderer *const mRenderer;
StreamingIndexBufferInterface *mStreamingBufferShort;
StreamingIndexBufferInterface *mStreamingBufferInt;
......
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