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.

355 lines
11KB

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