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.

497 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. namespace
  20. {
  21. #if JUCE_WINDOWS
  22. enum
  23. {
  24. GL_FRAMEBUFFER_EXT = 0x8D40,
  25. GL_RENDERBUFFER_EXT = 0x8D41,
  26. GL_FRAMEBUFFER_BINDING_EXT = 0x8CA6,
  27. GL_COLOR_ATTACHMENT0_EXT = 0x8CE0,
  28. GL_DEPTH_ATTACHMENT_EXT = 0x8D00,
  29. GL_STENCIL_ATTACHMENT_EXT = 0x8D20,
  30. GL_FRAMEBUFFER_COMPLETE_EXT = 0x8CD5,
  31. GL_DEPTH24_STENCIL8_EXT = 0x88F0,
  32. GL_RENDERBUFFER_DEPTH_SIZE_EXT = 0x8D54
  33. };
  34. #endif
  35. #if JUCE_WINDOWS || JUCE_LINUX
  36. #define FRAMEBUFFER_FUNCTION_LIST(USE_FUNCTION) \
  37. USE_FUNCTION (glIsRenderbufferEXT, GLboolean, (GLuint renderbuffer))\
  38. USE_FUNCTION (glBindRenderbufferEXT, void, (GLenum target, GLuint renderbuffer))\
  39. USE_FUNCTION (glDeleteRenderbuffersEXT, void, (GLsizei n, const GLuint *renderbuffers))\
  40. USE_FUNCTION (glGenRenderbuffersEXT, void, (GLsizei n, GLuint *renderbuffers))\
  41. USE_FUNCTION (glRenderbufferStorageEXT, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height))\
  42. USE_FUNCTION (glGetRenderbufferParameterivEXT, void, (GLenum target, GLenum pname, GLint* params))\
  43. USE_FUNCTION (glIsFramebufferEXT, GLboolean, (GLuint framebuffer))\
  44. USE_FUNCTION (glBindFramebufferEXT, void, (GLenum target, GLuint framebuffer))\
  45. USE_FUNCTION (glDeleteFramebuffersEXT, void, (GLsizei n, const GLuint *framebuffers))\
  46. USE_FUNCTION (glGenFramebuffersEXT, void, (GLsizei n, GLuint *framebuffers))\
  47. USE_FUNCTION (glCheckFramebufferStatusEXT, GLenum, (GLenum target))\
  48. USE_FUNCTION (glFramebufferTexture1DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  49. USE_FUNCTION (glFramebufferTexture2DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  50. USE_FUNCTION (glFramebufferTexture3DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset))\
  51. USE_FUNCTION (glFramebufferRenderbufferEXT, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer))\
  52. USE_FUNCTION (glGetFramebufferAttachmentParameterivEXT, void, (GLenum target, GLenum attachment, GLenum pname, GLint *params))\
  53. USE_FUNCTION (glGenerateMipmapEXT, void, (GLenum target))\
  54. FRAMEBUFFER_FUNCTION_LIST (JUCE_DECLARE_GL_EXTENSION_FUNCTION)
  55. static bool framebufferFunctionsInitialised = false;
  56. void initialiseFrameBufferFunctions()
  57. {
  58. if (! framebufferFunctionsInitialised)
  59. {
  60. framebufferFunctionsInitialised = true;
  61. #define FIND_FUNCTION(name, returnType, params) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  62. FRAMEBUFFER_FUNCTION_LIST (FIND_FUNCTION)
  63. #undef FIND_FUNCTION
  64. }
  65. }
  66. #undef FRAMEBUFFER_FUNCTION_LIST
  67. //==============================================================================
  68. #elif JUCE_OPENGL_ES
  69. #define glIsRenderbufferEXT glIsRenderbufferOES
  70. #define glBindRenderbufferEXT glBindRenderbufferOES
  71. #define glDeleteRenderbuffersEXT glDeleteRenderbuffersOES
  72. #define glGenRenderbuffersEXT glGenRenderbuffersOES
  73. #define glRenderbufferStorageEXT glRenderbufferStorageOES
  74. #define glGetRenderbufferParameterivEXT glGetRenderbufferParameterivOES
  75. #define glIsFramebufferEXT glIsFramebufferOES
  76. #define glBindFramebufferEXT glBindFramebufferOES
  77. #define glDeleteFramebuffersEXT glDeleteFramebuffersOES
  78. #define glGenFramebuffersEXT glGenFramebuffersOES
  79. #define glCheckFramebufferStatusEXT glCheckFramebufferStatusOES
  80. #define glFramebufferTexture1DEXT glFramebufferTexture1DOES
  81. #define glFramebufferTexture2DEXT glFramebufferTexture2DOES
  82. #define glFramebufferTexture3DEXT glFramebufferTexture3DOES
  83. #define glFramebufferRenderbufferEXT glFramebufferRenderbufferOES
  84. #define glGetFramebufferAttachmentParameterivEXT glGetFramebufferAttachmentParameterivOES
  85. #define glGenerateMipmapEXT glGenerateMipmapOES
  86. #define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER_OES
  87. #define GL_FRAMEBUFFER_BINDING_EXT GL_FRAMEBUFFER_BINDING_OES
  88. #define GL_RGBA8 GL_RGBA
  89. #define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0_OES
  90. #define GL_RENDERBUFFER_EXT GL_RENDERBUFFER_OES
  91. #define GL_DEPTH24_STENCIL8_EXT GL_DEPTH24_STENCIL8_OES
  92. #define GL_RENDERBUFFER_DEPTH_SIZE_EXT GL_RENDERBUFFER_DEPTH_SIZE_OES
  93. #define GL_DEPTH_ATTACHMENT_EXT GL_DEPTH_ATTACHMENT_OES
  94. #define GL_STENCIL_ATTACHMENT_EXT GL_STENCIL_ATTACHMENT_OES
  95. #define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE_OES
  96. #define GL_FRAMEBUFFER_UNSUPPORTED_EXT GL_FRAMEBUFFER_UNSUPPORTED_OES
  97. #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_OES
  98. #endif
  99. }
  100. //==============================================================================
  101. class OpenGLFrameBuffer::Pimpl
  102. {
  103. public:
  104. Pimpl (const int width_, const int height_,
  105. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  106. : width (width_),
  107. height (height_),
  108. textureID (0),
  109. frameBufferHandle (0),
  110. depthOrStencilBuffer (0),
  111. hasDepthBuffer (false),
  112. hasStencilBuffer (false),
  113. ok (true)
  114. {
  115. // Framebuffer objects can only be created when the current thread has an active OpenGL
  116. // context. You'll need to make an OpenGLComponent active before calling this.
  117. jassert (OpenGLHelpers::isContextActive());
  118. #if JUCE_WINDOWS || JUCE_LINUX
  119. initialiseFrameBufferFunctions();
  120. if (glGenFramebuffersEXT == nullptr)
  121. return;
  122. #endif
  123. glGenFramebuffersEXT (1, &frameBufferHandle);
  124. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferHandle);
  125. glGenTextures (1, &textureID);
  126. glBindTexture (GL_TEXTURE_2D, textureID);
  127. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  128. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  129. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  130. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  131. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  132. glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureID, 0);
  133. if (wantsDepthBuffer || wantsStencilBuffer)
  134. {
  135. glGenRenderbuffersEXT (1, &depthOrStencilBuffer);
  136. glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  137. jassert (glIsRenderbufferEXT (depthOrStencilBuffer));
  138. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT,
  139. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8_EXT
  140. #if JUCE_OPENGL_ES
  141. : GL_DEPTH_COMPONENT16,
  142. #else
  143. : GL_DEPTH_COMPONENT,
  144. #endif
  145. width, height);
  146. GLint params = 0;
  147. glGetRenderbufferParameterivEXT (GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &params);
  148. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  149. if (wantsStencilBuffer)
  150. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  151. hasDepthBuffer = wantsDepthBuffer;
  152. hasStencilBuffer = wantsStencilBuffer;
  153. }
  154. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  155. }
  156. ~Pimpl()
  157. {
  158. if (textureID != 0)
  159. glDeleteTextures (1, &textureID);
  160. if (depthOrStencilBuffer != 0)
  161. glDeleteRenderbuffersEXT (1, &depthOrStencilBuffer);
  162. if (frameBufferHandle != 0)
  163. glDeleteFramebuffersEXT (1, &frameBufferHandle);
  164. }
  165. void bind() { glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferHandle); }
  166. void unbind() { glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0); }
  167. const int width, height;
  168. GLuint textureID, frameBufferHandle, depthOrStencilBuffer;
  169. bool hasDepthBuffer, hasStencilBuffer, ok;
  170. private:
  171. static bool checkStatus() noexcept
  172. {
  173. const GLenum status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
  174. return status == GL_NO_ERROR
  175. || status == GL_FRAMEBUFFER_COMPLETE_EXT;
  176. }
  177. JUCE_DECLARE_NON_COPYABLE (Pimpl);
  178. };
  179. //==============================================================================
  180. class OpenGLFrameBuffer::SavedState
  181. {
  182. public:
  183. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  184. : width (w), height (h),
  185. data (w * h)
  186. {
  187. buffer.readPixels (data, Rectangle<int> (w, h));
  188. }
  189. bool restore (OpenGLFrameBuffer& buffer)
  190. {
  191. if (buffer.initialise (width, height))
  192. {
  193. buffer.writePixels (data, Rectangle<int> (width, height));
  194. return true;
  195. }
  196. return false;
  197. }
  198. private:
  199. const int width, height;
  200. HeapBlock <PixelARGB> data;
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState);
  202. };
  203. //==============================================================================
  204. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  205. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  206. bool OpenGLFrameBuffer::initialise (int width, int height)
  207. {
  208. pimpl = nullptr;
  209. pimpl = new Pimpl (width, height, false, false);
  210. if (! pimpl->ok)
  211. pimpl = nullptr;
  212. return pimpl != nullptr;
  213. }
  214. bool OpenGLFrameBuffer::initialise (const Image& image)
  215. {
  216. if (! image.isARGB())
  217. return initialise (image.convertedToFormat (Image::ARGB));
  218. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  219. return initialise (bitmap.width, bitmap.height)
  220. && writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
  221. }
  222. bool OpenGLFrameBuffer::initialise (const OpenGLFrameBuffer& other)
  223. {
  224. const Pimpl* const p = other.pimpl;
  225. if (p == nullptr)
  226. {
  227. pimpl = nullptr;
  228. return true;
  229. }
  230. if (initialise (p->width, p->height))
  231. {
  232. pimpl->bind();
  233. OpenGLHelpers::prepareFor2D (p->width, p->height);
  234. glDisable (GL_BLEND);
  235. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  236. other.drawAt (0, 0);
  237. pimpl->unbind();
  238. return true;
  239. }
  240. return false;
  241. }
  242. void OpenGLFrameBuffer::release()
  243. {
  244. pimpl = nullptr;
  245. savedState = nullptr;
  246. }
  247. void OpenGLFrameBuffer::saveAndRelease()
  248. {
  249. if (pimpl != nullptr)
  250. {
  251. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  252. pimpl = nullptr;
  253. }
  254. }
  255. bool OpenGLFrameBuffer::reloadSavedCopy()
  256. {
  257. if (savedState != nullptr)
  258. {
  259. ScopedPointer<SavedState> state (savedState);
  260. if (state->restore (*this))
  261. return true;
  262. savedState = state;
  263. }
  264. return false;
  265. }
  266. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  267. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  268. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  269. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  270. {
  271. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  272. // reloadSavedCopy() to put it back into GPU memory before using it..
  273. jassert (savedState == nullptr);
  274. if (pimpl == nullptr)
  275. return false;
  276. pimpl->bind();
  277. return true;
  278. }
  279. void OpenGLFrameBuffer::setCurrentFrameBufferTarget (GLuint frameBufferID)
  280. {
  281. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferID);
  282. }
  283. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget()
  284. {
  285. GLint fb;
  286. glGetIntegerv (GL_FRAMEBUFFER_BINDING_EXT, &fb);
  287. return (GLuint) fb;
  288. }
  289. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  290. {
  291. setCurrentFrameBufferTarget (0);
  292. }
  293. void OpenGLFrameBuffer::clear (const Colour& colour)
  294. {
  295. if (makeCurrentRenderingTarget())
  296. {
  297. OpenGLHelpers::clear (colour);
  298. releaseAsRenderingTarget();
  299. }
  300. }
  301. void OpenGLFrameBuffer::makeCurrentAndClear()
  302. {
  303. if (makeCurrentRenderingTarget())
  304. {
  305. glClearColor (0, 0, 0, 0);
  306. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  307. }
  308. }
  309. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  310. {
  311. if (! makeCurrentRenderingTarget())
  312. return false;
  313. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  314. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, target);
  315. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  316. return true;
  317. }
  318. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  319. {
  320. if (! makeCurrentRenderingTarget())
  321. return false;
  322. OpenGLHelpers::prepareFor2D (pimpl->width, pimpl->height);
  323. glDisable (GL_DEPTH_TEST);
  324. glDisable (GL_BLEND);
  325. OpenGLTexture tex;
  326. tex.loadARGBFlipped (data, area.getWidth(), area.getHeight());
  327. const int texH = tex.getHeight();
  328. #if JUCE_OPENGL_ES
  329. tex.bind();
  330. const GLint cropRect[4] = { 0, texH - area.getHeight(), area.getWidth(), area.getHeight() };
  331. glTexParameteriv (GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  332. glEnable (GL_TEXTURE_2D);
  333. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  334. glDrawTexiOES (area.getX(), area.getY(), 1, area.getWidth(), area.getHeight());
  335. glBindTexture (GL_TEXTURE_2D, 0);
  336. #else
  337. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  338. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  339. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  340. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  341. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  342. const int x = area.getX();
  343. const int y = area.getY() - (texH - area.getHeight());
  344. const GLfloat x1 = (GLfloat) x;
  345. const GLfloat y1 = (GLfloat) y;
  346. const GLfloat x2 = (GLfloat) (x + tex.getWidth());
  347. const GLfloat y2 = (GLfloat) (y + texH);
  348. const GLfloat vertices[] = { x1, y1, x2, y1, x1, y2, x2, y2 };
  349. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  350. OpenGLHelpers::drawTriangleStrip (vertices, textureCoords, 4, tex.getTextureID());
  351. #endif
  352. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  353. return true;
  354. }
  355. void OpenGLFrameBuffer::draw2D (float x1, float y1,
  356. float x2, float y2,
  357. float x3, float y3,
  358. float x4, float y4,
  359. const Colour& colour) const
  360. {
  361. if (pimpl != nullptr)
  362. {
  363. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  364. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  365. glBindTexture (GL_TEXTURE_2D, 0);
  366. }
  367. }
  368. void OpenGLFrameBuffer::draw3D (float x1, float y1, float z1,
  369. float x2, float y2, float z2,
  370. float x3, float y3, float z3,
  371. float x4, float y4, float z4,
  372. const Colour& colour) const
  373. {
  374. if (pimpl != nullptr)
  375. {
  376. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  377. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  378. glBindTexture (GL_TEXTURE_2D, 0);
  379. }
  380. }
  381. void OpenGLFrameBuffer::drawAt (float x1, float y1) const
  382. {
  383. if (pimpl != nullptr)
  384. {
  385. glEnable (GL_TEXTURE_2D);
  386. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  387. glDisableClientState (GL_COLOR_ARRAY);
  388. glDisableClientState (GL_NORMAL_ARRAY);
  389. const GLfloat vertices[] = { x1, y1,
  390. x1 + pimpl->width, y1,
  391. x1, y1 + pimpl->height,
  392. x1 + pimpl->width, y1 + pimpl->height };
  393. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  394. glEnableClientState (GL_VERTEX_ARRAY);
  395. glVertexPointer (2, GL_FLOAT, 0, vertices);
  396. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  397. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  398. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  399. glBindTexture (GL_TEXTURE_2D, 0);
  400. }
  401. }
  402. END_JUCE_NAMESPACE