void LLVertexBuffer::allocateBuffer(S32 nverts, S32 nindices, bool create)
{
	LLMemType mt(LLMemType::MTYPE_VERTEX_DATA);
		
	if (nverts < 0 || nindices < 0 ||
		nverts > 65535)
	{
		llerrs << "Bad vertex buffer allocation: " << nverts << " : " << nindices << llendl;
	}

	updateNumVerts(nverts);
	updateNumIndices(nindices);
	
	if (mMappedData)
	{
		llerrs << "LLVertexBuffer::allocateBuffer() called redundantly." << llendl;
	}
	if (create && (nverts || nindices))
	{
		createGLBuffer();
		createGLIndices();
	}
	
	sAllocatedBytes += getSize() + getIndicesSize();
}
void LLVertexBuffer::allocateBuffer(S32 nverts, S32 nindices, bool create)
{
	LLMemType mt2(LLMemType::MTYPE_VERTEX_ALLOCATE_BUFFER);
		
	updateNumVerts(nverts);
	updateNumIndices(nindices);
	
	if (mMappedData)
	{
		llerrs << "LLVertexBuffer::allocateBuffer() called redundantly." << llendl;
	}
	if (create && (nverts || nindices))
	{
		createGLBuffer();
		createGLIndices();
	}
	
	sAllocatedBytes += getSize() + getIndicesSize();
}
Exemple #3
0
void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
{
	LLMemType mt(LLMemType::MTYPE_VERTEX_DATA);
	mDynamicSize = TRUE;
	if (mUsage == GL_STATIC_DRAW_ARB)
	{ //always delete/allocate static buffers on resize
		destroyGLBuffer();
		destroyGLIndices();
		allocateBuffer(newnverts, newnindices, TRUE);
		mFinal = FALSE;
	}
	else if (newnverts > mNumVerts || newnindices > mNumIndices ||
			 newnverts < mNumVerts/2 || newnindices < mNumIndices/2)
	{
		sAllocatedBytes -= getSize() + getIndicesSize();
		
		S32 oldsize = getSize();
		S32 old_index_size = getIndicesSize();

		updateNumVerts(newnverts);		
		updateNumIndices(newnindices);
		
		S32 newsize = getSize();
		S32 new_index_size = getIndicesSize();

		sAllocatedBytes += newsize + new_index_size;

		if (newsize)
		{
			if (!mGLBuffer)
			{ //no buffer exists, create a new one
				createGLBuffer();
			}
			else
			{
				//delete old buffer, keep GL buffer for now
				U8* old = mMappedData;
				mMappedData = new U8[newsize];
				if (old)
				{	
					memcpy(mMappedData, old, llmin(newsize, oldsize));
					if (newsize > oldsize)
					{
						memset(mMappedData+oldsize, 0, newsize-oldsize);
					}

					delete [] old;
				}
				else
				{
					memset(mMappedData, 0, newsize);
					mEmpty = TRUE;
				}
				mResized = TRUE;
			}
		}
		else if (mGLBuffer)
		{
			destroyGLBuffer();
		}
		
		if (new_index_size)
		{
			if (!mGLIndices)
			{
				createGLIndices();
			}
			else
			{
				//delete old buffer, keep GL buffer for now
				U8* old = mMappedIndexData;
				mMappedIndexData = new U8[new_index_size];
				if (old)
				{	
					memcpy(mMappedIndexData, old, llmin(new_index_size, old_index_size));
					if (new_index_size > old_index_size)
					{
						memset(mMappedIndexData+old_index_size, 0, new_index_size - old_index_size);
					}
					delete [] old;
				}
				else
				{
					memset(mMappedIndexData, 0, new_index_size);
					mEmpty = TRUE;
				}
				mResized = TRUE;
			}
		}
		else if (mGLIndices)
		{
			destroyGLIndices();
		}
	}
}
void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
{
	llassert(newnverts >= 0);
	llassert(newnindices >= 0);

	mRequestedNumVerts = newnverts;
	mRequestedNumIndices = newnindices;

	LLMemType mt(LLMemType::MTYPE_VERTEX_DATA);
	mDynamicSize = TRUE;
	if (mUsage == GL_STATIC_DRAW_ARB)
	{ //always delete/allocate static buffers on resize
		destroyGLBuffer();
		destroyGLIndices();
		allocateBuffer(newnverts, newnindices, TRUE);
		mFinal = FALSE;
	}
	else if (newnverts > mNumVerts || newnindices > mNumIndices ||
			 newnverts < mNumVerts/2 || newnindices < mNumIndices/2)
	{
		sAllocatedBytes -= getSize() + getIndicesSize();
		
		S32 oldsize = getSize();
		S32 old_index_size = getIndicesSize();

		updateNumVerts(newnverts);		
		updateNumIndices(newnindices);
		
		S32 newsize = getSize();
		S32 new_index_size = getIndicesSize();

		sAllocatedBytes += newsize + new_index_size;

		if (newsize)
		{
			if (!mGLBuffer)
			{ //no buffer exists, create a new one
				createGLBuffer();
			}
			else
			{
				//delete old buffer, keep GL buffer for now
				if (!useVBOs())
				{
					volatile U8* old = mMappedData;
					mMappedData =  (U8*) ll_aligned_malloc_16(newsize);
					if (old)
					{	
						memcpy((void*)mMappedData, (void*)old, llmin(newsize, oldsize));
						if ((newsize > oldsize) && !sOmitBlank)
						{
							memset((void*)(mMappedData+oldsize), 0, newsize-oldsize);
						}

						ll_aligned_free_16((void*)old);
					}
					else
					{
						if (!sOmitBlank) memset((void*)mMappedData, 0, newsize);
						mEmpty = TRUE;
					}
				}
				mResized = TRUE;
			}
		}
		else if (mGLBuffer)
		{
			destroyGLBuffer();
		}
		
		if (new_index_size)
		{
			if (!mGLIndices)
			{
				createGLIndices();
			}
			else
			{
				if (!useVBOs())
				{
					//delete old buffer, keep GL buffer for now
					volatile U8* old = mMappedIndexData;
					mMappedIndexData = (U8*) ll_aligned_malloc_16(new_index_size);
					
					if (old)
					{	
						memcpy((void*)mMappedIndexData, (void*)old, llmin(new_index_size, old_index_size));
						if ((new_index_size > old_index_size) && !sOmitBlank)
						{
							memset((void*)(mMappedIndexData+old_index_size), 0, new_index_size - old_index_size);
						}
						ll_aligned_free_16((void*)old);
					}
					else
					{
						if (!sOmitBlank) memset((void*)mMappedIndexData, 0, new_index_size);
						mEmpty = TRUE;
					}
				}
				mResized = TRUE;
			}
		}
		else if (mGLIndices)
		{
			destroyGLIndices();
		}
	}

	if (mResized && useVBOs())
	{
		freeClientBuffer() ;
		setBuffer(0);
	}
}