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.

424 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class OpenGLFrameBuffer::Pimpl
  18. {
  19. public:
  20. Pimpl (OpenGLContext& c, const int w, const int h,
  21. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  22. : context (c), width (w), height (h),
  23. textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
  24. hasDepthBuffer (false), hasStencilBuffer (false)
  25. {
  26. // Framebuffer objects can only be created when the current thread has an active OpenGL
  27. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  28. jassert (OpenGLHelpers::isContextActive());
  29. #if JUCE_WINDOWS || JUCE_LINUX
  30. if (context.extensions.glGenFramebuffers == nullptr)
  31. return;
  32. #endif
  33. context.extensions.glGenFramebuffers (1, &frameBufferID);
  34. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  35. JUCE_CHECK_OPENGL_ERROR
  36. glGenTextures (1, &textureID);
  37. glBindTexture (GL_TEXTURE_2D, textureID);
  38. JUCE_CHECK_OPENGL_ERROR
  39. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  40. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  41. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  42. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  43. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  44. JUCE_CHECK_OPENGL_ERROR
  45. context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
  46. if (wantsDepthBuffer || wantsStencilBuffer)
  47. {
  48. context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
  49. context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
  50. jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
  51. context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
  52. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
  53. #if JUCE_OPENGL_ES
  54. : GL_DEPTH_COMPONENT16,
  55. #else
  56. : GL_DEPTH_COMPONENT,
  57. #endif
  58. width, height);
  59. GLint params = 0;
  60. context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
  61. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  62. if (wantsStencilBuffer)
  63. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  64. hasDepthBuffer = wantsDepthBuffer;
  65. hasStencilBuffer = wantsStencilBuffer;
  66. }
  67. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, 0);
  68. }
  69. ~Pimpl()
  70. {
  71. if (OpenGLHelpers::isContextActive())
  72. {
  73. if (textureID != 0)
  74. glDeleteTextures (1, &textureID);
  75. if (depthOrStencilBuffer != 0)
  76. context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
  77. if (frameBufferID != 0)
  78. context.extensions.glDeleteFramebuffers (1, &frameBufferID);
  79. JUCE_CHECK_OPENGL_ERROR
  80. }
  81. }
  82. bool createdOk() const
  83. {
  84. return frameBufferID != 0 && textureID != 0;
  85. }
  86. void bind()
  87. {
  88. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  89. JUCE_CHECK_OPENGL_ERROR
  90. }
  91. void unbind()
  92. {
  93. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, 0);
  94. JUCE_CHECK_OPENGL_ERROR
  95. }
  96. OpenGLContext& context;
  97. const int width, height;
  98. GLuint textureID, frameBufferID, depthOrStencilBuffer;
  99. bool hasDepthBuffer, hasStencilBuffer;
  100. private:
  101. bool checkStatus() noexcept
  102. {
  103. const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
  104. return status == GL_NO_ERROR
  105. || status == GL_FRAMEBUFFER_COMPLETE;
  106. }
  107. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  108. };
  109. //==============================================================================
  110. class OpenGLFrameBuffer::SavedState
  111. {
  112. public:
  113. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  114. : width (w), height (h),
  115. data ((size_t) (w * h))
  116. {
  117. buffer.readPixels (data, Rectangle<int> (w, h));
  118. }
  119. bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
  120. {
  121. if (buffer.initialise (context, width, height))
  122. {
  123. buffer.writePixels (data, Rectangle<int> (width, height));
  124. return true;
  125. }
  126. return false;
  127. }
  128. private:
  129. const int width, height;
  130. HeapBlock <PixelARGB> data;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
  132. };
  133. //==============================================================================
  134. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  135. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  136. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
  137. {
  138. jassert (context.isActive()); // The context must be active when creating a framebuffer!
  139. pimpl = nullptr;
  140. pimpl = new Pimpl (context, width, height, false, false);
  141. if (! pimpl->createdOk())
  142. pimpl = nullptr;
  143. return pimpl != nullptr;
  144. }
  145. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
  146. {
  147. if (! image.isARGB())
  148. return initialise (context, image.convertedToFormat (Image::ARGB));
  149. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  150. return initialise (context, bitmap.width, bitmap.height)
  151. && writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
  152. }
  153. bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
  154. {
  155. const Pimpl* const p = other.pimpl;
  156. if (p == nullptr)
  157. {
  158. pimpl = nullptr;
  159. return true;
  160. }
  161. const Rectangle<int> area (pimpl->width, pimpl->height);
  162. if (initialise (p->context, area.getWidth(), area.getHeight()))
  163. {
  164. pimpl->bind();
  165. #if ! JUCE_ANDROID
  166. glEnable (GL_TEXTURE_2D);
  167. clearGLError();
  168. #endif
  169. glBindTexture (GL_TEXTURE_2D, p->textureID);
  170. pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
  171. glBindTexture (GL_TEXTURE_2D, 0);
  172. JUCE_CHECK_OPENGL_ERROR
  173. pimpl->unbind();
  174. return true;
  175. }
  176. return false;
  177. }
  178. void OpenGLFrameBuffer::release()
  179. {
  180. pimpl = nullptr;
  181. savedState = nullptr;
  182. }
  183. void OpenGLFrameBuffer::saveAndRelease()
  184. {
  185. if (pimpl != nullptr)
  186. {
  187. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  188. pimpl = nullptr;
  189. }
  190. }
  191. bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
  192. {
  193. if (savedState != nullptr)
  194. {
  195. ScopedPointer<SavedState> state (savedState);
  196. if (state->restore (context, *this))
  197. return true;
  198. savedState = state;
  199. }
  200. return false;
  201. }
  202. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  203. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  204. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  205. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  206. {
  207. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  208. // reloadSavedCopy() to put it back into GPU memory before using it..
  209. jassert (savedState == nullptr);
  210. if (pimpl == nullptr)
  211. return false;
  212. pimpl->bind();
  213. return true;
  214. }
  215. GLuint OpenGLFrameBuffer::getFrameBufferID() const
  216. {
  217. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  218. }
  219. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget()
  220. {
  221. GLint fb;
  222. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  223. return (GLuint) fb;
  224. }
  225. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  226. {
  227. if (pimpl != nullptr)
  228. pimpl->context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, 0);
  229. }
  230. void OpenGLFrameBuffer::clear (Colour colour)
  231. {
  232. if (makeCurrentRenderingTarget())
  233. {
  234. OpenGLHelpers::clear (colour);
  235. releaseAsRenderingTarget();
  236. }
  237. }
  238. void OpenGLFrameBuffer::makeCurrentAndClear()
  239. {
  240. if (makeCurrentRenderingTarget())
  241. {
  242. glClearColor (0, 0, 0, 0);
  243. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  244. }
  245. }
  246. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  247. {
  248. if (! makeCurrentRenderingTarget())
  249. return false;
  250. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  251. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  252. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  253. pimpl->context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, 0);
  254. JUCE_CHECK_OPENGL_ERROR
  255. return true;
  256. }
  257. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  258. {
  259. OpenGLTargetSaver ts (pimpl->context);
  260. if (! makeCurrentRenderingTarget())
  261. return false;
  262. glDisable (GL_DEPTH_TEST);
  263. glDisable (GL_BLEND);
  264. JUCE_CHECK_OPENGL_ERROR
  265. #if JUCE_OPENGL_ES && JUCE_USE_OPENGL_FIXED_FUNCTION
  266. OpenGLTexture tex;
  267. tex.loadARGBFlipped (data, area.getWidth(), area.getHeight());
  268. const int texH = tex.getHeight();
  269. tex.bind();
  270. const GLint cropRect[4] = { 0, texH - area.getHeight(), area.getWidth(), area.getHeight() };
  271. glTexParameteriv (GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  272. glEnable (GL_TEXTURE_2D);
  273. clearGLError();
  274. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  275. glDrawTexiOES (area.getX(), area.getY(), 1, area.getWidth(), area.getHeight());
  276. glBindTexture (GL_TEXTURE_2D, 0);
  277. #else
  278. OpenGLTexture tex;
  279. tex.loadARGB (data, area.getWidth(), area.getHeight());
  280. glViewport (0, 0, pimpl->width, pimpl->height);
  281. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  282. tex.getWidth(), tex.getHeight()),
  283. pimpl->width, pimpl->height, true);
  284. #endif
  285. JUCE_CHECK_OPENGL_ERROR
  286. return true;
  287. }
  288. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  289. void OpenGLFrameBuffer::draw2D (float x1, float y1,
  290. float x2, float y2,
  291. float x3, float y3,
  292. float x4, float y4,
  293. Colour colour) const
  294. {
  295. if (pimpl != nullptr)
  296. {
  297. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  298. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  299. glBindTexture (GL_TEXTURE_2D, 0);
  300. }
  301. }
  302. void OpenGLFrameBuffer::draw3D (float x1, float y1, float z1,
  303. float x2, float y2, float z2,
  304. float x3, float y3, float z3,
  305. float x4, float y4, float z4,
  306. Colour colour) const
  307. {
  308. if (pimpl != nullptr)
  309. {
  310. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  311. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  312. glBindTexture (GL_TEXTURE_2D, 0);
  313. }
  314. }
  315. void OpenGLFrameBuffer::drawAt (float x1, float y1) const
  316. {
  317. if (pimpl != nullptr)
  318. {
  319. #if ! JUCE_ANDROID
  320. glEnable (GL_TEXTURE_2D);
  321. clearGLError();
  322. #endif
  323. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  324. glDisableClientState (GL_COLOR_ARRAY);
  325. glDisableClientState (GL_NORMAL_ARRAY);
  326. const GLfloat vertices[] = { x1, y1,
  327. x1 + pimpl->width, y1,
  328. x1, y1 + pimpl->height,
  329. x1 + pimpl->width, y1 + pimpl->height };
  330. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  331. glEnableClientState (GL_VERTEX_ARRAY);
  332. glVertexPointer (2, GL_FLOAT, 0, vertices);
  333. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  334. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  335. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  336. glBindTexture (GL_TEXTURE_2D, 0);
  337. }
  338. }
  339. #endif