2
0

BsGLPixelBuffer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @param[in] allLayers Should all layers of the texture be bound, or just one (zoffset is ignored if true).
  45. */
  46. virtual void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers);
  47. /** Returns internal OpenGL pixel format used by the buffer. */
  48. GLenum getGLFormat() const { return mGLInternalFormat; }
  49. /**
  50. * Blits the contents of the provided buffer into this pixel buffer. Data is bilinearily interpolated in case buffer
  51. * sizes don't match.
  52. */
  53. virtual void blitFromTexture(GLTextureBuffer* src);
  54. /**
  55. * Blits contents of a sub-region of the provided buffer into a sub-region of this pixel buffer. Data is bilinearily
  56. * 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. /** @copydoc PixelBuffer::lockImpl */
  61. PixelData lockImpl(PixelVolume lockBox, GpuLockOptions options) override;
  62. /** @copydoc PixelBuffer::unlockImpl */
  63. void unlockImpl() override;
  64. /** Allocates an internal buffer on the CPU, the size of the hardware buffer. */
  65. void allocateBuffer();
  66. /** Deallocates the internal CPU buffer. */
  67. void freeBuffer();
  68. protected:
  69. PixelData mBuffer;
  70. GLenum mGLInternalFormat;
  71. GpuLockOptions mCurrentLockOptions;
  72. };
  73. /** Pixel buffer specialization that represents a single surface in a texture. */
  74. class BS_RSGL_EXPORT GLTextureBuffer : public GLPixelBuffer
  75. {
  76. public:
  77. /**
  78. * Constructs a new texture buffer from a specific surface in the provided texture.
  79. *
  80. * @param[in] target OpenGL type of the texture to retrieve the surface from.
  81. * @param[in] id OpenGL handle to the texture to retrieve the surface from.
  82. * @param[in] face Face index of the texture in the case of cube textures or texture arrays.
  83. * @param[in] level Mip level of the texture.
  84. * @param[in] usage Usage signaling the render system how we plan on using the buffer.
  85. * @param[in] writeGamma True if the parent texture was created with SRGB support.
  86. * @param[in] multisampleCount Number of samples the parent texture was created with.
  87. */
  88. GLTextureBuffer(GLenum target, GLuint id, GLint face,
  89. GLint level, GpuBufferUsage usage, bool writeGamma, UINT32 multisampleCount);
  90. ~GLTextureBuffer();
  91. /** @copydoc GLPixelBuffer::bindToFramebuffer */
  92. void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers) override;
  93. /** @copydoc GLPixelBuffer::upload */
  94. void upload(const PixelData &data, const PixelVolume &dest) override;
  95. /** @copydoc GLPixelBuffer::download */
  96. void download(const PixelData &data) override;
  97. /** @copydoc GLPixelBuffer::blitFromTexture */
  98. void blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox) override;
  99. /**
  100. * Populate texture buffer with the data in the currently attached frame buffer.
  101. *
  102. * @param[in] zoffset 3D slice of the texture to copy to. 0 if texture is not 3D.
  103. */
  104. void copyFromFramebuffer(UINT32 zoffset);
  105. protected:
  106. GLenum mTarget;
  107. GLenum mFaceTarget;
  108. GLuint mTextureID;
  109. GLint mFace;
  110. GLint mLevel;
  111. UINT32 mMultisampleCount;
  112. };
  113. /** Pixel buffer specialization that represents a render buffer. */
  114. class BS_RSGL_EXPORT GLRenderBuffer : public GLPixelBuffer
  115. {
  116. public:
  117. /**
  118. * Initializes a new render buffer.
  119. *
  120. * @param[in] format OpenGL pixel format.
  121. * @param[in] width Width of the render buffer in pixels.
  122. * @param[in] height Height of the render buffer in pixels.
  123. * @param[in] numSamples Number of samples to support.
  124. */
  125. GLRenderBuffer(GLenum format, UINT32 width, UINT32 height, GLsizei numSamples);
  126. ~GLRenderBuffer();
  127. /** @copydoc GLPixelBuffer::bindToFramebuffer */
  128. void bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers) override;
  129. protected:
  130. GLuint mRenderbufferID;
  131. };
  132. /** @} */
  133. };