1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 33 #if ENABLE(ACCELERATED_2D_CANVAS) 34 35 #include "ConvolutionShader.h" 36 37 #include "GraphicsContext3D.h" 38 #include "StringExtras.h" 39 40 namespace WebCore { 41 42 ConvolutionShader::ConvolutionShader(GraphicsContext3D* context, unsigned program, int kernelWidth) 43 : Shader(context, program) 44 , m_kernelWidth(kernelWidth) 45 , m_matrixLocation(context->getUniformLocation(program, "matrix")) 46 , m_texMatrixLocation(context->getUniformLocation(program, "texMatrix")) 47 , m_kernelLocation(context->getUniformLocation(program, "kernel")) 48 , m_imageLocation(context->getUniformLocation(program, "image")) 49 , m_imageIncrementLocation(context->getUniformLocation(program, "imageIncrement")) 50 , m_positionLocation(context->getAttribLocation(program, "position")) 51 { 52 } 53 54 PassOwnPtr<ConvolutionShader> ConvolutionShader::create(GraphicsContext3D* context, int kernelWidth) 55 { 56 static const char* vertexShaderRaw = 57 "#define KERNEL_WIDTH %d\n" 58 "uniform mat3 matrix;\n" 59 "uniform mat3 texMatrix;\n" 60 "uniform vec2 imageIncrement;\n" 61 "attribute vec2 position;\n" 62 "varying vec2 imageCoord;\n" 63 "void main() {\n" 64 " vec3 pos = vec3(position, 1.0);\n" 65 " // Offset image coords by half of kernel width, in image texels\n" 66 " gl_Position = vec4(matrix * pos, 1.0);\n" 67 " float scale = (float(KERNEL_WIDTH) - 1.0) / 2.0;\n" 68 " imageCoord = (texMatrix * pos).xy - vec2(scale, scale) * imageIncrement;\n" 69 "}\n"; 70 char vertexShaderSource[1024]; 71 snprintf(vertexShaderSource, sizeof(vertexShaderSource), vertexShaderRaw, kernelWidth); 72 static const char* fragmentShaderRaw = 73 "#ifdef GL_ES\n" 74 "precision mediump float;\n" 75 "#endif\n" 76 "#define KERNEL_WIDTH %d\n" 77 "uniform sampler2D image;\n" 78 "uniform float kernel[KERNEL_WIDTH];\n" 79 "uniform vec2 imageIncrement;\n" 80 "varying vec2 imageCoord;\n" 81 "void main() {\n" 82 " vec2 coord = imageCoord;\n" 83 " vec4 sum = vec4(0, 0, 0, 0);\n" 84 " for (int i = 0; i < KERNEL_WIDTH; i++) {\n" 85 " sum += texture2D(image, coord) * kernel[i];\n" 86 " coord += imageIncrement;\n" 87 " }\n" 88 " gl_FragColor = sum;\n" 89 "}\n"; 90 char fragmentShaderSource[1024]; 91 snprintf(fragmentShaderSource, sizeof(fragmentShaderSource), fragmentShaderRaw, kernelWidth); 92 93 unsigned program = loadProgram(context, vertexShaderSource, fragmentShaderSource); 94 if (!program) 95 return 0; 96 return new ConvolutionShader(context, program, kernelWidth); 97 } 98 99 void ConvolutionShader::use(const AffineTransform& transform, const AffineTransform& texTransform, const float* kernel, int kernelWidth, float imageIncrement[2]) 100 { 101 m_context->useProgram(m_program); 102 float matrix[9]; 103 affineTo3x3(transform, matrix); 104 m_context->uniformMatrix3fv(m_matrixLocation, false /*transpose*/, matrix, 1 /*count*/); 105 106 float texMatrix[9]; 107 affineTo3x3(texTransform, texMatrix); 108 m_context->uniformMatrix3fv(m_texMatrixLocation, false /*transpose*/, texMatrix, 1 /*count*/); 109 110 m_context->uniform2f(m_imageIncrementLocation, imageIncrement[0], imageIncrement[1]); 111 112 // For now, we always use texture unit 0. If that ever changes, we should 113 // expose this parameter to the caller. 114 m_context->uniform1i(m_imageLocation, 0); 115 if (kernelWidth > m_kernelWidth) 116 kernelWidth = m_kernelWidth; 117 m_context->uniform1fv(m_kernelLocation, const_cast<float*>(kernel), kernelWidth); 118 119 m_context->vertexAttribPointer(m_positionLocation, 2, GraphicsContext3D::FLOAT, false, 0, 0); 120 121 m_context->enableVertexAttribArray(m_positionLocation); 122 } 123 124 } 125 126 #endif 127