| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org/
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #include "CmGLHardwareBufferManager.h"
- #include "CmGLHardwareVertexBuffer.h"
- #include "CmGLHardwareIndexBuffer.h"
- #include "CmHardwareBuffer.h"
- #include "CmRenderSystem.h"
- #include "CmRenderSystemCapabilities.h"
- namespace CamelotEngine {
- //-----------------------------------------------------------------------
- // Scratch pool management (32 bit structure)
- struct GLScratchBufferAlloc
- {
- /// Size in bytes
- UINT32 size: 31;
- /// Free? (pack with size)
- UINT32 free: 1;
- };
- #define SCRATCH_POOL_SIZE 1 * 1024 * 1024
- #define SCRATCH_ALIGNMENT 32
- //---------------------------------------------------------------------
- GLHardwareBufferManagerBase::GLHardwareBufferManagerBase()
- : mScratchBufferPool(NULL), mMapBufferThreshold(OGRE_GL_DEFAULT_MAP_BUFFER_THRESHOLD)
- {
- // Init scratch pool
- // TODO make it a configurable size?
- // 32-bit aligned buffer
- mScratchBufferPool = static_cast<char*>(_aligned_malloc(SCRATCH_POOL_SIZE, SCRATCH_ALIGNMENT));
- GLScratchBufferAlloc* ptrAlloc = (GLScratchBufferAlloc*)mScratchBufferPool;
- ptrAlloc->size = SCRATCH_POOL_SIZE - sizeof(GLScratchBufferAlloc);
- ptrAlloc->free = 1;
- // non-Win32 machines are having issues glBufferSubData, looks like buffer corruption
- // disable for now until we figure out where the problem lies
- # if CM_PLATFORM != CM_PLATFORM_WIN32
- mMapBufferThreshold = 0;
- # endif
- // Win32 machines with ATI GPU are having issues glMapBuffer, looks like buffer corruption
- // disable for now until we figure out where the problem lies
- # if CM_PLATFORM == CM_PLATFORM_WIN32
- if (CamelotEngine::RenderSystem::instancePtr()->getCapabilities()->getVendor() == GPU_ATI)
- {
- mMapBufferThreshold = 0xffffffffUL /* maximum unsigned long value */;
- }
- # endif
- }
- //-----------------------------------------------------------------------
- GLHardwareBufferManagerBase::~GLHardwareBufferManagerBase()
- {
- destroyAllBindings();
- _aligned_free(mScratchBufferPool);
- }
- //-----------------------------------------------------------------------
- HardwareVertexBufferPtr GLHardwareBufferManagerBase::createVertexBuffer(
- UINT32 vertexSize, UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut)
- {
- GLHardwareVertexBuffer* buf =
- new GLHardwareVertexBuffer(this, vertexSize, numVerts, usage);
- {
- mVertexBuffers.insert(buf);
- }
- return HardwareVertexBufferPtr(buf);
- }
- //-----------------------------------------------------------------------
- HardwareIndexBufferPtr
- GLHardwareBufferManagerBase::createIndexBuffer(
- HardwareIndexBuffer::IndexType itype, UINT32 numIndexes,
- HardwareBuffer::Usage usage)
- {
- GLHardwareIndexBuffer* buf =
- new GLHardwareIndexBuffer(this, itype, numIndexes, usage);
- {
- mIndexBuffers.insert(buf);
- }
- return HardwareIndexBufferPtr(buf);
- }
- //---------------------------------------------------------------------
- GpuParamBlockPtr GLHardwareBufferManagerBase::createGpuParamBlock(const GpuParamBlockDesc& paramDesc)
- {
- return GpuParamBlockPtr(new GpuParamBlock(paramDesc));
- }
- //---------------------------------------------------------------------
- GLenum GLHardwareBufferManagerBase::getGLUsage(unsigned int usage)
- {
- switch(usage)
- {
- case HardwareBuffer::HBU_STATIC:
- case HardwareBuffer::HBU_STATIC_WRITE_ONLY:
- return GL_STATIC_DRAW_ARB;
- case HardwareBuffer::HBU_DYNAMIC:
- case HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY:
- return GL_DYNAMIC_DRAW_ARB;
- case HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE:
- return GL_STREAM_DRAW_ARB;
- default:
- return GL_DYNAMIC_DRAW_ARB;
- };
- }
- //---------------------------------------------------------------------
- GLenum GLHardwareBufferManagerBase::getGLType(unsigned int type)
- {
- switch(type)
- {
- case VET_FLOAT1:
- case VET_FLOAT2:
- case VET_FLOAT3:
- case VET_FLOAT4:
- return GL_FLOAT;
- case VET_SHORT1:
- case VET_SHORT2:
- case VET_SHORT3:
- case VET_SHORT4:
- return GL_SHORT;
- case VET_COLOR:
- case VET_COLOR_ABGR:
- case VET_COLOR_ARGB:
- case VET_UBYTE4:
- return GL_UNSIGNED_BYTE;
- default:
- return 0;
- };
- }
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- void* GLHardwareBufferManagerBase::allocateScratch(UINT32 size)
- {
- // simple forward link search based on alloc sizes
- // not that fast but the list should never get that long since not many
- // locks at once (hopefully)
- CM_LOCK_MUTEX(mScratchMutex)
- // Alignment - round up the size to 32 bits
- // control blocks are 32 bits too so this packs nicely
- if (size % 4 != 0)
- {
- size += 4 - (size % 4);
- }
- UINT32 bufferPos = 0;
- while (bufferPos < SCRATCH_POOL_SIZE)
- {
- GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
- // Big enough?
- if (pNext->free && pNext->size >= size)
- {
- // split? And enough space for control block
- if(pNext->size > size + sizeof(GLScratchBufferAlloc))
- {
- UINT32 offset = (UINT32)sizeof(GLScratchBufferAlloc) + size;
- GLScratchBufferAlloc* pSplitAlloc = (GLScratchBufferAlloc*)
- (mScratchBufferPool + bufferPos + offset);
- pSplitAlloc->free = 1;
- // split size is remainder minus new control block
- pSplitAlloc->size = pNext->size - size - sizeof(GLScratchBufferAlloc);
- // New size of current
- pNext->size = size;
- }
- // allocate and return
- pNext->free = 0;
- // return pointer just after this control block (++ will do that for us)
- return ++pNext;
- }
- bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pNext->size;
- }
- // no available alloc
- return 0;
- }
- //---------------------------------------------------------------------
- void GLHardwareBufferManagerBase::deallocateScratch(void* ptr)
- {
- CM_LOCK_MUTEX(mScratchMutex)
- // Simple linear search dealloc
- UINT32 bufferPos = 0;
- GLScratchBufferAlloc* pLast = 0;
- while (bufferPos < SCRATCH_POOL_SIZE)
- {
- GLScratchBufferAlloc* pCurrent = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
-
- // Pointers match?
- if ((mScratchBufferPool + bufferPos + sizeof(GLScratchBufferAlloc))
- == ptr)
- {
- // dealloc
- pCurrent->free = 1;
-
- // merge with previous
- if (pLast && pLast->free)
- {
- // adjust buffer pos
- bufferPos -= (pLast->size + (UINT32)sizeof(GLScratchBufferAlloc));
- // merge free space
- pLast->size += pCurrent->size + sizeof(GLScratchBufferAlloc);
- pCurrent = pLast;
- }
- // merge with next
- UINT32 offset = bufferPos + pCurrent->size + (UINT32)sizeof(GLScratchBufferAlloc);
- if (offset < SCRATCH_POOL_SIZE)
- {
- GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(
- mScratchBufferPool + offset);
- if (pNext->free)
- {
- pCurrent->size += pNext->size + sizeof(GLScratchBufferAlloc);
- }
- }
- // done
- return;
- }
- bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pCurrent->size;
- pLast = pCurrent;
- }
- // Should never get here unless there's a corruption
- assert (false && "Memory deallocation error");
- }
- //---------------------------------------------------------------------
- const UINT32 GLHardwareBufferManagerBase::getGLMapBufferThreshold() const
- {
- return mMapBufferThreshold;
- }
- //---------------------------------------------------------------------
- void GLHardwareBufferManagerBase::setGLMapBufferThreshold( const UINT32 value )
- {
- mMapBufferThreshold = value;
- }
- }
|