Updated the index buffer classes to use Error objects.

BUG=angle:520

Change-Id: Ifc249058a3ed3ffffe163a9e3ec21d6fc8c75bd0
Reviewed-on: https://chromium-review.googlesource.com/217101
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index 985a756..aef626a 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -1788,16 +1788,16 @@
     VertexArray *vao = mState.getVertexArray();
     rx::TranslatedIndexData indexInfo;
     indexInfo.indexRange = indexRange;
-    GLenum err = mRenderer->applyIndexBuffer(indices, vao->getElementArrayBuffer(), count, mode, type, &indexInfo);
-    if (err != GL_NO_ERROR)
+    Error error = mRenderer->applyIndexBuffer(indices, vao->getElementArrayBuffer(), count, mode, type, &indexInfo);
+    if (error.isError())
     {
-        return gl::error(err);
+        return gl::error(error.getCode());
     }
 
     GLsizei vertexCount = indexInfo.indexRange.length() + 1;
-    err = mRenderer->applyVertexBuffer(programBinary, vao->getVertexAttributes(),
-                                       mState.getVertexAttribCurrentValues(),
-                                       indexInfo.indexRange.start, vertexCount, instances);
+    GLenum err = mRenderer->applyVertexBuffer(programBinary, vao->getVertexAttributes(),
+                                              mState.getVertexAttribCurrentValues(),
+                                              indexInfo.indexRange.start, vertexCount, instances);
     if (err != GL_NO_ERROR)
     {
         return gl::error(err);
diff --git a/src/libGLESv2/renderer/Renderer.h b/src/libGLESv2/renderer/Renderer.h
index 0148ed7..2d45cdb 100644
--- a/src/libGLESv2/renderer/Renderer.h
+++ b/src/libGLESv2/renderer/Renderer.h
@@ -135,7 +135,7 @@
     virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount) = 0;
     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
                                      GLint first, GLsizei count, GLsizei instances) = 0;
-    virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) = 0;
+    virtual gl::Error applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) = 0;
     virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]) = 0;
 
     virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive) = 0;
diff --git a/src/libGLESv2/renderer/d3d/IndexBuffer.cpp b/src/libGLESv2/renderer/d3d/IndexBuffer.cpp
index 8bf9f2a..1dce127 100644
--- a/src/libGLESv2/renderer/d3d/IndexBuffer.cpp
+++ b/src/libGLESv2/renderer/d3d/IndexBuffer.cpp
@@ -66,21 +66,22 @@
     return mIndexBuffer->getSerial();
 }
 
-bool IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory, unsigned int *streamOffset)
+gl::Error IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory, unsigned int *streamOffset)
 {
     // Protect against integer overflow
     if (mWritePosition + size < mWritePosition)
     {
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Mapping of internal index buffer would cause an integer overflow.");
     }
 
-    if (!mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory))
+    gl::Error error = mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory);
+    if (error.isError())
     {
         if (outMappedMemory)
         {
             *outMappedMemory = NULL;
         }
-        return false;
+        return error;
     }
 
     if (streamOffset)
@@ -89,10 +90,10 @@
     }
 
     mWritePosition += size;
-    return true;
+    return gl::Error(GL_NO_ERROR);
 }
 
-bool IndexBufferInterface::unmapBuffer()
+gl::Error IndexBufferInterface::unmapBuffer()
 {
     return mIndexBuffer->unmapBuffer();
 }
@@ -112,12 +113,12 @@
     mWritePosition = writePosition;
 }
 
-bool IndexBufferInterface::discard()
+gl::Error IndexBufferInterface::discard()
 {
     return mIndexBuffer->discard();
 }
 
-bool IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType)
+gl::Error IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType)
 {
     if (mIndexBuffer->getBufferSize() == 0)
     {
@@ -137,26 +138,30 @@
 {
 }
 
-bool StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
+gl::Error StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
 {
-    bool result = true;
     unsigned int curBufferSize = getBufferSize();
     unsigned int writePos = getWritePosition();
     if (size > curBufferSize)
     {
-        result = setBufferSize(std::max(size, 2 * curBufferSize), indexType);
+        gl::Error error = setBufferSize(std::max(size, 2 * curBufferSize), indexType);
+        if (error.isError())
+        {
+            return error;
+        }
         setWritePosition(0);
     }
     else if (writePos + size > curBufferSize || writePos + size < writePos)
     {
-        if (!discard())
+        gl::Error error = discard();
+        if (error.isError())
         {
-            return false;
+            return error;
         }
         setWritePosition(0);
     }
 
-    return result;
+    return gl::Error(GL_NO_ERROR);
 }
 
 
@@ -168,7 +173,7 @@
 {
 }
 
-bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
+gl::Error StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType)
 {
     unsigned int curSize = getBufferSize();
     if (curSize == 0)
@@ -177,13 +182,12 @@
     }
     else if (curSize >= size && indexType == getIndexType())
     {
-        return true;
+        return gl::Error(GL_NO_ERROR);
     }
     else
     {
-        ERR("Static index buffers can't be resized");
         UNREACHABLE();
-        return false;
+        return gl::Error(GL_INVALID_OPERATION, "Internal static index buffers can't be resized");
     }
 }
 
diff --git a/src/libGLESv2/renderer/d3d/IndexBuffer.h b/src/libGLESv2/renderer/d3d/IndexBuffer.h
index 6fb885a..1bb5ae2 100644
--- a/src/libGLESv2/renderer/d3d/IndexBuffer.h
+++ b/src/libGLESv2/renderer/d3d/IndexBuffer.h
@@ -11,6 +11,7 @@
 #define LIBGLESV2_RENDERER_INDEXBUFFER_H_
 
 #include "common/angleutils.h"
+#include "libGLESv2/Error.h"
 #include "libGLESv2/renderer/IndexRangeCache.h"
 
 namespace rx
@@ -23,16 +24,16 @@
     IndexBuffer();
     virtual ~IndexBuffer();
 
-    virtual bool initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) = 0;
+    virtual gl::Error initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) = 0;
 
-    virtual bool mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) = 0;
-    virtual bool unmapBuffer() = 0;
+    virtual gl::Error mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) = 0;
+    virtual gl::Error unmapBuffer() = 0;
 
-    virtual bool discard() = 0;
+    virtual gl::Error discard() = 0;
 
     virtual GLenum getIndexType() const = 0;
     virtual unsigned int getBufferSize() const = 0;
-    virtual bool setSize(unsigned int bufferSize, GLenum indexType) = 0;
+    virtual gl::Error setSize(unsigned int bufferSize, GLenum indexType) = 0;
 
     unsigned int getSerial() const;
 
@@ -52,15 +53,15 @@
     IndexBufferInterface(Renderer *renderer, bool dynamic);
     virtual ~IndexBufferInterface();
 
-    virtual bool reserveBufferSpace(unsigned int size, GLenum indexType) = 0;
+    virtual gl::Error reserveBufferSpace(unsigned int size, GLenum indexType) = 0;
 
     GLenum getIndexType() const;
     unsigned int getBufferSize() const;
 
     unsigned int getSerial() const;
 
-    bool mapBuffer(unsigned int size, void** outMappedMemory, unsigned int *streamOffset);
-    bool unmapBuffer();
+    gl::Error mapBuffer(unsigned int size, void** outMappedMemory, unsigned int *streamOffset);
+    gl::Error unmapBuffer();
 
     IndexBuffer *getIndexBuffer() const;
 
@@ -68,9 +69,9 @@
     unsigned int getWritePosition() const;
     void setWritePosition(unsigned int writePosition);
 
-    bool discard();
+    gl::Error discard();
 
-    bool setBufferSize(unsigned int bufferSize, GLenum indexType);
+    gl::Error setBufferSize(unsigned int bufferSize, GLenum indexType);
 
   private:
     DISALLOW_COPY_AND_ASSIGN(IndexBufferInterface);
@@ -89,7 +90,7 @@
     StreamingIndexBufferInterface(Renderer *renderer);
     ~StreamingIndexBufferInterface();
 
-    virtual bool reserveBufferSpace(unsigned int size, GLenum indexType);
+    virtual gl::Error reserveBufferSpace(unsigned int size, GLenum indexType);
 };
 
 class StaticIndexBufferInterface : public IndexBufferInterface
@@ -98,7 +99,7 @@
     explicit StaticIndexBufferInterface(Renderer *renderer);
     ~StaticIndexBufferInterface();
 
-    virtual bool reserveBufferSpace(unsigned int size, GLenum indexType);
+    virtual gl::Error reserveBufferSpace(unsigned int size, GLenum indexType);
 
     IndexRangeCache *getIndexRangeCache();
 
@@ -108,4 +109,4 @@
 
 }
 
-#endif // LIBGLESV2_RENDERER_INDEXBUFFER_H_
\ No newline at end of file
+#endif // LIBGLESV2_RENDERER_INDEXBUFFER_H_
diff --git a/src/libGLESv2/renderer/d3d/IndexDataManager.cpp b/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
index cd775c1..8d455b4 100644
--- a/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
+++ b/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
@@ -70,7 +70,7 @@
     SafeDelete(mStreamingBufferInt);
 }
 
-GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
+gl::Error IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
 {
     const gl::Type &typeInfo = gl::GetTypeInfo(type);
 
@@ -83,10 +83,6 @@
 
     if (buffer != NULL)
     {
-        if (reinterpret_cast<uintptr_t>(indices) > std::numeric_limits<unsigned int>::max())
-        {
-            return GL_OUT_OF_MEMORY;
-        }
         offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
 
         storage = BufferD3D::makeBufferD3D(buffer->getImplementation());
@@ -143,10 +139,10 @@
 
     if (!directStorage && !indexBuffer)
     {
-        GLenum err = getStreamingIndexBuffer(destinationIndexType, &indexBuffer);
-        if (err != GL_NO_ERROR)
+        gl::Error error = getStreamingIndexBuffer(destinationIndexType, &indexBuffer);
+        if (error.isError())
         {
-            return err;
+            return error;
         }
 
         unsigned int convertCount = count;
@@ -169,30 +165,30 @@
 
         if (convertCount > std::numeric_limits<unsigned int>::max() / destTypeInfo.bytes)
         {
-            ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, destTypeInfo.bytes);
-            return GL_OUT_OF_MEMORY;
+            return gl::Error(GL_OUT_OF_MEMORY, "Reserving %u indices of %u bytes each exceeds the maximum buffer size.",
+                             convertCount, destTypeInfo.bytes);
         }
 
         unsigned int bufferSizeRequired = convertCount * destTypeInfo.bytes;
-        if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type))
+        error = indexBuffer->reserveBufferSpace(bufferSizeRequired, type);
+        if (error.isError())
         {
-            ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired);
-            return GL_OUT_OF_MEMORY;
+            return error;
         }
 
         void* output = NULL;
-        if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset))
+        error = indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset);
+        if (error.isError())
         {
-            ERR("Failed to map index buffer.");
-            return GL_OUT_OF_MEMORY;
+            return error;
         }
 
         ConvertIndices(type, destinationIndexType, staticBuffer ? storage->getData() : indices, convertCount, output);
 
-        if (!indexBuffer->unmapBuffer())
+        error = indexBuffer->unmapBuffer();
+        if (error.isError())
         {
-            ERR("Failed to unmap index buffer.");
-            return GL_OUT_OF_MEMORY;
+            return error;
         }
 
         if (staticBuffer)
@@ -214,10 +210,10 @@
         storage->promoteStaticUsage(count * typeInfo.bytes);
     }
 
-    return GL_NO_ERROR;
+    return gl::Error(GL_NO_ERROR);
 }
 
-GLenum IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer)
+gl::Error IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer)
 {
     ASSERT(outBuffer);
     if (destinationIndexType == GL_UNSIGNED_INT)
@@ -225,16 +221,16 @@
         if (!mStreamingBufferInt)
         {
             mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer);
-            if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
+            gl::Error error = mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
+            if (error.isError())
             {
                 SafeDelete(mStreamingBufferInt);
-                ERR("Failed to allocate the streaming GL_UNSIGNED_INT index buffer.");
-                return GL_OUT_OF_MEMORY;
+                return error;
             }
         }
 
         *outBuffer = mStreamingBufferInt;
-        return GL_NO_ERROR;
+        return gl::Error(GL_NO_ERROR);
     }
     else
     {
@@ -243,16 +239,16 @@
         if (!mStreamingBufferShort)
         {
             mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer);
-            if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
+            gl::Error error = mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT);
+            if (error.isError())
             {
                 SafeDelete(mStreamingBufferShort);
-                ERR("Failed to allocate the streaming GL_UNSIGNED_SHORT index buffer.");
-                return GL_OUT_OF_MEMORY;
+                return error;
             }
         }
 
         *outBuffer = mStreamingBufferShort;
-        return GL_NO_ERROR;
+        return gl::Error(GL_NO_ERROR);
     }
 }
 
diff --git a/src/libGLESv2/renderer/d3d/IndexDataManager.h b/src/libGLESv2/renderer/d3d/IndexDataManager.h
index fef7f32..6d0b89e 100644
--- a/src/libGLESv2/renderer/d3d/IndexDataManager.h
+++ b/src/libGLESv2/renderer/d3d/IndexDataManager.h
@@ -12,6 +12,7 @@
 
 #include "common/angleutils.h"
 #include "common/mathutil.h"
+#include "libGLESv2/Error.h"
 
 #include <GLES2/gl2.h>
 
@@ -52,10 +53,10 @@
     explicit IndexDataManager(Renderer *renderer);
     virtual ~IndexDataManager();
 
-    GLenum prepareIndexData(GLenum type, GLsizei count, gl::Buffer *arrayElementBuffer, const GLvoid *indices, TranslatedIndexData *translated);
+    gl::Error prepareIndexData(GLenum type, GLsizei count, gl::Buffer *arrayElementBuffer, const GLvoid *indices, TranslatedIndexData *translated);
 
   private:
-    GLenum getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer);
+     gl::Error getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer);
 
     DISALLOW_COPY_AND_ASSIGN(IndexDataManager);
 
diff --git a/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.cpp
index f2dd505..9a61182 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.cpp
@@ -24,7 +24,7 @@
     SafeRelease(mBuffer);
 }
 
-bool IndexBuffer11::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic)
+gl::Error IndexBuffer11::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic)
 {
     SafeRelease(mBuffer);
 
@@ -45,7 +45,7 @@
         HRESULT result = dxDevice->CreateBuffer(&bufferDesc, NULL, &mBuffer);
         if (FAILED(result))
         {
-            return false;
+            return gl::Error(GL_OUT_OF_MEMORY, "Failed to allocate internal index buffer of size, %lu.", bufferSize);
         }
     }
 
@@ -53,7 +53,7 @@
     mIndexType = indexType;
     mDynamicUsage = dynamic;
 
-    return true;
+    return gl::Error(GL_NO_ERROR);
 }
 
 IndexBuffer11 *IndexBuffer11::makeIndexBuffer11(IndexBuffer *indexBuffer)
@@ -62,50 +62,42 @@
     return static_cast<IndexBuffer11*>(indexBuffer);
 }
 
-bool IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory)
+gl::Error IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory)
 {
-    if (mBuffer)
+    if (!mBuffer)
     {
-        // Check for integer overflows and out-out-bounds map requests
-        if (offset + size < offset || offset + size > mBufferSize)
-        {
-            ERR("Index buffer map range is not inside the buffer.");
-            return false;
-        }
-
-        ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
-
-        D3D11_MAPPED_SUBRESOURCE mappedResource;
-        HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource);
-        if (FAILED(result))
-        {
-            ERR("Index buffer map failed with error 0x%08x", result);
-            return false;
-        }
-
-        *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset;
-        return true;
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
     }
-    else
+
+    // Check for integer overflows and out-out-bounds map requests
+    if (offset + size < offset || offset + size > mBufferSize)
     {
-        ERR("Index buffer not initialized.");
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Index buffer map range is not inside the buffer.");
     }
+
+    ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
+
+    D3D11_MAPPED_SUBRESOURCE mappedResource;
+    HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource);
+    if (FAILED(result))
+    {
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to map internal index buffer, HRESULT: 0x%08x.", result);
+    }
+
+    *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset;
+    return gl::Error(GL_NO_ERROR);
 }
 
-bool IndexBuffer11::unmapBuffer()
+gl::Error IndexBuffer11::unmapBuffer()
 {
-    if (mBuffer)
+    if (!mBuffer)
     {
-        ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
-        dxContext->Unmap(mBuffer, 0);
-        return true;
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
     }
-    else
-    {
-        ERR("Index buffer not initialized.");
-        return false;
-    }
+
+    ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
+    dxContext->Unmap(mBuffer, 0);
+    return gl::Error(GL_NO_ERROR);
 }
 
 GLenum IndexBuffer11::getIndexType() const
@@ -118,7 +110,7 @@
     return mBufferSize;
 }
 
-bool IndexBuffer11::setSize(unsigned int bufferSize, GLenum indexType)
+gl::Error IndexBuffer11::setSize(unsigned int bufferSize, GLenum indexType)
 {
     if (bufferSize > mBufferSize || indexType != mIndexType)
     {
@@ -126,33 +118,29 @@
     }
     else
     {
-        return true;
+        return gl::Error(GL_NO_ERROR);
     }
 }
 
-bool IndexBuffer11::discard()
+gl::Error IndexBuffer11::discard()
 {
-    if (mBuffer)
+    if (!mBuffer)
     {
-        ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
-
-        D3D11_MAPPED_SUBRESOURCE mappedResource;
-        HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
-        if (FAILED(result))
-        {
-            ERR("Index buffer map failed with error 0x%08x", result);
-            return false;
-        }
-
-        dxContext->Unmap(mBuffer, 0);
-
-        return true;
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
     }
-    else
+
+    ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
+
+    D3D11_MAPPED_SUBRESOURCE mappedResource;
+    HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+    if (FAILED(result))
     {
-        ERR("Index buffer not initialized.");
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to map internal index buffer, HRESULT: 0x%08x.", result);
     }
+
+    dxContext->Unmap(mBuffer, 0);
+
+    return gl::Error(GL_NO_ERROR);
 }
 
 DXGI_FORMAT IndexBuffer11::getIndexFormat() const
@@ -171,4 +159,4 @@
     return mBuffer;
 }
 
-}
\ No newline at end of file
+}
diff --git a/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.h b/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.h
index e821b7f..f7c2b38 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.h
+++ b/src/libGLESv2/renderer/d3d/d3d11/IndexBuffer11.h
@@ -21,18 +21,18 @@
     explicit IndexBuffer11(Renderer11 *const renderer);
     virtual ~IndexBuffer11();
 
-    virtual bool initialize(unsigned int bufferSize, GLenum indexType, bool dynamic);
+    virtual gl::Error initialize(unsigned int bufferSize, GLenum indexType, bool dynamic);
 
     static IndexBuffer11 *makeIndexBuffer11(IndexBuffer *indexBuffer);
 
-    virtual bool mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory);
-    virtual bool unmapBuffer();
+    virtual gl::Error mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory);
+    virtual gl::Error unmapBuffer();
 
     virtual GLenum getIndexType() const;
     virtual unsigned int getBufferSize() const;
-    virtual bool setSize(unsigned int bufferSize, GLenum indexType);
+    virtual gl::Error setSize(unsigned int bufferSize, GLenum indexType);
 
-    virtual bool discard();
+    virtual gl::Error discard();
 
     DXGI_FORMAT getIndexFormat() const;
     ID3D11Buffer *getBuffer() const;
diff --git a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
index 9bb2c5c..de85071 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
@@ -941,37 +941,38 @@
     return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
 }
 
-GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
+gl::Error Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
 {
-    GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
-
-    if (err == GL_NO_ERROR)
+    gl::Error error = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
+    if (error.isError())
     {
-        ID3D11Buffer *buffer = NULL;
-        DXGI_FORMAT bufferFormat = (indexInfo->indexType == GL_UNSIGNED_INT) ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
-
-        if (indexInfo->storage)
-        {
-            Buffer11 *storage = Buffer11::makeBuffer11(indexInfo->storage);
-            buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
-        }
-        else
-        {
-            IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
-            buffer = indexBuffer->getBuffer();
-        }
-
-        if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
-        {
-            mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
-
-            mAppliedIB = buffer;
-            mAppliedIBFormat = bufferFormat;
-            mAppliedIBOffset = indexInfo->startOffset;
-        }
+        return error;
     }
 
-    return err;
+    ID3D11Buffer *buffer = NULL;
+    DXGI_FORMAT bufferFormat = (indexInfo->indexType == GL_UNSIGNED_INT) ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
+
+    if (indexInfo->storage)
+    {
+        Buffer11 *storage = Buffer11::makeBuffer11(indexInfo->storage);
+        buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
+    }
+    else
+    {
+        IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
+        buffer = indexBuffer->getBuffer();
+    }
+
+    if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
+    {
+        mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
+
+        mAppliedIB = buffer;
+        mAppliedIBFormat = bufferFormat;
+        mAppliedIBOffset = indexInfo->startOffset;
+    }
+
+    return gl::Error(GL_NO_ERROR);
 }
 
 void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
@@ -1101,10 +1102,10 @@
     if (!mLineLoopIB)
     {
         mLineLoopIB = new StreamingIndexBufferInterface(this);
-        if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
+        gl::Error error = mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
+        if (error.isError())
         {
-            delete mLineLoopIB;
-            mLineLoopIB = NULL;
+            SafeDelete(mLineLoopIB);
 
             ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1121,7 +1122,8 @@
     }
 
     const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
-    if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
+    gl::Error error = mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT);
+    if (error.isError())
     {
         ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
         return gl::error(GL_OUT_OF_MEMORY);
@@ -1129,7 +1131,8 @@
 
     void* mappedMemory = NULL;
     unsigned int offset;
-    if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
+    error = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset);
+    if (error.isError())
     {
         ERR("Could not map index buffer for GL_LINE_LOOP.");
         return gl::error(GL_OUT_OF_MEMORY);
@@ -1171,7 +1174,8 @@
       default: UNREACHABLE();
     }
 
-    if (!mLineLoopIB->unmapBuffer())
+    error = mLineLoopIB->unmapBuffer();
+    if (error.isError())
     {
         ERR("Could not unmap index buffer for GL_LINE_LOOP.");
         return gl::error(GL_OUT_OF_MEMORY);
@@ -1206,10 +1210,10 @@
     if (!mTriangleFanIB)
     {
         mTriangleFanIB = new StreamingIndexBufferInterface(this);
-        if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
+        gl::Error error = mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
+        if (error.isError())
         {
-            delete mTriangleFanIB;
-            mTriangleFanIB = NULL;
+            SafeDelete(mTriangleFanIB);
 
             ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1228,7 +1232,8 @@
     }
 
     const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
-    if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
+    gl::Error error = mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT);
+    if (error.isError())
     {
         ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
         return gl::error(GL_OUT_OF_MEMORY);
@@ -1236,7 +1241,8 @@
 
     void* mappedMemory = NULL;
     unsigned int offset;
-    if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
+    error = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset);
+    if (error.isError())
     {
         ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
         return gl::error(GL_OUT_OF_MEMORY);
@@ -1282,7 +1288,8 @@
       default: UNREACHABLE();
     }
 
-    if (!mTriangleFanIB->unmapBuffer())
+    error = mTriangleFanIB->unmapBuffer();
+    if (error.isError())
     {
         ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
         return gl::error(GL_OUT_OF_MEMORY);
diff --git a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.h b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.h
index 64de7b0..ae6d284 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.h
+++ b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.h
@@ -82,7 +82,7 @@
     virtual void applyUniforms(const gl::ProgramBinary &programBinary);
     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
                                      GLint first, GLsizei count, GLsizei instances);
-    virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
+    virtual gl::Error applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
     virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
 
     virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive);
diff --git a/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.cpp b/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.cpp
index 451ede4..1c51b9e 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.cpp
@@ -25,7 +25,7 @@
     SafeRelease(mIndexBuffer);
 }
 
-bool IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic)
+gl::Error IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic)
 {
     SafeRelease(mIndexBuffer);
 
@@ -33,28 +33,17 @@
 
     if (bufferSize > 0)
     {
-        D3DFORMAT format;
+        D3DFORMAT format = D3DFMT_UNKNOWN;
         if (indexType == GL_UNSIGNED_SHORT || indexType == GL_UNSIGNED_BYTE)
         {
             format = D3DFMT_INDEX16;
         }
         else if (indexType == GL_UNSIGNED_INT)
         {
-            if (mRenderer->getRendererExtensions().elementIndexUint)
-            {
-                format = D3DFMT_INDEX32;
-            }
-            else
-            {
-                ERR("Attempted to create a 32-bit index buffer but renderer does not support 32-bit indices.");
-                return false;
-            }
+            ASSERT(mRenderer->getRendererExtensions().elementIndexUint);
+            format = D3DFMT_INDEX32;
         }
-        else
-        {
-            ERR("Invalid index type %u.", indexType);
-            return false;
-        }
+        else UNREACHABLE();
 
         DWORD usageFlags = D3DUSAGE_WRITEONLY;
         if (dynamic)
@@ -65,8 +54,7 @@
         HRESULT result = mRenderer->createIndexBuffer(bufferSize, usageFlags, format, &mIndexBuffer);
         if (FAILED(result))
         {
-            ERR("Failed to create an index buffer of size %u, result: 0x%08x.", mBufferSize, result);
-            return false;
+            return gl::Error(GL_OUT_OF_MEMORY, "Failed to allocate internal index buffer of size, %lu.", bufferSize);
         }
     }
 
@@ -74,7 +62,7 @@
     mIndexType = indexType;
     mDynamic = dynamic;
 
-    return true;
+    return gl::Error(GL_NO_ERROR);
 }
 
 IndexBuffer9 *IndexBuffer9::makeIndexBuffer9(IndexBuffer *indexBuffer)
@@ -83,48 +71,40 @@
     return static_cast<IndexBuffer9*>(indexBuffer);
 }
 
-bool IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory)
+gl::Error IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory)
 {
-    if (mIndexBuffer)
+    if (!mIndexBuffer)
     {
-        DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0;
-
-        void *mapPtr = NULL;
-        HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags);
-        if (FAILED(result))
-        {
-            ERR("Index buffer lock failed with error 0x%08x", result);
-            return false;
-        }
-
-        *outMappedMemory = mapPtr;
-        return true;
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
     }
-    else
+
+    DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0;
+
+    void *mapPtr = NULL;
+    HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags);
+    if (FAILED(result))
     {
-        ERR("Index buffer not initialized.");
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock internal index buffer, HRESULT: 0x%08x.", result);
     }
+
+    *outMappedMemory = mapPtr;
+    return gl::Error(GL_NO_ERROR);
 }
 
-bool IndexBuffer9::unmapBuffer()
+gl::Error IndexBuffer9::unmapBuffer()
 {
-    if (mIndexBuffer)
+    if (!mIndexBuffer)
     {
-        HRESULT result = mIndexBuffer->Unlock();
-        if (FAILED(result))
-        {
-            ERR("Index buffer unlock failed with error 0x%08x", result);
-            return false;
-        }
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
+    }
 
-        return true;
-    }
-    else
+    HRESULT result = mIndexBuffer->Unlock();
+    if (FAILED(result))
     {
-        ERR("Index buffer not initialized.");
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to unlock internal index buffer, HRESULT: 0x%08x.", result);
     }
+
+    return gl::Error(GL_NO_ERROR);
 }
 
 GLenum IndexBuffer9::getIndexType() const
@@ -137,7 +117,7 @@
     return mBufferSize;
 }
 
-bool IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType)
+gl::Error IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType)
 {
     if (bufferSize > mBufferSize || indexType != mIndexType)
     {
@@ -145,38 +125,33 @@
     }
     else
     {
-        return true;
+        return gl::Error(GL_NO_ERROR);
     }
 }
 
-bool IndexBuffer9::discard()
+gl::Error IndexBuffer9::discard()
 {
-    if (mIndexBuffer)
+    if (!mIndexBuffer)
     {
-        void *dummy;
-        HRESULT result;
-
-        result = mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
-        if (FAILED(result))
-        {
-            ERR("Discard lock failed with error 0x%08x", result);
-            return false;
-        }
-
-        result = mIndexBuffer->Unlock();
-        if (FAILED(result))
-        {
-            ERR("Discard unlock failed with error 0x%08x", result);
-            return false;
-        }
-
-        return true;
+        return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized.");
     }
-    else
+
+    void *dummy;
+    HRESULT result;
+
+    result = mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
+    if (FAILED(result))
     {
-        ERR("Index buffer not initialized.");
-        return false;
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock internal index buffer, HRESULT: 0x%08x.", result);
     }
+
+    result = mIndexBuffer->Unlock();
+    if (FAILED(result))
+    {
+        return gl::Error(GL_OUT_OF_MEMORY, "Failed to unlock internal index buffer, HRESULT: 0x%08x.", result);
+    }
+
+    return gl::Error(GL_NO_ERROR);
 }
 
 D3DFORMAT IndexBuffer9::getIndexFormat() const
@@ -195,4 +170,4 @@
     return mIndexBuffer;
 }
 
-}
\ No newline at end of file
+}
diff --git a/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.h b/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.h
index cfc20e1..d0970d6 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.h
+++ b/src/libGLESv2/renderer/d3d/d3d9/IndexBuffer9.h
@@ -21,18 +21,18 @@
     explicit IndexBuffer9(Renderer9 *const renderer);
     virtual ~IndexBuffer9();
 
-    virtual bool initialize(unsigned int bufferSize, GLenum indexType, bool dynamic);
+    virtual gl::Error initialize(unsigned int bufferSize, GLenum indexType, bool dynamic);
 
     static IndexBuffer9 *makeIndexBuffer9(IndexBuffer *indexBuffer);
 
-    virtual bool mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory);
-    virtual bool unmapBuffer();
+    virtual gl::Error mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory);
+    virtual gl::Error unmapBuffer();
 
     virtual GLenum getIndexType() const;
     virtual unsigned int getBufferSize() const;
-    virtual bool setSize(unsigned int bufferSize, GLenum indexType);
+    virtual gl::Error setSize(unsigned int bufferSize, GLenum indexType);
 
-    virtual bool discard();
+    virtual gl::Error discard();
 
     D3DFORMAT getIndexFormat() const;
     IDirect3DIndexBuffer9 *getBuffer() const;
diff --git a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
index c267fca..c791342 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
@@ -1269,25 +1269,26 @@
 }
 
 // Applies the indices and element array bindings to the Direct3D 9 device
-GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
+gl::Error Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
 {
-    GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
-
-    if (err == GL_NO_ERROR)
+    gl::Error error = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
+    if (error.isError())
     {
-        // Directly binding the storage buffer is not supported for d3d9
-        ASSERT(indexInfo->storage == NULL);
-
-        if (indexInfo->serial != mAppliedIBSerial)
-        {
-            IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
-
-            mDevice->SetIndices(indexBuffer->getBuffer());
-            mAppliedIBSerial = indexInfo->serial;
-        }
+        return error;
     }
 
-    return err;
+    // Directly binding the storage buffer is not supported for d3d9
+    ASSERT(indexInfo->storage == NULL);
+
+    if (indexInfo->serial != mAppliedIBSerial)
+    {
+        IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
+
+        mDevice->SetIndices(indexBuffer->getBuffer());
+        mAppliedIBSerial = indexInfo->serial;
+    }
+
+    return gl::Error(GL_NO_ERROR);
 }
 
 void Renderer9::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
@@ -1375,10 +1376,10 @@
         if (!mLineLoopIB)
         {
             mLineLoopIB = new StreamingIndexBufferInterface(this);
-            if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
+            gl::Error error = mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
+            if (error.isError())
             {
-                delete mLineLoopIB;
-                mLineLoopIB = NULL;
+                SafeDelete(mLineLoopIB);
 
                 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
                 return gl::error(GL_OUT_OF_MEMORY);
@@ -1394,8 +1395,9 @@
             return gl::error(GL_OUT_OF_MEMORY);
         }
 
-        const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
-        if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
+        const unsigned int spaceNeeded = (static_cast<unsigned int>(count)+1) * sizeof(unsigned int);
+        gl::Error error = mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT);
+        if (error.isError())
         {
             ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1403,7 +1405,8 @@
 
         void* mappedMemory = NULL;
         unsigned int offset = 0;
-        if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
+        error = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset);
+        if (error.isError())
         {
             ERR("Could not map index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1445,7 +1448,8 @@
           default: UNREACHABLE();
         }
 
-        if (!mLineLoopIB->unmapBuffer())
+        error = mLineLoopIB->unmapBuffer();
+        if (error.isError())
         {
             ERR("Could not unmap index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1456,10 +1460,10 @@
         if (!mLineLoopIB)
         {
             mLineLoopIB = new StreamingIndexBufferInterface(this);
-            if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
+            gl::Error error = mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT);
+            if (error.isError())
             {
-                delete mLineLoopIB;
-                mLineLoopIB = NULL;
+                SafeDelete(mLineLoopIB);
 
                 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
                 return gl::error(GL_OUT_OF_MEMORY);
@@ -1476,7 +1480,8 @@
         }
 
         const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
-        if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
+        gl::Error error = mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT);
+        if (error.isError())
         {
             ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1484,7 +1489,8 @@
 
         void* mappedMemory = NULL;
         unsigned int offset;
-        if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
+        error = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset);
+        if (error.isError())
         {
             ERR("Could not map index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1526,7 +1532,8 @@
           default: UNREACHABLE();
         }
 
-        if (!mLineLoopIB->unmapBuffer())
+        error = mLineLoopIB->unmapBuffer();
+        if (error.isError())
         {
             ERR("Could not unmap index buffer for GL_LINE_LOOP.");
             return gl::error(GL_OUT_OF_MEMORY);
@@ -1589,7 +1596,8 @@
             mCountingIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT);
 
             void *mappedMemory = NULL;
-            if (!mCountingIB->mapBuffer(spaceNeeded, &mappedMemory, NULL))
+            gl::Error error = mCountingIB->mapBuffer(spaceNeeded, &mappedMemory, NULL);
+            if (error.isError())
             {
                 ERR("Failed to map counting buffer.");
                 return NULL;
@@ -1601,7 +1609,8 @@
                 data[i] = i;
             }
 
-            if (!mCountingIB->unmapBuffer())
+            error = mCountingIB->unmapBuffer();
+            if (error.isError())
             {
                 ERR("Failed to unmap counting buffer.");
                 return NULL;
@@ -1621,7 +1630,8 @@
             mCountingIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT);
 
             void *mappedMemory = NULL;
-            if (!mCountingIB->mapBuffer(spaceNeeded, &mappedMemory, NULL))
+            gl::Error error = mCountingIB->mapBuffer(spaceNeeded, &mappedMemory, NULL);
+            if (error.isError())
             {
                 ERR("Failed to map counting buffer.");
                 return NULL;
@@ -1633,7 +1643,8 @@
                 data[i] = i;
             }
 
-            if (!mCountingIB->unmapBuffer())
+            error = mCountingIB->unmapBuffer();
+            if (error.isError())
             {
                 ERR("Failed to unmap counting buffer.");
                 return NULL;
diff --git a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.h b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.h
index d6a41b6..6dfad26 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.h
+++ b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.h
@@ -83,7 +83,7 @@
     virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount);
     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
                                      GLint first, GLsizei count, GLsizei instances);
-    virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
+    virtual gl::Error applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
 
     virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);