ConvolutionShader.cpp [plain text]
#include "config.h"
#if ENABLE(ACCELERATED_2D_CANVAS)
#include "ConvolutionShader.h"
#include "GraphicsContext3D.h"
#include "StringExtras.h"
namespace WebCore {
ConvolutionShader::ConvolutionShader(GraphicsContext3D* context, unsigned program, int kernelWidth)
: Shader(context, program)
, m_kernelWidth(kernelWidth)
, m_matrixLocation(context->getUniformLocation(program, "matrix"))
, m_texMatrixLocation(context->getUniformLocation(program, "texMatrix"))
, m_kernelLocation(context->getUniformLocation(program, "kernel"))
, m_imageLocation(context->getUniformLocation(program, "image"))
, m_imageIncrementLocation(context->getUniformLocation(program, "imageIncrement"))
, m_positionLocation(context->getAttribLocation(program, "position"))
{
}
PassOwnPtr<ConvolutionShader> ConvolutionShader::create(GraphicsContext3D* context, int kernelWidth)
{
static const char* vertexShaderRaw =
"#define KERNEL_WIDTH %d\n"
"uniform mat3 matrix;\n"
"uniform mat3 texMatrix;\n"
"uniform vec2 imageIncrement;\n"
"attribute vec2 position;\n"
"varying vec2 imageCoord;\n"
"void main() {\n"
" vec3 pos = vec3(position, 1.0);\n"
" // Offset image coords by half of kernel width, in image texels\n"
" gl_Position = vec4(matrix * pos, 1.0);\n"
" float scale = (float(KERNEL_WIDTH) - 1.0) / 2.0;\n"
" imageCoord = (texMatrix * pos).xy - vec2(scale, scale) * imageIncrement;\n"
"}\n";
char vertexShaderSource[1024];
snprintf(vertexShaderSource, sizeof(vertexShaderSource), vertexShaderRaw, kernelWidth);
static const char* fragmentShaderRaw =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"#define KERNEL_WIDTH %d\n"
"uniform sampler2D image;\n"
"uniform float kernel[KERNEL_WIDTH];\n"
"uniform vec2 imageIncrement;\n"
"varying vec2 imageCoord;\n"
"void main() {\n"
" vec2 coord = imageCoord;\n"
" vec4 sum = vec4(0, 0, 0, 0);\n"
" for (int i = 0; i < KERNEL_WIDTH; i++) {\n"
" sum += texture2D(image, coord) * kernel[i];\n"
" coord += imageIncrement;\n"
" }\n"
" gl_FragColor = sum;\n"
"}\n";
char fragmentShaderSource[1024];
snprintf(fragmentShaderSource, sizeof(fragmentShaderSource), fragmentShaderRaw, kernelWidth);
unsigned program = loadProgram(context, vertexShaderSource, fragmentShaderSource);
if (!program)
return nullptr;
return adoptPtr(new ConvolutionShader(context, program, kernelWidth));
}
void ConvolutionShader::use(const AffineTransform& transform, const AffineTransform& texTransform, const float* kernel, int kernelWidth, float imageIncrement[2])
{
m_context->useProgram(m_program);
float matrix[9];
affineTo3x3(transform, matrix);
m_context->uniformMatrix3fv(m_matrixLocation, false , matrix, 1 );
float texMatrix[9];
affineTo3x3(texTransform, texMatrix);
m_context->uniformMatrix3fv(m_texMatrixLocation, false , texMatrix, 1 );
m_context->uniform2f(m_imageIncrementLocation, imageIncrement[0], imageIncrement[1]);
m_context->uniform1i(m_imageLocation, 0);
if (kernelWidth > m_kernelWidth)
kernelWidth = m_kernelWidth;
m_context->uniform1fv(m_kernelLocation, const_cast<float*>(kernel), kernelWidth);
m_context->vertexAttribPointer(m_positionLocation, 2, GraphicsContext3D::FLOAT, false, 0, 0);
m_context->enableVertexAttribArray(m_positionLocation);
}
}
#endif