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.

357 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. if (! pimpl->context.isCoreProfile())
  170. glEnable (GL_TEXTURE_2D);
  171. clearGLError();
  172. #endif
  173. glBindTexture (GL_TEXTURE_2D, p->textureID);
  174. pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
  175. glBindTexture (GL_TEXTURE_2D, 0);
  176. JUCE_CHECK_OPENGL_ERROR
  177. pimpl->unbind();
  178. return true;
  179. }
  180. return false;
  181. }
  182. void OpenGLFrameBuffer::release()
  183. {
  184. pimpl.reset();
  185. savedState.reset();
  186. }
  187. void OpenGLFrameBuffer::saveAndRelease()
  188. {
  189. if (pimpl != nullptr)
  190. {
  191. savedState.reset (new SavedState (*this, pimpl->width, pimpl->height));
  192. pimpl.reset();
  193. }
  194. }
  195. bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
  196. {
  197. if (savedState != nullptr)
  198. {
  199. std::unique_ptr<SavedState> state;
  200. std::swap (state, savedState);
  201. if (state->restore (context, *this))
  202. return true;
  203. std::swap (state, savedState);
  204. }
  205. return false;
  206. }
  207. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  208. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  209. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  210. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  211. {
  212. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  213. // reloadSavedCopy() to put it back into GPU memory before using it..
  214. jassert (savedState == nullptr);
  215. if (pimpl == nullptr)
  216. return false;
  217. pimpl->bind();
  218. return true;
  219. }
  220. GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
  221. {
  222. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  223. }
  224. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
  225. {
  226. GLint fb = {};
  227. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  228. return (GLuint) fb;
  229. }
  230. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  231. {
  232. if (pimpl != nullptr)
  233. pimpl->unbind();
  234. }
  235. void OpenGLFrameBuffer::clear (Colour colour)
  236. {
  237. if (makeCurrentRenderingTarget())
  238. {
  239. OpenGLHelpers::clear (colour);
  240. releaseAsRenderingTarget();
  241. }
  242. }
  243. void OpenGLFrameBuffer::makeCurrentAndClear()
  244. {
  245. if (makeCurrentRenderingTarget())
  246. {
  247. glClearColor (0, 0, 0, 0);
  248. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  249. }
  250. }
  251. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  252. {
  253. if (! makeCurrentRenderingTarget())
  254. return false;
  255. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  256. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  257. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  258. pimpl->unbind();
  259. return true;
  260. }
  261. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  262. {
  263. OpenGLTargetSaver ts (pimpl->context);
  264. if (! makeCurrentRenderingTarget())
  265. return false;
  266. glDisable (GL_DEPTH_TEST);
  267. glDisable (GL_BLEND);
  268. JUCE_CHECK_OPENGL_ERROR
  269. OpenGLTexture tex;
  270. tex.loadARGB (data, area.getWidth(), area.getHeight());
  271. glViewport (0, 0, pimpl->width, pimpl->height);
  272. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  273. tex.getWidth(), tex.getHeight()),
  274. pimpl->width, pimpl->height, true);
  275. JUCE_CHECK_OPENGL_ERROR
  276. return true;
  277. }
  278. } // namespace juce