GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *buffer, const void *indices, TranslatedIndexData *translated)
{
	if(!mStreamingBuffer)
	{
		return GL_OUT_OF_MEMORY;
	}

	intptr_t offset = reinterpret_cast<intptr_t>(indices);

	if(buffer != NULL)
	{
		if(typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size()))
		{
			return GL_INVALID_OPERATION;
		}

		indices = static_cast<const GLubyte*>(buffer->data()) + offset;
	}

	StreamingIndexBuffer *streamingBuffer = mStreamingBuffer;

	sw::Resource *staticBuffer = buffer ? buffer->getResource() : NULL;

	if(staticBuffer)
	{
		computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);

		translated->indexBuffer = staticBuffer;
		translated->indexOffset = offset;
	}
	else
	{
		unsigned int streamOffset = 0;
		int convertCount = count;

		streamingBuffer->reserveSpace(convertCount * typeSize(type), type);
		void *output = streamingBuffer->map(typeSize(type) * convertCount, &streamOffset);

		if(output == NULL)
		{
			ERR("Failed to map index buffer.");
			return GL_OUT_OF_MEMORY;
		}

		copyIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output);
		streamingBuffer->unmap();

		computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);

		translated->indexBuffer = streamingBuffer->getResource();
		translated->indexOffset = streamOffset;
	}

	return GL_NO_ERROR;
}
void computeRange(GLenum type, const void *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex)
{
	if(type == GL_UNSIGNED_BYTE)
	{
		computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex);
	}
	else if(type == GL_UNSIGNED_SHORT)
	{
		computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex);
	}
	else UNREACHABLE(type);
}
示例#3
0
void Light::writeBinary(FILE* file)
{
    Object::writeBinary(file);
    write(_lightType, file);
    write(_color, COLOR_SIZE, file);

    // Compute an approximate light range with Collada's attenuation parameters.
    // This facilitates bringing in the light nodes directly from maya to gameplay.
    if (_range == -1.0f)
    {
        _range = computeRange(_constantAttenuation, _linearAttenuation, _quadraticAttenuation);
    }

    if (_lightType == SpotLight)
    {
        // Compute an approximate inner angle of the spot light using Collada's outer angle.
        _outerAngle = _falloffAngle / 2.0f;
        
        if (_innerAngle == -1.0f)
        {
            _innerAngle = computeInnerAngle(_outerAngle);
        }

        write(_range, file);
        write(MATH_DEG_TO_RAD(_innerAngle), file);
        write(MATH_DEG_TO_RAD(_outerAngle), file);
    }
    else if (_lightType == PointLight)
    {
        write(_range, file);
    }
}
示例#4
0
void Light::writeText(FILE* file)
{
    fprintElementStart(file);
    fprintfElement(file, "lightType", _lightType);
    fprintfElement(file, "color", _color, COLOR_SIZE);

    // Compute an approximate light range with Collada's attenuation parameters.
    // This facilitates bringing in the light nodes directly from maya to gameplay.
    if (_range == -1.0f)
    {
        _range = computeRange(_constantAttenuation, _linearAttenuation, _quadraticAttenuation);
    }

    if (_lightType == SpotLight)
    {
        // Compute an approximate inner angle of the spot light using Collada's outer angle.
        _outerAngle = _falloffAngle / 2.0f;
        if (_innerAngle == -1.0f)
        {
            _innerAngle = computeInnerAngle(_outerAngle);
        }

        fprintfElement(file, "range", _range);
        fprintfElement(file, "innerAngle", MATH_DEG_TO_RAD(_innerAngle));
        fprintfElement(file, "outerAngle", MATH_DEG_TO_RAD(_outerAngle));
    }
    else if (_lightType == PointLight)
    {
        fprintfElement(file, "range", _range);
    }

    fprintElementEnd(file);
}
示例#5
0
    Pimpl(const vpz::Vpz& vpz, uint32_t rank, uint32_t size)
        : mVpz(vpz), mRank(rank), mWorld(size), mCompleteSize(0), mMin(0),
        mMax(0)
    {
        if (rank >= size) {
            throw utils::InternalError(_("Bad rank"));
        }

        computeRange();
    }
示例#6
0
    Pimpl(const std::string& filename, uint32_t rank, uint32_t size)
        : mVpz(filename), mRank(rank), mWorld(size), mCompleteSize(0), mMin(0),
        mMax(0)
    {
        if (rank >= size) {
            throw utils::InternalError(_("Bad rank"));
        }

        computeRange();
    }
 /**
  * @param A an integer array
  * @return  A list of integers includes the index of 
  *          the first number and the index of the last number
  */
 vector<int> continuousSubarraySumII(vector<int>& A) {
     // Write your code here
     
     int sum = 0;
     for (auto& a : A) {
         sum += a;
     }
     vector<int> presum(A.size() + 1, 0);
     presum[0] = 0;
     for (int i = 1; i <= A.size(); i++) {
         if (i == 1) {
             presum[i] = A[i - 1];
         } else {
             presum[i] = presum[i - 1] + A[i - 1];
         }
     }
     
     // get largest subarray
     vector<int> ans;
     int smallest_index = 0;
     int biggest = INT_MIN;
     for (int i = 1; i < presum.size(); i++) {
         if (presum[i] - presum[smallest_index] > biggest) {
             biggest = presum[i] - presum[smallest_index];
             ans.clear();
             ans.push_back(smallest_index);
             ans.push_back(i - 1);
         }
         if (presum[smallest_index] > presum[i]) {
             smallest_index = i;
         }
     }
     
     
     // get smallest subarray
     vector<int> ans2;
     int largest_index = 0;
     int smallest = INT_MAX;
     for (int i = 1; i < presum.size(); i++) {
         if (presum[i] - presum[largest_index] < smallest) {
             smallest = presum[i] - presum[largest_index];
             ans2 = computeRange(largest_index, i - 1, A.size());
         }
         if (presum[largest_index] < presum[i]) {
             largest_index = i;
         }
     }
     
     if (sum == smallest) { // needs better explain
         return ans;
     } else {
         return sum - smallest > biggest ? ans2 : ans;
     }
 }
示例#8
0
GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
{
    if (!mStreamingBufferShort)
    {
        return GL_OUT_OF_MEMORY;
    }

    D3DFORMAT format = (type == GL_UNSIGNED_INT) ? D3DFMT_INDEX32 : D3DFMT_INDEX16;
    intptr_t offset = reinterpret_cast<intptr_t>(indices);
    bool alignedOffset = false;

    if (buffer != NULL)
    {
        switch (type)
        {
          case GL_UNSIGNED_BYTE:  alignedOffset = (offset % sizeof(GLubyte) == 0);  break;
          case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
          case GL_UNSIGNED_INT:   alignedOffset = (offset % sizeof(GLuint) == 0);   break;
          default: UNREACHABLE(); alignedOffset = false;
        }

        if (typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size()))
        {
            return GL_INVALID_OPERATION;
        }

        indices = static_cast<const GLubyte*>(buffer->data()) + offset;
    }

    StreamingIndexBuffer *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort;

    StaticIndexBuffer *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL;
    IndexBuffer *indexBuffer = streamingBuffer;
    UINT streamOffset = 0;

    if (staticBuffer && staticBuffer->lookupType(type) && alignedOffset)
    {
        indexBuffer = staticBuffer;
        streamOffset = staticBuffer->lookupRange(offset, count, &translated->minIndex, &translated->maxIndex);

        if (streamOffset == -1)
        {
            streamOffset = (offset / typeSize(type)) * indexSize(format);
            computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
            staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset);
        }
    }
    else
    {
        int convertCount = count;

        if (staticBuffer)
        {
            if (staticBuffer->size() == 0 && alignedOffset)
            {
                indexBuffer = staticBuffer;
                convertCount = buffer->size() / typeSize(type);
            }
            else
            {
                buffer->invalidateStaticData();
                staticBuffer = NULL;
            }
        }

        void *output = NULL;
        
        if (indexBuffer)
        {
            indexBuffer->reserveSpace(convertCount * indexSize(format), type);
            output = indexBuffer->map(indexSize(format) * convertCount, &streamOffset);
        }
        
        if (output == NULL)
        {
            ERR("Failed to map index buffer.");
            return GL_OUT_OF_MEMORY;
        }

        convertIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output);
        indexBuffer->unmap();

        computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);

        if (staticBuffer)
        {
            streamOffset = (offset / typeSize(type)) * indexSize(format);
            staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset);
        }
    }

    translated->indexBuffer = indexBuffer->getBuffer();
    translated->serial = indexBuffer->getSerial();
    translated->startIndex = streamOffset / indexSize(format);

    if (buffer)
    {
        buffer->promoteStaticUsage(count * typeSize(type));
    }

    return GL_NO_ERROR;
}
示例#9
0
GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
{
    if (!mStreamingBufferShort)
    {
        return GL_OUT_OF_MEMORY;
    }

    GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
    unsigned int offset = 0;
    bool alignedOffset = false;

    BufferD3D *storage = 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));

        storage = BufferD3D::makeBufferD3D(buffer->getImplementation());

        switch (type)
        {
          case GL_UNSIGNED_BYTE:  alignedOffset = (offset % sizeof(GLubyte) == 0);  break;
          case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
          case GL_UNSIGNED_INT:   alignedOffset = (offset % sizeof(GLuint) == 0);   break;
          default: UNREACHABLE(); alignedOffset = false;
        }

        unsigned int typeSize = gl::GetTypeBytes(type);

        // check for integer overflows
        if (static_cast<unsigned int>(count) > (std::numeric_limits<unsigned int>::max() / typeSize) ||
            typeSize * static_cast<unsigned int>(count) + offset < offset)
        {
            return GL_OUT_OF_MEMORY;
        }

        if (typeSize * static_cast<unsigned int>(count) + offset > storage->getSize())
        {
            return GL_INVALID_OPERATION;
        }

        indices = static_cast<const GLubyte*>(storage->getData()) + offset;
    }

    StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort;

    StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL;
    IndexBufferInterface *indexBuffer = streamingBuffer;
    bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() &&
                         destinationIndexType == type;
    unsigned int streamOffset = 0;

    if (directStorage)
    {
        indexBuffer = streamingBuffer;
        streamOffset = offset;

        if (!storage->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex,
                                                     &translated->maxIndex, NULL))
        {
            computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
            storage->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
                                                   translated->maxIndex, offset);
        }
    }
    else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset)
    {
        indexBuffer = staticBuffer;

        if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex,
                                                           &translated->maxIndex, &streamOffset))
        {
            streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType);
            computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
            staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
                                                         translated->maxIndex, streamOffset);
        }
    }
    else
    {
        unsigned int convertCount = count;

        if (staticBuffer)
        {
            if (staticBuffer->getBufferSize() == 0 && alignedOffset)
            {
                indexBuffer = staticBuffer;
                convertCount = storage->getSize() / gl::GetTypeBytes(type);
            }
            else
            {
                storage->invalidateStaticData();
                staticBuffer = NULL;
            }
        }

        if (!indexBuffer)
        {
            ERR("No valid index buffer.");
            return GL_INVALID_OPERATION;
        }

        unsigned int indexTypeSize = gl::GetTypeBytes(destinationIndexType);
        if (convertCount > std::numeric_limits<unsigned int>::max() / indexTypeSize)
        {
            ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, indexTypeSize);
            return GL_OUT_OF_MEMORY;
        }

        unsigned int bufferSizeRequired = convertCount * indexTypeSize;
        if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type))
        {
            ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired);
            return GL_OUT_OF_MEMORY;
        }

        void* output = NULL;
        if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset))
        {
            ERR("Failed to map index buffer.");
            return GL_OUT_OF_MEMORY;
        }

        convertIndices(type, staticBuffer ? storage->getData() : indices, convertCount, output);

        if (!indexBuffer->unmapBuffer())
        {
            ERR("Failed to unmap index buffer.");
            return GL_OUT_OF_MEMORY;
        }

        computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);

        if (staticBuffer)
        {
            streamOffset = (offset / gl::GetTypeBytes(type)) * gl::GetTypeBytes(destinationIndexType);
            staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
                                                         translated->maxIndex, streamOffset);
        }
    }

    translated->storage = directStorage ? storage : NULL;
    translated->indexBuffer = indexBuffer->getIndexBuffer();
    translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial();
    translated->startIndex = streamOffset / gl::GetTypeBytes(destinationIndexType);
    translated->startOffset = streamOffset;

    if (storage)
    {
        storage->promoteStaticUsage(count * gl::GetTypeBytes(type));
    }

    return GL_NO_ERROR;
}