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.

486 lines
18KB

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