CmHardwareBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. namespace CamelotEngine {
  29. /** \addtogroup Core
  30. * @{
  31. */
  32. /** \addtogroup RenderSystem
  33. * @{
  34. */
  35. /** Abstract class defining common features of hardware buffers.
  36. @remarks
  37. A 'hardware buffer' is any area of memory held outside of core system ram,
  38. and in our case refers mostly to video ram, although in theory this class
  39. could be used with other memory areas such as sound card memory, custom
  40. coprocessor memory etc.
  41. @par
  42. This reflects the fact that memory held outside of main system RAM must
  43. be interacted with in a more formal fashion in order to promote
  44. cooperative and optimal usage of the buffers between the various
  45. processing units which manipulate them.
  46. @par
  47. This abstract class defines the core interface which is common to all
  48. buffers, whether it be vertex buffers, index buffers, texture memory
  49. or framebuffer memory etc.
  50. @par
  51. Buffers have the ability to be 'shadowed' in system memory, this is because
  52. the kinds of access allowed on hardware buffers is not always as flexible as
  53. that allowed for areas of system memory - for example it is often either
  54. impossible, or extremely undesirable from a performance standpoint to read from
  55. a hardware buffer; when writing to hardware buffers, you should also write every
  56. byte and do it sequentially. In situations where this is too restrictive,
  57. it is possible to create a hardware, write-only buffer (the most efficient kind)
  58. and to back it with a system memory 'shadow' copy which can be read and updated arbitrarily.
  59. Ogre handles synchronising this buffer with the real hardware buffer (which should still be
  60. created with the HBU_DYNAMIC flag if you intend to update it very frequently). Whilst this
  61. approach does have it's own costs, such as increased memory overhead, these costs can
  62. often be outweighed by the performance benefits of using a more hardware efficient buffer.
  63. You should look for the 'useShadowBuffer' parameter on the creation methods used to create
  64. the buffer of the type you require (see HardwareBufferManager) to enable this feature.
  65. */
  66. class CM_EXPORT HardwareBuffer
  67. {
  68. public:
  69. /// Enums describing buffer usage; not mutually exclusive
  70. enum Usage
  71. {
  72. /** Static buffer which the application rarely modifies once created. Modifying
  73. the contents of this buffer will involve a performance hit.
  74. */
  75. HBU_STATIC = 1,
  76. /** Indicates the application would like to modify this buffer with the CPU
  77. fairly often.
  78. Buffers created with this flag will typically end up in AGP memory rather
  79. than video memory.
  80. */
  81. HBU_DYNAMIC = 2,
  82. /** Indicates the application will never read the contents of the buffer back,
  83. it will only ever write data. Locking a buffer with this flag will ALWAYS
  84. return a pointer to new, blank memory rather than the memory associated
  85. with the contents of the buffer; this avoids DMA stalls because you can
  86. write to a new memory area while the previous one is being used.
  87. */
  88. HBU_WRITE_ONLY = 4,
  89. /** Indicates that the application will be refilling the contents
  90. of the buffer regularly (not just updating, but generating the
  91. contents from scratch), and therefore does not mind if the contents
  92. of the buffer are lost somehow and need to be recreated. This
  93. allows and additional level of optimisation on the buffer.
  94. This option only really makes sense when combined with
  95. HBU_DYNAMIC_WRITE_ONLY.
  96. */
  97. HBU_DISCARDABLE = 8,
  98. /// Combination of HBU_STATIC and HBU_WRITE_ONLY
  99. HBU_STATIC_WRITE_ONLY = 5,
  100. /** Combination of HBU_DYNAMIC and HBU_WRITE_ONLY. If you use
  101. this, strongly consider using HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
  102. instead if you update the entire contents of the buffer very
  103. regularly.
  104. */
  105. HBU_DYNAMIC_WRITE_ONLY = 6,
  106. /// Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE
  107. HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
  108. };
  109. /// Locking options
  110. enum LockOptions
  111. {
  112. /** Normal mode, ie allows read/write and contents are preserved. */
  113. HBL_NORMAL,
  114. /** Discards the <em>entire</em> buffer while locking; this allows optimisation to be
  115. performed because synchronisation issues are relaxed. Only allowed on buffers
  116. created with the HBU_DYNAMIC flag.
  117. */
  118. HBL_DISCARD,
  119. /** Lock the buffer for reading only. Not allowed in buffers which are created with HBU_WRITE_ONLY.
  120. Mandatory on static buffers, i.e. those created without the HBU_DYNAMIC flag.
  121. */
  122. HBL_READ_ONLY,
  123. /** As HBL_NORMAL, except the application guarantees not to overwrite any
  124. region of the buffer which has already been used in this frame, can allow
  125. some optimisation on some APIs. */
  126. HBL_NO_OVERWRITE
  127. };
  128. protected:
  129. UINT32 mSizeInBytes;
  130. Usage mUsage;
  131. bool mIsLocked;
  132. UINT32 mLockStart;
  133. UINT32 mLockSize;
  134. bool mSystemMemory;
  135. /// Internal implementation of lock()
  136. virtual void* lockImpl(UINT32 offset, UINT32 length, LockOptions options) = 0;
  137. /// Internal implementation of unlock()
  138. virtual void unlockImpl(void) = 0;
  139. public:
  140. /// Constructor, to be called by HardwareBufferManager only
  141. HardwareBuffer(Usage usage, bool systemMemory)
  142. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  143. { }
  144. virtual ~HardwareBuffer() {}
  145. /** Lock the buffer for (potentially) reading / writing.
  146. @param offset The byte offset from the start of the buffer to lock
  147. @param length The size of the area to lock, in bytes
  148. @param options Locking options
  149. @returns Pointer to the locked memory
  150. */
  151. virtual void* lock(UINT32 offset, UINT32 length, LockOptions options)
  152. {
  153. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  154. void* ret = lockImpl(offset, length, options);
  155. mIsLocked = true;
  156. mLockStart = offset;
  157. mLockSize = length;
  158. return ret;
  159. }
  160. /** Lock the entire buffer for (potentially) reading / writing.
  161. @param options Locking options
  162. @returns Pointer to the locked memory
  163. */
  164. void* lock(LockOptions options)
  165. {
  166. return this->lock(0, mSizeInBytes, options);
  167. }
  168. /** Releases the lock on this buffer.
  169. @remarks
  170. Locking and unlocking a buffer can, in some rare circumstances such as
  171. switching video modes whilst the buffer is locked, corrupt the
  172. contents of a buffer. This is pretty rare, but if it occurs,
  173. this method will throw an exception, meaning you
  174. must re-upload the data.
  175. @par
  176. Note that using the 'read' and 'write' forms of updating the buffer does not
  177. suffer from this problem, so if you want to be 100% sure your
  178. data will not be lost, use the 'read' and 'write' forms instead.
  179. */
  180. virtual void unlock(void)
  181. {
  182. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  183. unlockImpl();
  184. mIsLocked = false;
  185. }
  186. /** Reads data from the buffer and places it in the memory pointed to by pDest.
  187. @param offset The byte offset from the start of the buffer to read
  188. @param length The size of the area to read, in bytes
  189. @param pDest The area of memory in which to place the data, must be large enough to
  190. accommodate the data!
  191. */
  192. virtual void readData(UINT32 offset, UINT32 length, void* pDest) = 0;
  193. /** Writes data to the buffer from an area of system memory; note that you must
  194. ensure that your buffer is big enough.
  195. @param offset The byte offset from the start of the buffer to start writing
  196. @param length The size of the data to write to, in bytes
  197. @param pSource The source of the data to be written
  198. @param discardWholeBuffer If true, this allows the driver to discard the entire buffer when writing,
  199. such that DMA stalls can be avoided; use if you can.
  200. */
  201. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource,
  202. bool discardWholeBuffer = false) = 0;
  203. /** Copy data from another buffer into this one.
  204. @remarks
  205. Note that the source buffer must not be created with the
  206. usage HBU_WRITE_ONLY otherwise this will fail.
  207. @param srcBuffer The buffer from which to read the copied data
  208. @param srcOffset Offset in the source buffer at which to start reading
  209. @param dstOffset Offset in the destination buffer to start writing
  210. @param length Length of the data to copy, in bytes.
  211. @param discardWholeBuffer If true, will discard the entire contents of this buffer before copying
  212. */
  213. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  214. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false)
  215. {
  216. const void *srcData = srcBuffer.lock(
  217. srcOffset, length, HBL_READ_ONLY);
  218. this->writeData(dstOffset, length, srcData, discardWholeBuffer);
  219. srcBuffer.unlock();
  220. }
  221. /** Copy all data from another buffer into this one.
  222. @remarks
  223. Normally these buffers should be of identical size, but if they're
  224. not, the routine will use the smallest of the two sizes.
  225. */
  226. virtual void copyData(HardwareBuffer& srcBuffer)
  227. {
  228. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  229. copyData(srcBuffer, 0, 0, sz, true);
  230. }
  231. /// Returns the size of this buffer in bytes
  232. UINT32 getSizeInBytes(void) const { return mSizeInBytes; }
  233. /// Returns the Usage flags with which this buffer was created
  234. Usage getUsage(void) const { return mUsage; }
  235. /// Returns whether this buffer is held in system memory
  236. bool isSystemMemory(void) const { return mSystemMemory; }
  237. /// Returns whether or not this buffer is currently locked.
  238. bool isLocked(void) const {
  239. return mIsLocked;
  240. }
  241. };
  242. /** @} */
  243. /** @} */
  244. }
  245. #endif