michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // VertexDataManager.h: Defines the VertexDataManager, a class that michael@0: // runs the Buffer translation process. michael@0: michael@0: #include "libGLESv2/renderer/VertexDataManager.h" michael@0: #include "libGLESv2/renderer/BufferStorage.h" michael@0: michael@0: #include "libGLESv2/Buffer.h" michael@0: #include "libGLESv2/ProgramBinary.h" michael@0: #include "libGLESv2/Context.h" michael@0: #include "libGLESv2/renderer/VertexBuffer.h" michael@0: michael@0: namespace michael@0: { michael@0: enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; michael@0: // This has to be at least 4k or else it fails on ATI cards. michael@0: enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 }; michael@0: } michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: static int elementsInBuffer(const gl::VertexAttribute &attribute, unsigned int size) michael@0: { michael@0: // Size cannot be larger than a GLsizei michael@0: if (size > static_cast(std::numeric_limits::max())) michael@0: { michael@0: size = static_cast(std::numeric_limits::max()); michael@0: } michael@0: michael@0: GLsizei stride = attribute.stride(); michael@0: return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride; michael@0: } michael@0: michael@0: VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer) michael@0: { michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mCurrentValue[i][0] = std::numeric_limits::quiet_NaN(); michael@0: mCurrentValue[i][1] = std::numeric_limits::quiet_NaN(); michael@0: mCurrentValue[i][2] = std::numeric_limits::quiet_NaN(); michael@0: mCurrentValue[i][3] = std::numeric_limits::quiet_NaN(); michael@0: mCurrentValueBuffer[i] = NULL; michael@0: mCurrentValueOffsets[i] = 0; michael@0: } michael@0: michael@0: mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); michael@0: michael@0: if (!mStreamingBuffer) michael@0: { michael@0: ERR("Failed to allocate the streaming vertex buffer."); michael@0: } michael@0: } michael@0: michael@0: VertexDataManager::~VertexDataManager() michael@0: { michael@0: delete mStreamingBuffer; michael@0: michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: delete mCurrentValueBuffer[i]; michael@0: } michael@0: } michael@0: michael@0: static bool directStoragePossible(VertexBufferInterface* vb, const gl::VertexAttribute& attrib) michael@0: { michael@0: gl::Buffer *buffer = attrib.mBoundBuffer.get(); michael@0: BufferStorage *storage = buffer ? buffer->getStorage() : NULL; michael@0: michael@0: const bool isAligned = (attrib.stride() % 4 == 0) && (attrib.mOffset % 4 == 0); michael@0: michael@0: return storage && storage->supportsDirectBinding() && !vb->getVertexBuffer()->requiresConversion(attrib) && isAligned; michael@0: } michael@0: michael@0: GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances) michael@0: { michael@0: if (!mStreamingBuffer) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) michael@0: { michael@0: translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1); michael@0: } michael@0: michael@0: // Invalidate static buffers that don't contain matching attributes michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (translated[i].active && attribs[i].mArrayEnabled) michael@0: { michael@0: gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); michael@0: StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; michael@0: michael@0: if (staticBuffer && staticBuffer->getBufferSize() > 0 && !staticBuffer->lookupAttribute(attribs[i], NULL) && michael@0: !directStoragePossible(staticBuffer, attribs[i])) michael@0: { michael@0: buffer->invalidateStaticData(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Reserve the required space in the buffers michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (translated[i].active && attribs[i].mArrayEnabled) michael@0: { michael@0: gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); michael@0: StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; michael@0: VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast(mStreamingBuffer); michael@0: michael@0: if (!directStoragePossible(vertexBuffer, attribs[i])) michael@0: { michael@0: if (staticBuffer) michael@0: { michael@0: if (staticBuffer->getBufferSize() == 0) michael@0: { michael@0: int totalCount = elementsInBuffer(attribs[i], buffer->size()); michael@0: if (!staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (!mStreamingBuffer->reserveVertexSpace(attribs[i], count, instances)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Perform the vertex data translations michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (translated[i].active) michael@0: { michael@0: if (attribs[i].mArrayEnabled) michael@0: { michael@0: gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); michael@0: michael@0: if (!buffer && attribs[i].mPointer == NULL) michael@0: { michael@0: // This is an application error that would normally result in a crash, but we catch it and return an error michael@0: ERR("An enabled vertex array has no buffer and no pointer."); michael@0: return GL_INVALID_OPERATION; michael@0: } michael@0: michael@0: StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; michael@0: VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast(mStreamingBuffer); michael@0: michael@0: BufferStorage *storage = buffer ? buffer->getStorage() : NULL; michael@0: bool directStorage = directStoragePossible(vertexBuffer, attribs[i]); michael@0: michael@0: unsigned int streamOffset = 0; michael@0: unsigned int outputElementSize = 0; michael@0: michael@0: if (directStorage) michael@0: { michael@0: outputElementSize = attribs[i].stride(); michael@0: streamOffset = attribs[i].mOffset + outputElementSize * start; michael@0: storage->markBufferUsage(); michael@0: } michael@0: else if (staticBuffer) michael@0: { michael@0: if (!staticBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0, &outputElementSize)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (!staticBuffer->lookupAttribute(attribs[i], &streamOffset)) michael@0: { michael@0: // Convert the entire buffer michael@0: int totalCount = elementsInBuffer(attribs[i], storage->getSize()); michael@0: int startIndex = attribs[i].mOffset / attribs[i].stride(); michael@0: michael@0: if (!staticBuffer->storeVertexAttributes(attribs[i], -startIndex, totalCount, 0, &streamOffset)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: michael@0: unsigned int firstElementOffset = (attribs[i].mOffset / attribs[i].stride()) * outputElementSize; michael@0: unsigned int startOffset = (instances == 0 || attribs[i].mDivisor == 0) ? start * outputElementSize : 0; michael@0: if (streamOffset + firstElementOffset + startOffset < streamOffset) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: streamOffset += firstElementOffset + startOffset; michael@0: } michael@0: else michael@0: { michael@0: if (!mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0, &outputElementSize) || michael@0: !mStreamingBuffer->storeVertexAttributes(attribs[i], start, count, instances, &streamOffset)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: michael@0: translated[i].storage = directStorage ? storage : NULL; michael@0: translated[i].vertexBuffer = vertexBuffer->getVertexBuffer(); michael@0: translated[i].serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial(); michael@0: translated[i].divisor = attribs[i].mDivisor; michael@0: michael@0: translated[i].attribute = &attribs[i]; michael@0: translated[i].stride = outputElementSize; michael@0: translated[i].offset = streamOffset; michael@0: } michael@0: else michael@0: { michael@0: if (!mCurrentValueBuffer[i]) michael@0: { michael@0: mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE); michael@0: } michael@0: michael@0: StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i]; michael@0: michael@0: if (mCurrentValue[i][0] != attribs[i].mCurrentValue[0] || michael@0: mCurrentValue[i][1] != attribs[i].mCurrentValue[1] || michael@0: mCurrentValue[i][2] != attribs[i].mCurrentValue[2] || michael@0: mCurrentValue[i][3] != attribs[i].mCurrentValue[3]) michael@0: { michael@0: unsigned int requiredSpace = sizeof(float) * 4; michael@0: if (!buffer->reserveRawDataSpace(requiredSpace)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: unsigned int streamOffset; michael@0: if (!buffer->storeRawData(attribs[i].mCurrentValue, requiredSpace, &streamOffset)) michael@0: { michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: mCurrentValueOffsets[i] = streamOffset; michael@0: } michael@0: michael@0: translated[i].storage = NULL; michael@0: translated[i].vertexBuffer = mCurrentValueBuffer[i]->getVertexBuffer(); michael@0: translated[i].serial = mCurrentValueBuffer[i]->getSerial(); michael@0: translated[i].divisor = 0; michael@0: michael@0: translated[i].attribute = &attribs[i]; michael@0: translated[i].stride = 0; michael@0: translated[i].offset = mCurrentValueOffsets[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (translated[i].active && attribs[i].mArrayEnabled) michael@0: { michael@0: gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); michael@0: michael@0: if (buffer) michael@0: { michael@0: buffer->promoteStaticUsage(count * attribs[i].typeSize()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return GL_NO_ERROR; michael@0: } michael@0: michael@0: }