OGRE  1.9.0
OgreHardwareBuffer.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26-----------------------------------------------------------------------------
27*/
28#ifndef __HardwareBuffer__
29#define __HardwareBuffer__
30
31// Precompiler options
32#include "OgrePrerequisites.h"
33#include "OgreException.h"
34
35namespace Ogre {
36
75 {
76
77 public:
120
142 protected:
147 size_t mLockSize;
153
155 virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
157 virtual void unlockImpl(void) = 0;
158
159 public:
161 HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
162 : mUsage(usage), mIsLocked(false), mLockStart(0), mLockSize(0), mSystemMemory(systemMemory),
163 mUseShadowBuffer(useShadowBuffer), mShadowBuffer(NULL), mShadowUpdated(false),
165 {
166 // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
167 if (useShadowBuffer && usage == HBU_DYNAMIC)
168 {
170 }
171 else if (useShadowBuffer && usage == HBU_STATIC)
172 {
174 }
175 }
176 virtual ~HardwareBuffer() {}
183 virtual void* lock(size_t offset, size_t length, LockOptions options)
184 {
185 assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
186
187 void* ret = NULL;
188 if ((length + offset) > mSizeInBytes)
189 {
191 "Lock request out of bounds.",
192 "HardwareBuffer::lock");
193 }
194 else if (mUseShadowBuffer)
195 {
196 if (options != HBL_READ_ONLY)
197 {
198 // we have to assume a read / write lock so we use the shadow buffer
199 // and tag for sync on unlock()
200 mShadowUpdated = true;
201 }
202
203 ret = mShadowBuffer->lock(offset, length, options);
204 }
205 else
206 {
207 // Lock the real buffer if there is no shadow buffer
208 ret = lockImpl(offset, length, options);
209 mIsLocked = true;
210 }
211 mLockStart = offset;
212 mLockSize = length;
213 return ret;
214 }
215
220 void* lock(LockOptions options)
221 {
222 return this->lock(0, mSizeInBytes, options);
223 }
224
236 virtual void unlock(void)
237 {
238 assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
239
240 // If we used the shadow buffer this time...
241 if (mUseShadowBuffer && mShadowBuffer->isLocked())
242 {
243 mShadowBuffer->unlock();
244 // Potentially update the 'real' buffer from the shadow buffer
246 }
247 else
248 {
249 // Otherwise, unlock the real one
250 unlockImpl();
251 mIsLocked = false;
252 }
253
254 }
255
262 virtual void readData(size_t offset, size_t length, void* pDest) = 0;
271 virtual void writeData(size_t offset, size_t length, const void* pSource,
272 bool discardWholeBuffer = false) = 0;
273
284 virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
285 size_t dstOffset, size_t length, bool discardWholeBuffer = false)
286 {
287 const void *srcData = srcBuffer.lock(
288 srcOffset, length, HBL_READ_ONLY);
289 this->writeData(dstOffset, length, srcData, discardWholeBuffer);
290 srcBuffer.unlock();
291 }
292
298 virtual void copyData(HardwareBuffer& srcBuffer)
299 {
300 size_t sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
301 copyData(srcBuffer, 0, 0, sz, true);
302 }
303
305 virtual void _updateFromShadow(void)
306 {
308 {
309 // Do this manually to avoid locking problems
310 const void *srcData = mShadowBuffer->lockImpl(
312 // Lock with discard if the whole buffer was locked, otherwise normal
313 LockOptions lockOpt;
314 if (mLockStart == 0 && mLockSize == mSizeInBytes)
315 lockOpt = HBL_DISCARD;
316 else
317 lockOpt = HBL_NORMAL;
318
319 void *destData = this->lockImpl(
320 mLockStart, mLockSize, lockOpt);
321 // Copy shadow to real
322 memcpy(destData, srcData, mLockSize);
323 this->unlockImpl();
324 mShadowBuffer->unlockImpl();
325 mShadowUpdated = false;
326 }
327 }
328
330 size_t getSizeInBytes(void) const { return mSizeInBytes; }
332 Usage getUsage(void) const { return mUsage; }
334 bool isSystemMemory(void) const { return mSystemMemory; }
336 bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
338 bool isLocked(void) const {
339 return mIsLocked || (mUseShadowBuffer && mShadowBuffer->isLocked());
340 }
341
342 void suppressHardwareUpdate(bool suppress) {
343 mSuppressHardwareUpdate = suppress;
344 if (!suppress)
346 }
347
348
349
350
351
352 };
353
355
357 template <typename T> struct HardwareBufferLockGuard
358 {
360 : pBuf(p)
361 {
362 pData = pBuf->lock(options);
363 }
364 HardwareBufferLockGuard(const T& p, size_t offset, size_t length, HardwareBuffer::LockOptions options)
365 : pBuf(p)
366 {
367 pData = pBuf->lock(offset, length, options);
368 }
370 {
371 pBuf->unlock();
372 }
373
374 const T& pBuf;
375 void* pData;
376 };
377}
378#endif
379
380
#define _OgreExport
virtual void copyData(HardwareBuffer &srcBuffer)
Copy all data from another buffer into this one.
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.
bool hasShadowBuffer(void) const
Returns whether this buffer has a system memory shadow for quicker reading.
HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer)
Constructor, to be called by HardwareBufferManager only.
virtual void readData(size_t offset, size_t length, void *pDest)=0
Reads data from the buffer and places it in the memory pointed to by pDest.
virtual void unlock(void)
Releases the lock on this buffer.
void suppressHardwareUpdate(bool suppress)
Pass true to suppress hardware upload of shadow buffer changes.
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.
virtual void * lock(size_t offset, size_t length, LockOptions options)
Lock the buffer for (potentially) reading / writing.
bool isLocked(void) const
Returns whether or not this buffer is currently locked.
virtual void unlockImpl(void)=0
Internal implementation of unlock()
virtual void writeData(size_t offset, size_t length, const void *pSource, bool discardWholeBuffer=false)=0
Writes data to the buffer from an area of system memory; note that you must ensure that your buffer i...
size_t getSizeInBytes(void) const
Returns the size of this buffer in bytes.
Usage getUsage(void) const
Returns the Usage flags with which this buffer was created.
HardwareBuffer * mShadowBuffer
virtual void * lockImpl(size_t offset, size_t length, LockOptions options)=0
Internal implementation of lock()
LockOptions
Locking options.
@ HBL_DISCARD
Discards the entire buffer while locking; this allows optimisation to be performed because synchronis...
@ HBL_NO_OVERWRITE
As HBL_DISCARD, except the application guarantees not to overwrite any region of the buffer which has...
@ HBL_READ_ONLY
Lock the buffer for reading only.
@ HBL_NORMAL
Normal mode, ie allows read/write and contents are preserved.
@ HBL_WRITE_ONLY
Lock the buffer for writing only.
void * lock(LockOptions options)
Lock the entire buffer for (potentially) reading / writing.
Usage
Enums describing buffer usage; not mutually exclusive.
@ HBU_DISCARDABLE
Indicates that the application will be refilling the contents of the buffer regularly (not just updat...
@ HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE.
@ HBU_WRITE_ONLY
Indicates the application will never read the contents of the buffer back, it will only ever write da...
@ HBU_STATIC_WRITE_ONLY
Combination of HBU_STATIC and HBU_WRITE_ONLY.
@ HBU_STATIC
Static buffer which the application rarely modifies once created.
@ HBU_DYNAMIC_WRITE_ONLY
Combination of HBU_DYNAMIC and HBU_WRITE_ONLY.
@ HBU_DYNAMIC
Indicates the application would like to modify this buffer with the CPU fairly often.
#define OGRE_EXCEPT(code, desc, src)
RenderSysAllocatedObject BufferAlloc
HardwareBufferLockGuard(const T &p, size_t offset, size_t length, HardwareBuffer::LockOptions options)
HardwareBufferLockGuard(const T &p, HardwareBuffer::LockOptions options)