| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- -----------------------------------------------------------------------------
- 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 "CmIndexData.h"
- #include "CmVertexData.h"
- #include "CmHardwareBufferManager.h"
- #include "CmVertexBuffer.h"
- #include "CmVector3.h"
- #include "CmException.h"
- #include "CmRenderSystem.h"
- namespace CamelotFramework
- {
- VertexData::VertexData(HardwareBufferManager* mgr)
- :mOwnsDeclaration(true)
- {
- mMgr = mgr ? mgr : HardwareBufferManager::instancePtr();
- vertexDeclaration = mMgr->createVertexDeclaration();
- vertexCount = 0;
- }
- VertexData::VertexData(VertexDeclarationPtr dcl)
- :mOwnsDeclaration(false)
- {
- // this is a fallback rather than actively used
- mMgr = HardwareBufferManager::instancePtr();
- vertexDeclaration = dcl;
- vertexCount = 0;
- }
- VertexData::~VertexData()
- {
- }
- void VertexData::setBuffer(UINT32 index, VertexBufferPtr buffer)
- {
- mVertexBuffers[index] = buffer;
- }
- VertexBufferPtr VertexData::getBuffer(UINT32 index) const
- {
- auto iterFind = mVertexBuffers.find(index);
- if(iterFind != mVertexBuffers.end())
- {
- return iterFind->second;
- }
- return nullptr;
- }
- bool VertexData::isBufferBound(UINT32 index) const
- {
- auto iterFind = mVertexBuffers.find(index);
- if(iterFind != mVertexBuffers.end())
- {
- if(iterFind->second != nullptr)
- return true;
- }
- return false;
- }
- void VertexData::convertPackedColour(
- VertexElementType srcType, VertexElementType destType)
- {
- if (destType != VET_COLOR_ABGR && destType != VET_COLOR_ARGB)
- {
- CM_EXCEPT(InvalidParametersException,
- "Invalid destType parameter");
- }
- if (srcType != VET_COLOR_ABGR && srcType != VET_COLOR_ARGB)
- {
- CM_EXCEPT(InvalidParametersException,
- "Invalid srcType parameter");
- }
- for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
- {
- VertexDeclaration::VertexElementList elems =
- vertexDeclaration->findElementsBySource(iter->first);
- bool conversionNeeded = false;
- VertexDeclaration::VertexElementList::iterator elemi;
- for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
- {
- VertexElement& elem = *elemi;
- if (elem.getType() == VET_COLOR ||
- ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
- && elem.getType() != destType))
- {
- conversionNeeded = true;
- }
- }
- if (conversionNeeded)
- {
- void* pBase = iter->second->lock(GBL_READ_WRITE);
- for (UINT32 v = 0; v < iter->second->getNumVertices(); ++v)
- {
- for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
- {
- VertexElement& elem = *elemi;
- VertexElementType currType = (elem.getType() == VET_COLOR) ?
- srcType : elem.getType();
- if (elem.getType() == VET_COLOR ||
- ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
- && elem.getType() != destType))
- {
- UINT32* pRGBA;
- elem.baseVertexPointerToElement(pBase, &pRGBA);
- VertexElement::convertColourValue(currType, destType, pRGBA);
- }
- }
- pBase = static_cast<void*>(
- static_cast<char*>(pBase) + iter->second->getVertexSize());
- }
- iter->second->unlock();
- // Modify the elements to reflect the changed type
- const VertexDeclaration::VertexElementList& allelems =
- vertexDeclaration->getElements();
- VertexDeclaration::VertexElementList::const_iterator ai;
- unsigned short elemIndex = 0;
- for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
- {
- const VertexElement& elem = *ai;
- if (elem.getType() == VET_COLOR ||
- ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
- && elem.getType() != destType))
- {
- vertexDeclaration->modifyElement(elemIndex,
- elem.getStreamIdx(), elem.getOffset(), destType,
- elem.getSemantic(), elem.getSemanticIdx());
- }
- }
- }
- } // each buffer
- }
- void VertexCacheProfiler::profile(const IndexBufferPtr& indexBuffer)
- {
- if (indexBuffer->isLocked()) return;
- UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(GBL_READ_ONLY);
- if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
- for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
- inCache(shortbuffer[i]);
- else
- {
- UINT32 *buffer = (UINT32 *)shortbuffer;
- for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
- inCache(buffer[i]);
- }
- indexBuffer->unlock();
- }
- bool VertexCacheProfiler::inCache(unsigned int index)
- {
- for (unsigned int i = 0; i < buffersize; ++i)
- {
- if (index == cache[i])
- {
- hit++;
- return true;
- }
- }
- miss++;
- cache[tail++] = index;
- tail %= size;
- if (buffersize < size) buffersize++;
- return false;
- }
- }
|