Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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-2006 Torus Knot Software Ltd
    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 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef __HardwareBuffer__
    00030 #define __HardwareBuffer__
    00031
    00032 // Precompiler options
    00033 #include "OgrePrerequisites.h"
    00034
    00035 namespace Ogre {
    00036
    00068     class _OgreExport HardwareBuffer
    00069     {
    00070
    00071         public:
    00073             enum Usage
    00074             {
    00078                 HBU_STATIC = 1,
    00084                 HBU_DYNAMIC = 2,
    00091                 HBU_WRITE_ONLY = 4,
    00100                 HBU_DISCARDABLE = 8,
    00102                 HBU_STATIC_WRITE_ONLY = 5,
    00108                 HBU_DYNAMIC_WRITE_ONLY = 6,
    00110                 HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
    00111
    00112
    00113             };
    00115             enum LockOptions
    00116             {
    00118                 HBL_NORMAL,
    00123                 HBL_DISCARD,
    00127                 HBL_READ_ONLY,
    00131                 HBL_NO_OVERWRITE
    00132
    00133             };
    00134         protected:
    00135             size_t mSizeInBytes;
    00136             Usage mUsage;
    00137             bool mIsLocked;
    00138             size_t mLockStart;
    00139             size_t mLockSize;
    00140             bool mSystemMemory;
    00141             bool mUseShadowBuffer;
    00142             HardwareBuffer* mpShadowBuffer;
    00143             bool mShadowUpdated;
    00144             bool mSuppressHardwareUpdate;
    00145
    00147             virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
    00149             virtual void unlockImpl(void) = 0;
    00150
    00151     public:
    00153             HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
    00154                 : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory),
    00155                 mUseShadowBuffer(useShadowBuffer), mpShadowBuffer(NULL), mShadowUpdated(false),
    00156                 mSuppressHardwareUpdate(false)
    00157             {
    00158                 // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
    00159                 if (useShadowBuffer && usage == HBU_DYNAMIC)
    00160                 {
    00161                     mUsage = HBU_DYNAMIC_WRITE_ONLY;
    00162                 }
    00163                 else if (useShadowBuffer && usage == HBU_STATIC)
    00164                 {
    00165                     mUsage = HBU_STATIC_WRITE_ONLY;
    00166                 }
    00167             }
    00168             virtual ~HardwareBuffer() {}
    00175             virtual void* lock(size_t offset, size_t length, LockOptions options)
    00176             {
    00177                 assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
    00178                 void* ret;
    00179                 if (mUseShadowBuffer)
    00180                 {
    00181                     if (options != HBL_READ_ONLY)
    00182                     {
    00183                         // we have to assume a read / write lock so we use the shadow buffer
    00184                         // and tag for sync on unlock()
    00185                         mShadowUpdated = true;
    00186                     }
    00187
    00188                     ret = mpShadowBuffer->lock(offset, length, options);
    00189                 }
    00190                 else
    00191                 {
    00192                     // Lock the real buffer if there is no shadow buffer
    00193                     ret = lockImpl(offset, length, options);
    00194                     mIsLocked = true;
    00195                 }
    00196                 mLockStart = offset;
    00197                 mLockSize = length;
    00198                 return ret;
    00199             }
    00200
    00205             void* lock(LockOptions options)
    00206             {
    00207                 return this->lock(0, mSizeInBytes, options);
    00208             }
    00221             virtual void unlock(void)
    00222             {
    00223                 assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
    00224
    00225                 // If we used the shadow buffer this time...
    00226                 if (mUseShadowBuffer && mpShadowBuffer->isLocked())
    00227                 {
    00228                     mpShadowBuffer->unlock();
    00229                     // Potentially update the 'real' buffer from the shadow buffer
    00230                     _updateFromShadow();
    00231                 }
    00232                 else
    00233                 {
    00234                     // Otherwise, unlock the real one
    00235                     unlockImpl();
    00236                     mIsLocked = false;
    00237                 }
    00238
    00239             }
    00240
    00247             virtual void readData(size_t offset, size_t length, void* pDest) = 0;
    00256             virtual void writeData(size_t offset, size_t length, const void* pSource,
    00257                     bool discardWholeBuffer = false) = 0;
    00258
    00269             virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
    00270                 size_t dstOffset, size_t length, bool discardWholeBuffer = false)
    00271             {
    00272                 const void *srcData = srcBuffer.lock(
    00273                     srcOffset, length, HBL_READ_ONLY);
    00274                 this->writeData(dstOffset, length, srcData, discardWholeBuffer);
    00275                 srcBuffer.unlock();
    00276             }
    00277
    00279             virtual void _updateFromShadow(void)
    00280             {
    00281                 if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
    00282                 {
    00283                     // Do this manually to avoid locking problems
    00284                     const void *srcData = mpShadowBuffer->lockImpl(
    00285                         mLockStart, mLockSize, HBL_READ_ONLY);
    00286                     // Lock with discard if the whole buffer was locked, otherwise normal
    00287                     LockOptions lockOpt;
    00288                     if (mLockStart == 0 && mLockSize == mSizeInBytes)
    00289                         lockOpt = HBL_DISCARD;
    00290                     else
    00291                         lockOpt = HBL_NORMAL;
    00292
    00293                     void *destData = this->lockImpl(
    00294                         mLockStart, mLockSize, lockOpt);
    00295                     // Copy shadow to real
    00296                     memcpy(destData, srcData, mLockSize);
    00297                     this->unlockImpl();
    00298                     mpShadowBuffer->unlockImpl();
    00299                     mShadowUpdated = false;
    00300                 }
    00301             }
    00302
    00304             size_t getSizeInBytes(void) const { return mSizeInBytes; }
    00306             Usage getUsage(void) const { return mUsage; }
    00308             bool isSystemMemory(void) const { return mSystemMemory; }
    00310             bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
    00312             bool isLocked(void) const {
    00313                 return mIsLocked || (mUseShadowBuffer && mpShadowBuffer->isLocked());
    00314             }
    00316             void suppressHardwareUpdate(bool suppress) {
    00317                 mSuppressHardwareUpdate = suppress;
    00318                 if (!suppress)
    00319                     _updateFromShadow();
    00320             }
    00321
    00322
    00323
    00324
    00325
    00326     };
    00327 }
    00328 #endif
    00329
    00330

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 30 10:50:56 2007