From af0276b698593a4ce66de04648cb1092d64d7771 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 30 May 2014 11:25:09 +0100 Subject: [PATCH] Refactored some openGL 2D shaders to move some interpolation from the fragment shader into varying variables. --- .../juce_opengl/opengl/juce_OpenGLContext.cpp | 38 +++++----- .../opengl/juce_OpenGLGraphicsContext.cpp | 71 ++++++++++++------- 2 files changed, 66 insertions(+), 43 deletions(-) diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp index 27faff4510..ee98359712 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp @@ -856,27 +856,27 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, ProgramBuilder (OpenGLShaderProgram& prog) { prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 ( - "attribute " JUCE_HIGHP " vec2 position;" - "uniform " JUCE_HIGHP " vec2 screenSize;" - "varying " JUCE_HIGHP " vec2 pixelPos;" - "void main()" - "{" - "pixelPos = position;" - JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);" - "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);" - "}")); + "attribute " JUCE_HIGHP " vec2 position;" + "uniform " JUCE_HIGHP " vec2 screenSize;" + "uniform " JUCE_HIGHP " float textureBounds[4];" + "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;" + "varying " JUCE_HIGHP " vec2 texturePos;" + "void main()" + "{" + JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);" + "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);" + "texturePos = (position - vec2 (textureBounds[0], textureBounds[1])) / vec2 (textureBounds[2], textureBounds[3]);" + "texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);" + "}")); prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 ( - "uniform sampler2D imageTexture;" - "uniform " JUCE_HIGHP " float textureBounds[4];" - "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;" - "varying " JUCE_HIGHP " vec2 pixelPos;" - "void main()" - "{" - JUCE_HIGHP " vec2 texturePos = (pixelPos - vec2 (textureBounds[0], textureBounds[1]))" - "/ vec2 (textureBounds[2], textureBounds[3]);" - "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y));" - "}")); + "uniform sampler2D imageTexture;" + "varying " JUCE_HIGHP " vec2 texturePos;" + "void main()" + "{" + "gl_FragColor = texture2D (imageTexture, texturePos);" + "}")); + prog.link(); } }; diff --git a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp index 311dfa663a..a1cd30b0b0 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp @@ -336,30 +336,36 @@ public: //============================================================================== struct ShaderProgramHolder { - ShaderProgramHolder (OpenGLContext& context, const char* fragmentShader) + ShaderProgramHolder (OpenGLContext& context, const char* fragmentShader, const char* vertexShader) : program (context) { JUCE_CHECK_OPENGL_ERROR - program.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 ( - "attribute vec2 position;" - "attribute vec4 colour;" - "uniform vec4 screenBounds;" - "varying " JUCE_MEDIUMP " vec4 frontColour;" - "varying " JUCE_HIGHP " vec2 pixelPos;" - "void main()" - "{" - " frontColour = colour;" - " vec2 adjustedPos = position - screenBounds.xy;" - " pixelPos = adjustedPos;" - " vec2 scaledPos = adjustedPos / screenBounds.zw;" - " gl_Position = vec4 (scaledPos.x - 1.0, 1.0 - scaledPos.y, 0, 1.0);" - "}")); - - if (! program.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))) - lastError = program.getLastError(); - program.link(); - JUCE_CHECK_OPENGL_ERROR + if (vertexShader == nullptr) + vertexShader = "attribute vec2 position;" + "attribute vec4 colour;" + "uniform vec4 screenBounds;" + "varying " JUCE_MEDIUMP " vec4 frontColour;" + "varying " JUCE_HIGHP " vec2 pixelPos;" + "void main()" + "{" + "frontColour = colour;" + "vec2 adjustedPos = position - screenBounds.xy;" + "pixelPos = adjustedPos;" + "vec2 scaledPos = adjustedPos / screenBounds.zw;" + "gl_Position = vec4 (scaledPos.x - 1.0, 1.0 - scaledPos.y, 0, 1.0);" + "}"; + + if (program.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (vertexShader)) + && program.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader)) + && program.link()) + { + JUCE_CHECK_OPENGL_ERROR + } + else + { + lastError = program.getLastError(); + } } OpenGLShaderProgram program; @@ -368,8 +374,8 @@ public: struct ShaderBase : public ShaderProgramHolder { - ShaderBase (OpenGLContext& context, const char* fragmentShader) - : ShaderProgramHolder (context, fragmentShader), + ShaderBase (OpenGLContext& context, const char* fragmentShader, const char* vertexShader = nullptr) + : ShaderProgramHolder (context, fragmentShader, vertexShader), positionAttribute (program, "position"), colourAttribute (program, "colour"), screenBounds (program, "screenBounds") @@ -656,11 +662,28 @@ public: struct ImageProgram : public ShaderBase { ImageProgram (OpenGLContext& context) - : ShaderBase (context, JUCE_DECLARE_IMAGE_UNIFORMS JUCE_DECLARE_SWIZZLE_FUNCTION + : ShaderBase (context, JUCE_DECLARE_VARYING_COLOUR JUCE_DECLARE_SWIZZLE_FUNCTION + "uniform sampler2D imageTexture;" + "varying " JUCE_HIGHP " vec2 texturePos;" "void main()" "{" - JUCE_CLAMP_TEXTURE_COORD "gl_FragColor = frontColour.a * " JUCE_GET_IMAGE_PIXEL ";" + "}", + "uniform " JUCE_MEDIUMP " vec2 imageLimits;" + JUCE_DECLARE_MATRIX_UNIFORM + "attribute vec2 position;" + "attribute vec4 colour;" + "uniform vec4 screenBounds;" + "varying " JUCE_MEDIUMP " vec4 frontColour;" + "varying " JUCE_HIGHP " vec2 texturePos;" + "void main()" + "{" + "frontColour = colour;" + "vec2 adjustedPos = position - screenBounds.xy;" + "vec2 pixelPos = adjustedPos;" + "texturePos = clamp (" JUCE_MATRIX_TIMES_FRAGCOORD ", vec2 (0, 0), imageLimits);" + "vec2 scaledPos = adjustedPos / screenBounds.zw;" + "gl_Position = vec4 (scaledPos.x - 1.0, 1.0 - scaledPos.y, 0, 1.0);" "}"), imageParams (program) {}