OGRE  1.8
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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-2013 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 #include "OgreException.h"
34 
35 namespace Ogre {
36 
75  {
76 
77  public:
79  enum Usage
80  {
84  HBU_STATIC = 1,
90  HBU_DYNAMIC = 2,
97  HBU_WRITE_ONLY = 4,
106  HBU_DISCARDABLE = 8,
108  HBU_STATIC_WRITE_ONLY = 5,
114  HBU_DYNAMIC_WRITE_ONLY = 6,
116  HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
117 
118 
119  };
122  {
137  HBL_NO_OVERWRITE
138 
139  };
140  protected:
141  size_t mSizeInBytes;
143  bool mIsLocked;
144  size_t mLockStart;
145  size_t mLockSize;
151 
153  virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
155  virtual void unlockImpl(void) = 0;
156 
157  public:
159  HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
160  : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory),
161  mUseShadowBuffer(useShadowBuffer), mShadowBuffer(NULL), mShadowUpdated(false),
162  mSuppressHardwareUpdate(false)
163  {
164  // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
165  if (useShadowBuffer && usage == HBU_DYNAMIC)
166  {
167  mUsage = HBU_DYNAMIC_WRITE_ONLY;
168  }
169  else if (useShadowBuffer && usage == HBU_STATIC)
170  {
171  mUsage = HBU_STATIC_WRITE_ONLY;
172  }
173  }
174  virtual ~HardwareBuffer() {}
181  virtual void* lock(size_t offset, size_t length, LockOptions options)
182  {
183  assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
184 
185  void* ret = NULL;
186  if ((length + offset) > mSizeInBytes)
187  {
189  "Lock request out of bounds.",
190  "HardwareBuffer::lock");
191  }
192  else if (mUseShadowBuffer)
193  {
194  if (options != HBL_READ_ONLY)
195  {
196  // we have to assume a read / write lock so we use the shadow buffer
197  // and tag for sync on unlock()
198  mShadowUpdated = true;
199  }
200 
201  ret = mShadowBuffer->lock(offset, length, options);
202  }
203  else
204  {
205  // Lock the real buffer if there is no shadow buffer
206  ret = lockImpl(offset, length, options);
207  mIsLocked = true;
208  }
209  mLockStart = offset;
210  mLockSize = length;
211  return ret;
212  }
213 
218  void* lock(LockOptions options)
219  {
220  return this->lock(0, mSizeInBytes, options);
221  }
234  virtual void unlock(void)
235  {
236  assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
237 
238  // If we used the shadow buffer this time...
239  if (mUseShadowBuffer && mShadowBuffer->isLocked())
240  {
241  mShadowBuffer->unlock();
242  // Potentially update the 'real' buffer from the shadow buffer
243  _updateFromShadow();
244  }
245  else
246  {
247  // Otherwise, unlock the real one
248  unlockImpl();
249  mIsLocked = false;
250  }
251 
252  }
253 
260  virtual void readData(size_t offset, size_t length, void* pDest) = 0;
269  virtual void writeData(size_t offset, size_t length, const void* pSource,
270  bool discardWholeBuffer = false) = 0;
271 
282  virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
283  size_t dstOffset, size_t length, bool discardWholeBuffer = false)
284  {
285  const void *srcData = srcBuffer.lock(
286  srcOffset, length, HBL_READ_ONLY);
287  this->writeData(dstOffset, length, srcData, discardWholeBuffer);
288  srcBuffer.unlock();
289  }
290 
296  virtual void copyData(HardwareBuffer& srcBuffer)
297  {
298  size_t sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
299  copyData(srcBuffer, 0, 0, sz, true);
300  }
301 
303  virtual void _updateFromShadow(void)
304  {
305  if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
306  {
307  // Do this manually to avoid locking problems
308  const void *srcData = mShadowBuffer->lockImpl(
309  mLockStart, mLockSize, HBL_READ_ONLY);
310  // Lock with discard if the whole buffer was locked, otherwise normal
311  LockOptions lockOpt;
312  if (mLockStart == 0 && mLockSize == mSizeInBytes)
313  lockOpt = HBL_DISCARD;
314  else
315  lockOpt = HBL_NORMAL;
316 
317  void *destData = this->lockImpl(
318  mLockStart, mLockSize, lockOpt);
319  // Copy shadow to real
320  memcpy(destData, srcData, mLockSize);
321  this->unlockImpl();
322  mShadowBuffer->unlockImpl();
323  mShadowUpdated = false;
324  }
325  }
326 
328  size_t getSizeInBytes(void) const { return mSizeInBytes; }
330  Usage getUsage(void) const { return mUsage; }
332  bool isSystemMemory(void) const { return mSystemMemory; }
334  bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
336  bool isLocked(void) const {
337  return mIsLocked || (mUseShadowBuffer && mShadowBuffer->isLocked());
338  }
340  void suppressHardwareUpdate(bool suppress) {
341  mSuppressHardwareUpdate = suppress;
342  if (!suppress)
343  _updateFromShadow();
344  }
345 
346 
347 
348 
349 
350  };
353 }
354 #endif
355 
356 
size_t getSizeInBytes(void) const
Returns the size of this buffer in bytes.
#define _OgreExport
Definition: OgrePlatform.h:233
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.
HardwareBuffer * mShadowBuffer
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.
#define OGRE_EXCEPT(num, desc, src)
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.