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.

269 lines
11KB

  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. OpenGLPixelFormat::OpenGLPixelFormat (const int bitsPerRGBComponent,
  19. const int alphaBits_,
  20. const int depthBufferBits_,
  21. const int stencilBufferBits_) noexcept
  22. : redBits (bitsPerRGBComponent),
  23. greenBits (bitsPerRGBComponent),
  24. blueBits (bitsPerRGBComponent),
  25. alphaBits (alphaBits_),
  26. depthBufferBits (depthBufferBits_),
  27. stencilBufferBits (stencilBufferBits_),
  28. accumulationBufferRedBits (0),
  29. accumulationBufferGreenBits (0),
  30. accumulationBufferBlueBits (0),
  31. accumulationBufferAlphaBits (0),
  32. multisamplingLevel (0)
  33. {
  34. }
  35. OpenGLPixelFormat::OpenGLPixelFormat (const OpenGLPixelFormat& other) noexcept
  36. : redBits (other.redBits),
  37. greenBits (other.greenBits),
  38. blueBits (other.blueBits),
  39. alphaBits (other.alphaBits),
  40. depthBufferBits (other.depthBufferBits),
  41. stencilBufferBits (other.stencilBufferBits),
  42. accumulationBufferRedBits (other.accumulationBufferRedBits),
  43. accumulationBufferGreenBits (other.accumulationBufferGreenBits),
  44. accumulationBufferBlueBits (other.accumulationBufferBlueBits),
  45. accumulationBufferAlphaBits (other.accumulationBufferAlphaBits),
  46. multisamplingLevel (other.multisamplingLevel)
  47. {
  48. }
  49. OpenGLPixelFormat& OpenGLPixelFormat::operator= (const OpenGLPixelFormat& other) noexcept
  50. {
  51. redBits = other.redBits;
  52. greenBits = other.greenBits;
  53. blueBits = other.blueBits;
  54. alphaBits = other.alphaBits;
  55. depthBufferBits = other.depthBufferBits;
  56. stencilBufferBits = other.stencilBufferBits;
  57. accumulationBufferRedBits = other.accumulationBufferRedBits;
  58. accumulationBufferGreenBits = other.accumulationBufferGreenBits;
  59. accumulationBufferBlueBits = other.accumulationBufferBlueBits;
  60. accumulationBufferAlphaBits = other.accumulationBufferAlphaBits;
  61. multisamplingLevel = other.multisamplingLevel;
  62. return *this;
  63. }
  64. bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const noexcept
  65. {
  66. return redBits == other.redBits
  67. && greenBits == other.greenBits
  68. && blueBits == other.blueBits
  69. && alphaBits == other.alphaBits
  70. && depthBufferBits == other.depthBufferBits
  71. && stencilBufferBits == other.stencilBufferBits
  72. && accumulationBufferRedBits == other.accumulationBufferRedBits
  73. && accumulationBufferGreenBits == other.accumulationBufferGreenBits
  74. && accumulationBufferBlueBits == other.accumulationBufferBlueBits
  75. && accumulationBufferAlphaBits == other.accumulationBufferAlphaBits
  76. && multisamplingLevel == other.multisamplingLevel;
  77. }
  78. //==============================================================================
  79. static Array<OpenGLContext*> knownContexts;
  80. OpenGLContext::OpenGLContext() noexcept
  81. : shaderLanguageAvailable (0)
  82. {
  83. knownContexts.add (this);
  84. }
  85. OpenGLContext::~OpenGLContext()
  86. {
  87. knownContexts.removeValue (this);
  88. }
  89. OpenGLContext* OpenGLContext::getCurrentContext()
  90. {
  91. for (int i = knownContexts.size(); --i >= 0;)
  92. {
  93. OpenGLContext* const oglc = knownContexts.getUnchecked(i);
  94. if (oglc->isActive())
  95. return oglc;
  96. }
  97. return nullptr;
  98. }
  99. bool OpenGLContext::areShadersAvailable() const
  100. {
  101. #if JUCE_USE_OPENGL_SHADERS
  102. if (shaderLanguageAvailable == 0)
  103. shaderLanguageAvailable = (OpenGLShaderProgram::getLanguageVersion() > 0) ? 1 : -1;
  104. return shaderLanguageAvailable > 0;
  105. #else
  106. return false;
  107. #endif
  108. }
  109. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  110. const Rectangle<int>& anchorPosAndTextureSize,
  111. int contextWidth, int contextHeight)
  112. {
  113. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  114. glEnable (GL_BLEND);
  115. #if JUCE_USE_OPENGL_SHADERS
  116. if (areShadersAvailable())
  117. {
  118. struct OverlayShaderProgram : public ReferenceCountedObject
  119. {
  120. OverlayShaderProgram (OpenGLContext& context)
  121. : program (context), builder (program), params (program)
  122. {}
  123. static const OverlayShaderProgram& select (OpenGLContext& context)
  124. {
  125. static const Identifier programValueID ("juceGLComponentOverlayShader");
  126. OverlayShaderProgram* program = dynamic_cast <OverlayShaderProgram*> (context.properties [programValueID].getObject());
  127. if (program == nullptr)
  128. {
  129. program = new OverlayShaderProgram (context);
  130. context.properties.set (programValueID, var (program));
  131. }
  132. program->program.use();
  133. return *program;
  134. }
  135. struct ProgramBuilder
  136. {
  137. ProgramBuilder (OpenGLShaderProgram& program)
  138. {
  139. program.addShader ("attribute " JUCE_HIGHP " vec2 position;"
  140. "uniform " JUCE_HIGHP " vec2 screenSize;"
  141. "varying " JUCE_HIGHP " vec2 pixelPos;"
  142. "void main()"
  143. "{"
  144. "pixelPos = position;"
  145. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  146. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  147. "}",
  148. GL_VERTEX_SHADER);
  149. program.addShader ("uniform sampler2D imageTexture;"
  150. "uniform " JUCE_HIGHP " float matrix[6];"
  151. "varying " JUCE_HIGHP " vec2 pixelPos;"
  152. "void main()"
  153. "{"
  154. JUCE_HIGHP " vec2 texturePos = mat2 (matrix[0], matrix[3], matrix[1], matrix[4]) * pixelPos"
  155. " + vec2 (matrix[2], matrix[5]);"
  156. "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, 1.0 - texturePos.y));"
  157. "}",
  158. GL_FRAGMENT_SHADER);
  159. program.link();
  160. }
  161. };
  162. struct Params
  163. {
  164. Params (OpenGLShaderProgram& program)
  165. : positionAttribute (program, "position"),
  166. screenSize (program, "screenSize"),
  167. imageTexture (program, "imageTexture"),
  168. matrix (program, "matrix")
  169. {}
  170. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& anchorPosAndTextureSize) const
  171. {
  172. const AffineTransform t (AffineTransform::translation (anchorPosAndTextureSize.getX(),
  173. anchorPosAndTextureSize.getY())
  174. .inverted().scaled (1.0f / anchorPosAndTextureSize.getWidth(),
  175. 1.0f / anchorPosAndTextureSize.getHeight()));
  176. const GLfloat m[] = { t.mat00, t.mat01, t.mat02, t.mat10, t.mat11, t.mat12 };
  177. matrix.set (m, 6);
  178. imageTexture.set (0);
  179. screenSize.set (targetWidth, targetHeight);
  180. }
  181. OpenGLShaderProgram::Attribute positionAttribute;
  182. OpenGLShaderProgram::Uniform screenSize, imageTexture, matrix;
  183. };
  184. OpenGLShaderProgram program;
  185. ProgramBuilder builder;
  186. Params params;
  187. };
  188. const GLshort left = (GLshort) targetClipArea.getX();
  189. const GLshort top = (GLshort) targetClipArea.getY();
  190. const GLshort right = (GLshort) targetClipArea.getRight();
  191. const GLshort bottom = (GLshort) targetClipArea.getBottom();
  192. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  193. const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
  194. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat());
  195. extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
  196. extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
  197. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  198. extensions.glUseProgram (0);
  199. }
  200. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  201. else
  202. #endif
  203. #endif
  204. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  205. {
  206. glEnable (GL_SCISSOR_TEST);
  207. glScissor (targetClipArea.getX(), contextHeight - targetClipArea.getBottom(),
  208. targetClipArea.getWidth(), targetClipArea.getHeight());
  209. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  210. glDisableClientState (GL_COLOR_ARRAY);
  211. glDisableClientState (GL_NORMAL_ARRAY);
  212. glEnableClientState (GL_VERTEX_ARRAY);
  213. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  214. OpenGLHelpers::prepareFor2D (contextWidth, contextHeight);
  215. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  216. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  217. const GLshort left = (GLshort) anchorPosAndTextureSize.getX();
  218. const GLshort right = (GLshort) anchorPosAndTextureSize.getRight();
  219. const GLshort top = (GLshort) (contextHeight - anchorPosAndTextureSize.getY());
  220. const GLshort bottom = (GLshort) (contextHeight - anchorPosAndTextureSize.getBottom());
  221. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  222. glVertexPointer (2, GL_SHORT, 0, vertices);
  223. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  224. glDisable (GL_SCISSOR_TEST);
  225. }
  226. #endif
  227. }