Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreHardwareBuffer.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __HardwareBuffer__
00026 #define __HardwareBuffer__
00027 
00028 // Precompiler options
00029 #include "OgrePrerequisites.h"
00030 
00031 namespace Ogre {
00032 
00064     class _OgreExport HardwareBuffer 
00065     {
00066 
00067         public:
00069             enum Usage 
00070             {
00074                 HBU_STATIC = 1,
00080                 HBU_DYNAMIC = 2,
00087                 HBU_WRITE_ONLY = 4,
00096                 HBU_DISCARDABLE = 8,
00098                 HBU_STATIC_WRITE_ONLY = 5, 
00104                 HBU_DYNAMIC_WRITE_ONLY = 6,
00106                 HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
00107 
00108 
00109             };
00111             enum LockOptions
00112             {
00114                 HBL_NORMAL,
00119                 HBL_DISCARD,
00123                 HBL_READ_ONLY,
00127                 HBL_NO_OVERWRITE
00128                 
00129             };
00130         protected:
00131             size_t mSizeInBytes;
00132             Usage mUsage;
00133             bool mIsLocked;
00134             size_t mLockStart;
00135             size_t mLockSize;
00136             bool mSystemMemory;
00137             bool mUseShadowBuffer;
00138             HardwareBuffer* mpShadowBuffer;
00139             bool mShadowUpdated;
00140             bool mSuppressHardwareUpdate;
00141             
00143             virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
00145             virtual void unlockImpl(void) = 0;
00146 
00147     public:
00149             HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer) 
00150                 : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory), 
00151                 mUseShadowBuffer(useShadowBuffer), mpShadowBuffer(NULL), mShadowUpdated(false), 
00152                 mSuppressHardwareUpdate(false) 
00153             {
00154                 // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
00155                 if (useShadowBuffer && usage == HBU_DYNAMIC)
00156                 {
00157                     mUsage = HBU_DYNAMIC_WRITE_ONLY;
00158                 }
00159                 else if (useShadowBuffer && usage == HBU_STATIC)
00160                 {
00161                     mUsage = HBU_STATIC_WRITE_ONLY;
00162                 }
00163             }
00164             virtual ~HardwareBuffer() {}
00171             virtual void* lock(size_t offset, size_t length, LockOptions options)
00172             {
00173                 assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
00174                 void* ret;
00175                 if (mUseShadowBuffer)
00176                 {
00177                     if (options != HBL_READ_ONLY)
00178                     {
00179                         // we have to assume a read / write lock so we use the shadow buffer
00180                         // and tag for sync on unlock()
00181                         mShadowUpdated = true;
00182                     }
00183 
00184                     ret = mpShadowBuffer->lock(offset, length, options);
00185                 }
00186                 else
00187                 {
00188                     // Lock the real buffer if there is no shadow buffer 
00189                     ret = lockImpl(offset, length, options);
00190                     mIsLocked = true;
00191                 }
00192                 mLockStart = offset;
00193                 mLockSize = length;
00194                 return ret;
00195             }
00196 
00201             void* lock(LockOptions options)
00202             {
00203                 return this->lock(0, mSizeInBytes, options);
00204             }
00217             virtual void unlock(void)
00218             {
00219                 assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
00220 
00221                 // If we used the shadow buffer this time...
00222                 if (mUseShadowBuffer && mpShadowBuffer->isLocked())
00223                 {
00224                     mpShadowBuffer->unlock();
00225                     // Potentially update the 'real' buffer from the shadow buffer
00226                     _updateFromShadow();
00227                 }
00228                 else
00229                 {
00230                     // Otherwise, unlock the real one
00231                     unlockImpl();
00232                     mIsLocked = false;
00233                 }
00234 
00235             }
00236 
00243             virtual void readData(size_t offset, size_t length, void* pDest) = 0;
00252             virtual void writeData(size_t offset, size_t length, const void* pSource,
00253                     bool discardWholeBuffer = false) = 0;
00254 
00265             virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset, 
00266                 size_t dstOffset, size_t length, bool discardWholeBuffer = false)
00267             {
00268                 const void *srcData = srcBuffer.lock(
00269                     srcOffset, length, HBL_READ_ONLY);
00270                 this->writeData(dstOffset, length, srcData, discardWholeBuffer);
00271                 srcBuffer.unlock();
00272             }
00273 
00275             virtual void _updateFromShadow(void)
00276             {
00277                 if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
00278                 {
00279                     // Do this manually to avoid locking problems
00280                     const void *srcData = mpShadowBuffer->lockImpl(
00281                         mLockStart, mLockSize, HBL_READ_ONLY);
00282                     // Lock with discard if the whole buffer was locked, otherwise normal
00283                     LockOptions lockOpt;
00284                     if (mLockStart == 0 && mLockSize == mSizeInBytes)
00285                         lockOpt = HBL_DISCARD;
00286                     else
00287                         lockOpt = HBL_NORMAL;
00288                     
00289                     void *destData = this->lockImpl(
00290                         mLockStart, mLockSize, lockOpt);
00291                     // Copy shadow to real
00292                     memcpy(destData, srcData, mLockSize);
00293                     this->unlockImpl();
00294                     mpShadowBuffer->unlockImpl();
00295                     mShadowUpdated = false;
00296                 }
00297             }
00298 
00300             size_t getSizeInBytes(void) const { return mSizeInBytes; }
00302             Usage getUsage(void) const { return mUsage; }
00304             bool isSystemMemory(void) const { return mSystemMemory; }
00306             bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
00308             bool isLocked(void) const { 
00309                 return mIsLocked || (mUseShadowBuffer && mpShadowBuffer->isLocked()); 
00310             }
00312             void suppressHardwareUpdate(bool suppress) {
00313                 mSuppressHardwareUpdate = suppress;
00314                 if (!suppress)
00315                     _updateFromShadow();
00316             }
00317 
00318 
00319 
00320 
00321             
00322     };
00323 }
00324 #endif
00325 
00326 

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:42 2006