CmHardwareBuffer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __HardwareBuffer__
  25. #define __HardwareBuffer__
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include "CmCommonEnums.h"
  29. namespace CamelotFramework
  30. {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup RenderSystem
  35. * @{
  36. */
  37. /** Abstract class defining common features of hardware buffers.
  38. @remarks
  39. A 'hardware buffer' is any area of memory held outside of core system ram,
  40. and in our case refers mostly to video ram, although in theory this class
  41. could be used with other memory areas such as sound card memory, custom
  42. coprocessor memory etc.
  43. @par
  44. This reflects the fact that memory held outside of main system RAM must
  45. be interacted with in a more formal fashion in order to promote
  46. cooperative and optimal usage of the buffers between the various
  47. processing units which manipulate them.
  48. @par
  49. This abstract class defines the core interface which is common to all
  50. buffers, whether it be vertex buffers, index buffers, texture memory
  51. or framebuffer memory etc.
  52. @par
  53. Buffers have the ability to be 'shadowed' in system memory, this is because
  54. the kinds of access allowed on hardware buffers is not always as flexible as
  55. that allowed for areas of system memory - for example it is often either
  56. impossible, or extremely undesirable from a performance standpoint to read from
  57. a hardware buffer; when writing to hardware buffers, you should also write every
  58. byte and do it sequentially. In situations where this is too restrictive,
  59. it is possible to create a hardware, write-only buffer (the most efficient kind)
  60. and to back it with a system memory 'shadow' copy which can be read and updated arbitrarily.
  61. Ogre handles synchronising this buffer with the real hardware buffer (which should still be
  62. created with the HBU_DYNAMIC flag if you intend to update it very frequently). Whilst this
  63. approach does have it's own costs, such as increased memory overhead, these costs can
  64. often be outweighed by the performance benefits of using a more hardware efficient buffer.
  65. You should look for the 'useShadowBuffer' parameter on the creation methods used to create
  66. the buffer of the type you require (see HardwareBufferManager) to enable this feature.
  67. */
  68. class CM_EXPORT HardwareBuffer
  69. {
  70. protected:
  71. UINT32 mSizeInBytes;
  72. GpuBufferUsage mUsage;
  73. bool mIsLocked;
  74. UINT32 mLockStart;
  75. UINT32 mLockSize;
  76. bool mSystemMemory;
  77. /// Internal implementation of lock()
  78. virtual void* lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  79. /// Internal implementation of unlock()
  80. virtual void unlockImpl(void) = 0;
  81. public:
  82. /// Constructor, to be called by HardwareBufferManager only
  83. HardwareBuffer(GpuBufferUsage usage, bool systemMemory)
  84. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  85. { }
  86. virtual ~HardwareBuffer() {}
  87. /** Lock the buffer for (potentially) reading / writing.
  88. @param offset The byte offset from the start of the buffer to lock
  89. @param length The size of the area to lock, in bytes
  90. @param options Locking options
  91. @returns Pointer to the locked memory
  92. */
  93. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  94. {
  95. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  96. void* ret = lockImpl(offset, length, options);
  97. mIsLocked = true;
  98. mLockStart = offset;
  99. mLockSize = length;
  100. return ret;
  101. }
  102. /** Lock the entire buffer for (potentially) reading / writing.
  103. @param options Locking options
  104. @returns Pointer to the locked memory
  105. */
  106. void* lock(GpuLockOptions options)
  107. {
  108. return this->lock(0, mSizeInBytes, options);
  109. }
  110. /** Releases the lock on this buffer.
  111. @remarks
  112. Locking and unlocking a buffer can, in some rare circumstances such as
  113. switching video modes whilst the buffer is locked, corrupt the
  114. contents of a buffer. This is pretty rare, but if it occurs,
  115. this method will throw an exception, meaning you
  116. must re-upload the data.
  117. @par
  118. Note that using the 'read' and 'write' forms of updating the buffer does not
  119. suffer from this problem, so if you want to be 100% sure your
  120. data will not be lost, use the 'read' and 'write' forms instead.
  121. */
  122. virtual void unlock(void)
  123. {
  124. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  125. unlockImpl();
  126. mIsLocked = false;
  127. }
  128. /** Reads data from the buffer and places it in the memory pointed to by pDest.
  129. @param offset The byte offset from the start of the buffer to read
  130. @param length The size of the area to read, in bytes
  131. @param pDest The area of memory in which to place the data, must be large enough to
  132. accommodate the data!
  133. */
  134. virtual void readData(UINT32 offset, UINT32 length, void* pDest) = 0;
  135. /** Writes data to the buffer from an area of system memory; note that you must
  136. ensure that your buffer is big enough.
  137. @param offset The byte offset from the start of the buffer to start writing
  138. @param length The size of the data to write to, in bytes
  139. @param pSource The source of the data to be written
  140. @param discardWholeBuffer If true, this allows the driver to discard the entire buffer when writing,
  141. such that stalls can be avoided. Always use when you can.
  142. @param noOverwrite If true, you are guaranteeing to the driver that you will not write to any area of the
  143. buffer the GPU is currently using. This will avoid stalls. Be aware that guaranteeing that
  144. something isn't used on the GPU is problematic.
  145. */
  146. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource,
  147. BufferWriteType writeFlags = BufferWriteType::Normal) = 0;
  148. /** Copy data from another buffer into this one.
  149. @remarks
  150. Note that the source buffer must not be created with the
  151. usage HBU_WRITE_ONLY otherwise this will fail.
  152. @param srcBuffer The buffer from which to read the copied data
  153. @param srcOffset Offset in the source buffer at which to start reading
  154. @param dstOffset Offset in the destination buffer to start writing
  155. @param length Length of the data to copy, in bytes.
  156. @param discardWholeBuffer If true, will discard the entire contents of this buffer before copying
  157. */
  158. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  159. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false)
  160. {
  161. const void *srcData = srcBuffer.lock(
  162. srcOffset, length, GBL_READ_ONLY);
  163. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BufferWriteType::Discard : BufferWriteType::Normal);
  164. srcBuffer.unlock();
  165. }
  166. /** Copy all data from another buffer into this one.
  167. @remarks
  168. Normally these buffers should be of identical size, but if they're
  169. not, the routine will use the smallest of the two sizes.
  170. */
  171. virtual void copyData(HardwareBuffer& srcBuffer)
  172. {
  173. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  174. copyData(srcBuffer, 0, 0, sz, true);
  175. }
  176. /// Returns the size of this buffer in bytes
  177. UINT32 getSizeInBytes(void) const { return mSizeInBytes; }
  178. /// Returns the Usage flags with which this buffer was created
  179. GpuBufferUsage getUsage(void) const { return mUsage; }
  180. /// Returns whether this buffer is held in system memory
  181. bool isSystemMemory(void) const { return mSystemMemory; }
  182. /// Returns whether or not this buffer is currently locked.
  183. bool isLocked(void) const {
  184. return mIsLocked;
  185. }
  186. };
  187. /** @} */
  188. /** @} */
  189. }
  190. #endif