Commit c9e69b19 by Geoff Lang

Updated the index buffer classes to use Error objects.

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