OGRE  1.12.13
Object-Oriented Graphics Rendering Engine
Ogre::HardwareBuffer Class Reference

Abstract class defining common features of hardware buffers. More...

#include <OgreHardwareBuffer.h>

+ Inheritance diagram for Ogre::HardwareBuffer:

Public Types

enum  LockOptions {
  HBL_NORMAL, HBL_DISCARD, HBL_READ_ONLY, HBL_NO_OVERWRITE,
  HBL_WRITE_ONLY
}
 Locking options. More...
 
typedef int Usage
 
enum  UsageEnum {
  HBU_STATIC = HBU_GPU_TO_CPU, HBU_DYNAMIC = HBU_CPU_ONLY, HBU_WRITE_ONLY = HBU_DETAIL_WRITE_ONLY, HBU_DISCARDABLE = 8,
  HBU_STATIC_WRITE_ONLY = HBU_GPU_ONLY, HBU_DYNAMIC_WRITE_ONLY = HBU_CPU_TO_GPU, HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = HBU_CPU_TO_GPU
}
 Rather use HardwareBufferUsage. More...
 

Public Member Functions

 HardwareBuffer (Usage usage, bool systemMemory, bool useShadowBuffer)
 Constructor, to be called by HardwareBufferManager only. More...
 
virtual ~HardwareBuffer ()
 
template<typename T >
T * _getImpl ()
 
virtual void _updateFromShadow (void)
 Updates the real buffer from the shadow buffer, if required. More...
 
void copyData (HardwareBuffer &srcBuffer)
 Copy all data from another buffer into this one. More...
 
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. More...
 
size_t getSizeInBytes (void) const
 Returns the size of this buffer in bytes. More...
 
Usage getUsage (void) const
 Returns the Usage flags with which this buffer was created. More...
 
bool hasShadowBuffer (void) const
 Returns whether this buffer has a system memory shadow for quicker reading. More...
 
bool isLocked (void) const
 Returns whether or not this buffer is currently locked. More...
 
bool isSystemMemory (void) const
 Returns whether this buffer is held in system memory. More...
 
void * lock (LockOptions options)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
virtual void * lock (size_t offset, size_t length, LockOptions options)
 Lock the buffer for (potentially) reading / writing. More...
 
virtual void readData (size_t offset, size_t length, void *pDest)
 Reads data from the buffer and places it in the memory pointed to by pDest. More...
 
void suppressHardwareUpdate (bool suppress)
 Pass true to suppress hardware upload of shadow buffer changes. More...
 
void unlock (void)
 Releases the lock on this buffer. More...
 
virtual void writeData (size_t offset, size_t length, const void *pSource, bool discardWholeBuffer=false)
 Writes data to the buffer from an area of system memory; note that you must ensure that your buffer is big enough. More...
 

Detailed Description

Abstract class defining common features of hardware buffers.

Remarks
A 'hardware buffer' is any area of memory held outside of core system ram, and in our case refers mostly to video ram, although in theory this class could be used with other memory areas such as sound card memory, custom coprocessor memory etc.
This reflects the fact that memory held outside of main system RAM must be interacted with in a more formal fashion in order to promote cooperative and optimal usage of the buffers between the various processing units which manipulate them.
This abstract class defines the core interface which is common to all buffers, whether it be vertex buffers, index buffers, texture memory or framebuffer memory etc.
Buffers have the ability to be 'shadowed' in system memory, this is because the kinds of access allowed on hardware buffers is not always as flexible as that allowed for areas of system memory - for example it is often either impossible, or extremely undesirable from a performance standpoint to read from a hardware buffer; when writing to hardware buffers, you should also write every byte and do it sequentially. In situations where this is too restrictive, it is possible to create a hardware, write-only buffer (the most efficient kind) and to back it with a system memory 'shadow' copy which can be read and updated arbitrarily. Ogre handles synchronising this buffer with the real hardware buffer (which should still be created with the HBU_DYNAMIC flag if you intend to update it very frequently). Whilst this approach does have its own costs, such as increased memory overhead, these costs can often be outweighed by the performance benefits of using a more hardware efficient buffer. You should look for the 'useShadowBuffer' parameter on the creation methods used to create the buffer of the type you require (see HardwareBufferManager) to enable this feature.

Member Typedef Documentation

◆ Usage

Member Enumeration Documentation

◆ UsageEnum

Rather use HardwareBufferUsage.

Enumerator
HBU_STATIC 

same as HBU_GPU_TO_CPU

HBU_DYNAMIC 

same as HBU_CPU_ONLY

HBU_WRITE_ONLY 
Deprecated:
use HBU_DETAIL_WRITE_ONLY
HBU_DISCARDABLE 
Deprecated:
do not use
HBU_STATIC_WRITE_ONLY 

same as HBU_GPU_ONLY

HBU_DYNAMIC_WRITE_ONLY 

same as HBU_CPU_TO_GPU

HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE 
Deprecated:
do not use

◆ LockOptions

Locking options.

Enumerator
HBL_NORMAL 

Normal mode, ie allows read/write and contents are preserved.

This kind of lock allows reading and writing from the buffer - it’s also the least optimal because basically you’re telling the card you could be doing anything at all. If you’re not using a shadow buffer, it requires the buffer to be transferred from the card and back again. If you’re using a shadow buffer the effect is minimal.

HBL_DISCARD 

Discards the entire buffer while locking.

This means you are happy for the card to discard the entire current contents of the buffer. Implicitly this means you are not going to read the data - it also means that the card can avoid any stalls if the buffer is currently being rendered from, because it will actually give you an entirely different one. Use this wherever possible when you are locking a buffer which was not created with a shadow buffer. If you are using a shadow buffer it matters less, although with a shadow buffer it’s preferable to lock the entire buffer at once, because that allows the shadow buffer to use HBL_DISCARD when it uploads the updated contents to the real buffer.

Note
Only useful on buffers created with the HBU_CPU_TO_GPU flag.
HBL_READ_ONLY 

Lock the buffer for reading only.

Not allowed in buffers which are created with HBU_WRITE_ONLY. Mandatory on static buffers, i.e. those created without the HBU_DYNAMIC flag.

HBL_NO_OVERWRITE 

As HBL_DISCARD, except the application guarantees not to overwrite any region of the buffer which has already been used in this frame, can allow some optimisation on some APIs.

Note
Only useful on buffers with no shadow buffer.
HBL_WRITE_ONLY 

Lock the buffer for writing only.

Constructor & Destructor Documentation

◆ HardwareBuffer()

Ogre::HardwareBuffer::HardwareBuffer ( Usage  usage,
bool  systemMemory,
bool  useShadowBuffer 
)
inline

◆ ~HardwareBuffer()

virtual Ogre::HardwareBuffer::~HardwareBuffer ( )
inlinevirtual

Member Function Documentation

◆ lock() [1/2]

virtual void* Ogre::HardwareBuffer::lock ( size_t  offset,
size_t  length,
LockOptions  options 
)
inlinevirtual

Lock the buffer for (potentially) reading / writing.

Parameters
offsetThe byte offset from the start of the buffer to lock
lengthThe size of the area to lock, in bytes
optionsLocking options
Returns
Pointer to the locked memory

References OgreAssert.

Referenced by copyData(), and Ogre::HardwareBufferLockGuard::lock().

◆ lock() [2/2]

void* Ogre::HardwareBuffer::lock ( LockOptions  options)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ unlock()

void Ogre::HardwareBuffer::unlock ( void  )
inline

Releases the lock on this buffer.

Remarks
Locking and unlocking a buffer can, in some rare circumstances such as switching video modes whilst the buffer is locked, corrupt the contents of a buffer. This is pretty rare, but if it occurs, this method will throw an exception, meaning you must re-upload the data.
Note that using the 'read' and 'write' forms of updating the buffer does not suffer from this problem, so if you want to be 100% sure your data will not be lost, use the 'read' and 'write' forms instead.

References OgreAssert.

Referenced by copyData(), and Ogre::HardwareBufferLockGuard::unlock().

◆ readData()

virtual void Ogre::HardwareBuffer::readData ( size_t  offset,
size_t  length,
void *  pDest 
)
inlinevirtual

Reads data from the buffer and places it in the memory pointed to by pDest.

Parameters
offsetThe byte offset from the start of the buffer to read
lengthThe size of the area to read, in bytes
pDestThe area of memory in which to place the data, must be large enough to accommodate the data!

Reimplemented in Ogre::MetalHardwareBufferCommon, Ogre::D3D11HardwareBuffer, Ogre::GLES2HardwareBuffer, Ogre::DefaultHardwareBuffer, Ogre::GL3PlusHardwareBuffer, Ogre::HardwarePixelBuffer, Ogre::GLHardwareVertexBuffer, and Ogre::D3D9HardwareBuffer.

◆ writeData()

virtual void Ogre::HardwareBuffer::writeData ( size_t  offset,
size_t  length,
const void *  pSource,
bool  discardWholeBuffer = false 
)
inlinevirtual

Writes data to the buffer from an area of system memory; note that you must ensure that your buffer is big enough.

Parameters
offsetThe byte offset from the start of the buffer to start writing
lengthThe size of the data to write to, in bytes
pSourceThe source of the data to be written
discardWholeBufferIf true, this allows the driver to discard the entire buffer when writing, such that DMA stalls can be avoided; use if you can.

Reimplemented in Ogre::MetalHardwareBufferCommon, Ogre::D3D11HardwareBuffer, Ogre::GLES2HardwareBuffer, Ogre::DefaultHardwareBuffer, Ogre::HardwarePixelBuffer, Ogre::GLHardwareVertexBuffer, Ogre::D3D9HardwareBuffer, and Ogre::GL3PlusHardwareBuffer.

◆ copyData() [1/2]

virtual void Ogre::HardwareBuffer::copyData ( HardwareBuffer srcBuffer,
size_t  srcOffset,
size_t  dstOffset,
size_t  length,
bool  discardWholeBuffer = false 
)
inlinevirtual

Copy data from another buffer into this one.

Remarks
Note that the source buffer must not be created with the usage HBU_WRITE_ONLY otherwise this will fail.
Parameters
srcBufferThe buffer from which to read the copied data
srcOffsetOffset in the source buffer at which to start reading
dstOffsetOffset in the destination buffer to start writing
lengthLength of the data to copy, in bytes.
discardWholeBufferIf true, will discard the entire contents of this buffer before copying

Reimplemented in Ogre::MetalHardwareBufferCommon, Ogre::D3D11HardwareBuffer, Ogre::GLES2HardwareBuffer, and Ogre::GL3PlusHardwareBuffer.

References isSystemMemory(), lock(), and unlock().

◆ copyData() [2/2]

void Ogre::HardwareBuffer::copyData ( HardwareBuffer srcBuffer)
inline

Copy all data from another buffer into this one.

Remarks
Normally these buffers should be of identical size, but if they're not, the routine will use the smallest of the two sizes.

References getSizeInBytes().

◆ _updateFromShadow()

virtual void Ogre::HardwareBuffer::_updateFromShadow ( void  )
inlinevirtual

Updates the real buffer from the shadow buffer, if required.

Reimplemented in Ogre::MetalHardwareBufferCommon, Ogre::D3D11HardwareBuffer, Ogre::GLHardwareVertexBuffer, Ogre::GLES2HardwareBuffer, and Ogre::GL3PlusHardwareBuffer.

◆ getSizeInBytes()

size_t Ogre::HardwareBuffer::getSizeInBytes ( void  ) const
inline

Returns the size of this buffer in bytes.

Referenced by copyData().

◆ getUsage()

Usage Ogre::HardwareBuffer::getUsage ( void  ) const
inline

Returns the Usage flags with which this buffer was created.

◆ isSystemMemory()

bool Ogre::HardwareBuffer::isSystemMemory ( void  ) const
inline

Returns whether this buffer is held in system memory.

Referenced by copyData().

◆ hasShadowBuffer()

bool Ogre::HardwareBuffer::hasShadowBuffer ( void  ) const
inline

Returns whether this buffer has a system memory shadow for quicker reading.

◆ isLocked()

bool Ogre::HardwareBuffer::isLocked ( void  ) const
inline

Returns whether or not this buffer is currently locked.

◆ suppressHardwareUpdate()

void Ogre::HardwareBuffer::suppressHardwareUpdate ( bool  suppress)
inline

Pass true to suppress hardware upload of shadow buffer changes.

◆ _getImpl()

template<typename T >
T* Ogre::HardwareBuffer::_getImpl ( )
inline

The documentation for this class was generated from the following file: