BsGLPixelBuffer.h 5.7 KB

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