[1809] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #ifndef __HardwareBuffer__
|
---|
| 26 | #define __HardwareBuffer__
|
---|
| 27 |
|
---|
| 28 | // Precompiler options
|
---|
| 29 | #include "OgrePrerequisites.h"
|
---|
| 30 |
|
---|
| 31 | namespace Ogre {
|
---|
| 32 |
|
---|
| 33 | /** Abstract class defining common features of hardware buffers.
|
---|
| 34 | @remarks
|
---|
| 35 | A 'hardware buffer' is any area of memory held outside of core system ram,
|
---|
| 36 | and in our case refers mostly to video ram, although in theory this class
|
---|
| 37 | could be used with other memory areas such as sound card memory, custom
|
---|
| 38 | coprocessor memory etc.
|
---|
| 39 | @par
|
---|
| 40 | This reflects the fact that memory held outside of main system RAM must
|
---|
| 41 | be interacted with in a more formal fashion in order to promote
|
---|
| 42 | cooperative and optimal usage of the buffers between the various
|
---|
| 43 | processing units which manipulate them.
|
---|
| 44 | @par
|
---|
| 45 | This abstract class defines the core interface which is common to all
|
---|
| 46 | buffers, whether it be vertex buffers, index buffers, texture memory
|
---|
| 47 | or framebuffer memory etc.
|
---|
| 48 | @par
|
---|
| 49 | Buffers have the ability to be 'shadowed' in system memory, this is because
|
---|
| 50 | the kinds of access allowed on hardware buffers is not always as flexible as
|
---|
| 51 | that allowed for areas of system memory - for example it is often either
|
---|
| 52 | impossible, or extremely undesirable from a performance standpoint to read from
|
---|
| 53 | a hardware buffer; when writing to hardware buffers, you should also write every
|
---|
| 54 | byte and do it sequentially. In situations where this is too restrictive,
|
---|
| 55 | it is possible to create a hardware, write-only buffer (the most efficient kind)
|
---|
| 56 | and to back it with a system memory 'shadow' copy which can be read and updated arbitrarily.
|
---|
| 57 | Ogre handles synchronising this buffer with the real hardware buffer (which should still be
|
---|
| 58 | created with the HBU_DYNAMIC flag if you intend to update it very frequently). Whilst this
|
---|
| 59 | approach does have it's own costs, such as increased memory overhead, these costs can
|
---|
| 60 | often be outweighed by the performance benefits of using a more hardware efficient buffer.
|
---|
| 61 | You should look for the 'useShadowBuffer' parameter on the creation methods used to create
|
---|
| 62 | the buffer of the type you require (see HardwareBufferManager) to enable this feature.
|
---|
| 63 | */
|
---|
| 64 | class _OgreExport HardwareBuffer
|
---|
| 65 | {
|
---|
| 66 |
|
---|
| 67 | public:
|
---|
| 68 | /// Enums describing buffer usage; not mutually exclusive
|
---|
| 69 | enum Usage
|
---|
| 70 | {
|
---|
| 71 | /** Static buffer which the application rarely modifies once created. Modifying
|
---|
| 72 | the contents of this buffer will involve a performance hit.
|
---|
| 73 | */
|
---|
| 74 | HBU_STATIC = 1,
|
---|
| 75 | /** Indicates the application would like to modify this buffer with the CPU
|
---|
| 76 | fairly often.
|
---|
| 77 | Buffers created with this flag will typically end up in AGP memory rather
|
---|
| 78 | than video memory.
|
---|
| 79 | */
|
---|
| 80 | HBU_DYNAMIC = 2,
|
---|
| 81 | /** Indicates the application will never read the contents of the buffer back,
|
---|
| 82 | it will only ever write data. Locking a buffer with this flag will ALWAYS
|
---|
| 83 | return a pointer to new, blank memory rather than the memory associated
|
---|
| 84 | with the contents of the buffer; this avoids DMA stalls because you can
|
---|
| 85 | write to a new memory area while the previous one is being used.
|
---|
| 86 | */
|
---|
| 87 | HBU_WRITE_ONLY = 4,
|
---|
| 88 | /** Indicates that the application will be refilling the contents
|
---|
| 89 | of the buffer regularly (not just updating, but generating the
|
---|
| 90 | contents from scratch), and therefore does not mind if the contents
|
---|
| 91 | of the buffer are lost somehow and need to be recreated. This
|
---|
| 92 | allows and additional level of optimisation on the buffer.
|
---|
| 93 | This option only really makes sense when combined with
|
---|
| 94 | HBU_DYNAMIC_WRITE_ONLY.
|
---|
| 95 | */
|
---|
| 96 | HBU_DISCARDABLE = 8,
|
---|
| 97 | /// Combination of HBU_STATIC and HBU_WRITE_ONLY
|
---|
| 98 | HBU_STATIC_WRITE_ONLY = 5,
|
---|
| 99 | /** Combination of HBU_DYNAMIC and HBU_WRITE_ONLY. If you use
|
---|
| 100 | this, strongly consider using HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
|
---|
| 101 | instead if you update the entire contents of the buffer very
|
---|
| 102 | regularly.
|
---|
| 103 | */
|
---|
| 104 | HBU_DYNAMIC_WRITE_ONLY = 6,
|
---|
| 105 | /// Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE
|
---|
| 106 | HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | };
|
---|
| 110 | /// Locking options
|
---|
| 111 | enum LockOptions
|
---|
| 112 | {
|
---|
| 113 | /** Normal mode, ie allows read/write and contents are preserved. */
|
---|
| 114 | HBL_NORMAL,
|
---|
| 115 | /** Discards the <em>entire</em> buffer while locking; this allows optimisation to be
|
---|
| 116 | performed because synchronisation issues are relaxed. Only allowed on buffers
|
---|
| 117 | created with the HBU_DYNAMIC flag.
|
---|
| 118 | */
|
---|
| 119 | HBL_DISCARD,
|
---|
| 120 | /** Lock the buffer for reading only. Not allowed in buffers which are created with HBU_WRITE_ONLY.
|
---|
| 121 | Mandatory on statuc buffers, ie those created without the HBU_DYNAMIC flag.
|
---|
| 122 | */
|
---|
| 123 | HBL_READ_ONLY,
|
---|
| 124 | /** As HBL_NORMAL, except the application guarantees not to overwrite any
|
---|
| 125 | region of the buffer which has already been used in this frame, can allow
|
---|
| 126 | some optimisation on some APIs. */
|
---|
| 127 | HBL_NO_OVERWRITE
|
---|
| 128 |
|
---|
| 129 | };
|
---|
| 130 | protected:
|
---|
| 131 | size_t mSizeInBytes;
|
---|
| 132 | Usage mUsage;
|
---|
| 133 | bool mIsLocked;
|
---|
| 134 | size_t mLockStart;
|
---|
| 135 | size_t mLockSize;
|
---|
| 136 | bool mSystemMemory;
|
---|
| 137 | bool mUseShadowBuffer;
|
---|
| 138 | HardwareBuffer* mpShadowBuffer;
|
---|
| 139 | bool mShadowUpdated;
|
---|
| 140 | bool mSuppressHardwareUpdate;
|
---|
| 141 |
|
---|
| 142 | /// Internal implementation of lock()
|
---|
| 143 | virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
|
---|
| 144 | /// Internal implementation of unlock()
|
---|
| 145 | virtual void unlockImpl(void) = 0;
|
---|
| 146 |
|
---|
| 147 | public:
|
---|
| 148 | /// Constructor, to be called by HardwareBufferManager only
|
---|
| 149 | HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
|
---|
| 150 | : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory),
|
---|
| 151 | mUseShadowBuffer(useShadowBuffer), mpShadowBuffer(NULL), mShadowUpdated(false),
|
---|
| 152 | mSuppressHardwareUpdate(false)
|
---|
| 153 | {
|
---|
| 154 | // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
|
---|
| 155 | if (useShadowBuffer && usage == HBU_DYNAMIC)
|
---|
| 156 | {
|
---|
| 157 | mUsage = HBU_DYNAMIC_WRITE_ONLY;
|
---|
| 158 | }
|
---|
| 159 | else if (useShadowBuffer && usage == HBU_STATIC)
|
---|
| 160 | {
|
---|
| 161 | mUsage = HBU_STATIC_WRITE_ONLY;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | virtual ~HardwareBuffer() {}
|
---|
| 165 | /** Lock the buffer for (potentially) reading / writing.
|
---|
| 166 | @param offset The byte offset from the start of the buffer to lock
|
---|
| 167 | @param length The size of the area to lock, in bytes
|
---|
| 168 | @param options Locking options
|
---|
| 169 | @returns Pointer to the locked memory
|
---|
| 170 | */
|
---|
| 171 | virtual void* lock(size_t offset, size_t length, LockOptions options)
|
---|
| 172 | {
|
---|
| 173 | assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
|
---|
| 174 | void* ret;
|
---|
| 175 | if (mUseShadowBuffer)
|
---|
| 176 | {
|
---|
| 177 | if (options != HBL_READ_ONLY)
|
---|
| 178 | {
|
---|
| 179 | // we have to assume a read / write lock so we use the shadow buffer
|
---|
| 180 | // and tag for sync on unlock()
|
---|
| 181 | mShadowUpdated = true;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | ret = mpShadowBuffer->lock(offset, length, options);
|
---|
| 185 | }
|
---|
| 186 | else
|
---|
| 187 | {
|
---|
| 188 | // Lock the real buffer if there is no shadow buffer
|
---|
| 189 | ret = lockImpl(offset, length, options);
|
---|
| 190 | mIsLocked = true;
|
---|
| 191 | }
|
---|
| 192 | mLockStart = offset;
|
---|
| 193 | mLockSize = length;
|
---|
| 194 | return ret;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /** Lock the entire buffer for (potentially) reading / writing.
|
---|
| 198 | @param options Locking options
|
---|
| 199 | @returns Pointer to the locked memory
|
---|
| 200 | */
|
---|
| 201 | void* lock(LockOptions options)
|
---|
| 202 | {
|
---|
| 203 | return this->lock(0, mSizeInBytes, options);
|
---|
| 204 | }
|
---|
| 205 | /** Releases the lock on this buffer.
|
---|
| 206 | @remarks
|
---|
| 207 | Locking and unlocking a buffer can, in some rare circumstances such as
|
---|
| 208 | switching video modes whilst the buffer is locked, corrupt the
|
---|
| 209 | contents of a buffer. This is pretty rare, but if it occurs,
|
---|
| 210 | this method will throw an exception, meaning you
|
---|
| 211 | must re-upload the data.
|
---|
| 212 | @par
|
---|
| 213 | Note that using the 'read' and 'write' forms of updating the buffer does not
|
---|
| 214 | suffer from this problem, so if you want to be 100% sure your
|
---|
| 215 | data will not be lost, use the 'read' and 'write' forms instead.
|
---|
| 216 | */
|
---|
| 217 | virtual void unlock(void)
|
---|
| 218 | {
|
---|
| 219 | assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
|
---|
| 220 |
|
---|
| 221 | // If we used the shadow buffer this time...
|
---|
| 222 | if (mUseShadowBuffer && mpShadowBuffer->isLocked())
|
---|
| 223 | {
|
---|
| 224 | mpShadowBuffer->unlock();
|
---|
| 225 | // Potentially update the 'real' buffer from the shadow buffer
|
---|
| 226 | _updateFromShadow();
|
---|
| 227 | }
|
---|
| 228 | else
|
---|
| 229 | {
|
---|
| 230 | // Otherwise, unlock the real one
|
---|
| 231 | unlockImpl();
|
---|
| 232 | mIsLocked = false;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Reads data from the buffer and places it in the memory pointed to by pDest.
|
---|
| 238 | @param offset The byte offset from the start of the buffer to read
|
---|
| 239 | @param length The size of the area to read, in bytes
|
---|
| 240 | @param pDest The area of memory in which to place the data, must be large enough to
|
---|
| 241 | accommodate the data!
|
---|
| 242 | */
|
---|
| 243 | virtual void readData(size_t offset, size_t length, void* pDest) = 0;
|
---|
| 244 | /** Writes data to the buffer from an area of system memory; note that you must
|
---|
| 245 | ensure that your buffer is big enough.
|
---|
| 246 | @param offset The byte offset from the start of the buffer to start writing
|
---|
| 247 | @param length The size of the data to write to, in bytes
|
---|
| 248 | @param pSource The source of the data to be written
|
---|
| 249 | @param discardWholeBuffer If true, this allows the driver to discard the entire buffer when writing,
|
---|
| 250 | such that DMA stalls can be avoided; use if you can.
|
---|
| 251 | */
|
---|
| 252 | virtual void writeData(size_t offset, size_t length, const void* pSource,
|
---|
| 253 | bool discardWholeBuffer = false) = 0;
|
---|
| 254 |
|
---|
| 255 | /** Copy data from another buffer into this one.
|
---|
| 256 | @remarks
|
---|
| 257 | Note that the source buffer must not be created with the
|
---|
| 258 | usage HBU_WRITE_ONLY otherwise this will fail.
|
---|
| 259 | @param srcBuffer The buffer from which to read the copied data
|
---|
| 260 | @param srcOffset Offset in the source buffer at which to start reading
|
---|
| 261 | @param dstOffset Offset in the destination buffer to start writing
|
---|
| 262 | @param length Length of the data to copy, in bytes.
|
---|
| 263 | @param discardWholeBuffer If true, will discard the entire contents of this buffer before copying
|
---|
| 264 | */
|
---|
| 265 | virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
|
---|
| 266 | size_t dstOffset, size_t length, bool discardWholeBuffer = false)
|
---|
| 267 | {
|
---|
| 268 | const void *srcData = srcBuffer.lock(
|
---|
| 269 | srcOffset, length, HBL_READ_ONLY);
|
---|
| 270 | this->writeData(dstOffset, length, srcData, discardWholeBuffer);
|
---|
| 271 | srcBuffer.unlock();
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /// Updates the real buffer from the shadow buffer, if required
|
---|
| 275 | virtual void _updateFromShadow(void)
|
---|
| 276 | {
|
---|
| 277 | if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
|
---|
| 278 | {
|
---|
| 279 | // Do this manually to avoid locking problems
|
---|
| 280 | const void *srcData = mpShadowBuffer->lockImpl(
|
---|
| 281 | mLockStart, mLockSize, HBL_READ_ONLY);
|
---|
| 282 | // Lock with discard if the whole buffer was locked, otherwise normal
|
---|
| 283 | LockOptions lockOpt;
|
---|
| 284 | if (mLockStart == 0 && mLockSize == mSizeInBytes)
|
---|
| 285 | lockOpt = HBL_DISCARD;
|
---|
| 286 | else
|
---|
| 287 | lockOpt = HBL_NORMAL;
|
---|
| 288 |
|
---|
| 289 | void *destData = this->lockImpl(
|
---|
| 290 | mLockStart, mLockSize, lockOpt);
|
---|
| 291 | // Copy shadow to real
|
---|
| 292 | memcpy(destData, srcData, mLockSize);
|
---|
| 293 | this->unlockImpl();
|
---|
| 294 | mpShadowBuffer->unlockImpl();
|
---|
| 295 | mShadowUpdated = false;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /// Returns the size of this buffer in bytes
|
---|
| 300 | size_t getSizeInBytes(void) const { return mSizeInBytes; }
|
---|
| 301 | /// Returns the Usage flags with which this buffer was created
|
---|
| 302 | Usage getUsage(void) const { return mUsage; }
|
---|
| 303 | /// Returns whether this buffer is held in system memory
|
---|
| 304 | bool isSystemMemory(void) const { return mSystemMemory; }
|
---|
| 305 | /// Returns whether this buffer has a system memory shadow for quicker reading
|
---|
| 306 | bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
|
---|
| 307 | /// Returns whether or not this buffer is currently locked.
|
---|
| 308 | bool isLocked(void) const {
|
---|
| 309 | return mIsLocked || (mUseShadowBuffer && mpShadowBuffer->isLocked());
|
---|
| 310 | }
|
---|
| 311 | /// Pass true to suppress hardware upload of shadow buffer changes
|
---|
| 312 | void suppressHardwareUpdate(bool suppress) {
|
---|
| 313 | mSuppressHardwareUpdate = suppress;
|
---|
| 314 | if (!suppress)
|
---|
| 315 | _updateFromShadow();
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 |
|
---|
| 322 | };
|
---|
| 323 | }
|
---|
| 324 | #endif
|
---|
| 325 |
|
---|
| 326 |
|
---|