OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgreHardwareBuffer.h
Go to the documentation of this file.
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-2011 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __HardwareBuffer__
29 #define __HardwareBuffer__
30 
31 // Precompiler options
32 #include "OgrePrerequisites.h"
33 
34 namespace Ogre {
35 
74  {
75 
76  public:
78  enum Usage
79  {
83  HBU_STATIC = 1,
89  HBU_DYNAMIC = 2,
96  HBU_WRITE_ONLY = 4,
105  HBU_DISCARDABLE = 8,
107  HBU_STATIC_WRITE_ONLY = 5,
113  HBU_DYNAMIC_WRITE_ONLY = 6,
115  HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
116 
117 
118  };
121  {
136  HBL_NO_OVERWRITE
137 
138  };
139  protected:
140  size_t mSizeInBytes;
142  bool mIsLocked;
143  size_t mLockStart;
144  size_t mLockSize;
150 
152  virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
154  virtual void unlockImpl(void) = 0;
155 
156  public:
158  HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
159  : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory),
160  mUseShadowBuffer(useShadowBuffer), mpShadowBuffer(NULL), mShadowUpdated(false),
161  mSuppressHardwareUpdate(false)
162  {
163  // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
164  if (useShadowBuffer && usage == HBU_DYNAMIC)
165  {
166  mUsage = HBU_DYNAMIC_WRITE_ONLY;
167  }
168  else if (useShadowBuffer && usage == HBU_STATIC)
169  {
170  mUsage = HBU_STATIC_WRITE_ONLY;
171  }
172  }
173  virtual ~HardwareBuffer() {}
180  virtual void* lock(size_t offset, size_t length, LockOptions options)
181  {
182  assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
183  void* ret;
184  if (mUseShadowBuffer)
185  {
186  if (options != HBL_READ_ONLY)
187  {
188  // we have to assume a read / write lock so we use the shadow buffer
189  // and tag for sync on unlock()
190  mShadowUpdated = true;
191  }
192 
193  ret = mpShadowBuffer->lock(offset, length, options);
194  }
195  else
196  {
197  // Lock the real buffer if there is no shadow buffer
198  ret = lockImpl(offset, length, options);
199  mIsLocked = true;
200  }
201  mLockStart = offset;
202  mLockSize = length;
203  return ret;
204  }
205 
210  void* lock(LockOptions options)
211  {
212  return this->lock(0, mSizeInBytes, options);
213  }
226  virtual void unlock(void)
227  {
228  assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
229 
230  // If we used the shadow buffer this time...
231  if (mUseShadowBuffer && mpShadowBuffer->isLocked())
232  {
233  mpShadowBuffer->unlock();
234  // Potentially update the 'real' buffer from the shadow buffer
235  _updateFromShadow();
236  }
237  else
238  {
239  // Otherwise, unlock the real one
240  unlockImpl();
241  mIsLocked = false;
242  }
243 
244  }
245 
252  virtual void readData(size_t offset, size_t length, void* pDest) = 0;
261  virtual void writeData(size_t offset, size_t length, const void* pSource,
262  bool discardWholeBuffer = false) = 0;
263 
274  virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
275  size_t dstOffset, size_t length, bool discardWholeBuffer = false)
276  {
277  const void *srcData = srcBuffer.lock(
278  srcOffset, length, HBL_READ_ONLY);
279  this->writeData(dstOffset, length, srcData, discardWholeBuffer);
280  srcBuffer.unlock();
281  }
282 
288  virtual void copyData(HardwareBuffer& srcBuffer)
289  {
290  size_t sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
291  copyData(srcBuffer, 0, 0, sz, true);
292  }
293 
295  virtual void _updateFromShadow(void)
296  {
297  if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
298  {
299  // Do this manually to avoid locking problems
300  const void *srcData = mpShadowBuffer->lockImpl(
301  mLockStart, mLockSize, HBL_READ_ONLY);
302  // Lock with discard if the whole buffer was locked, otherwise normal
303  LockOptions lockOpt;
304  if (mLockStart == 0 && mLockSize == mSizeInBytes)
305  lockOpt = HBL_DISCARD;
306  else
307  lockOpt = HBL_NORMAL;
308 
309  void *destData = this->lockImpl(
310  mLockStart, mLockSize, lockOpt);
311  // Copy shadow to real
312  memcpy(destData, srcData, mLockSize);
313  this->unlockImpl();
314  mpShadowBuffer->unlockImpl();
315  mShadowUpdated = false;
316  }
317  }
318 
320  size_t getSizeInBytes(void) const { return mSizeInBytes; }
322  Usage getUsage(void) const { return mUsage; }
324  bool isSystemMemory(void) const { return mSystemMemory; }
326  bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
328  bool isLocked(void) const {
329  return mIsLocked || (mUseShadowBuffer && mpShadowBuffer->isLocked());
330  }
332  void suppressHardwareUpdate(bool suppress) {
333  mSuppressHardwareUpdate = suppress;
334  if (!suppress)
335  _updateFromShadow();
336  }
337 
338 
339 
340 
341 
342  };
345 }
346 #endif
347 
348 
size_t getSizeInBytes(void) const
Returns the size of this buffer in bytes.
#define _OgreExport
Definition: OgrePlatform.h:203
HardwareBuffer * mpShadowBuffer
virtual void * lock(size_t offset, size_t length, LockOptions options)
Lock the buffer for (potentially) reading / writing.
HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
Constructor, to be called by HardwareBufferManager only.
virtual void copyData(HardwareBuffer &srcBuffer, size_t srcOffset, size_t dstOffset, size_t length, bool discardWholeBuffer=false)
Copy data from another buffer into this one.
Discards the entire buffer while locking; this allows optimisation to be performed because synchronis...
void * lock(LockOptions options)
Lock the entire buffer for (potentially) reading / writing.
LockOptions
Locking options.
Normal mode, ie allows read/write and contents are preserved.
bool isLocked(void) const
Returns whether or not this buffer is currently locked.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
void suppressHardwareUpdate(bool suppress)
Pass true to suppress hardware upload of shadow buffer changes.
bool hasShadowBuffer(void) const
Returns whether this buffer has a system memory shadow for quicker reading.
Usage
Enums describing buffer usage; not mutually exclusive.
Usage getUsage(void) const
Returns the Usage flags with which this buffer was created.
virtual void unlock(void)
Releases the lock on this buffer.
Lock the buffer for reading only.
virtual void copyData(HardwareBuffer &srcBuffer)
Copy all data from another buffer into this one.
Abstract class defining common features of hardware buffers.
bool isSystemMemory(void) const
Returns whether this buffer is held in system memory.
virtual void _updateFromShadow(void)
Updates the real buffer from the shadow buffer, if required.