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.

257 lines
10KB

  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. : shaderLanguageVersion (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. #if JUCE_USE_OPENGL_SHADERS
  100. double OpenGLContext::getShaderLanguageVersion()
  101. {
  102. if (shaderLanguageVersion == 0)
  103. shaderLanguageVersion = OpenGLShaderProgram::getLanguageVersion();
  104. return shaderLanguageVersion;
  105. }
  106. #endif
  107. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  108. const Rectangle<int>& anchorPosAndTextureSize,
  109. float alpha)
  110. {
  111. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  112. glEnable (GL_BLEND);
  113. glColor4f (1.0f, 1.0f, 1.0f, alpha);
  114. const GLshort y = (GLshort) (getHeight() - anchorPosAndTextureSize.getHeight());
  115. const GLshort width = (GLshort) anchorPosAndTextureSize.getWidth();
  116. const GLshort bottom = (GLshort) (y + anchorPosAndTextureSize.getHeight());
  117. const GLshort vertices[] = { 0, y, width, y, 0, bottom, width, bottom };
  118. #if JUCE_USE_OPENGL_SHADERS
  119. if (getShaderLanguageVersion() > 1.199)
  120. {
  121. struct OverlayShaderProgram : public ReferenceCountedObject
  122. {
  123. OverlayShaderProgram (OpenGLContext& context)
  124. : program (context),
  125. builder (program),
  126. params (program)
  127. {}
  128. static const OverlayShaderProgram& select (OpenGLContext& context)
  129. {
  130. static const Identifier programValueID ("juceGLComponentOverlayShader");
  131. OverlayShaderProgram* program = dynamic_cast <OverlayShaderProgram*> (context.properties [programValueID].getObject());
  132. if (program == nullptr)
  133. {
  134. program = new OverlayShaderProgram (context);
  135. context.properties.set (programValueID, var (program));
  136. }
  137. program->program.use();
  138. return *program;
  139. }
  140. struct ProgramBuilder
  141. {
  142. ProgramBuilder (OpenGLShaderProgram& program)
  143. {
  144. program.addShader ("attribute vec2 position;"
  145. "uniform vec2 screenSize;"
  146. "void main()"
  147. "{"
  148. " vec2 scaled = position / (0.5 * screenSize.xy);"
  149. " gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  150. "}",
  151. GL_VERTEX_SHADER);
  152. program.addShader ("#version 120\n"
  153. "uniform sampler2D imageTexture;"
  154. "uniform float matrix[6];"
  155. "void main()"
  156. "{"
  157. "vec2 texturePos = mat2 (matrix[0], matrix[3], matrix[1], matrix[4]) * gl_FragCoord.xy"
  158. " + vec2 (matrix[2], matrix[5]);"
  159. "gl_FragColor = gl_Color.a * texture2D (imageTexture, vec2 (texturePos.x, 1.0 - texturePos.y));"
  160. "}",
  161. GL_FRAGMENT_SHADER);
  162. program.link();
  163. }
  164. };
  165. struct Params
  166. {
  167. Params (OpenGLShaderProgram& program)
  168. : positionAttribute (program, "position"),
  169. screenSize (program, "screenSize"),
  170. imageTexture (program, "imageTexture"),
  171. matrix (program, "matrix")
  172. {}
  173. void set (const int targetWidth, const int targetHeight, const Rectangle<float>& anchorPosAndTextureSize) const
  174. {
  175. const AffineTransform t (AffineTransform::translation (-anchorPosAndTextureSize.getX(),
  176. -anchorPosAndTextureSize.getY())
  177. .followedBy (AffineTransform::verticalFlip (targetHeight))
  178. .inverted().scaled (1.0f / anchorPosAndTextureSize.getWidth(),
  179. 1.0f / anchorPosAndTextureSize.getHeight()));
  180. const GLfloat m[] = { t.mat00, t.mat01, t.mat02, t.mat10, t.mat11, t.mat12 };
  181. matrix.set (m, 6);
  182. imageTexture.set (0);
  183. screenSize.set (targetWidth, targetHeight);
  184. }
  185. OpenGLShaderProgram::Attribute positionAttribute;
  186. OpenGLShaderProgram::Uniform screenSize, imageTexture, matrix;
  187. };
  188. OpenGLShaderProgram program;
  189. ProgramBuilder builder;
  190. Params params;
  191. };
  192. const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
  193. program.params.set (getWidth(), getHeight(), anchorPosAndTextureSize.toFloat());
  194. extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
  195. extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
  196. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  197. extensions.glUseProgram (0);
  198. }
  199. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  200. else
  201. #endif
  202. #endif
  203. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  204. {
  205. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  206. glDisableClientState (GL_COLOR_ARRAY);
  207. glDisableClientState (GL_NORMAL_ARRAY);
  208. glEnableClientState (GL_VERTEX_ARRAY);
  209. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  210. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  211. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  212. glVertexPointer (2, GL_SHORT, 0, vertices);
  213. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  214. }
  215. #endif
  216. }