CmHardwarePixelBuffer.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 __HardwarePixelBuffer__
  25. #define __HardwarePixelBuffer__
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include "CmHardwareBuffer.h"
  29. #include "CmPixelUtil.h"
  30. namespace CamelotEngine {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup RenderSystem
  35. * @{
  36. */
  37. /** Specialisation of HardwareBuffer for a pixel buffer. The
  38. HardwarePixelbuffer abstracts an 1D, 2D or 3D quantity of pixels
  39. stored by the rendering API. The buffer can be located on the card
  40. or in main memory depending on its usage. One mipmap level of a
  41. texture is an example of a HardwarePixelBuffer.
  42. */
  43. class CM_EXPORT HardwarePixelBuffer : public HardwareBuffer
  44. {
  45. protected:
  46. // Extents
  47. UINT32 mWidth, mHeight, mDepth;
  48. // Pitches (offsets between rows and slices)
  49. UINT32 mRowPitch, mSlicePitch;
  50. // Internal format
  51. PixelFormat mFormat;
  52. // Currently locked region (local coords)
  53. PixelData mCurrentLock;
  54. // The current locked box of this surface (entire surface coords)
  55. Box mLockedBox;
  56. /// Internal implementation of lock(), must be overridden in subclasses
  57. virtual PixelData lockImpl(const Box lockBox, LockOptions options) = 0;
  58. /// Internal implementation of lock(), do not OVERRIDE or CALL this
  59. /// for HardwarePixelBuffer implementations, but override the previous method
  60. virtual void* lockImpl(UINT32 offset, UINT32 length, LockOptions options);
  61. /// Internal implementation of unlock(), must be overridden in subclasses
  62. // virtual void unlockImpl(void) = 0;
  63. /** Notify TextureBuffer of destruction of render target.
  64. Called by RenderTexture when destroyed.
  65. */
  66. virtual void _clearSliceRTT(UINT32 zoffset);
  67. friend class RenderTexture;
  68. public:
  69. /// Should be called by HardwareBufferManager
  70. HardwarePixelBuffer(UINT32 mWidth, UINT32 mHeight, UINT32 mDepth,
  71. PixelFormat mFormat,
  72. HardwareBuffer::Usage usage, bool useSystemMemory);
  73. ~HardwarePixelBuffer();
  74. /** make every lock method from HardwareBuffer available.
  75. See http://www.research.att.com/~bs/bs_faq2.html#overloadderived
  76. */
  77. using HardwareBuffer::lock;
  78. /** Lock the buffer for (potentially) reading / writing.
  79. @param lockBox Region of the buffer to lock
  80. @param options Locking options
  81. @returns PixelBox containing the locked region, the pitches and
  82. the pixel format
  83. */
  84. virtual const PixelData& lock(const Box& lockBox, LockOptions options);
  85. /// @copydoc HardwareBuffer::lock
  86. virtual void* lock(UINT32 offset, UINT32 length, LockOptions options);
  87. /** Get the current locked region. This is the same value as returned
  88. by lock(const Image::Box, LockOptions)
  89. @returns PixelBox containing the locked region
  90. */
  91. const PixelData& getCurrentLock();
  92. /// @copydoc HardwareBuffer::readData
  93. virtual void readData(UINT32 offset, UINT32 length, void* pDest);
  94. /// @copydoc HardwareBuffer::writeData
  95. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource,
  96. bool discardWholeBuffer = false);
  97. /** Copies a box from another PixelBuffer to a region of the
  98. this PixelBuffer.
  99. @param dst Source pixel buffer
  100. @param srcBox Image::Box describing the source region in src
  101. @param dstBox Image::Box describing the destination region in this buffer
  102. @remarks The source and destination regions dimensions don't have to match, in which
  103. case scaling is done. This scaling is generally done using a bilinear filter in hardware,
  104. but it is faster to pass the source image in the right dimensions.
  105. @note Only call this function when both buffers are unlocked.
  106. */
  107. virtual void blit(const HardwarePixelBufferPtr &src, const Box &srcBox, const Box &dstBox);
  108. /** Convenience function that blits the entire source pixel buffer to this buffer.
  109. If source and destination dimensions don't match, scaling is done.
  110. @param src PixelBox containing the source pixels and format in memory
  111. @note Only call this function when the buffer is unlocked.
  112. */
  113. void blit(const HardwarePixelBufferPtr &src);
  114. /** Copies a region from normal memory to a region of this pixelbuffer. The source
  115. image can be in any pixel format supported by OGRE, and in any size.
  116. @param src PixelBox containing the source pixels and format in memory
  117. @param dstBox Image::Box describing the destination region in this buffer
  118. @remarks The source and destination regions dimensions don't have to match, in which
  119. case scaling is done. This scaling is generally done using a bilinear filter in hardware,
  120. but it is faster to pass the source image in the right dimensions.
  121. @note Only call this function when the buffer is unlocked.
  122. */
  123. virtual void blitFromMemory(const PixelData &src, const Box &dstBox) = 0;
  124. /** Convenience function that blits a pixelbox from memory to the entire
  125. buffer. The source image is scaled as needed.
  126. @param src PixelBox containing the source pixels and format in memory
  127. @note Only call this function when the buffer is unlocked.
  128. */
  129. void blitFromMemory(const PixelData &src)
  130. {
  131. blitFromMemory(src, Box(0,0,0,mWidth,mHeight,mDepth));
  132. }
  133. /** Copies a region of this pixelbuffer to normal memory.
  134. @param srcBox Image::Box describing the source region of this buffer
  135. @param dst PixelBox describing the destination pixels and format in memory
  136. @remarks The source and destination regions don't have to match, in which
  137. case scaling is done.
  138. @note Only call this function when the buffer is unlocked.
  139. */
  140. virtual void blitToMemory(const Box &srcBox, const PixelData &dst) = 0;
  141. /** Convience function that blits this entire buffer to a pixelbox.
  142. The image is scaled as needed.
  143. @param src PixelBox containing the source pixels and format in memory
  144. @note Only call this function when the buffer is unlocked.
  145. */
  146. void blitToMemory(const PixelData &dst)
  147. {
  148. blitToMemory(Box(0,0,0,mWidth,mHeight,mDepth), dst);
  149. }
  150. /** Get a render target for this PixelBuffer, or a slice of it. The texture this
  151. was acquired from must have TU_RENDERTARGET set, otherwise it is possible to
  152. render to it and this method will throw an ERR_RENDERSYSTEM exception.
  153. @param slice Which slice
  154. @returns A pointer to the render target. This pointer has the lifespan of this
  155. PixelBuffer.
  156. */
  157. virtual RenderTexture *getRenderTarget(UINT32 slice=0);
  158. /// Gets the width of this buffer
  159. UINT32 getWidth() const { return mWidth; }
  160. /// Gets the height of this buffer
  161. UINT32 getHeight() const { return mHeight; }
  162. /// Gets the depth of this buffer
  163. UINT32 getDepth() const { return mDepth; }
  164. /// Gets the native pixel format of this buffer
  165. PixelFormat getFormat() const { return mFormat; }
  166. };
  167. /** @} */
  168. }
  169. #endif