The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class OpenGLFrameBuffer::Pimpl
  16. {
  17. public:
  18. Pimpl (OpenGLContext& c, const int w, const int h,
  19. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  20. : context (c), width (w), height (h),
  21. textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
  22. hasDepthBuffer (false), hasStencilBuffer (false)
  23. {
  24. // Framebuffer objects can only be created when the current thread has an active OpenGL
  25. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  26. jassert (OpenGLHelpers::isContextActive());
  27. #if JUCE_WINDOWS || JUCE_LINUX
  28. if (context.extensions.glGenFramebuffers == nullptr)
  29. return;
  30. #endif
  31. context.extensions.glGenFramebuffers (1, &frameBufferID);
  32. bind();
  33. glGenTextures (1, &textureID);
  34. glBindTexture (GL_TEXTURE_2D, textureID);
  35. JUCE_CHECK_OPENGL_ERROR
  36. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  37. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  38. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  39. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  40. JUCE_CHECK_OPENGL_ERROR
  41. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  42. JUCE_CHECK_OPENGL_ERROR
  43. context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
  44. if (wantsDepthBuffer || wantsStencilBuffer)
  45. {
  46. context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
  47. context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
  48. jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
  49. context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
  50. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
  51. #if JUCE_OPENGL_ES
  52. : GL_DEPTH_COMPONENT16,
  53. #else
  54. : GL_DEPTH_COMPONENT,
  55. #endif
  56. width, height);
  57. GLint params = 0;
  58. context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
  59. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  60. if (wantsStencilBuffer)
  61. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  62. hasDepthBuffer = wantsDepthBuffer;
  63. hasStencilBuffer = wantsStencilBuffer;
  64. }
  65. unbind();
  66. }
  67. ~Pimpl()
  68. {
  69. if (OpenGLHelpers::isContextActive())
  70. {
  71. if (textureID != 0)
  72. glDeleteTextures (1, &textureID);
  73. if (depthOrStencilBuffer != 0)
  74. context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
  75. if (frameBufferID != 0)
  76. context.extensions.glDeleteFramebuffers (1, &frameBufferID);
  77. JUCE_CHECK_OPENGL_ERROR
  78. }
  79. }
  80. bool createdOk() const
  81. {
  82. return frameBufferID != 0 && textureID != 0;
  83. }
  84. void bind()
  85. {
  86. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  87. JUCE_CHECK_OPENGL_ERROR
  88. }
  89. void unbind()
  90. {
  91. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
  92. JUCE_CHECK_OPENGL_ERROR
  93. }
  94. OpenGLContext& context;
  95. const int width, height;
  96. GLuint textureID, frameBufferID, depthOrStencilBuffer;
  97. bool hasDepthBuffer, hasStencilBuffer;
  98. private:
  99. bool checkStatus() noexcept
  100. {
  101. const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
  102. return status == GL_NO_ERROR
  103. || status == GL_FRAMEBUFFER_COMPLETE;
  104. }
  105. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  106. };
  107. //==============================================================================
  108. class OpenGLFrameBuffer::SavedState
  109. {
  110. public:
  111. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  112. : width (w), height (h),
  113. data ((size_t) (w * h))
  114. {
  115. buffer.readPixels (data, Rectangle<int> (w, h));
  116. }
  117. bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
  118. {
  119. if (buffer.initialise (context, width, height))
  120. {
  121. buffer.writePixels (data, Rectangle<int> (width, height));
  122. return true;
  123. }
  124. return false;
  125. }
  126. private:
  127. const int width, height;
  128. HeapBlock<PixelARGB> data;
  129. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
  130. };
  131. //==============================================================================
  132. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  133. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  134. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
  135. {
  136. jassert (context.isActive()); // The context must be active when creating a framebuffer!
  137. pimpl.reset();
  138. pimpl.reset (new Pimpl (context, width, height, false, false));
  139. if (! pimpl->createdOk())
  140. pimpl.reset();
  141. return pimpl != nullptr;
  142. }
  143. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
  144. {
  145. if (! image.isARGB())
  146. return initialise (context, image.convertedToFormat (Image::ARGB));
  147. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  148. return initialise (context, bitmap.width, bitmap.height)
  149. && writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
  150. }
  151. bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
  152. {
  153. auto* p = other.pimpl.get();
  154. if (p == nullptr)
  155. {
  156. pimpl.reset();
  157. return true;
  158. }
  159. const Rectangle<int> area (pimpl->width, pimpl->height);
  160. if (initialise (p->context, area.getWidth(), area.getHeight()))
  161. {
  162. pimpl->bind();
  163. #if ! JUCE_ANDROID
  164. glEnable (GL_TEXTURE_2D);
  165. clearGLError();
  166. #endif
  167. glBindTexture (GL_TEXTURE_2D, p->textureID);
  168. pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
  169. glBindTexture (GL_TEXTURE_2D, 0);
  170. JUCE_CHECK_OPENGL_ERROR
  171. pimpl->unbind();
  172. return true;
  173. }
  174. return false;
  175. }
  176. void OpenGLFrameBuffer::release()
  177. {
  178. pimpl.reset();
  179. savedState.reset();
  180. }
  181. void OpenGLFrameBuffer::saveAndRelease()
  182. {
  183. if (pimpl != nullptr)
  184. {
  185. savedState.reset (new SavedState (*this, pimpl->width, pimpl->height));
  186. pimpl.reset();
  187. }
  188. }
  189. bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
  190. {
  191. if (savedState != nullptr)
  192. {
  193. std::unique_ptr<SavedState> state;
  194. std::swap (state, savedState);
  195. if (state->restore (context, *this))
  196. return true;
  197. std::swap (state, savedState);
  198. }
  199. return false;
  200. }
  201. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  202. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  203. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  204. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  205. {
  206. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  207. // reloadSavedCopy() to put it back into GPU memory before using it..
  208. jassert (savedState == nullptr);
  209. if (pimpl == nullptr)
  210. return false;
  211. pimpl->bind();
  212. return true;
  213. }
  214. GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
  215. {
  216. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  217. }
  218. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
  219. {
  220. GLint fb;
  221. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  222. return (GLuint) fb;
  223. }
  224. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  225. {
  226. if (pimpl != nullptr)
  227. pimpl->unbind();
  228. }
  229. void OpenGLFrameBuffer::clear (Colour colour)
  230. {
  231. if (makeCurrentRenderingTarget())
  232. {
  233. OpenGLHelpers::clear (colour);
  234. releaseAsRenderingTarget();
  235. }
  236. }
  237. void OpenGLFrameBuffer::makeCurrentAndClear()
  238. {
  239. if (makeCurrentRenderingTarget())
  240. {
  241. glClearColor (0, 0, 0, 0);
  242. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  243. }
  244. }
  245. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  246. {
  247. if (! makeCurrentRenderingTarget())
  248. return false;
  249. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  250. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  251. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  252. pimpl->unbind();
  253. return true;
  254. }
  255. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  256. {
  257. OpenGLTargetSaver ts (pimpl->context);
  258. if (! makeCurrentRenderingTarget())
  259. return false;
  260. glDisable (GL_DEPTH_TEST);
  261. glDisable (GL_BLEND);
  262. JUCE_CHECK_OPENGL_ERROR
  263. OpenGLTexture tex;
  264. tex.loadARGB (data, area.getWidth(), area.getHeight());
  265. glViewport (0, 0, pimpl->width, pimpl->height);
  266. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  267. tex.getWidth(), tex.getHeight()),
  268. pimpl->width, pimpl->height, true);
  269. JUCE_CHECK_OPENGL_ERROR
  270. return true;
  271. }
  272. } // namespace juce