CmPixelBuffer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 CamelotFramework {
  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 PixelBuffer : 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, GpuLockOptions 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, GpuLockOptions options);
  61. friend class RenderTexture;
  62. public:
  63. /// Should be called by HardwareBufferManager
  64. PixelBuffer(UINT32 mWidth, UINT32 mHeight, UINT32 mDepth,
  65. PixelFormat mFormat, GpuBufferUsage usage, bool useSystemMemory);
  66. ~PixelBuffer();
  67. /** make every lock method from HardwareBuffer available.
  68. See http://www.research.att.com/~bs/bs_faq2.html#overloadderived
  69. */
  70. using HardwareBuffer::lock;
  71. /** Lock the buffer for (potentially) reading / writing.
  72. @param lockBox Region of the buffer to lock
  73. @param options Locking options
  74. @returns PixelBox containing the locked region, the pitches and
  75. the pixel format
  76. */
  77. virtual const PixelData& lock(const Box& lockBox, GpuLockOptions options);
  78. /// @copydoc HardwareBuffer::lock
  79. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options);
  80. /** Get the current locked region. This is the same value as returned
  81. by lock(const Image::Box, LockOptions)
  82. @returns PixelBox containing the locked region
  83. */
  84. const PixelData& getCurrentLock();
  85. /// @copydoc HardwareBuffer::readData
  86. virtual void readData(UINT32 offset, UINT32 length, void* pDest);
  87. /// @copydoc HardwareBuffer::writeData
  88. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource,
  89. bool discardWholeBuffer = false);
  90. /** Copies a box from another PixelBuffer to a region of the
  91. this PixelBuffer.
  92. @param dst Source pixel buffer
  93. @param srcBox Image::Box describing the source region in src
  94. @param dstBox Image::Box describing the destination region in this buffer
  95. @remarks The source and destination regions dimensions don't have to match, in which
  96. case scaling is done. This scaling is generally done using a bilinear filter in hardware,
  97. but it is faster to pass the source image in the right dimensions.
  98. @note Only call this function when both buffers are unlocked.
  99. */
  100. virtual void blit(const PixelBufferPtr &src, const Box &srcBox, const Box &dstBox);
  101. /** Convenience function that blits the entire source pixel buffer to this buffer.
  102. If source and destination dimensions don't match, scaling is done.
  103. @param src PixelBox containing the source pixels and format in memory
  104. @note Only call this function when the buffer is unlocked.
  105. */
  106. void blit(const PixelBufferPtr &src);
  107. /** Copies a region from normal memory to a region of this pixelbuffer. The source
  108. image can be in any pixel format supported by OGRE, and in any size.
  109. @param src PixelBox containing the source pixels and format in memory
  110. @param dstBox Image::Box describing the destination region in this buffer
  111. @remarks The source and destination regions dimensions don't have to match, in which
  112. case scaling is done. This scaling is generally done using a bilinear filter in hardware,
  113. but it is faster to pass the source image in the right dimensions.
  114. @note Only call this function when the buffer is unlocked.
  115. */
  116. virtual void blitFromMemory(const PixelData &src, const Box &dstBox) = 0;
  117. /** Convenience function that blits a pixelbox from memory to the entire
  118. buffer. The source image is scaled as needed.
  119. @param src PixelBox containing the source pixels and format in memory
  120. @note Only call this function when the buffer is unlocked.
  121. */
  122. void blitFromMemory(const PixelData &src)
  123. {
  124. blitFromMemory(src, Box(0,0,0,mWidth,mHeight,mDepth));
  125. }
  126. /** Copies a region of this pixelbuffer to normal memory.
  127. @param srcBox Image::Box describing the source region of this buffer
  128. @param dst PixelBox describing the destination pixels and format in memory
  129. @remarks The source and destination regions don't have to match, in which
  130. case scaling is done.
  131. @note Only call this function when the buffer is unlocked.
  132. */
  133. virtual void blitToMemory(const Box &srcBox, const PixelData &dst) = 0;
  134. /** Convience function that blits this entire buffer to a pixelbox.
  135. The image is scaled as needed.
  136. @param src PixelBox containing the source pixels and format in memory
  137. @note Only call this function when the buffer is unlocked.
  138. */
  139. void blitToMemory(const PixelData &dst)
  140. {
  141. blitToMemory(Box(0,0,0,mWidth,mHeight,mDepth), dst);
  142. }
  143. /// Gets the width of this buffer
  144. UINT32 getWidth() const { return mWidth; }
  145. /// Gets the height of this buffer
  146. UINT32 getHeight() const { return mHeight; }
  147. /// Gets the depth of this buffer
  148. UINT32 getDepth() const { return mDepth; }
  149. /// Gets the native pixel format of this buffer
  150. PixelFormat getFormat() const { return mFormat; }
  151. };
  152. /** @} */
  153. }
  154. #endif