aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrConvolutionEffect.cpp
blob: 0dd1606c81d8edccd29b08a57ab0671aceb81b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrConvolutionEffect.h"
#include "gl/GrGLProgramStage.h"
#include "gl/GrGLSL.h"
#include "gl/GrGLTexture.h"
#include "GrProgramStageFactory.h"

/////////////////////////////////////////////////////////////////////

class GrGLConvolutionEffect : public GrGLProgramStage {

public:

    GrGLConvolutionEffect(const GrProgramStageFactory& factory,
                          const GrCustomStage* stage);
    virtual void setupVariables(GrGLShaderBuilder* state,
                                int stage) SK_OVERRIDE;
    virtual void emitVS(GrGLShaderBuilder* state,
                        const char* vertexCoords) SK_OVERRIDE;
    virtual void emitFS(GrGLShaderBuilder* state,
                        const char* outputColor,
                        const char* inputColor,
                        const char* samplerName) SK_OVERRIDE;
    virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;

    virtual void setData(const GrGLInterface*, 
                         const GrGLTexture&,
                         GrCustomStage*,
                         int stageNum) SK_OVERRIDE;

    static inline StageKey GenKey(const GrCustomStage* s);
    
protected:

    unsigned int         fKernelWidth;
    const GrGLShaderVar* fKernelVar;
    const GrGLShaderVar* fImageIncrementVar;
 
    GrGLint              fKernelLocation;
    GrGLint              fImageIncrementLocation;

private:

    typedef GrGLProgramStage INHERITED;
};

GrGLConvolutionEffect::GrGLConvolutionEffect(
                                    const GrProgramStageFactory& factory,
                                    const GrCustomStage* data)
    : GrGLProgramStage(factory)
    , fKernelVar(NULL)
    , fImageIncrementVar(NULL)
    , fKernelLocation(0)
    , fImageIncrementLocation(0) {
    fKernelWidth = static_cast<const GrConvolutionEffect*>(data)->width();
}

void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* state,
                                           int stage) {
    fImageIncrementVar = &state->addUniform(
        GrGLShaderBuilder::kBoth_VariableLifetime,
        kVec2f_GrSLType, "uImageIncrement", stage);
    fKernelVar = &state->addUniform(
        GrGLShaderBuilder::kFragment_VariableLifetime,
        kFloat_GrSLType, "uKernel", stage, fKernelWidth);

    fImageIncrementLocation = kUseUniform;
    fKernelLocation = kUseUniform;
}

void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state,
                        const char* vertexCoords) {
    GrStringBuilder* code = &state->fVSCode;
    float scale = (fKernelWidth - 1) * 0.5f;
    code->appendf("\t\t%s -= vec2(%g, %g) * %s;\n",
                  vertexCoords, scale, scale,
                  fImageIncrementVar->getName().c_str());

}

void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state,
                        const char* outputColor,
                        const char* inputColor,
                        const char* samplerName) {
    GrStringBuilder* code = &state->fFSCode;
    const char* texFunc = "texture2D";
    bool complexCoord = false;

    GrStringBuilder modulate;
    if (NULL != inputColor) {
        modulate.printf(" * %s", inputColor);
    }

    // Creates the string "kernel[i]" with workarounds for
    // possible driver bugs
    GrStringBuilder kernelIndex;
    fKernelVar->appendArrayAccess("i", &kernelIndex);

    code->appendf("\t\tvec4 sum = vec4(0, 0, 0, 0);\n");
    code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str());
    code->appendf("\t\tfor (int i = 0; i < %d; i++) {\n",
                  fKernelWidth);

    code->appendf("\t\t\tsum += ");
    state->emitTextureLookup(samplerName, "coord");
    code->appendf(" * %s;\n", kernelIndex.c_str());

    code->appendf("\t\t\tcoord += %s;\n",
                  fImageIncrementVar->getName().c_str());
    code->appendf("\t\t}\n");
    code->appendf("\t\t%s = sum%s;\n", outputColor, modulate.c_str());
}

void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl,
                                         int programID) {
    GR_GL_CALL_RET(gl, fKernelLocation,
        GetUniformLocation(programID, fKernelVar->getName().c_str()));
    GR_GL_CALL_RET(gl, fImageIncrementLocation,
        GetUniformLocation(programID,
            fImageIncrementVar->getName().c_str()));
}

void GrGLConvolutionEffect::setData(const GrGLInterface* gl,
                                    const GrGLTexture& texture,
                                    GrCustomStage* data,
                                    int stageNum) {
    const GrConvolutionEffect* conv =
        static_cast<const GrConvolutionEffect*>(data);
    // the code we generated was for a specific kernel width
    GrAssert(conv->width() == fKernelWidth);
    GR_GL_CALL(gl, Uniform1fv(fKernelLocation,
                              fKernelWidth,
                              conv->kernel()));
    float imageIncrement[2] = { 0 };
    switch (conv->direction()) {
        case GrSamplerState::kX_FilterDirection:
            imageIncrement[0] = 1.0f / texture.width();
            break;
        case GrSamplerState::kY_FilterDirection:
            imageIncrement[1] = 1.0f / texture.width();
            break;
        default:
            GrCrash("Unknown filter direction.");
    }
    GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement));
}

GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
                                                    const GrCustomStage* s) {
    return static_cast<const GrConvolutionEffect*>(s)->width();
}

/////////////////////////////////////////////////////////////////////

GrConvolutionEffect::GrConvolutionEffect(
        GrSamplerState::FilterDirection direction,
        unsigned int kernelWidth,
        const float* kernel)
    : fDirection (direction)
    , fKernelWidth (kernelWidth) {
    GrAssert(kernelWidth <= MAX_KERNEL_WIDTH);
    for (unsigned int i = 0; i < kernelWidth; i++) {
        fKernel[i] = kernel[i];
    }
}

GrConvolutionEffect::~GrConvolutionEffect() {

}

const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
    return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
}

bool GrConvolutionEffect::isEqual(const GrCustomStage * sBase) const {
    const GrConvolutionEffect* s =
        static_cast<const GrConvolutionEffect*>(sBase);

    return (fKernelWidth == s->fKernelWidth &&
            fDirection == s->fDirection &&
            0 == memcmp(fKernel, s->fKernel, fKernelWidth * sizeof(float)));
}