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.

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