BsGLPixelBuffer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "RenderAPI/BsHardwareBuffer.h"
  6. #include "Image/BsPixelUtil.h"
  7. namespace bs { namespace ct
  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. /**
  92. * Blits the contents of the provided buffer into this pixel buffer. Data is bilinearily interpolated in case buffer
  93. * sizes don't match.
  94. */
  95. virtual void blitFromTexture(GLTextureBuffer* src);
  96. /**
  97. * Blits contents of a sub-region of the provided buffer into a sub-region of this pixel buffer. Data is bilinearily
  98. * interpolated in case source and destination sizes don't match.
  99. */
  100. virtual void blitFromTexture(GLTextureBuffer* src, const PixelVolume& srcBox, const PixelVolume& dstBox);
  101. protected:
  102. /** Allocates an internal buffer on the CPU, the size of the hardware buffer. */
  103. void allocateBuffer();
  104. /** Deallocates the internal CPU buffer. */
  105. void freeBuffer();
  106. protected:
  107. UINT32 mSizeInBytes;
  108. GpuBufferUsage mUsage;
  109. bool mIsLocked;
  110. UINT32 mWidth, mHeight, mDepth;
  111. PixelFormat mFormat;
  112. PixelData mCurrentLock;
  113. PixelVolume mLockedBox;
  114. PixelData mBuffer;
  115. GpuLockOptions mCurrentLockOptions;
  116. };
  117. /** Pixel buffer specialization that represents a single surface in a texture. */
  118. class GLTextureBuffer : public GLPixelBuffer
  119. {
  120. public:
  121. /**
  122. * Constructs a new texture buffer from a specific surface in the provided texture.
  123. *
  124. * @param[in] target OpenGL type of the texture to retrieve the surface from.
  125. * @param[in] id OpenGL handle to the texture to retrieve the surface from.
  126. * @param[in] face Face index of the texture in the case of cube textures or texture arrays.
  127. * @param[in] level Mip level of the texture.
  128. * @param[in] usage Usage signaling the render system how we plan on using the buffer.
  129. * @param[in] format Format of each pixel in the buffer.
  130. * @param[in] multisampleCount Number of samples the parent texture was created with.
  131. */
  132. GLTextureBuffer(GLenum target, GLuint id, GLint face,
  133. GLint level, GpuBufferUsage usage, PixelFormat format, UINT32 multisampleCount);
  134. ~GLTextureBuffer();
  135. /** @copydoc GLPixelBuffer::bindToFramebuffer */
  136. void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers) override;
  137. /** @copydoc GLPixelBuffer::upload */
  138. void upload(const PixelData &data, const PixelVolume &dest) override;
  139. /** @copydoc GLPixelBuffer::download */
  140. void download(const PixelData &data) override;
  141. /** @copydoc GLPixelBuffer::blitFromTexture */
  142. void blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox) override;
  143. /**
  144. * Populate texture buffer with the data in the currently attached frame buffer.
  145. *
  146. * @param[in] zoffset 3D slice of the texture to copy to. 0 if texture is not 3D.
  147. */
  148. void copyFromFramebuffer(UINT32 zoffset);
  149. protected:
  150. GLenum mTarget;
  151. GLenum mFaceTarget;
  152. GLuint mTextureID;
  153. GLint mFace;
  154. GLint mLevel;
  155. UINT32 mMultisampleCount;
  156. };
  157. /** @} */
  158. }}