BsGLPixelBuffer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "BsPixelBuffer.h"
  6. namespace BansheeEngine
  7. {
  8. class GLTextureBuffer;
  9. /**
  10. * @brief OpenGL implementation of a pixel buffer. Represents a hardware buffer
  11. * containing a surface of pixels.
  12. */
  13. class BS_RSGL_EXPORT GLPixelBuffer : public PixelBuffer
  14. {
  15. public:
  16. /**
  17. * @brief Constructs a new pixel buffer with the provided settings.
  18. *
  19. * @param width Width of the pixel buffer in pixels.
  20. * @param height Height of the pixel buffer in pixels.
  21. * @param depth Depth of the pixel buffer in pixels (number of 2D slices).
  22. * @param format Format of each pixel in the buffer.
  23. * @param usage Usage signaling the render system how we plan on using the buffer.
  24. */
  25. GLPixelBuffer(UINT32 mWidth, UINT32 mHeight, UINT32 mDepth,
  26. PixelFormat mFormat, GpuBufferUsage usage);
  27. ~GLPixelBuffer();
  28. /**
  29. * @brief Upload some pixel data to the buffer.
  30. *
  31. * @param data Data to upload.
  32. * @param dest Coordinates to which to upload the data.
  33. */
  34. virtual void upload(const PixelData& data, const PixelVolume& dest);
  35. /**
  36. * @brief Reads data from the pixel buffer into the provided object.
  37. * Caller must ensure the data object is of adequate size.
  38. */
  39. virtual void download(const PixelData& data);
  40. /**
  41. * @brief Binds the buffers to a frame buffer object at the specified attachment point.
  42. *
  43. * @param attachment Attachment point index in the range [0, BS_MAX_MULTIPLE_RENDER_TARGETS).
  44. * @param zoffset Depth slice to bind, in the case of a 3D texture.
  45. */
  46. virtual void bindToFramebuffer(GLenum attachment, UINT32 zoffset);
  47. /**
  48. * @brief Returns internal OpenGL pixel format used by the buffer.
  49. */
  50. GLenum getGLFormat() { return mGLInternalFormat; }
  51. /**
  52. * @brief Blits the contents of the provided buffer into this pixel buffer.
  53. * Data is bilinearily interpolated in case buffer sizes don't match.
  54. */
  55. virtual void blitFromTexture(GLTextureBuffer* src);
  56. /**
  57. * @brief Blits contents of a sub-region of the provided buffer into a sub-region of this pixel buffer.
  58. * Data is bilinearily interpolated in case source and destination sizes don't match.
  59. */
  60. virtual void blitFromTexture(GLTextureBuffer* src, const PixelVolume& srcBox, const PixelVolume& dstBox);
  61. protected:
  62. /**
  63. * @copydoc PixelBuffer::lockImpl
  64. */
  65. PixelData lockImpl(PixelVolume lockBox, GpuLockOptions options);
  66. /**
  67. * @copydoc PixelBuffer::unlockImpl
  68. */
  69. void unlockImpl();
  70. /**
  71. * @brief Allocates an internal buffer on the CPU, the size of the hardware buffer.
  72. */
  73. void allocateBuffer();
  74. /**
  75. * @brief Deallocates the internal CPU buffer.
  76. */
  77. void freeBuffer();
  78. protected:
  79. PixelData mBuffer;
  80. GLenum mGLInternalFormat;
  81. GpuLockOptions mCurrentLockOptions;
  82. };
  83. /**
  84. * @brief Pixel buffer specialization that represents a single surface in a texture.
  85. */
  86. class BS_RSGL_EXPORT GLTextureBuffer : public GLPixelBuffer
  87. {
  88. public:
  89. /**
  90. * @brief Constructs a new texture buffer from a specific surface in the provided texture.
  91. *
  92. * @param target OpenGL type of the texture to retrieve the surface from.
  93. * @param id OpenGL handle to the texture to retrieve the surface from.
  94. * @param face Face index of the texture in the case of cube textures or texture arrays.
  95. * @param level Mip level of the texture.
  96. * @param usage Usage signaling the render system how we plan on using the buffer.
  97. * @param writeGamma True if the parent texture was created with SRGB support.
  98. * @param multisampleCount Number of samples the parent texture was created with.
  99. */
  100. GLTextureBuffer(GLenum target, GLuint id, GLint face,
  101. GLint level, GpuBufferUsage usage, bool writeGamma, UINT32 multisampleCount);
  102. ~GLTextureBuffer();
  103. /**
  104. * @copydoc GLPixelBuffer::bindToFramebuffer
  105. */
  106. virtual void bindToFramebuffer(GLenum attachment, UINT32 zoffset);
  107. /**
  108. * @copydoc GLPixelBuffer::upload
  109. */
  110. virtual void upload(const PixelData &data, const PixelVolume &dest);
  111. /**
  112. * @copydoc GLPixelBuffer::download
  113. */
  114. virtual void download(const PixelData &data);
  115. /**
  116. * @copydoc GLPixelBuffer::blitFromTexture
  117. */
  118. void blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox);
  119. /**
  120. * @brief Populate texture buffer with the data in the currently attached frame buffer.
  121. *
  122. * @param zoffset 3D slice of the texture to copy to. 0 if texture is not 3D.
  123. */
  124. void copyFromFramebuffer(UINT32 zoffset);
  125. protected:
  126. GLenum mTarget;
  127. GLenum mFaceTarget;
  128. GLuint mTextureID;
  129. GLint mFace;
  130. GLint mLevel;
  131. UINT32 mMultisampleCount;
  132. };
  133. /**
  134. * @brief Pixel buffer specialization that represents a render buffer.
  135. */
  136. class BS_RSGL_EXPORT GLRenderBuffer : public GLPixelBuffer
  137. {
  138. public:
  139. /**
  140. * @brief Initializes a new render buffer.
  141. *
  142. * @param format OpenGL pixel format.
  143. * @param width Width of the render buffer in pixels.
  144. * @param height Height of the render buffer in pixels.
  145. * @param numSamples Number of samples to support.
  146. */
  147. GLRenderBuffer(GLenum format, UINT32 width, UINT32 height, GLsizei numSamples);
  148. ~GLRenderBuffer();
  149. /**
  150. * @copydoc GLPixelBuffer::bindToFramebuffer
  151. */
  152. virtual void bindToFramebuffer(GLenum attachment, UINT32 zoffset);
  153. protected:
  154. GLuint mRenderbufferID;
  155. };
  156. };