BsGLPixelBuffer.h 5.4 KB

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