FrameBuffer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "FrameBuffer.h"
  21. #include "RenderTarget.h"
  22. #include "base/CCGLUtils.h"
  23. RENDERER_BEGIN
  24. FrameBuffer::FrameBuffer()
  25. : _device(nullptr)
  26. , _depthBuffer(nullptr)
  27. , _stencilBuffer(nullptr)
  28. , _depthStencilBuffer(nullptr)
  29. {
  30. }
  31. FrameBuffer::~FrameBuffer()
  32. {
  33. destroy();
  34. }
  35. void FrameBuffer::destroy()
  36. {
  37. for (auto colorBufffer : _colorBuffers)
  38. RENDERER_SAFE_RELEASE(colorBufffer);
  39. _colorBuffers.clear();
  40. RENDERER_SAFE_RELEASE(_depthBuffer);
  41. _depthBuffer = nullptr;
  42. RENDERER_SAFE_RELEASE(_stencilBuffer);
  43. _stencilBuffer = nullptr;
  44. RENDERER_SAFE_RELEASE(_depthStencilBuffer);
  45. _depthStencilBuffer = nullptr;
  46. if (_glID != 0)
  47. {
  48. glDeleteFramebuffers(1, &_glID);
  49. _glID = 0;
  50. }
  51. }
  52. bool FrameBuffer::init(DeviceGraphics* device, uint16_t width, uint16_t height)
  53. {
  54. _device = device;
  55. _width = width;
  56. _height = height;
  57. glGenFramebuffers(1, &_glID);
  58. return true;
  59. }
  60. void FrameBuffer::setColorBuffer(RenderTarget* rt, int index)
  61. {
  62. if (index >= _colorBuffers.size())
  63. _colorBuffers.resize(index + 1);
  64. RENDERER_SAFE_RETAIN(rt);
  65. RENDERER_SAFE_RELEASE(_colorBuffers[index]);
  66. _colorBuffers[index] = rt;
  67. }
  68. void FrameBuffer::setColorBuffers(const std::vector<RenderTarget*>& renderTargets)
  69. {
  70. for (auto& colorBufffer : renderTargets)
  71. RENDERER_SAFE_RETAIN(colorBufffer);
  72. for (auto& colorBufffer : _colorBuffers)
  73. RENDERER_SAFE_RELEASE(colorBufffer);
  74. _colorBuffers = renderTargets;
  75. }
  76. void FrameBuffer::setDepthBuffer(RenderTarget* rt)
  77. {
  78. RENDERER_SAFE_RETAIN(rt);
  79. RENDERER_SAFE_RELEASE(_depthBuffer);
  80. _depthBuffer = rt;
  81. }
  82. void FrameBuffer::setStencilBuffer(RenderTarget* rt)
  83. {
  84. RENDERER_SAFE_RETAIN(rt);
  85. RENDERER_SAFE_RELEASE(_stencilBuffer);
  86. _stencilBuffer = rt;
  87. }
  88. void FrameBuffer::setDepthStencilBuffer(RenderTarget* rt)
  89. {
  90. RENDERER_SAFE_RETAIN(rt);
  91. RENDERER_SAFE_RELEASE(_depthStencilBuffer);
  92. _depthStencilBuffer = rt;
  93. }
  94. const std::vector<RenderTarget*>& FrameBuffer::getColorBuffers() const
  95. {
  96. return _colorBuffers;
  97. }
  98. const RenderTarget* FrameBuffer::getDepthBuffer() const
  99. {
  100. return _depthBuffer;
  101. }
  102. const RenderTarget* FrameBuffer::getStencilBuffer() const
  103. {
  104. return _stencilBuffer;
  105. }
  106. const RenderTarget* FrameBuffer::getDepthStencilBuffer() const
  107. {
  108. return _depthStencilBuffer;
  109. }
  110. RENDERER_END