BsGLPixelBuffer.h 5.7 KB

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