BsGLPixelBuffer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsGLPrerequisites.h"
  5. #include "BsHardwareBuffer.h"
  6. #include "BsPixelUtil.h"
  7. namespace bs
  8. {
  9. /** @addtogroup GL
  10. * @{
  11. */
  12. class GLTextureBuffer;
  13. /**
  14. * Represents a hardware buffer that stores a single pixel surface. This may be a 1D, 2D or 3D surface, but unlike a
  15. * texture it consists only of a single surface (no mip maps, cube map faces or similar).
  16. */
  17. class GLPixelBuffer
  18. {
  19. public:
  20. /**
  21. * Constructs a new pixel buffer with the provided settings.
  22. *
  23. * @param[in] width Width of the pixel buffer in pixels.
  24. * @param[in] height Height of the pixel buffer in pixels.
  25. * @param[in] depth Depth of the pixel buffer in pixels (number of 2D slices).
  26. * @param[in] format Format of each pixel in the buffer.
  27. * @param[in] usage Usage signaling the render system how we plan on using the buffer.
  28. */
  29. GLPixelBuffer(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format, GpuBufferUsage usage);
  30. virtual ~GLPixelBuffer();
  31. /** Returns width of the surface in pixels. */
  32. UINT32 getWidth() const { return mWidth; }
  33. /** Returns height of the surface in pixels. */
  34. UINT32 getHeight() const { return mHeight; }
  35. /** Returns depth of the surface in pixels. */
  36. UINT32 getDepth() const { return mDepth; }
  37. /** Returns format of the pixels in the surface. */
  38. PixelFormat getFormat() const { return mFormat; }
  39. /**
  40. * Locks a certain region of the pixel buffer for reading and returns a pointer to the locked region.
  41. *
  42. * @param[in] lockBox Region of the surface to lock.
  43. * @param[in] options Lock options that hint the hardware on what you intend to do with the locked data.
  44. *
  45. * @note Returned object is only valid while the lock is active.
  46. */
  47. const PixelData& lock(const PixelVolume& lockBox, GpuLockOptions options);
  48. /**
  49. * Locks a portion of the buffer and returns pointer to the locked area. You must call unlock() when done.
  50. *
  51. * @param[in] offset Offset in bytes from which to lock the buffer.
  52. * @param[in] length Length of the area you want to lock, in bytes.
  53. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  54. * anything he hasn't requested (for example don't try to read from the buffer unless you
  55. * requested it here).
  56. */
  57. void* lock(UINT32 offset, UINT32 length, GpuLockOptions options);
  58. /**
  59. * Locks the entire buffer and returns pointer to the locked area. You must call unlock() when done.
  60. *
  61. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  62. * anything he hasn't requested (for example don't try to read from the buffer unless you
  63. * requested it here).
  64. */
  65. void* lock(GpuLockOptions options)
  66. {
  67. return lock(0, mSizeInBytes, options);
  68. }
  69. /** Releases the lock on this buffer. */
  70. void unlock();
  71. /**
  72. * Upload some pixel data to the buffer.
  73. *
  74. * @param[in] data Data to upload.
  75. * @param[in] dest Coordinates to which to upload the data.
  76. */
  77. virtual void upload(const PixelData& data, const PixelVolume& dest);
  78. /**
  79. * Reads data from the pixel buffer into the provided object. Caller must ensure the data object is of adequate
  80. * size.
  81. */
  82. virtual void download(const PixelData& data);
  83. /**
  84. * Binds the buffers to a frame buffer object at the specified attachment point.
  85. *
  86. * @param[in] attachment Attachment point index in the range [0, BS_MAX_MULTIPLE_RENDER_TARGETS).
  87. * @param[in] zoffset Depth slice to bind, in the case of a 3D texture.
  88. * @param[in] allLayers Should all layers of the texture be bound, or just one (zoffset is ignored if true).
  89. */
  90. virtual void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers);
  91. /** Returns internal OpenGL pixel format used by the buffer. */
  92. GLenum getGLFormat() const { return mGLInternalFormat; }
  93. /**
  94. * Blits the contents of the provided buffer into this pixel buffer. Data is bilinearily interpolated in case buffer
  95. * sizes don't match.
  96. */
  97. virtual void blitFromTexture(GLTextureBuffer* src);
  98. /**
  99. * Blits contents of a sub-region of the provided buffer into a sub-region of this pixel buffer. Data is bilinearily
  100. * interpolated in case source and destination sizes don't match.
  101. */
  102. virtual void blitFromTexture(GLTextureBuffer* src, const PixelVolume& srcBox, const PixelVolume& dstBox);
  103. protected:
  104. /** Allocates an internal buffer on the CPU, the size of the hardware buffer. */
  105. void allocateBuffer();
  106. /** Deallocates the internal CPU buffer. */
  107. void freeBuffer();
  108. protected:
  109. UINT32 mSizeInBytes;
  110. GpuBufferUsage mUsage;
  111. bool mIsLocked;
  112. UINT32 mWidth, mHeight, mDepth;
  113. PixelFormat mFormat;
  114. PixelData mCurrentLock;
  115. PixelVolume mLockedBox;
  116. PixelData mBuffer;
  117. GLenum mGLInternalFormat;
  118. GpuLockOptions mCurrentLockOptions;
  119. };
  120. /** Pixel buffer specialization that represents a single surface in a texture. */
  121. class GLTextureBuffer : public GLPixelBuffer
  122. {
  123. public:
  124. /**
  125. * Constructs a new texture buffer from a specific surface in the provided texture.
  126. *
  127. * @param[in] target OpenGL type of the texture to retrieve the surface from.
  128. * @param[in] id OpenGL handle to the texture to retrieve the surface from.
  129. * @param[in] face Face index of the texture in the case of cube textures or texture arrays.
  130. * @param[in] level Mip level of the texture.
  131. * @param[in] usage Usage signaling the render system how we plan on using the buffer.
  132. * @param[in] writeGamma True if the parent texture was created with SRGB support.
  133. * @param[in] multisampleCount Number of samples the parent texture was created with.
  134. */
  135. GLTextureBuffer(GLenum target, GLuint id, GLint face,
  136. GLint level, GpuBufferUsage usage, bool writeGamma, UINT32 multisampleCount);
  137. ~GLTextureBuffer();
  138. /** @copydoc GLPixelBuffer::bindToFramebuffer */
  139. void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers) override;
  140. /** @copydoc GLPixelBuffer::upload */
  141. void upload(const PixelData &data, const PixelVolume &dest) override;
  142. /** @copydoc GLPixelBuffer::download */
  143. void download(const PixelData &data) override;
  144. /** @copydoc GLPixelBuffer::blitFromTexture */
  145. void blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox) override;
  146. /**
  147. * Populate texture buffer with the data in the currently attached frame buffer.
  148. *
  149. * @param[in] zoffset 3D slice of the texture to copy to. 0 if texture is not 3D.
  150. */
  151. void copyFromFramebuffer(UINT32 zoffset);
  152. protected:
  153. GLenum mTarget;
  154. GLenum mFaceTarget;
  155. GLuint mTextureID;
  156. GLint mFace;
  157. GLint mLevel;
  158. UINT32 mMultisampleCount;
  159. };
  160. /** Pixel buffer specialization that represents a render buffer. */
  161. class GLRenderBuffer : public GLPixelBuffer
  162. {
  163. public:
  164. /**
  165. * Initializes a new render buffer.
  166. *
  167. * @param[in] format OpenGL pixel format.
  168. * @param[in] width Width of the render buffer in pixels.
  169. * @param[in] height Height of the render buffer in pixels.
  170. * @param[in] numSamples Number of samples to support.
  171. */
  172. GLRenderBuffer(GLenum format, UINT32 width, UINT32 height, GLsizei numSamples);
  173. ~GLRenderBuffer();
  174. /** @copydoc GLPixelBuffer::bindToFramebuffer */
  175. void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers) override;
  176. protected:
  177. GLuint mRenderbufferID;
  178. };
  179. /** @} */
  180. };