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.

425 lines
14KB

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