Moved IndexBuffer into its own file.

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@1602 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/renderer/IndexBuffer.cpp b/src/libGLESv2/renderer/IndexBuffer.cpp
new file mode 100644
index 0000000..703af92
--- /dev/null
+++ b/src/libGLESv2/renderer/IndexBuffer.cpp
@@ -0,0 +1,209 @@
+//
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// IndexBuffer.cpp: Defines the abstract IndexBuffer class and IndexBufferInterface
+// class with derivations, classes that perform graphics API agnostic index buffer operations.
+
+#include "libGLESv2/renderer/IndexBuffer.h"
+
+#include "libGLESv2/renderer/Renderer9.h"
+
+namespace rx
+{
+
+unsigned int IndexBuffer::mCurrentSerial = 1;
+
+IndexBuffer::IndexBuffer(rx::Renderer9 *renderer, UINT size, D3DFORMAT format) : mRenderer(renderer), mBufferSize(size), mIndexBuffer(NULL)
+{
+    if (size > 0)
+    {
+        // D3D9_REPLACE
+        HRESULT result = mRenderer->createIndexBuffer(size, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, format, &mIndexBuffer);
+        mSerial = issueSerial();
+
+        if (FAILED(result))
+        {
+            ERR("Out of memory allocating an index buffer of size %lu.", size);
+        }
+    }
+}
+
+IndexBuffer::~IndexBuffer()
+{
+    if (mIndexBuffer)
+    {
+        mIndexBuffer->Release();
+    }
+}
+
+IDirect3DIndexBuffer9 *IndexBuffer::getBuffer() const
+{
+    return mIndexBuffer;
+}
+
+unsigned int IndexBuffer::getSerial() const
+{
+    return mSerial;
+}
+
+unsigned int IndexBuffer::issueSerial()
+{
+    return mCurrentSerial++;
+}
+
+void IndexBuffer::unmap()
+{
+    if (mIndexBuffer)
+    {
+        mIndexBuffer->Unlock();
+    }
+}
+
+StreamingIndexBuffer::StreamingIndexBuffer(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format) : IndexBuffer(renderer, initialSize, format)
+{
+    mWritePosition = 0;
+}
+
+StreamingIndexBuffer::~StreamingIndexBuffer()
+{
+}
+
+void *StreamingIndexBuffer::map(UINT requiredSpace, UINT *offset)
+{
+    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;
+        }
+
+        *offset = mWritePosition;
+        mWritePosition += requiredSpace;
+    }
+
+    return mapPtr;
+}
+
+void StreamingIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type)
+{
+    if (requiredSpace > mBufferSize)
+    {
+        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;
+    }
+    else if (mWritePosition + requiredSpace > mBufferSize)   // Recycle
+    {
+        void *dummy;
+        mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
+        mIndexBuffer->Unlock();
+
+        mWritePosition = 0;
+    }
+}
+
+StaticIndexBuffer::StaticIndexBuffer(rx::Renderer9 *renderer) : IndexBuffer(renderer, 0, D3DFMT_UNKNOWN)
+{
+    mCacheType = GL_NONE;
+}
+
+StaticIndexBuffer::~StaticIndexBuffer()
+{
+}
+
+void *StaticIndexBuffer::map(UINT requiredSpace, UINT *offset)
+{
+    void *mapPtr = NULL;
+
+    if (mIndexBuffer)
+    {
+        HRESULT result = mIndexBuffer->Lock(0, requiredSpace, &mapPtr, 0);
+
+        if (FAILED(result))
+        {
+            ERR(" Lock failed with error 0x%08x", result);
+            return NULL;
+        }
+
+        *offset = 0;
+    }
+
+    return mapPtr;
+}
+
+void StaticIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type)
+{
+    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);
+        }
+
+        mBufferSize = requiredSpace;
+        mCacheType = type;
+    }
+    else if (mIndexBuffer && mBufferSize >= requiredSpace && mCacheType == type)
+    {
+        // Already allocated
+    }
+    else UNREACHABLE();   // Static index buffers can't be resized
+}
+
+bool StaticIndexBuffer::lookupType(GLenum type)
+{
+    return mCacheType == type;
+}
+
+UINT StaticIndexBuffer::lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex)
+{
+    IndexRange range = {offset, count};
+
+    std::map<IndexRange, IndexResult>::iterator res = mCache.find(range);
+
+    if (res == mCache.end())
+    {
+        return -1;
+    }
+
+    *minIndex = res->second.minIndex;
+    *maxIndex = res->second.maxIndex;
+    return res->second.streamOffset;
+}
+
+void StaticIndexBuffer::addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset)
+{
+    IndexRange indexRange = {offset, count};
+    IndexResult indexResult = {minIndex, maxIndex, streamOffset};
+    mCache[indexRange] = indexResult;
+}
+
+}
+
diff --git a/src/libGLESv2/renderer/IndexBuffer.h b/src/libGLESv2/renderer/IndexBuffer.h
new file mode 100644
index 0000000..75c6e8b
--- /dev/null
+++ b/src/libGLESv2/renderer/IndexBuffer.h
@@ -0,0 +1,115 @@
+//
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// IndexBuffer.h: Defines the abstract IndexBuffer class and IndexBufferInterface
+// class with derivations, classes that perform graphics API agnostic index buffer operations.
+
+#ifndef LIBGLESV2_RENDERER_INDEXBUFFER_H_
+#define LIBGLESV2_RENDERER_INDEXBUFFER_H_
+
+#include <vector>
+#include <cstddef>
+
+#define GL_APICALL
+#include <GLES2/gl2.h>
+
+#include "libGLESv2/Context.h"
+#include "libGLESv2/renderer/Renderer.h"
+
+namespace rx
+{
+
+class Renderer9;
+
+class IndexBuffer
+{
+  public:
+    IndexBuffer(rx::Renderer9 *renderer, UINT size, D3DFORMAT format);
+    virtual ~IndexBuffer();
+
+    UINT size() const { return mBufferSize; }
+    virtual void *map(UINT requiredSpace, UINT *offset) = 0;
+    void unmap();
+    virtual void reserveSpace(UINT requiredSpace, GLenum type) = 0;
+
+    IDirect3DIndexBuffer9 *getBuffer() const;
+    unsigned int getSerial() const;
+
+  protected:
+    rx::Renderer9 *const mRenderer;   // D3D9_REPLACE
+
+    IDirect3DIndexBuffer9 *mIndexBuffer;
+    UINT mBufferSize;
+
+    unsigned int mSerial;
+    static unsigned int issueSerial();
+    static unsigned int mCurrentSerial;
+
+  private:
+    DISALLOW_COPY_AND_ASSIGN(IndexBuffer);
+};
+
+class StreamingIndexBuffer : public IndexBuffer
+{
+  public:
+    StreamingIndexBuffer(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format);
+    ~StreamingIndexBuffer();
+
+    virtual void *map(UINT requiredSpace, UINT *offset);
+    virtual void reserveSpace(UINT requiredSpace, GLenum type);
+
+  private:
+    UINT mWritePosition;
+};
+
+class StaticIndexBuffer : public IndexBuffer
+{
+  public:
+    explicit StaticIndexBuffer(rx::Renderer9 *renderer);
+    ~StaticIndexBuffer();
+
+    virtual void *map(UINT requiredSpace, UINT *offset);
+    virtual void reserveSpace(UINT requiredSpace, GLenum type);
+
+    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;
+        GLsizei count;
+
+        bool operator<(const IndexRange& rhs) const
+        {
+            if (offset != rhs.offset)
+            {
+                return offset < rhs.offset;
+            }
+            if (count != rhs.count)
+            {
+                return count < rhs.count;
+            }
+            return false;
+        }
+    };
+
+    struct IndexResult
+    {
+        UINT minIndex;
+        UINT maxIndex;
+        UINT streamOffset;
+    };
+
+    std::map<IndexRange, IndexResult> mCache;
+};
+
+}
+
+#endif // LIBGLESV2_RENDERER_INDEXBUFFER_H_
\ No newline at end of file
diff --git a/src/libGLESv2/renderer/IndexDataManager.cpp b/src/libGLESv2/renderer/IndexDataManager.cpp
index 3464770..08f89c2 100644
--- a/src/libGLESv2/renderer/IndexDataManager.cpp
+++ b/src/libGLESv2/renderer/IndexDataManager.cpp
@@ -17,7 +17,6 @@
 
 namespace rx
 {
-unsigned int IndexBuffer::mCurrentSerial = 1;
 
 IndexDataManager::IndexDataManager(rx::Renderer9 *renderer) : mRenderer(renderer)
 {
@@ -281,193 +280,4 @@
     return mCountingBuffer;
 }
 
-IndexBuffer::IndexBuffer(rx::Renderer9 *renderer, UINT size, D3DFORMAT format) : mRenderer(renderer), mBufferSize(size), mIndexBuffer(NULL)
-{
-    if (size > 0)
-    {
-        // D3D9_REPLACE
-        HRESULT result = mRenderer->createIndexBuffer(size, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, format, &mIndexBuffer);
-        mSerial = issueSerial();
-
-        if (FAILED(result))
-        {
-            ERR("Out of memory allocating an index buffer of size %lu.", size);
-        }
-    }
-}
-
-IndexBuffer::~IndexBuffer()
-{
-    if (mIndexBuffer)
-    {
-        mIndexBuffer->Release();
-    }
-}
-
-IDirect3DIndexBuffer9 *IndexBuffer::getBuffer() const
-{
-    return mIndexBuffer;
-}
-
-unsigned int IndexBuffer::getSerial() const
-{
-    return mSerial;
-}
-
-unsigned int IndexBuffer::issueSerial()
-{
-    return mCurrentSerial++;
-}
-
-void IndexBuffer::unmap()
-{
-    if (mIndexBuffer)
-    {
-        mIndexBuffer->Unlock();
-    }
-}
-
-StreamingIndexBuffer::StreamingIndexBuffer(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format) : IndexBuffer(renderer, initialSize, format)
-{
-    mWritePosition = 0;
-}
-
-StreamingIndexBuffer::~StreamingIndexBuffer()
-{
-}
-
-void *StreamingIndexBuffer::map(UINT requiredSpace, UINT *offset)
-{
-    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;
-        }
-
-        *offset = mWritePosition;
-        mWritePosition += requiredSpace;
-    }
-
-    return mapPtr;
-}
-
-void StreamingIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type)
-{
-    if (requiredSpace > mBufferSize)
-    {
-        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;
-    }
-    else if (mWritePosition + requiredSpace > mBufferSize)   // Recycle
-    {
-        void *dummy;
-        mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
-        mIndexBuffer->Unlock();
-
-        mWritePosition = 0;
-    }
-}
-
-StaticIndexBuffer::StaticIndexBuffer(rx::Renderer9 *renderer) : IndexBuffer(renderer, 0, D3DFMT_UNKNOWN)
-{
-    mCacheType = GL_NONE;
-}
-
-StaticIndexBuffer::~StaticIndexBuffer()
-{
-}
-
-void *StaticIndexBuffer::map(UINT requiredSpace, UINT *offset)
-{
-    void *mapPtr = NULL;
-
-    if (mIndexBuffer)
-    {
-        HRESULT result = mIndexBuffer->Lock(0, requiredSpace, &mapPtr, 0);
-
-        if (FAILED(result))
-        {
-            ERR(" Lock failed with error 0x%08x", result);
-            return NULL;
-        }
-
-        *offset = 0;
-    }
-
-    return mapPtr;
-}
-
-void StaticIndexBuffer::reserveSpace(UINT requiredSpace, GLenum type)
-{
-    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);
-        }
-
-        mBufferSize = requiredSpace;
-        mCacheType = type;
-    }
-    else if (mIndexBuffer && mBufferSize >= requiredSpace && mCacheType == type)
-    {
-        // Already allocated
-    }
-    else UNREACHABLE();   // Static index buffers can't be resized
-}
-
-bool StaticIndexBuffer::lookupType(GLenum type)
-{
-    return mCacheType == type;
-}
-
-UINT StaticIndexBuffer::lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex)
-{
-    IndexRange range = {offset, count};
-
-    std::map<IndexRange, IndexResult>::iterator res = mCache.find(range);
-
-    if (res == mCache.end())
-    {
-        return -1;
-    }
-
-    *minIndex = res->second.minIndex;
-    *maxIndex = res->second.maxIndex;
-    return res->second.streamOffset;
-}
-
-void StaticIndexBuffer::addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset)
-{
-    IndexRange indexRange = {offset, count};
-    IndexResult indexResult = {minIndex, maxIndex, streamOffset};
-    mCache[indexRange] = indexResult;
-}
-
 }
diff --git a/src/libGLESv2/renderer/IndexDataManager.h b/src/libGLESv2/renderer/IndexDataManager.h
index 53c67d1..ff30f32 100644
--- a/src/libGLESv2/renderer/IndexDataManager.h
+++ b/src/libGLESv2/renderer/IndexDataManager.h
@@ -17,6 +17,7 @@
 #include <GLES2/gl2.h>
 
 #include "libGLESv2/Context.h"
+#include "libGLESv2/renderer/IndexBuffer.h"
 
 namespace
 {
@@ -33,92 +34,6 @@
     UINT startIndex;
 };
 
-class IndexBuffer
-{
-  public:
-    IndexBuffer(rx::Renderer9 *renderer, UINT size, D3DFORMAT format);
-    virtual ~IndexBuffer();
-
-    UINT size() const { return mBufferSize; }
-    virtual void *map(UINT requiredSpace, UINT *offset) = 0;
-    void unmap();
-    virtual void reserveSpace(UINT requiredSpace, GLenum type) = 0;
-
-    IDirect3DIndexBuffer9 *getBuffer() const;
-    unsigned int getSerial() const;
-
-  protected:
-    rx::Renderer9 *const mRenderer;   // D3D9_REPLACE
-
-    IDirect3DIndexBuffer9 *mIndexBuffer;
-    UINT mBufferSize;
-
-    unsigned int mSerial;
-    static unsigned int issueSerial();
-    static unsigned int mCurrentSerial;
-
-  private:
-    DISALLOW_COPY_AND_ASSIGN(IndexBuffer);
-};
-
-class StreamingIndexBuffer : public IndexBuffer
-{
-  public:
-    StreamingIndexBuffer(rx::Renderer9 *renderer, UINT initialSize, D3DFORMAT format);
-    ~StreamingIndexBuffer();
-
-    virtual void *map(UINT requiredSpace, UINT *offset);
-    virtual void reserveSpace(UINT requiredSpace, GLenum type);
-
-  private:
-    UINT mWritePosition;
-};
-
-class StaticIndexBuffer : public IndexBuffer
-{
-  public:
-    explicit StaticIndexBuffer(rx::Renderer9 *renderer);
-    ~StaticIndexBuffer();
-
-    virtual void *map(UINT requiredSpace, UINT *offset);
-    virtual void reserveSpace(UINT requiredSpace, GLenum type);
-
-    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;
-        GLsizei count;
-
-        bool operator<(const IndexRange& rhs) const
-        {
-            if (offset != rhs.offset)
-            {
-                return offset < rhs.offset;
-            }
-            if (count != rhs.count)
-            {
-                return count < rhs.count;
-            }
-            return false;
-        }
-    };
-
-    struct IndexResult
-    {
-        UINT minIndex;
-        UINT maxIndex;
-        UINT streamOffset;
-    };
-
-    std::map<IndexRange, IndexResult> mCache;
-};
-
 class IndexDataManager
 {
   public: